9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-28 19:29:16 +00:00

Use flyway for database migrations, bump region varchar length to 64

This commit is contained in:
SamB440
2023-04-23 17:20:07 +01:00
parent e8fc4c9ff7
commit 2e6783acc6
7 changed files with 77 additions and 23 deletions

View File

@@ -40,6 +40,8 @@ dependencies {
exclude("org.bukkit")
exclude("org.spigotmc")
}
compileOnly("org.flywaydb:flyway-core:9.16.2") // IMPLEMENTED VIA LIBRARIES - db migration
compileOnly("org.flywaydb:flyway-mysql:9.16.3") // IMPLEMENTED VIA LIBRARIES
//compileOnly 'com.zaxxer:HikariCP:2.4.1' // IMPLEMENTED VIA LIBRARIES - database
compileOnly("me.clip:placeholderapi:2.10.4") // PAPI
compileOnly("com.github.MilkBowl:VaultAPI:1.7") { // vault

View File

@@ -1,6 +1,7 @@
package net.islandearth.rpgregions.managers.data;
import co.aikar.idb.DB;
import co.aikar.idb.DatabaseOptions;
import co.aikar.idb.DbRow;
import com.github.benmanes.caffeine.cache.AsyncCache;
import com.github.benmanes.caffeine.cache.Caffeine;
@@ -10,6 +11,10 @@ import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.managers.data.account.RPGRegionsAccount;
import net.islandearth.rpgregions.managers.data.region.Discovery;
import net.islandearth.rpgregions.managers.data.region.WorldDiscovery;
import org.bukkit.Bukkit;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.output.ValidateOutput;
import org.flywaydb.core.api.output.ValidateResult;
import org.intellij.lang.annotations.Language;
import java.sql.SQLException;
@@ -23,7 +28,6 @@ import java.util.concurrent.TimeUnit;
public abstract class SQLCommonStorage implements IStorageManager {
protected static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS rpgregions_discoveries (uuid varchar(32) NOT NULL, region varchar(36) NOT NULL, time varchar(64) NOT NULL, PRIMARY KEY(uuid, region))";
protected static final String SELECT_REGION = "SELECT * FROM rpgregions_discoveries WHERE uuid = ?";
protected static final String INSERT_DISCOVERY = "INSERT INTO rpgregions_discoveries (uuid, region, time) VALUES (?, ?, ?)";
protected static final String DELETE_DISCOVERIES = "DELETE * FROM rpgregions_discoveries WHERE uuid = ?";
@@ -32,9 +36,14 @@ public abstract class SQLCommonStorage implements IStorageManager {
private final AsyncCache<UUID, RPGRegionsAccount> cachedAccounts;
private final RPGRegions plugin;
private final DatabaseOptions options;
public SQLCommonStorage(RPGRegions plugin) {
public SQLCommonStorage(RPGRegions plugin, DatabaseOptions options) {
this.options = options;
this.plugin = plugin;
migrate();
this.cachedAccounts = Caffeine.newBuilder()
.maximumSize(1_000) // Realistically no server can support higher than this, even Folia
.scheduler(Scheduler.systemScheduler())
@@ -48,6 +57,53 @@ public abstract class SQLCommonStorage implements IStorageManager {
.buildAsync((key, executor) -> getAccount(key));
}
private void migrate() {
// Time to migrate!
// Create the Flyway instance and point it to the database
Flyway flyway = Flyway.configure(plugin.getClass().getClassLoader())
.baselineOnMigrate(true)
.dataSource("jdbc:" + options.getDsn(), options.getUser(), options.getPass()).load();
// Start the migration
flyway.migrate();
final ValidateResult result = flyway.validateWithResult();
if (!result.validationSuccessful) {
panic(result);
throw new IllegalStateException("Could not migrate the database!");
}
}
private void panic(ValidateResult result) {
plugin.getLogger().severe("=== UNABLE TO MIGRATE DATABASE, ERROR AS FOLLOWS ===");
plugin.getLogger().severe("=== BASIC INFO ===");
plugin.getLogger().severe("Flyway Version: " + result.flywayVersion);
plugin.getLogger().severe("Plugin Version: " + plugin.getDescription().getVersion());
plugin.getLogger().severe("Server Version: " + Bukkit.getServer().getVersion());
plugin.getLogger().severe("=== MIGRATION INFO ===");
plugin.getLogger().severe("Operation: " + result.operation);
plugin.getLogger().severe("Error messages (combined): " + result.getAllErrorMessages());
plugin.getLogger().severe("Error code: " + result.errorDetails.errorCode);
plugin.getLogger().severe("Error message: " + result.errorDetails.errorMessage);
plugin.getLogger().severe("=== INVALID MIGRATIONS ===");
for (ValidateOutput invalidMigration : result.invalidMigrations) {
plugin.getLogger().severe("-");
plugin.getLogger().severe("Error code: " + invalidMigration.errorDetails.errorCode);
plugin.getLogger().severe("Error message: " + invalidMigration.errorDetails.errorMessage);
plugin.getLogger().severe("Description: " + invalidMigration.description);
plugin.getLogger().severe("Version: " + invalidMigration.version);
plugin.getLogger().severe("Path: " + invalidMigration.filepath);
plugin.getLogger().severe("-");
}
plugin.getLogger().severe("=== MIGRATION WARNINGS ===");
for (String warning : result.warnings) {
plugin.getLogger().warning(warning);
}
plugin.getLogger().severe("=== END ERROR, EXITING ===");
}
protected DatabaseOptions getDatabaseOptions() {
return options;
}
@Override
public CompletableFuture<RPGRegionsAccount> getAccount(UUID uuid) {
// Check if cached

View File

@@ -7,22 +7,15 @@ import co.aikar.idb.PooledDatabaseOptions;
import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.managers.data.SQLCommonStorage;
import java.sql.SQLException;
public class SqlStorage extends SQLCommonStorage {
public SqlStorage(RPGRegions plugin) {
super(plugin);
DatabaseOptions options = DatabaseOptions.builder().mysql(plugin.getConfig().getString("settings.sql.user"),
super(plugin,
DatabaseOptions.builder().mysql(plugin.getConfig().getString("settings.sql.user"),
plugin.getConfig().getString("settings.sql.pass"),
plugin.getConfig().getString("settings.sql.db"),
plugin.getConfig().getString("settings.sql.host") + ":" + plugin.getConfig().getString("settings.sql.port")).build();
Database db = PooledDatabaseOptions.builder().options(options).createHikariDatabase();
plugin.getConfig().getString("settings.sql.host") + ":" + plugin.getConfig().getString("settings.sql.port")).build());
Database db = PooledDatabaseOptions.builder().options(getDatabaseOptions()).createHikariDatabase();
DB.setGlobalDatabase(db);
try {
db.executeUpdate(CREATE_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -7,19 +7,11 @@ import co.aikar.idb.PooledDatabaseOptions;
import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.managers.data.SQLCommonStorage;
import java.sql.SQLException;
public class SqliteStorage extends SQLCommonStorage {
public SqliteStorage(RPGRegions plugin) {
super(plugin);
DatabaseOptions options = DatabaseOptions.builder().sqlite(plugin.getDataFolder() + "/regions.sqlite").build();
Database db = PooledDatabaseOptions.builder().options(options).createHikariDatabase();
super(plugin, DatabaseOptions.builder().sqlite(plugin.getDataFolder() + "/regions.sqlite").build());
Database db = PooledDatabaseOptions.builder().options(getDatabaseOptions()).createHikariDatabase();
DB.setGlobalDatabase(db);
try {
db.executeUpdate(CREATE_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS rpgregions_discoveries (uuid varchar(32) NOT NULL, region varchar(32) NOT NULL, time varchar(64) NOT NULL, PRIMARY KEY(uuid, region))

View File

@@ -0,0 +1,7 @@
-- Ok, so sqlite does not support altering columns, so we have to do this hacky stuff.
-- I make a new temporary table with the new region varchar(64), copy all the data from the old table into the temp one,
-- then drop the old table and rename the new one to the old one.
CREATE TABLE IF NOT EXISTS migrate_rpgregions_discoveries (uuid varchar(32) NOT NULL, region varchar(64) NOT NULL, time varchar(64) NOT NULL, PRIMARY KEY(uuid, region));
INSERT INTO migrate_rpgregions_discoveries SELECT * FROM rpgregions_discoveries;
DROP TABLE rpgregions_discoveries;
ALTER TABLE migrate_rpgregions_discoveries RENAME TO rpgregions_discoveries;

View File

@@ -7,6 +7,9 @@ libraries:
- "net.kyori:adventure-platform-bukkit:4.3.0"
- "net.kyori:adventure-text-minimessage:4.13.0"
- "com.github.ben-manes.caffeine:caffeine:3.1.5"
- "org.xerial:sqlite-jdbc:3.30.1"
- "org.flywaydb:flyway-core:9.16.2" # db migration
- "org.flywaydb:flyway-mysql:9.16.3"
softdepend: [Hyperverse, Multiverse, UltraRegions, WorldGuard, PlaceholderAPI, HeadDatabase, Residence, Plan, GriefPrevention, GriefDefender, Vault, MythicMobs, AlonsoLevels, dynmap, ProtocolLib, Quests, BetonQuest, Lands, MMOCore, CustomStructures]
authors: [SamB440]
description: Discoverable regions