From 14fadd30045a932fc10ac820a2b25f4cbbc65c7f Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Thu, 5 Sep 2019 15:51:38 -0400 Subject: [PATCH] fix drop foreign key task --- .../ca/uhn/fhir/jpa/migrate/JdbcUtils.java | 6 +++++- .../migrate/taskdef/DropForeignKeyTask.java | 10 ++++++++-- .../tasks/HapiFhirJpaMigrationTasks.java | 4 ++-- .../migrate/tasks/api/BaseMigrationTasks.java | 8 +++++++- .../fhir/jpa/migrate/taskdef/AddIndexTest.java | 4 ++-- .../taskdef/DropForeignKeyTaskTest.java | 18 +++++++++--------- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java index 014de578bd4..32810675705 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java @@ -185,7 +185,11 @@ public class JdbcUtils { DatabaseMetaData metadata; try { metadata = connection.getMetaData(); - ResultSet indexes = metadata.getCrossReference(connection.getCatalog(), connection.getSchema(), massageIdentifier(metadata, theTableName), connection.getCatalog(), connection.getSchema(), massageIdentifier(metadata, theForeignTable)); + String catalog = connection.getCatalog(); + String schema = connection.getSchema(); + String parentTable = massageIdentifier(metadata, theTableName); + String foreignTable = massageIdentifier(metadata, theForeignTable); + ResultSet indexes = metadata.getCrossReference(catalog, schema, parentTable, catalog, schema, foreignTable); Set columnNames = new HashSet<>(); while (indexes.next()) { 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 3d777691df6..bea3f581096 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 @@ -30,26 +30,32 @@ import java.util.Set; import static org.apache.commons.lang3.StringUtils.isNotBlank; -public class DropForeignKeyTask extends BaseTableColumnTask { +public class DropForeignKeyTask extends BaseTableTask { private static final Logger ourLog = LoggerFactory.getLogger(DropForeignKeyTask.class); private String myConstraintName; + private String myForeignTableName; public void setConstraintName(String theConstraintName) { myConstraintName = theConstraintName; } + public void setForeignTableName(String theForeignTableName) { + myForeignTableName = theForeignTableName; + } + @Override public void validate() { super.validate(); Validate.isTrue(isNotBlank(myConstraintName)); + Validate.isTrue(isNotBlank(myForeignTableName)); } @Override public void execute() throws SQLException { - Set existing = JdbcUtils.getForeignKeys(getConnectionProperties(), null, null); + Set existing = JdbcUtils.getForeignKeys(getConnectionProperties(), getTableName(), myForeignTableName); if (!existing.contains(myConstraintName)) { ourLog.info("Don't have constraint named {} - No action performed", myConstraintName); return; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java index a2d2efce7b4..6973e2cb268 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java @@ -88,8 +88,8 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { .toColumn("VALUESET_PID") .references("TRM_VALUESET", "PID"); // Drop HFJ_SEARCH_RESULT foreign keys - version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_RES"); - version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_SEARCH"); + version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_RES", "HFJ_RESOURCE"); + version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_SEARCH", "HFJ_SEARCH"); // TermValueSet version.startSectionWithMessage("Processing table: TRM_VALUESET"); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java index d536578fd8b..890f573ff6b 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java @@ -241,9 +241,15 @@ public class BaseMigrationTasks { return this; } - public void dropForeignKey(String theFkName) { + /** + * + * @param theFkName the name of the foreign key + * @param theForeignTableName the name of the table that imports the foreign key (I know it's a confusing name, but it's what java.sql.DatabaseMetaData calls it) + */ + public void dropForeignKey(String theFkName, String theForeignTableName) { DropForeignKeyTask task = new DropForeignKeyTask(); task.setConstraintName(theFkName); + task.setForeignTableName(theForeignTableName); task.setTableName(getTableName()); addTask(task); } diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTest.java index 6e21947a2e7..0f78c9e1ad7 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTest.java @@ -1,12 +1,12 @@ package ca.uhn.fhir.jpa.migrate.taskdef; import ca.uhn.fhir.jpa.migrate.JdbcUtils; -import com.google.common.collect.Lists; import org.junit.Test; import java.sql.SQLException; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertThat; public class AddIndexTest extends BaseTest { diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTaskTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTaskTest.java index 4150d24c30e..ae6d3b17f02 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTaskTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropForeignKeyTaskTest.java @@ -1,33 +1,33 @@ package ca.uhn.fhir.jpa.migrate.taskdef; import ca.uhn.fhir.jpa.migrate.JdbcUtils; -import org.hamcrest.Matchers; import org.junit.Test; import java.sql.SQLException; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.junit.Assert.assertThat; public class DropForeignKeyTaskTest extends BaseTest { @Test public void testDropForeignKey() throws SQLException { - executeSql("create table HOME (PID bigint not null, TEXTCOL varchar(255), primary key (PID))"); - executeSql("create table FOREIGNTBL (PID bigint not null, HOMEREF bigint)"); - executeSql("alter table HOME add foreign key FK_FOO (PID) references FOREIGNTABLE(HOMEREF)"); + executeSql("create table PARENT (PID bigint not null, TEXTCOL varchar(255), primary key (PID))"); + executeSql("create table CHILD (PID bigint not null, PARENTREF bigint)"); + executeSql("alter table CHILD add constraint FK_MOM foreign key (PARENTREF) references PARENT(PID)"); - assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "HOME", "FOREIGNTBL"), empty()); + assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "PARENT", "CHILD"), hasSize(1)); DropForeignKeyTask task = new DropForeignKeyTask(); - task.setTableName("FOREIGNTBL"); - task.setColumnName("HOMEREF"); - task.setConstraintName("FK_FOO"); + task.setTableName("PARENT"); + task.setForeignTableName("CHILD"); + task.setConstraintName("FK_MOM"); getMigrator().addTask(task); getMigrator().migrate(); - assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "HOME", "FOREIGNTBL"), empty()); + assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "PARENT", "CHILD"), empty()); // Make sure additional calls don't crash getMigrator().migrate();