From 464b1ac20268da15bdf111a4d5a758bea89de8e4 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 29 Aug 2022 13:39:25 +0100 Subject: [PATCH] Add MySQL migration script --- .../huskhomes/migrator/LegacyMigrator.java | 22 ++++++- .../resources/migrator/mysql_migrator.sql | 65 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 common/src/main/resources/migrator/mysql_migrator.sql diff --git a/bukkit/src/main/java/net/william278/huskhomes/migrator/LegacyMigrator.java b/bukkit/src/main/java/net/william278/huskhomes/migrator/LegacyMigrator.java index 22f9195f..adf32874 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/migrator/LegacyMigrator.java +++ b/bukkit/src/main/java/net/william278/huskhomes/migrator/LegacyMigrator.java @@ -85,7 +85,7 @@ public class LegacyMigrator extends Migrator { "%target_homes_table%", plugin.getSettings().getTableName(Settings.TableName.HOME_DATA), "%source_warps_table%", sourceWarpsDataTable, "%target_warps_table%", plugin.getSettings().getTableName(Settings.TableName.WARP_DATA))) - .thenRun(() -> plugin.getLoggingAdapter().log(Level.INFO, "Migration complete!")) + .thenRun(() -> plugin.getLoggingAdapter().log(Level.INFO, "SQLite Migration complete!")) .exceptionally(e -> { plugin.getLoggingAdapter().log(Level.SEVERE, "Migration of SQLite database failed!", e); return null; @@ -93,8 +93,24 @@ public class LegacyMigrator extends Migrator { } private void migrateMySqlDatabase() { - plugin.getLoggingAdapter().log(Level.INFO, "Migrating same MySQL database..."); - //todo write script + plugin.getLoggingAdapter().log(Level.INFO, "Migrating MySQL database, please wait... This may take a while!"); + + // Execute the migration script + plugin.getDatabase().runScript(Objects.requireNonNull(plugin.getResource("migrator/mysql_migrator.sql")), + Map.of("%target_positions_table%", plugin.getSettings().getTableName(Settings.TableName.POSITION_DATA), + "%source_positions_table%", sourceLocationsDataTable, + "%target_users_table%", plugin.getSettings().getTableName(Settings.TableName.PLAYER_DATA), + "%source_users_table%", sourcePlayerDataTable, + "%target_saved_positions%", plugin.getSettings().getTableName(Settings.TableName.SAVED_POSITION_DATA), + "%source_homes_table%", sourceHomesDataTable, + "%target_homes_table%", plugin.getSettings().getTableName(Settings.TableName.HOME_DATA), + "%source_warps_table%", sourceWarpsDataTable, + "%target_warps_table%", plugin.getSettings().getTableName(Settings.TableName.WARP_DATA))) + .thenRun(() -> plugin.getLoggingAdapter().log(Level.INFO, "MySQL Migration complete!")) + .exceptionally(e -> { + plugin.getLoggingAdapter().log(Level.SEVERE, "Migration of MySQL database failed!", e); + return null; + }); } @Override diff --git a/common/src/main/resources/migrator/mysql_migrator.sql b/common/src/main/resources/migrator/mysql_migrator.sql new file mode 100644 index 00000000..cfa46c52 --- /dev/null +++ b/common/src/main/resources/migrator/mysql_migrator.sql @@ -0,0 +1,65 @@ +# MySQL migration script. + +# Migrate positions to the new schema +INSERT INTO `%target_positions_table%` (`id`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`) +SELECT `location_id` AS `id`, + `x`, + `y`, + `z`, + `yaw`, + `pitch`, + `world` AS `world_name`, + '00000000-0000-0000-0000-000000000000' AS `world_uuid`, + `server` AS `server_name` +FROM `%source_positions_table%`; + +# Migrate users to the new schema +INSERT INTO `%target_users_table%` (`uuid`, `username`, `last_position`, `offline_position`, `home_slots`, + `ignoring_requests`) +SELECT `user_uuid` AS `uuid`, + `username`, + `last_location_id` AS `last_position`, + `offline_location_id` AS `offline_position`, + `home_slots`, + `is_ignoring_requests` AS `ignoring_requests` +FROM `%source_users_table%`; + +# Migrate homes to the new schema +INSERT INTO `%target_saved_positions%` (`position_id`, `name`, `description`, `timestamp`) +SELECT `location_id` AS `position_id`, + `name`, + `description`, + `creation_time` AS `timestamp` +FROM `%source_homes_table%`; + +INSERT INTO `%target_homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`) +SELECT UUID() AS `uuid`, + `id` AS `saved_position_id`, + `user_uuid` AS `owner_uuid`, + `public` +FROM `%source_homes_table%` + INNER JOIN `%target_saved_positions%` + ON `%source_homes_table%`.`location_id` = `%target_saved_positions%`.`position_id` + INNER JOIN `%source_users_table%` + ON `%source_homes_table%`.`player_id` = `%source_users_table%`.`player_id`; + +# Migrate warps to the new schema +INSERT INTO `%target_saved_positions%` (`position_id`, `name`, `description`, `timestamp`) +SELECT `location_id` AS `position_id`, + `name`, + `description`, + `creation_time` AS `timestamp` +FROM `%source_warps_table%`; + +INSERT INTO `%target_warps_table%` (`uuid`, `saved_position_id`) +SELECT UUID() AS `uuid`, + `id` AS `saved_position_id` +FROM `%source_warps_table%` + INNER JOIN `%target_saved_positions%` + ON `%source_warps_table%`.`location_id` = `%target_saved_positions%`.`position_id`; + +-- Rename old tables +ALTER TABLE `%source_positions_table%` RENAME TO `old_%source_positions_table%`; +ALTER TABLE `%source_users_table%` RENAME TO `old_%source_users_table%`; +ALTER TABLE `%source_homes_table%` RENAME TO `old_%source_homes_table%`; +ALTER TABLE `%source_warps_table%` RENAME TO `old_%source_warps_table%`; \ No newline at end of file