2015-05-22 13:26:07 +02:00
package com.Acrobot.ChestShop.Database ;
import com.Acrobot.ChestShop.ChestShop ;
2017-07-03 01:46:43 +02:00
import com.j256.ormlite.dao.CloseableIterator ;
2015-05-22 13:26:07 +02:00
import com.j256.ormlite.dao.Dao ;
2017-07-03 01:46:43 +02:00
import com.j256.ormlite.dao.GenericRawResults ;
2015-05-22 13:26:07 +02:00
2018-04-18 15:58:15 +02:00
import java.io.IOException ;
2015-05-22 13:26:07 +02:00
import java.sql.SQLException ;
2017-07-03 01:46:43 +02:00
import java.util.Date ;
import java.util.UUID ;
import java.util.logging.Level ;
2015-05-22 13:26:07 +02:00
/ * *
* File handling the database migrations
*
* @author Andrzej Pomirski
* /
public class Migrations {
2019-03-30 15:40:31 +01:00
public static final int CURRENT_DATABASE_VERSION = 4 ;
2015-05-22 13:26:07 +02:00
/ * *
* Migrates a database from the given version
*
* @param currentVersion Current version of the database
* @return Current database version
* /
public static int migrate ( int currentVersion ) {
if ( currentVersion ! = CURRENT_DATABASE_VERSION ) {
ChestShop . getBukkitLogger ( ) . info ( " Updating database... " ) ;
}
switch ( currentVersion ) {
case 1 :
2017-11-26 18:45:35 +01:00
if ( migrateTo2 ( ) ) {
2015-05-22 13:26:07 +02:00
currentVersion + + ;
2017-11-26 18:45:35 +01:00
} else {
return - 1 ;
2015-05-22 13:26:07 +02:00
}
case 2 :
2017-11-26 18:45:35 +01:00
if ( migrateTo3 ( ) ) {
2017-07-01 18:14:41 +02:00
currentVersion + + ;
2017-11-26 18:45:35 +01:00
} else {
return - 1 ;
2017-07-01 18:14:41 +02:00
}
case 3 :
2019-03-30 15:40:31 +01:00
if ( migrateTo4 ( ) ) {
currentVersion + + ;
} else {
return - 1 ;
}
case 4 :
2015-05-22 13:26:07 +02:00
default :
2017-07-01 18:14:41 +02:00
break ;
2015-05-22 13:26:07 +02:00
//do nothing
}
return currentVersion ;
}
private static boolean migrateTo2 ( ) {
try {
Dao < Account , String > accounts = DaoCreator . getDao ( Account . class ) ;
accounts . executeRaw ( " ALTER TABLE `accounts` ADD COLUMN lastSeenName VARCHAR " ) ;
return true ;
} catch ( SQLException e ) {
2023-03-01 17:49:59 +01:00
ChestShop . getBukkitLogger ( ) . log ( Level . SEVERE , " Error while migrating database to v2 " , e ) ;
2015-05-22 13:26:07 +02:00
return false ;
}
}
2017-07-01 18:14:41 +02:00
private static boolean migrateTo3 ( ) {
try {
Dao < Account , String > accountsOld = DaoCreator . getDao ( Account . class ) ;
2017-07-03 01:46:43 +02:00
accountsOld . executeRawNoArgs ( " ALTER TABLE `accounts` RENAME TO `accounts-old` " ) ;
2017-07-01 18:14:41 +02:00
Dao < Account , String > accounts = DaoCreator . getDaoAndCreateTable ( Account . class ) ;
2017-07-03 01:46:43 +02:00
long start = System . currentTimeMillis ( ) ;
try {
accounts . executeRawNoArgs ( " INSERT INTO `accounts` (name, shortName, uuid) SELECT name, shortName, uuid FROM `accounts-old` " ) ;
} catch ( SQLException e ) {
2017-08-17 22:38:05 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " Fast accounts migration failed! \ n " + e + " \ nCause: " + e . getCause ( ) ) ;
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , " Starting slow migration... " ) ;
2017-07-03 01:46:43 +02:00
accounts . executeRawNoArgs ( " DELETE FROM `accounts` " ) ;
GenericRawResults < String [ ] > results = accounts . queryRaw ( " SELECT name, shortName, uuid FROM `accounts-old` " ) ;
Date zero = new Date ( 0 ) ;
int success = 0 ;
int error = 0 ;
CloseableIterator < String [ ] > resultIterator = results . closeableIterator ( ) ;
2017-08-17 22:38:05 +02:00
long lastInfo = System . currentTimeMillis ( ) ;
2017-07-03 01:46:43 +02:00
while ( resultIterator . hasNext ( ) ) {
String [ ] strings = resultIterator . next ( ) ;
Account account = new Account ( strings [ 0 ] , UUID . fromString ( strings [ 2 ] ) ) ;
account . setShortName ( strings [ 1 ] ) ;
account . setLastSeen ( zero ) ;
try {
accounts . create ( account ) ;
success + + ;
} catch ( SQLException x ) {
error + + ;
2017-08-17 22:38:05 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . SEVERE , " Could not migrate account " + account . getName ( ) + " / " + account . getShortName ( ) + " / " + account . getUuid ( ) + " to new database format: \ n " + x + " \ nCause: " + x . getCause ( ) ) ;
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , " If the cause is a constraint violation then this is nothing to worry about! " ) ;
}
if ( lastInfo + 60 * 1000 < System . currentTimeMillis ( ) ) {
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , " Slow migration in progress... " + ( System . currentTimeMillis ( ) - start ) / 1000 . 0 + " s elapsed - " + success + " migrated - " + error + " errors " ) ;
lastInfo = System . currentTimeMillis ( ) ;
2017-07-03 01:46:43 +02:00
}
}
2018-04-18 15:58:15 +02:00
try {
results . close ( ) ;
2022-06-28 17:24:17 +02:00
} catch ( Exception e1 ) {
2018-04-18 15:58:15 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " Error while closing results! " + e . getMessage ( ) ) ;
}
2017-07-03 01:46:43 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , success + " accounts successfully migrated. " + error + " accounts failed to migrate! " ) ;
}
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , " Migration of accounts table finished in " + ( System . currentTimeMillis ( ) - start ) / 1000 . 0 + " s! " ) ;
2017-07-01 18:14:41 +02:00
return true ;
} catch ( SQLException e ) {
2023-03-01 17:49:59 +01:00
ChestShop . getBukkitLogger ( ) . log ( Level . SEVERE , " Error while migrating database to v3 " , e ) ;
2017-07-01 18:14:41 +02:00
return false ;
}
}
2019-03-30 15:40:31 +01:00
private static boolean migrateTo4 ( ) {
try {
Dao < Item , Integer > itemsOld = DaoCreator . getDao ( Item . class ) ;
itemsOld . executeRawNoArgs ( " ALTER TABLE `items` RENAME TO `items-old` " ) ;
Dao < Item , Integer > items = DaoCreator . getDaoAndCreateTable ( Item . class ) ;
long start = System . currentTimeMillis ( ) ;
try {
items . executeRawNoArgs ( " INSERT INTO `items` (id, code) SELECT id, code uuid FROM `items-old` " ) ;
} catch ( SQLException e ) {
2023-03-01 18:27:04 +01:00
ChestShop . getBukkitLogger ( ) . log ( Level . SEVERE , " Error while inserting items into new database while migrating to v4 " , e ) ;
2019-03-30 15:40:31 +01:00
}
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , " Migration of items table finished in " + ( System . currentTimeMillis ( ) - start ) / 1000 . 0 + " s! " ) ;
return true ;
} catch ( SQLException e ) {
2023-03-01 17:49:59 +01:00
ChestShop . getBukkitLogger ( ) . log ( Level . SEVERE , " Error while migrating database to v4 " , e ) ;
2019-03-30 15:40:31 +01:00
return false ;
}
}
2015-05-22 13:26:07 +02:00
}