Add a sanity check for migrations, only execute when migration is newer than current table
This commit is contained in:
parent
b534287c51
commit
e7f289d709
2 changed files with 46 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
package dev.zontreck.libzontreck.memory.world;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.zontreck.libzontreck.LibZontreck;
|
||||
import dev.zontreck.libzontreck.events.RegisterMigrationsEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
|
@ -133,9 +134,52 @@ public class DatabaseMigrations
|
|||
|
||||
List<Migration> migration = Lists.reverse(migrations);
|
||||
|
||||
Migration lastTableChecked = null;
|
||||
for(Migration m : migration)
|
||||
{
|
||||
m.execute();
|
||||
if(lastTableChecked == null) lastTableChecked = getCurrentTable(m.tableID);
|
||||
else {
|
||||
if(lastTableChecked.tableID != m.tableID) lastTableChecked = getCurrentTable(m.tableID);
|
||||
}
|
||||
|
||||
if(m.version > lastTableChecked.version) {
|
||||
|
||||
LibZontreck.LOGGER.info("Executing migration " + m.tableID + ":" + m.version);
|
||||
m.execute();
|
||||
} else {
|
||||
LibZontreck.LOGGER.info("Skipping migration on table " + m.tableID + "; Current table version is " + lastTableChecked.version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current table's version using the Migration structure for data fields. Will be null if there is an error on any table except for migrations.
|
||||
* @return
|
||||
*/
|
||||
private static Migration getCurrentTable(String tableID)
|
||||
{
|
||||
try{
|
||||
PreparedStatement pst = DatabaseWrapper.get().prepareStatement("SELECT * FROM `migrations` WHERE tableID=?;");
|
||||
pst.setString(0, tableID);
|
||||
|
||||
var result = pst.executeQuery();
|
||||
if(!result.next())
|
||||
{
|
||||
return builder().withTableID(tableID).withVersion(0);
|
||||
}else {
|
||||
return builder().withTableID(tableID).withVersion(result.getInt("version"));
|
||||
}
|
||||
|
||||
}catch (SQLException ex)
|
||||
{
|
||||
if(tableID == "migrations")
|
||||
{
|
||||
return builder().withTableID(tableID)
|
||||
.withVersion(0);
|
||||
}
|
||||
ex.printStackTrace();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue