diff --git a/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java b/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java index de9aaa39..5bfc9986 100644 --- a/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java +++ b/src/main/java/org/sgrewritten/stargate/migration/DataMigration9.java @@ -26,7 +26,7 @@ import java.util.Set; /** - * A data migrator to migrate from 1.0.0.14 to 1.0.0.16 + * A data migrator to migrate from 1.0.0.14 to 1.0.0.17 */ public class DataMigration9 extends DataMigration { private final Properties configConversions = loadConfigConversions("/migration/config-migrations-9.properties"); @@ -44,6 +44,12 @@ public void run(@NotNull SQLDatabaseAPI database, StargateAPI stargateAPI) { } catch (SQLException | IOException e) { Stargate.log(e); } + + try { + new SQLDatabaseMigrator(database, tableNameConfiguration, "/migration/database/v-9", isInterServer).run(); + } catch (SQLException | IOException e) { + Stargate.log(e); + } } @Override diff --git a/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_FLAG_RELATION.sql b/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_FLAG_RELATION.sql index 4748d09a..d29d8924 100644 --- a/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_FLAG_RELATION.sql +++ b/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_FLAG_RELATION.sql @@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS {InterPortalFlagRelation} name, network ) - ON UPDATE CASCADE, + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY (flag) REFERENCES {Flag} (id) ); \ No newline at end of file diff --git a/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_POSITION.sql b/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_POSITION.sql index f29f5d36..6cde66e2 100644 --- a/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_POSITION.sql +++ b/src/main/resources/database/sqlite-queries/CREATE_TABLE_INTER_PORTAL_POSITION.sql @@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS {InterPortalPosition} name, network ) - ON UPDATE CASCADE, + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY (positionType) REFERENCES {PositionType} (id) ); \ No newline at end of file diff --git a/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_FLAG_RELATION.sql b/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_FLAG_RELATION.sql index 22fe9e70..dad72f24 100644 --- a/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_FLAG_RELATION.sql +++ b/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_FLAG_RELATION.sql @@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS {PortalFlagRelation} name, network ) - ON UPDATE CASCADE, + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY (flag) REFERENCES {Flag} (id) ); \ No newline at end of file diff --git a/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_POSITION.sql b/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_POSITION.sql index 8ffe14f5..64949a8f 100644 --- a/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_POSITION.sql +++ b/src/main/resources/database/sqlite-queries/CREATE_TABLE_PORTAL_POSITION.sql @@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS {PortalPosition} name, network ) - ON UPDATE CASCADE, + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY (positionType) REFERENCES {PositionType} (id) ); \ No newline at end of file diff --git a/src/main/resources/migration/database/v-9/inter_server/step0.sql b/src/main/resources/migration/database/v-9/inter_server/step0.sql new file mode 100644 index 00000000..94a50c4d --- /dev/null +++ b/src/main/resources/migration/database/v-9/inter_server/step0.sql @@ -0,0 +1,46 @@ +/* + * Add a update on cascade constraint to the InterPortalPosition table + * This is the only way of doing it in sqlite (by recreating the table) + */ + + +CREATE TABLE IF NOT EXISTS {InterPortalPosition}1 +( + portalName NVARCHAR (180) NOT NULL, + networkName NVARCHAR (180) NOT NULL, + xCoordinate INTEGER NOT NULL, + yCoordinate INTEGER NOT NULL, + zCoordinate INTEGER NOT NULL, + positionType INTEGER NOT NULL, + metaData TEXT, + pluginName VARCHAR(255) DEFAULT "Stargate", + PRIMARY KEY + ( + portalName, + networkName, + xCoordinate, + yCoordinate, + zCoordinate + ), + FOREIGN KEY + ( + portalName, + networkName + ) + REFERENCES {InterPortal} + ( + name, + network + ) + ON UPDATE CASCADE + ON DELETE CASCADE, + FOREIGN KEY (positionType) REFERENCES {PositionType} (id) +); + +INSERT INTO {InterPortalPosition}1 SELECT * +FROM + {InterPortalPosition}; + +DROP TABLE {InterPortalPosition}; + +ALTER TABLE {InterPortalPosition}1 RENAME TO {InterPortalPosition}; \ No newline at end of file diff --git a/src/main/resources/migration/database/v-9/inter_server/step1.sql b/src/main/resources/migration/database/v-9/inter_server/step1.sql new file mode 100644 index 00000000..7f6ece60 --- /dev/null +++ b/src/main/resources/migration/database/v-9/inter_server/step1.sql @@ -0,0 +1,42 @@ +/* + * Add a update on cascade constraint to the InterPortalFlagRelation table + * This is the only way of doing it in sqlite (by recreating the table) + */ + +CREATE TABLE IF NOT EXISTS {InterPortalFlagRelation}1 +( + name NVARCHAR (180) NOT NULL, + network NVARCHAR (180) NOT NULL, + flag INTEGER NOT NULL, + PRIMARY KEY + ( + name, + network, + flag + ), + FOREIGN KEY + ( + name, + network + ) + REFERENCES {InterPortal} + ( + name, + network + ) + ON UPDATE CASCADE + ON DELETE CASCADE, + FOREIGN KEY (flag) REFERENCES {Flag} (id) +); + +INSERT INTO {InterPortalFlagRelation}1 SELECT * +FROM + {InterPortalFlagRelation}; + +DROP VIEW {InterPortalView}; + +DROP TABLE {InterPortalFlagRelation}; + +ALTER TABLE {InterPortalFlagRelation}1 RENAME TO {InterPortalFlagRelation}; + +CREATE_VIEW_INTER_PORTAL; \ No newline at end of file diff --git a/src/main/resources/migration/database/v-9/local/step0.sql b/src/main/resources/migration/database/v-9/local/step0.sql new file mode 100644 index 00000000..efd64675 --- /dev/null +++ b/src/main/resources/migration/database/v-9/local/step0.sql @@ -0,0 +1,45 @@ +/* + * Add a update on cascade constraint to the PortalPosition table + * This is the only way of doing it in sqlite (by recreating the table) + */ + +CREATE TABLE IF NOT EXISTS {PortalPosition}1 +( + portalName NVARCHAR (180) NOT NULL, + networkName NVARCHAR (180) NOT NULL, + xCoordinate INTEGER NOT NULL, + yCoordinate INTEGER NOT NULL, + zCoordinate INTEGER NOT NULL, + positionType INTEGER NOT NULL, + metaData TEXT, + pluginName VARCHAR(255) DEFAULT "Stargate", + PRIMARY KEY + ( + portalName, + networkName, + xCoordinate, + yCoordinate, + zCoordinate + ), + FOREIGN KEY + ( + portalName, + networkName + ) + REFERENCES {Portal} + ( + name, + network + ) + ON UPDATE CASCADE + ON DELETE CASCADE, + FOREIGN KEY (positionType) REFERENCES {PositionType} (id) +); + +INSERT INTO {PortalPosition}1 SELECT * +FROM + {PortalPosition}; + +DROP TABLE {PortalPosition}; + +ALTER TABLE {PortalPosition}1 RENAME TO {PortalPosition}; \ No newline at end of file diff --git a/src/main/resources/migration/database/v-9/local/step1.sql b/src/main/resources/migration/database/v-9/local/step1.sql new file mode 100644 index 00000000..12e90286 --- /dev/null +++ b/src/main/resources/migration/database/v-9/local/step1.sql @@ -0,0 +1,42 @@ +/* + * Add a update on cascade constraint to the InterPortalFlagRelation table + * This is the only way of doing it in sqlite (by recreating the table) + */ + +CREATE TABLE IF NOT EXISTS {PortalFlagRelation}1 +( + name NVARCHAR (180) NOT NULL, + network NVARCHAR (180) NOT NULL, + flag INTEGER NOT NULL, + PRIMARY KEY + ( + name, + network, + flag + ), + FOREIGN KEY + ( + name, + network + ) + REFERENCES {Portal} + ( + name, + network + ) + ON UPDATE CASCADE + ON DELETE CASCADE, + FOREIGN KEY (flag) REFERENCES {Flag} (id) +); + +INSERT INTO {PortalFlagRelation}1 SELECT * +FROM + {PortalFlagRelation}; + +DROP VIEW {PortalView}; + +DROP TABLE {PortalFlagRelation}; + +ALTER TABLE {PortalFlagRelation}1 RENAME TO {PortalFlagRelation}; + +CREATE_VIEW_PORTAL; \ No newline at end of file diff --git a/src/test/java/org/sgrewritten/stargate/migration/SQLDatabaseMigratorTest.java b/src/test/java/org/sgrewritten/stargate/migration/SQLDatabaseMigratorTest.java index 605231da..356ca7ec 100644 --- a/src/test/java/org/sgrewritten/stargate/migration/SQLDatabaseMigratorTest.java +++ b/src/test/java/org/sgrewritten/stargate/migration/SQLDatabaseMigratorTest.java @@ -18,11 +18,12 @@ class SQLDatabaseMigratorTest { - private SQLDatabaseMigrator databaseMigrator; + private SQLDatabaseMigrator databaseMigrator7; private SQLiteDatabase database; private TableNameConfiguration nameConfiguration; private static final File sqlDatabaseFile = new File("src/test/resources", "alpha-1_0_0_11.db"); private static final File oldSqlDatabaseFile = new File("src/test/resources", "alpha-1_0_0_11.old"); + private SQLDatabaseMigrator databaseMigrator9; @BeforeEach void setUp() throws SQLException, IOException { @@ -31,7 +32,8 @@ void setUp() throws SQLException, IOException { database = new SQLiteDatabase(sqlDatabaseFile); nameConfiguration = new TableNameConfiguration("", ""); - databaseMigrator = new SQLDatabaseMigrator(database, nameConfiguration, "/migration/database/v-7", true); + databaseMigrator7 = new SQLDatabaseMigrator(database, nameConfiguration, "/migration/database/v-7", true); + databaseMigrator9 = new SQLDatabaseMigrator(database, nameConfiguration, "/migration/database/v-9", true); } @AfterEach @@ -43,7 +45,8 @@ void tearDown() { // CHECK IF THE UPDATE ON CASCADE OPTION IS THERE, BY LOOKING AT THE BEHAVIOR @Test void renamePortalPosition() throws SQLException, IOException { - databaseMigrator.run(); + databaseMigrator7.run(); + databaseMigrator9.run(); renamePortal(nameConfiguration.getPortalTableName()); try (Connection connection = database.getConnection()) { SQLTestHelper.checkIfHasNot(nameConfiguration.getPortalPositionTableName(), "portal", "network", @@ -54,7 +57,8 @@ void renamePortalPosition() throws SQLException, IOException { @Test void renameInterPortalPosition() throws SQLException, IOException { - databaseMigrator.run(); + databaseMigrator7.run(); + databaseMigrator9.run(); renamePortal(nameConfiguration.getInterPortalTableName()); try (Connection connection = database.getConnection()) { SQLTestHelper.checkIfHasNot(nameConfiguration.getInterPortalPositionTableName(), "portal", "network", @@ -66,7 +70,8 @@ void renameInterPortalPosition() throws SQLException, IOException { @Test void renamePortalFlag() throws SQLException, IOException { - databaseMigrator.run(); + databaseMigrator7.run(); + databaseMigrator9.run(); renamePortal(nameConfiguration.getPortalTableName()); try (Connection connection = database.getConnection()) { SQLTestHelper.checkIfHasNot(nameConfiguration.getFlagRelationTableName(), "portal", "network", connection); @@ -76,7 +81,8 @@ void renamePortalFlag() throws SQLException, IOException { @Test void renameInterPortalFlag() throws SQLException, IOException { - databaseMigrator.run(); + databaseMigrator7.run(); + databaseMigrator9.run(); renamePortal(nameConfiguration.getInterPortalTableName()); try (Connection connection = database.getConnection()) { SQLTestHelper.checkIfHasNot(nameConfiguration.getInterFlagRelationTableName(), "portal", "network", @@ -88,7 +94,8 @@ void renameInterPortalFlag() throws SQLException, IOException { @Test void portalPosition_checkPluginName() throws SQLException, IOException { - databaseMigrator.run(); + databaseMigrator7.run(); + databaseMigrator9.run(); try (Connection connection = database.getConnection()) { SQLTestHelper.checkIfColumnIs(nameConfiguration.getPortalPositionTableName(), "pluginName", "portal", "network", "Stargate", connection); }