From c4dd06ad0cbaf9a3b61ada563604ba41d746341f Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 15 Feb 2019 13:27:07 -0500 Subject: [PATCH] Make it easier to add tables with migrator tools --- .../jpa/migrate/taskdef/AddTableByColumnTask.java | 6 ++++-- .../jpa/migrate/tasks/api/BaseMigrationTasks.java | 6 +++++- .../migrate/taskdef/AddTableByColumnTaskTest.java | 12 +++++++++--- .../migrate/tasks/HapiFhirJpaMigrationTasksTest.java | 2 -- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java index b984804a03b..b2c68e0d05a 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java @@ -9,9 +9,9 @@ package ca.uhn.fhir.jpa.migrate.taskdef; * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,6 +21,7 @@ package ca.uhn.fhir.jpa.migrate.taskdef; */ import ca.uhn.fhir.jpa.migrate.JdbcUtils; +import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,6 +37,7 @@ public class AddTableByColumnTask extends BaseTableTask { private String myPkColumn; public void addAddColumnTask(AddColumnTask theTask) { + Validate.notNull(theTask); myAddColumnTasks.add(theTask); } 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 91bba26581c..e53546b7731 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 @@ -353,7 +353,11 @@ public class BaseMigrationTasks { @Override public void addTask(BaseTask theTask) { - myTask.addAddColumnTask((AddColumnTask) theTask); + if (theTask instanceof AddColumnTask) { + myTask.addAddColumnTask((AddColumnTask) theTask); + } else { + super.addTask(theTask); + } } } diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTaskTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTaskTest.java index 658c1f71ab5..72a32454fc7 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTaskTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTaskTest.java @@ -19,9 +19,7 @@ public class AddTableByColumnTaskTest extends BaseTest { getMigrator().addTasks(migrator.getTasks(VersionEnum.V3_3_0, VersionEnum.V3_6_0)); getMigrator().migrate(); - assertThat(JdbcUtils.getTableNames(getConnectionProperties()), containsInAnyOrder("FOO_TABLE")); - - + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), containsInAnyOrder("FOO_TABLE", "TGT_TABLE")); } @@ -29,9 +27,17 @@ public class AddTableByColumnTaskTest extends BaseTest { public MyMigrationTasks() { Builder v = forVersion(VersionEnum.V3_5_0); + + Builder.BuilderWithTableName targetTable = v.addTableByColumns("TGT_TABLE", "PID"); + targetTable.addColumn("PID").nonNullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.LONG); + Builder.BuilderAddTableByColumns fooTable = v.addTableByColumns("FOO_TABLE", "PID"); fooTable.addColumn("PID").nonNullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.LONG); fooTable.addColumn("HELLO").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 200); + fooTable.addColumn("COL_REF").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.LONG); + fooTable.addIndex("IDX_HELLO").unique(true).withColumns("HELLO"); + fooTable.addForeignKey("FK_REF").toColumn("COL_REF").references("TGT_TABLE", "PID"); + } diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasksTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasksTest.java index 4d29e9588a1..d311df1e6e5 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasksTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasksTest.java @@ -11,6 +11,4 @@ public class HapiFhirJpaMigrationTasksTest { new HapiFhirJpaMigrationTasks(Collections.emptySet()); } - - }