Merge pull request #1729 from jamesagnew/ks-20200204-quiet-migrate-in-test

Ks 20200204 quiet migrate in test
This commit is contained in:
Ken Stevens 2020-02-25 14:04:32 -05:00 committed by GitHub
commit 5656ca5b5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 8 deletions

View File

@ -21,6 +21,7 @@ package ca.uhn.fhir.jpa.migrate;
*/ */
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask; import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
import ca.uhn.fhir.jpa.migrate.taskdef.InitializeSchemaTask;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import org.flywaydb.core.Flyway; import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfoService; import org.flywaydb.core.api.MigrationInfoService;
@ -87,8 +88,12 @@ public class FlywayMigrator extends BaseMigrator {
@Override @Override
public void addTasks(List<BaseTask> theTasks) { public void addTasks(List<BaseTask> theTasks) {
if ("true".equals(System.getProperty("unit_test_mode"))) {
theTasks.stream().filter(task -> task instanceof InitializeSchemaTask).forEach(this::addTask);
} else {
theTasks.forEach(this::addTask); theTasks.forEach(this::addTask);
} }
}
@Override @Override
public Optional<MigrationInfoService> getMigrationInfo() { public Optional<MigrationInfoService> getMigrationInfo() {

View File

@ -108,7 +108,9 @@ public abstract class BaseTask<T extends BaseTask> {
JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate(); JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate();
try { try {
int changesCount = jdbcTemplate.update(theSql, theArguments); int changesCount = jdbcTemplate.update(theSql, theArguments);
if (!"true".equals(System.getProperty("unit_test_mode"))) {
logInfo(ourLog, "SQL \"{}\" returned {}", theSql, changesCount); logInfo(ourLog, "SQL \"{}\" returned {}", theSql, changesCount);
}
return changesCount; return changesCount;
} catch (DataAccessException e) { } catch (DataAccessException e) {
if (myFailureAllowed) { if (myFailureAllowed) {

View File

@ -34,12 +34,13 @@ import java.util.Set;
public class InitializeSchemaTask extends BaseTask<InitializeSchemaTask> { public class InitializeSchemaTask extends BaseTask<InitializeSchemaTask> {
private static final Logger ourLog = LoggerFactory.getLogger(InitializeSchemaTask.class); private static final Logger ourLog = LoggerFactory.getLogger(InitializeSchemaTask.class);
private final ISchemaInitializationProvider mySchemaInitializationProvider; private final ISchemaInitializationProvider mySchemaInitializationProvider;
public InitializeSchemaTask(String theProductVersion, String theSchemaVersion, ISchemaInitializationProvider theSchemaInitializationProvider) { public InitializeSchemaTask(String theProductVersion, String theSchemaVersion, ISchemaInitializationProvider theSchemaInitializationProvider) {
super(theProductVersion, theSchemaVersion); super(theProductVersion, theSchemaVersion);
mySchemaInitializationProvider = theSchemaInitializationProvider; mySchemaInitializationProvider = theSchemaInitializationProvider;
setDescription("Initialize schema"); setDescription("Initialize schema for " + mySchemaInitializationProvider.getSchemaDescription());
} }
@Override @Override
@ -58,13 +59,15 @@ public class InitializeSchemaTask extends BaseTask<InitializeSchemaTask> {
return; return;
} }
logInfo(ourLog, "Initializing schema for {}", driverType); logInfo(ourLog, "Initializing {} schema for {}", driverType, mySchemaInitializationProvider.getSchemaDescription());
List<String> sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType); List<String> sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType);
for (String nextSql : sqlStatements) { for (String nextSql : sqlStatements) {
executeSql(null, nextSql); executeSql(null, nextSql);
} }
logInfo(ourLog, "{} schema for {} initialized successfully", driverType, mySchemaInitializationProvider.getSchemaDescription());
} }
@Override @Override
@ -77,4 +80,8 @@ public class InitializeSchemaTask extends BaseTask<InitializeSchemaTask> {
protected void generateHashCode(HashCodeBuilder theBuilder) { protected void generateHashCode(HashCodeBuilder theBuilder) {
theBuilder.append(mySchemaInitializationProvider); theBuilder.append(mySchemaInitializationProvider);
} }
public ISchemaInitializationProvider getSchemaInitializationProvider() {
return mySchemaInitializationProvider;
}
} }

View File

@ -901,10 +901,10 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
} }
private void init330() { // 20180114 - 20180329 protected void init330() { // 20180114 - 20180329
Builder version = forVersion(VersionEnum.V3_3_0); Builder version = forVersion(VersionEnum.V3_3_0);
version.initializeSchema("20180115.0", new SchemaInitializationProvider("/ca/uhn/hapi/fhir/jpa/docs/database", "HFJ_RESOURCE")); version.initializeSchema("20180115.0", new SchemaInitializationProvider("HAPI FHIR", "/ca/uhn/hapi/fhir/jpa/docs/database", "HFJ_RESOURCE"));
Builder.BuilderWithTableName hfjResource = version.onTable("HFJ_RESOURCE"); Builder.BuilderWithTableName hfjResource = version.onTable("HFJ_RESOURCE");
version.startSectionWithMessage("Starting work on table: " + hfjResource.getTableName()); version.startSectionWithMessage("Starting work on table: " + hfjResource.getTableName());

View File

@ -37,14 +37,17 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
public class SchemaInitializationProvider implements ISchemaInitializationProvider { public class SchemaInitializationProvider implements ISchemaInitializationProvider {
private final String mySchemaFileClassPath; private String mySchemaFileClassPath;
private String mySchemaDescription;
private final String mySchemaExistsIndicatorTable; private final String mySchemaExistsIndicatorTable;
/** /**
* @param theSchemaFileClassPath pathname to script used to initialize schema * @param theSchemaFileClassPath pathname to script used to initialize schema
* @param theSchemaExistsIndicatorTable a table name we can use to determine if this schema has already been initialized * @param theSchemaExistsIndicatorTable a table name we can use to determine if this schema has already been initialized
*/ */
public SchemaInitializationProvider(String theSchemaFileClassPath, String theSchemaExistsIndicatorTable) { public SchemaInitializationProvider(String theSchemaDescription, String theSchemaFileClassPath, String theSchemaExistsIndicatorTable) {
mySchemaDescription = theSchemaDescription;
mySchemaFileClassPath = theSchemaFileClassPath; mySchemaFileClassPath = theSchemaFileClassPath;
mySchemaExistsIndicatorTable = theSchemaExistsIndicatorTable; mySchemaExistsIndicatorTable = theSchemaExistsIndicatorTable;
} }
@ -110,5 +113,21 @@ public class SchemaInitializationProvider implements ISchemaInitializationProvid
public String getSchemaExistsIndicatorTable() { public String getSchemaExistsIndicatorTable() {
return mySchemaExistsIndicatorTable; return mySchemaExistsIndicatorTable;
} }
public SchemaInitializationProvider setSchemaFileClassPath(String theSchemaFileClassPath) {
mySchemaFileClassPath = theSchemaFileClassPath;
return this;
}
@Override
public String getSchemaDescription() {
return mySchemaDescription;
}
@Override
public SchemaInitializationProvider setSchemaDescription(String theSchemaDescription) {
mySchemaDescription = theSchemaDescription;
return this;
}
} }

View File

@ -81,6 +81,13 @@ public class BaseMigrationTasks<T extends Enum> {
return retval; return retval;
} }
protected BaseTask getTaskWithVersion(String theFlywayVersion) {
return myTasks.values().stream()
.filter(task -> theFlywayVersion.equals(task.getFlywayVersion()))
.findFirst()
.get();
}
void validate(Collection<BaseTask> theTasks) { void validate(Collection<BaseTask> theTasks) {
for (BaseTask task: theTasks) { for (BaseTask task: theTasks) {
task.validateVersion(); task.validateVersion();

View File

@ -21,6 +21,7 @@ package ca.uhn.fhir.jpa.migrate.tasks.api;
*/ */
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.tasks.SchemaInitializationProvider;
import java.util.List; import java.util.List;
@ -28,4 +29,8 @@ public interface ISchemaInitializationProvider {
List<String> getSqlStatements(DriverTypeEnum theDriverType); List<String> getSqlStatements(DriverTypeEnum theDriverType);
String getSchemaExistsIndicatorTable(); String getSchemaExistsIndicatorTable();
String getSchemaDescription();
SchemaInitializationProvider setSchemaDescription(String theSchemaDescription);
} }

View File

@ -40,6 +40,11 @@ public class InitializeSchemaTaskTest extends BaseTest {
return "DONT_MATCH_ME"; return "DONT_MATCH_ME";
} }
@Override
public String getSchemaDescription() {
return "TEST";
}
@Override @Override
public boolean equals(Object theO) { public boolean equals(Object theO) {
if (this == theO) return true; if (this == theO) return true;