From 053094f2a697c3766062e1aa4aee002c1ba47f00 Mon Sep 17 00:00:00 2001 From: ianmarshall Date: Wed, 24 Jun 2020 17:42:46 -0400 Subject: [PATCH] Apply recent fixes for MySQL migration tasks to MariaDB as well. --- .../java/ca/uhn/fhir/jpa/migrate/taskdef/AddColumnTask.java | 2 +- .../java/ca/uhn/fhir/jpa/migrate/taskdef/DropColumnTask.java | 5 +++-- .../ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTask.java | 2 +- .../java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java | 4 +--- .../ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java | 4 +--- .../ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java | 2 +- .../jpa/migrate/taskdef/RenameColumnTaskDbSpecificTest.java | 2 +- 7 files changed, 9 insertions(+), 12 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddColumnTask.java index bf76353ce9a..fe9e0f0f5a8 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddColumnTask.java @@ -54,11 +54,11 @@ public class AddColumnTask extends BaseTableColumnTypeTask { String sql; switch (getDriverType()) { case MYSQL_5_7: + case MARIADB_10_1: // Quote the column name as "SYSTEM" is a reserved word in MySQL sql = "alter table " + getTableName() + " add column `" + getColumnName() + "` " + typeStatement; break; case DERBY_EMBEDDED: - case MARIADB_10_1: case POSTGRES_9_4: sql = "alter table " + getTableName() + " add column " + getColumnName() + " " + typeStatement; break; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropColumnTask.java index 8b7ff289526..9b1d7816e31 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropColumnTask.java @@ -26,6 +26,7 @@ import org.intellij.lang.annotations.Language; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.sql.Driver; import java.sql.SQLException; import java.util.List; import java.util.Set; @@ -52,8 +53,8 @@ public class DropColumnTask extends BaseTableColumnTask { return; } - if(getDriverType().equals(DriverTypeEnum.MYSQL_5_7)) { - // Some DBs such as MYSQL require that foreign keys depending on the column be dropped before the column itself is dropped. + if(getDriverType().equals(DriverTypeEnum.MYSQL_5_7) || getDriverType().equals(DriverTypeEnum.MARIADB_10_1)) { + // Some DBs such as MYSQL and Maria DB require that foreign keys depending on the column be dropped before the column itself is dropped. logInfo(ourLog, "Dropping any foreign keys on table {} depending on column {}", getTableName(), getColumnName()); Set foreignKeys = JdbcUtils.getForeignKeysForColumn(getConnectionProperties(), getColumnName(), getTableName()); if(foreignKeys != null) { diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTask.java index 088682ffc1d..ac0f156bff5 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTask.java @@ -101,10 +101,10 @@ public class DropForeignKeyTask extends BaseTableTask { List sqls = new ArrayList<>(); switch (theDriverType) { case MYSQL_5_7: + case MARIADB_10_1: // Lousy MYQL.... sqls.add("alter table " + theTableName + " drop foreign key " + theConstraintName); break; - case MARIADB_10_1: case POSTGRES_9_4: case DERBY_EMBEDDED: case H2_EMBEDDED: diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java index cbac30507c0..9d5b3171f3b 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java @@ -107,12 +107,10 @@ public class DropIndexTask extends BaseTableTask { // Drop constraint switch (theDriverType) { case MYSQL_5_7: + case MARIADB_10_1: // Need to quote the index name as the word "PRIMARY" is reserved in MySQL sql.add("alter table " + theTableName + " drop index `" + theIndexName + "`"); break; - case MARIADB_10_1: - sql.add("alter table " + theTableName + " drop index " + theIndexName); - break; case H2_EMBEDDED: sql.add("drop index " + theIndexName); break; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java index 7e40827ffa1..49abd9b5a83 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java @@ -139,10 +139,8 @@ public class RenameColumnTask extends BaseTableTask { case DERBY_EMBEDDED: sql = "RENAME COLUMN " + getTableName() + "." + myOldName + " TO " + myNewName; break; - case MARIADB_10_1: - sql = "ALTER TABLE " + getTableName() + " CHANGE COLUMN " + myOldName + " TO " + myNewName; - break; case MYSQL_5_7: + case MARIADB_10_1: // Quote the column names as "SYSTEM" is a reserved word in MySQL sql = "ALTER TABLE " + getTableName() + " CHANGE COLUMN `" + myOldName + "` `" + myNewName + "` " + theExistingType + " " + theExistingNotNull; break; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java index b2edd22caf2..ebaf40f78d2 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java @@ -110,10 +110,10 @@ public class RenameIndexTask extends BaseTableTask { // Drop constraint switch (theDriverType) { case MYSQL_5_7: + case MARIADB_10_1: // Quote the index names as "PRIMARY" is a reserved word in MySQL sql.add("rename index `" + theOldIndexName + "` to `" + theNewIndexName + "`"); break; - case MARIADB_10_1: case DERBY_EMBEDDED: sql.add("rename index " + theOldIndexName + " to " + theNewIndexName); break; diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTaskDbSpecificTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTaskDbSpecificTest.java index 9c4fec4fe74..cd88accb134 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTaskDbSpecificTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTaskDbSpecificTest.java @@ -27,7 +27,7 @@ public class RenameColumnTaskDbSpecificTest { @Test public void testBuildSqlStatementForMariaDB() { - assertEquals("ALTER TABLE SOMETABLE CHANGE COLUMN myTextCol TO TEXTCOL", createRenameColumnSql(DriverTypeEnum.MARIADB_10_1)); + assertEquals("ALTER TABLE SOMETABLE CHANGE COLUMN `myTextCol` `TEXTCOL` integer null", createRenameColumnSql(DriverTypeEnum.MARIADB_10_1)); } @Test