From e7f289d7098fad7492cea77e922c5fe575d294a0 Mon Sep 17 00:00:00 2001 From: zontreck Date: Wed, 24 Apr 2024 00:19:41 -0700 Subject: [PATCH] Add a sanity check for migrations, only execute when migration is newer than current table --- gradle.properties | 2 +- .../memory/world/DatabaseMigrations.java | 46 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index bd69812..26dc9d2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseMigrations.java b/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseMigrations.java index f248fda..756ada2 100644 --- a/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseMigrations.java +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseMigrations.java @@ -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 = 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; } }