changed to use hibernate auto ddl flag as a check

This commit is contained in:
Ken Stevens 2019-10-31 15:47:52 -04:00
parent f95e4d387d
commit 0cfea8a871
3 changed files with 22 additions and 15 deletions

View File

@ -6,31 +6,42 @@ import ca.uhn.fhir.jpa.subscription.module.subscriber.websocket.WebsocketConnect
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import org.flywaydb.core.api.MigrationInfo; import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.MigrationInfoService; import org.flywaydb.core.api.MigrationInfoService;
import org.flywaydb.core.api.MigrationVersion; import org.hibernate.cfg.AvailableSettings;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Properties;
public class MigrationValidator { public class MigrationValidator {
private static final Logger ourLog = LoggerFactory.getLogger(WebsocketConnectionValidator.class); private static final Logger ourLog = LoggerFactory.getLogger(MigrationValidator.class);
private final BasicDataSource myDataSource; private final BasicDataSource myDataSource;
private final FlywayMigrator myMigrator; private final FlywayMigrator myMigrator;
private final boolean mySkipValidation;
public MigrationValidator(BasicDataSource theDataSource, List<BaseTask<?>> theMigrationTasks) { public MigrationValidator(BasicDataSource theDataSource, Properties jpaProperties, List<BaseTask<?>> theMigrationTasks) {
myDataSource = theDataSource; 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(theDataSource);
myMigrator.addTasks(theMigrationTasks); myMigrator.addTasks(theMigrationTasks);
} }
public void validate() { public void validate() {
if (mySkipValidation) {
ourLog.info("Database running in hibernate auto-update mode. Skipping schema validation.");
return;
}
try (Connection connection = myDataSource.getConnection()) { try (Connection connection = myDataSource.getConnection()) {
MigrationInfoService migrationInfo = myMigrator.getMigrationInfo(); MigrationInfoService migrationInfo = myMigrator.getMigrationInfo();
if (migrationInfo.pending().length > 0) { if (migrationInfo.pending().length > 0) {
throw new ConfigurationException("The database schema for " + connection.getCatalog() + " is out of date. " + throw new ConfigurationException("The database schema for " + myDataSource.getUrl() + " is out of date. " +
"Current database schema version is " + getCurrentVersion(migrationInfo) + ". Schema version required by application is " + "Current database schema version is " + getCurrentVersion(migrationInfo) + ". Schema version required by application is " +
getLastVersion(migrationInfo) + ". Please run the database migrator."); getLastVersion(migrationInfo) + ". Please run the database migrator.");
} }

View File

@ -3,12 +3,13 @@ package ca.uhn.fhir.jpa.migrate;
import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.jpa.migrate.taskdef.AddTableRawSqlTask; import ca.uhn.fhir.jpa.migrate.taskdef.AddTableRawSqlTask;
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTest; import ca.uhn.fhir.jpa.migrate.taskdef.BaseTest;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Collections; import java.util.Collections;
import java.util.Properties;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class MigrationValidatorTest extends BaseTest { public class MigrationValidatorTest extends BaseTest {
@ -19,12 +20,13 @@ public class MigrationValidatorTest 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);
MigrationValidator migrationValidator = new MigrationValidator(getDataSource(), Collections.singletonList(task)); MigrationValidator migrationValidator = new MigrationValidator(getDataSource(), new Properties(), Collections.singletonList(task));
try { try {
migrationValidator.validate(); migrationValidator.validate();
fail(); fail();
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
assertEquals("The database schema for " + getDatabaseName() + " 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()); 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(); getMigrator().migrate();
migrationValidator.validate(); migrationValidator.validate();

View File

@ -2,7 +2,6 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
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.Migrator;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import org.intellij.lang.annotations.Language; import org.intellij.lang.annotations.Language;
import org.junit.After; import org.junit.After;
@ -32,8 +31,7 @@ public class BaseTest {
@Before() @Before()
public void before() { public void before() {
org.h2.Driver.class.toString(); org.h2.Driver.class.toString();
++ourDatabaseUrl; myUrl = "jdbc:h2:mem:" + DATABASE_NAME + ourDatabaseUrl++;
myUrl = "jdbc:h2:mem:" + getDatabaseName();
myConnectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(myUrl, "SA", "SA"); myConnectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(myUrl, "SA", "SA");
myDataSource.setUrl(myUrl); myDataSource.setUrl(myUrl);
@ -43,10 +41,6 @@ public class BaseTest {
myMigrator = new FlywayMigrator(myDataSource); myMigrator = new FlywayMigrator(myDataSource);
} }
protected String getDatabaseName() {
return DATABASE_NAME + ourDatabaseUrl;
}
protected BasicDataSource getDataSource() { protected BasicDataSource getDataSource() {
return myDataSource; return myDataSource;
} }