fix drop foreign key task

This commit is contained in:
Ken Stevens 2019-09-05 15:51:38 -04:00
parent 080be22eeb
commit 14fadd3004
6 changed files with 33 additions and 17 deletions

View File

@ -185,7 +185,11 @@ public class JdbcUtils {
DatabaseMetaData metadata; DatabaseMetaData metadata;
try { try {
metadata = connection.getMetaData(); 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<String> columnNames = new HashSet<>(); Set<String> columnNames = new HashSet<>();
while (indexes.next()) { while (indexes.next()) {

View File

@ -30,26 +30,32 @@ import java.util.Set;
import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank;
public class DropForeignKeyTask extends BaseTableColumnTask<DropForeignKeyTask> { public class DropForeignKeyTask extends BaseTableTask<DropForeignKeyTask> {
private static final Logger ourLog = LoggerFactory.getLogger(DropForeignKeyTask.class); private static final Logger ourLog = LoggerFactory.getLogger(DropForeignKeyTask.class);
private String myConstraintName; private String myConstraintName;
private String myForeignTableName;
public void setConstraintName(String theConstraintName) { public void setConstraintName(String theConstraintName) {
myConstraintName = theConstraintName; myConstraintName = theConstraintName;
} }
public void setForeignTableName(String theForeignTableName) {
myForeignTableName = theForeignTableName;
}
@Override @Override
public void validate() { public void validate() {
super.validate(); super.validate();
Validate.isTrue(isNotBlank(myConstraintName)); Validate.isTrue(isNotBlank(myConstraintName));
Validate.isTrue(isNotBlank(myForeignTableName));
} }
@Override @Override
public void execute() throws SQLException { public void execute() throws SQLException {
Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), null, null); Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), getTableName(), myForeignTableName);
if (!existing.contains(myConstraintName)) { if (!existing.contains(myConstraintName)) {
ourLog.info("Don't have constraint named {} - No action performed", myConstraintName); ourLog.info("Don't have constraint named {} - No action performed", myConstraintName);
return; return;

View File

@ -88,8 +88,8 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
.toColumn("VALUESET_PID") .toColumn("VALUESET_PID")
.references("TRM_VALUESET", "PID"); .references("TRM_VALUESET", "PID");
// Drop HFJ_SEARCH_RESULT foreign keys // Drop HFJ_SEARCH_RESULT foreign keys
version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_RES"); version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_RES", "HFJ_RESOURCE");
version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_SEARCH"); version.onTable("HFJ_SEARCH_RESULT").dropForeignKey("FK_SEARCHRES_SEARCH", "HFJ_SEARCH");
// TermValueSet // TermValueSet
version.startSectionWithMessage("Processing table: TRM_VALUESET"); version.startSectionWithMessage("Processing table: TRM_VALUESET");

View File

@ -241,9 +241,15 @@ public class BaseMigrationTasks<T extends Enum> {
return this; 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(); DropForeignKeyTask task = new DropForeignKeyTask();
task.setConstraintName(theFkName); task.setConstraintName(theFkName);
task.setForeignTableName(theForeignTableName);
task.setTableName(getTableName()); task.setTableName(getTableName());
addTask(task); addTask(task);
} }

View File

@ -1,12 +1,12 @@
package ca.uhn.fhir.jpa.migrate.taskdef; package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import com.google.common.collect.Lists;
import org.junit.Test; import org.junit.Test;
import java.sql.SQLException; 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; import static org.junit.Assert.assertThat;
public class AddIndexTest extends BaseTest { public class AddIndexTest extends BaseTest {

View File

@ -1,33 +1,33 @@
package ca.uhn.fhir.jpa.migrate.taskdef; package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import java.sql.SQLException; import java.sql.SQLException;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
public class DropForeignKeyTaskTest extends BaseTest { public class DropForeignKeyTaskTest extends BaseTest {
@Test @Test
public void testDropForeignKey() throws SQLException { public void testDropForeignKey() throws SQLException {
executeSql("create table HOME (PID bigint not null, TEXTCOL varchar(255), primary key (PID))"); executeSql("create table PARENT (PID bigint not null, TEXTCOL varchar(255), primary key (PID))");
executeSql("create table FOREIGNTBL (PID bigint not null, HOMEREF bigint)"); executeSql("create table CHILD (PID bigint not null, PARENTREF bigint)");
executeSql("alter table HOME add foreign key FK_FOO (PID) references FOREIGNTABLE(HOMEREF)"); 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(); DropForeignKeyTask task = new DropForeignKeyTask();
task.setTableName("FOREIGNTBL"); task.setTableName("PARENT");
task.setColumnName("HOMEREF"); task.setForeignTableName("CHILD");
task.setConstraintName("FK_FOO"); task.setConstraintName("FK_MOM");
getMigrator().addTask(task); getMigrator().addTask(task);
getMigrator().migrate(); getMigrator().migrate();
assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "HOME", "FOREIGNTBL"), empty()); assertThat(JdbcUtils.getForeignKeys(getConnectionProperties(), "PARENT", "CHILD"), empty());
// Make sure additional calls don't crash // Make sure additional calls don't crash
getMigrator().migrate(); getMigrator().migrate();