fix drop foreign key task
This commit is contained in:
parent
080be22eeb
commit
14fadd3004
|
@ -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<String> columnNames = new HashSet<>();
|
||||
while (indexes.next()) {
|
||||
|
|
|
@ -30,26 +30,32 @@ import java.util.Set;
|
|||
|
||||
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 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<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), null, null);
|
||||
Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), getTableName(), myForeignTableName);
|
||||
if (!existing.contains(myConstraintName)) {
|
||||
ourLog.info("Don't have constraint named {} - No action performed", myConstraintName);
|
||||
return;
|
||||
|
|
|
@ -88,8 +88,8 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
|
|||
.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");
|
||||
|
|
|
@ -241,9 +241,15 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue