Add a sanity check for migrations, only execute when migration is newer than current table

This commit is contained in:
zontreck 2024-04-24 00:19:41 -07:00
parent b534287c51
commit e7f289d709
2 changed files with 46 additions and 2 deletions

View file

@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3
# The mod version. See https://semver.org/
mod_version=1201.13.042324.2345
mod_version=1201.13.042424.0010
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -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)
{
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;
}
}