make migration table name configurable

This commit is contained in:
Ken Stevens 2019-11-26 09:07:53 -05:00
parent 8945e54346
commit 3d00634b16
5 changed files with 17 additions and 9 deletions

View File

@ -22,6 +22,7 @@ package ca.uhn.fhir.cli;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.FlywayMigrator; 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.CommandLine;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
@ -37,6 +38,7 @@ import static org.apache.commons.lang3.StringUtils.defaultString;
public abstract class BaseFlywayMigrateDatabaseCommand<T extends Enum> extends BaseCommand { public abstract class BaseFlywayMigrateDatabaseCommand<T extends Enum> extends BaseCommand {
public static final String MIGRATE_DATABASE = "migrate-database"; public static final String MIGRATE_DATABASE = "migrate-database";
private Set<String> myFlags; private Set<String> myFlags;
@ -108,7 +110,7 @@ public abstract class BaseFlywayMigrateDatabaseCommand<T extends Enum> extends B
.filter(StringUtils::isNotBlank) .filter(StringUtils::isNotBlank)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
FlywayMigrator migrator = new FlywayMigrator(); FlywayMigrator migrator = new FlywayMigrator(SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME);
migrator.setConnectionUrl(url); migrator.setConnectionUrl(url);
migrator.setDriverType(driverType); migrator.setDriverType(driverType);
migrator.setUsername(username); migrator.setUsername(username);

View File

@ -37,6 +37,7 @@ public class FlywayMigrator {
private static final Logger ourLog = LoggerFactory.getLogger(FlywayMigrator.class); private static final Logger ourLog = LoggerFactory.getLogger(FlywayMigrator.class);
private DriverTypeEnum myDriverType; private DriverTypeEnum myDriverType;
private final String myMigrationTableName;
private String myConnectionUrl; private String myConnectionUrl;
private String myUsername; private String myUsername;
private String myPassword; private String myPassword;
@ -44,9 +45,8 @@ public class FlywayMigrator {
private boolean myDryRun; private boolean myDryRun;
private boolean myNoColumnShrink; private boolean myNoColumnShrink;
public FlywayMigrator() {} public FlywayMigrator(String theMigrationTableName, BasicDataSource theDataSource) {
this(theMigrationTableName);
public FlywayMigrator(BasicDataSource theDataSource) {
myConnectionUrl = theDataSource.getUrl(); myConnectionUrl = theDataSource.getUrl();
myUsername = theDataSource.getUsername(); myUsername = theDataSource.getUsername();
myPassword = theDataSource.getPassword(); myPassword = theDataSource.getPassword();
@ -62,6 +62,10 @@ public class FlywayMigrator {
} }
} }
public FlywayMigrator(String theMigrationTableName) {
myMigrationTableName = theMigrationTableName;
}
public void setDriverType(DriverTypeEnum theDriverType) { public void setDriverType(DriverTypeEnum theDriverType) {
myDriverType = theDriverType; myDriverType = theDriverType;
} }
@ -97,7 +101,8 @@ public class FlywayMigrator {
private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) { private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) {
// TODO KHS Is there a way we can use datasource instead of url, username, password here // 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) .dataSource(myConnectionUrl, myUsername, myPassword)
.baselineOnMigrate(true) .baselineOnMigrate(true)
.javaMigrations(myTasks.toArray(new JavaMigration[0])) .javaMigrations(myTasks.toArray(new JavaMigration[0]))

View File

@ -16,19 +16,20 @@ import java.util.Properties;
public class SchemaMigrator { public class SchemaMigrator {
private static final Logger ourLog = LoggerFactory.getLogger(SchemaMigrator.class); 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 BasicDataSource myDataSource;
private final FlywayMigrator myMigrator; private final FlywayMigrator myMigrator;
private final boolean mySkipValidation; private final boolean mySkipValidation;
public SchemaMigrator(BasicDataSource theDataSource, Properties jpaProperties, List<BaseTask<?>> theMigrationTasks) { public SchemaMigrator(String theMigrationTableName, BasicDataSource theDataSource, Properties jpaProperties, List<BaseTask<?>> theMigrationTasks) {
myDataSource = theDataSource; myDataSource = theDataSource;
if (jpaProperties.containsKey(AvailableSettings.HBM2DDL_AUTO) && "update".equals(jpaProperties.getProperty(AvailableSettings.HBM2DDL_AUTO))) { if (jpaProperties.containsKey(AvailableSettings.HBM2DDL_AUTO) && "update".equals(jpaProperties.getProperty(AvailableSettings.HBM2DDL_AUTO))) {
mySkipValidation = true; mySkipValidation = true;
} else { } else {
mySkipValidation = false; mySkipValidation = false;
} }
myMigrator = new FlywayMigrator(theDataSource); myMigrator = new FlywayMigrator(theMigrationTableName, theDataSource);
myMigrator.addTasks(theMigrationTasks); myMigrator.addTasks(theMigrationTasks);
} }

View File

@ -20,7 +20,7 @@ public class SchemaMigratorTest extends BaseTest {
task.addSql(DriverTypeEnum.H2_EMBEDDED, "create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))"); task.addSql(DriverTypeEnum.H2_EMBEDDED, "create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))");
getMigrator().addTask(task); 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 { try {
schemaMigrator.validate(); schemaMigrator.validate();

View File

@ -41,7 +41,7 @@ public class BaseTest {
myDataSource.setUsername("SA"); myDataSource.setUsername("SA");
myDataSource.setPassword("SA"); myDataSource.setPassword("SA");
myDataSource.setDriverClassName(DriverTypeEnum.H2_EMBEDDED.getDriverClassName()); myDataSource.setDriverClassName(DriverTypeEnum.H2_EMBEDDED.getDriverClassName());
myMigrator = new FlywayMigrator(myDataSource); myMigrator = new FlywayMigrator("TEST_MIGRATION", myDataSource);
} }
protected BasicDataSource getDataSource() { protected BasicDataSource getDataSource() {