persistence migrate from zero works.
This commit is contained in:
parent
49265dea32
commit
7066f35e49
|
@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.migrate;
|
|||
* #L%
|
||||
*/
|
||||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
|
@ -40,16 +41,24 @@ public class FlywayMigrator {
|
|||
private String myConnectionUrl;
|
||||
private String myUsername;
|
||||
private String myPassword;
|
||||
// FIXME KHS
|
||||
// private String myDefaultSchema;
|
||||
private List<FlywayMigration> myTasks = new ArrayList<>();
|
||||
private boolean myDryRun;
|
||||
private boolean myNoColumnShrink;
|
||||
|
||||
public FlywayMigrator() {}
|
||||
|
||||
// FIXME KHS test with nonexistent database
|
||||
public FlywayMigrator(BasicDataSource theDataSource) {
|
||||
myConnectionUrl = theDataSource.getUrl();
|
||||
myUsername = theDataSource.getUsername();
|
||||
myPassword = theDataSource.getPassword();
|
||||
// myDefaultSchema = theDataSource.getDefaultSchema();
|
||||
// FIXME KHS
|
||||
// if (myDefaultSchema == null) {
|
||||
// myDefaultSchema = "kentest";
|
||||
// }
|
||||
String driverClassName = theDataSource.getDriverClassName();
|
||||
if (driverClassName == null) {
|
||||
ourLog.error(this.getClass().getSimpleName() + " constructed without a database driver");
|
||||
|
@ -89,11 +98,18 @@ public class FlywayMigrator {
|
|||
try (DriverTypeEnum.ConnectionProperties connectionProperties = myDriverType.newConnectionProperties(myConnectionUrl, myUsername, myPassword)) {
|
||||
Flyway flyway = initFlyway(connectionProperties);
|
||||
flyway.migrate();
|
||||
} catch (Exception e) {
|
||||
throw new ConfigurationException("Failed to migrate schema", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) {
|
||||
// FIXME KHS instantiate from database, not this other stuff
|
||||
// FIXME KHS ensure we have a default schema
|
||||
|
||||
Flyway flyway = Flyway.configure()
|
||||
// FIXME KHS required?
|
||||
// .schemas(myDefaultSchema)
|
||||
.dataSource(myConnectionUrl, myUsername, myPassword)
|
||||
.baselineOnMigrate(true)
|
||||
.javaMigrations(myTasks.toArray(new JavaMigration[0]))
|
||||
|
|
|
@ -2,7 +2,6 @@ package ca.uhn.fhir.jpa.migrate;
|
|||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
|
||||
import ca.uhn.fhir.jpa.subscription.module.subscriber.websocket.WebsocketConnectionValidator;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.flywaydb.core.api.MigrationInfo;
|
||||
import org.flywaydb.core.api.MigrationInfoService;
|
||||
|
@ -15,14 +14,14 @@ import java.sql.SQLException;
|
|||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MigrationValidator {
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(MigrationValidator.class);
|
||||
public class SchemaMigrator {
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(SchemaMigrator.class);
|
||||
|
||||
private final BasicDataSource myDataSource;
|
||||
private final FlywayMigrator myMigrator;
|
||||
private final boolean mySkipValidation;
|
||||
|
||||
public MigrationValidator(BasicDataSource theDataSource, Properties jpaProperties, List<BaseTask<?>> theMigrationTasks) {
|
||||
public SchemaMigrator(BasicDataSource theDataSource, Properties jpaProperties, List<BaseTask<?>> theMigrationTasks) {
|
||||
myDataSource = theDataSource;
|
||||
if (jpaProperties.containsKey(AvailableSettings.HBM2DDL_AUTO) && "update".equals(jpaProperties.getProperty(AvailableSettings.HBM2DDL_AUTO))) {
|
||||
mySkipValidation = true;
|
||||
|
@ -51,6 +50,14 @@ public class MigrationValidator {
|
|||
}
|
||||
}
|
||||
|
||||
public void migrate() {
|
||||
if (mySkipValidation) {
|
||||
ourLog.info("Database running in hibernate auto-update mode. Skipping schema migration.");
|
||||
return;
|
||||
}
|
||||
myMigrator.migrate();
|
||||
}
|
||||
|
||||
private String getCurrentVersion(MigrationInfoService theMigrationInfo) {
|
||||
MigrationInfo migrationInfo = theMigrationInfo.current();
|
||||
if (migrationInfo == null) {
|
|
@ -26,9 +26,12 @@ public abstract class BaseSchemaInitializationProvider implements ISchemaInitial
|
|||
if (sqlFileInputStream == null) {
|
||||
throw new ConfigurationException("Schema initialization script " + initScript + " not found on classpath");
|
||||
}
|
||||
LineIterator iterator = IOUtils.lineIterator(sqlFileInputStream, Charsets.UTF_8);
|
||||
while (iterator.hasNext()) {
|
||||
retval.add(iterator.nextLine());
|
||||
// Assumes no escaped semicolons...
|
||||
String[] statements = IOUtils.toString(sqlFileInputStream, Charsets.UTF_8).split("\\;");
|
||||
for (String statement : statements) {
|
||||
if (!statement.trim().isEmpty()) {
|
||||
retval.add(statement);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ConfigurationException("Error reading schema initialization script " + initScript, e);
|
||||
|
|
|
@ -11,7 +11,7 @@ import java.util.Properties;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class MigrationValidatorTest extends BaseTest {
|
||||
public class SchemaMigratorTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void migrationRequired() {
|
||||
|
@ -20,15 +20,15 @@ public class MigrationValidatorTest extends BaseTest {
|
|||
task.addSql(DriverTypeEnum.H2_EMBEDDED, "create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))");
|
||||
getMigrator().addTask(task);
|
||||
|
||||
MigrationValidator migrationValidator = new MigrationValidator(getDataSource(), new Properties(), Collections.singletonList(task));
|
||||
SchemaMigrator schemaMigrator = new SchemaMigrator(getDataSource(), new Properties(), Collections.singletonList(task));
|
||||
|
||||
try {
|
||||
migrationValidator.validate();
|
||||
schemaMigrator.validate();
|
||||
fail();
|
||||
} catch (ConfigurationException e) {
|
||||
assertEquals("The database schema for " + getUrl() + " is out of date. Current database schema version is unknown. Schema version required by application is " + task.getFlywayVersion() + ". Please run the database migrator.", e.getMessage());
|
||||
}
|
||||
getMigrator().migrate();
|
||||
migrationValidator.validate();
|
||||
schemaMigrator.validate();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue