diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java index 4a8e4e81d00..95d82fa305a 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java @@ -22,6 +22,7 @@ package ca.uhn.fhir.cli; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.FlywayMigrator; +import ca.uhn.fhir.jpa.migrate.SchemaMigrator; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; @@ -37,6 +38,7 @@ import static org.apache.commons.lang3.StringUtils.defaultString; public abstract class BaseFlywayMigrateDatabaseCommand extends BaseCommand { + public static final String MIGRATE_DATABASE = "migrate-database"; private Set myFlags; @@ -108,7 +110,7 @@ public abstract class BaseFlywayMigrateDatabaseCommand extends B .filter(StringUtils::isNotBlank) .collect(Collectors.toSet()); - FlywayMigrator migrator = new FlywayMigrator(); + FlywayMigrator migrator = new FlywayMigrator(SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME); migrator.setConnectionUrl(url); migrator.setDriverType(driverType); migrator.setUsername(username); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java index 3853cb926a7..a10981d50be 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java @@ -37,6 +37,7 @@ public class FlywayMigrator { private static final Logger ourLog = LoggerFactory.getLogger(FlywayMigrator.class); private DriverTypeEnum myDriverType; + private final String myMigrationTableName; private String myConnectionUrl; private String myUsername; private String myPassword; @@ -44,9 +45,8 @@ public class FlywayMigrator { private boolean myDryRun; private boolean myNoColumnShrink; - public FlywayMigrator() {} - - public FlywayMigrator(BasicDataSource theDataSource) { + public FlywayMigrator(String theMigrationTableName, BasicDataSource theDataSource) { + this(theMigrationTableName); myConnectionUrl = theDataSource.getUrl(); myUsername = theDataSource.getUsername(); myPassword = theDataSource.getPassword(); @@ -62,6 +62,10 @@ public class FlywayMigrator { } } + public FlywayMigrator(String theMigrationTableName) { + myMigrationTableName = theMigrationTableName; + } + public void setDriverType(DriverTypeEnum theDriverType) { myDriverType = theDriverType; } @@ -97,7 +101,8 @@ public class FlywayMigrator { private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) { // TODO KHS Is there a way we can use datasource instead of url, username, password here - Flyway flyway = Flyway.configure() + Flyway flyway = Flyway.configure() + .table(myMigrationTableName) .dataSource(myConnectionUrl, myUsername, myPassword) .baselineOnMigrate(true) .javaMigrations(myTasks.toArray(new JavaMigration[0])) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java index 2bed73a9eeb..3eb6cc7d58a 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java @@ -16,19 +16,20 @@ import java.util.Properties; public class SchemaMigrator { private static final Logger ourLog = LoggerFactory.getLogger(SchemaMigrator.class); + public static final String HAPI_FHIR_MIGRATION_TABLENAME = "FLY_HFJ_MIGRATION"; private final BasicDataSource myDataSource; private final FlywayMigrator myMigrator; private final boolean mySkipValidation; - public SchemaMigrator(BasicDataSource theDataSource, Properties jpaProperties, List> theMigrationTasks) { + public SchemaMigrator(String theMigrationTableName, BasicDataSource theDataSource, Properties jpaProperties, List> theMigrationTasks) { myDataSource = theDataSource; if (jpaProperties.containsKey(AvailableSettings.HBM2DDL_AUTO) && "update".equals(jpaProperties.getProperty(AvailableSettings.HBM2DDL_AUTO))) { mySkipValidation = true; } else { mySkipValidation = false; } - myMigrator = new FlywayMigrator(theDataSource); + myMigrator = new FlywayMigrator(theMigrationTableName, theDataSource); myMigrator.addTasks(theMigrationTasks); } diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java index df27b704802..498fb7859f9 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java @@ -20,7 +20,7 @@ public class SchemaMigratorTest extends BaseTest { task.addSql(DriverTypeEnum.H2_EMBEDDED, "create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))"); getMigrator().addTask(task); - SchemaMigrator schemaMigrator = new SchemaMigrator(getDataSource(), new Properties(), Collections.singletonList(task)); + SchemaMigrator schemaMigrator = new SchemaMigrator("TEST_MIGRATION", getDataSource(), new Properties(), Collections.singletonList(task)); try { schemaMigrator.validate(); diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java index 29aabf4245b..8cea316944f 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java @@ -41,7 +41,7 @@ public class BaseTest { myDataSource.setUsername("SA"); myDataSource.setPassword("SA"); myDataSource.setDriverClassName(DriverTypeEnum.H2_EMBEDDED.getDriverClassName()); - myMigrator = new FlywayMigrator(myDataSource); + myMigrator = new FlywayMigrator("TEST_MIGRATION", myDataSource); } protected BasicDataSource getDataSource() {