down to 16 failed tests

This commit is contained in:
Ken Stevens 2022-09-17 15:21:18 -04:00
parent afcb6e68c9
commit 828e6ebe9f
10 changed files with 58 additions and 19 deletions

View File

@ -46,7 +46,7 @@ public abstract class BaseTest {
dataSource.setUsername("SA");
dataSource.setPassword("SA");
dataSource.setDriverClassName(DriverTypeEnum.H2_EMBEDDED.getDriverClassName());
HapiMigrator migrator = new HapiMigrator(SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, dataSource, DriverTypeEnum.H2_EMBEDDED);
HapiMigrator migrator = new HapiMigrator(DriverTypeEnum.H2_EMBEDDED, dataSource, SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME);
return new TestDatabaseDetails(url, connectionProperties, dataSource, migrator);
}
@ -67,7 +67,7 @@ public abstract class BaseTest {
dataSource.setUsername("SA");
dataSource.setPassword("SA");
dataSource.setDriverClassName(DriverTypeEnum.DERBY_EMBEDDED.getDriverClassName());
HapiMigrator migrator = new HapiMigrator(SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, dataSource, DriverTypeEnum.DERBY_EMBEDDED);
HapiMigrator migrator = new HapiMigrator(DriverTypeEnum.DERBY_EMBEDDED, dataSource, SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME);
return new TestDatabaseDetails(url, connectionProperties, dataSource, migrator);
}

View File

@ -1,4 +1,9 @@
package ca.uhn.fhir.jpa.migrate;
import org.springframework.dao.DataAccessException;
public class HapiMigrationException extends RuntimeException {
public HapiMigrationException(String theMessage, DataAccessException theException) {
super(theMessage, theException);
}
}

View File

@ -1,6 +1,7 @@
package ca.uhn.fhir.jpa.migrate;
import ca.uhn.fhir.jpa.migrate.dao.HapiMigrationDao;
import ca.uhn.fhir.jpa.migrate.entity.HapiMigrationEntity;
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
import org.flywaydb.core.api.MigrationVersion;
@ -35,4 +36,11 @@ public class HapiMigrationStorageSvc {
.reduce((first, second) -> second)
.orElse("unknown");
}
public void saveTask(BaseTask theBaseTask, Integer theMillis, boolean theSuccess) {
HapiMigrationEntity entity = HapiMigrationEntity.fromBaseTask(theBaseTask);
entity.setExecutionTime(theMillis);
entity.setSuccess(theSuccess);
myHapiMigrationDao.save(entity);
}
}

View File

@ -24,6 +24,7 @@ import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.jpa.migrate.dao.HapiMigrationDao;
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.util.StopWatch;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.Validate;
import org.flywaydb.core.api.callback.Callback;
@ -120,6 +121,7 @@ public class HapiMigrator {
next.setNoColumnShrink(isNoColumnShrink());
next.setConnectionProperties(connectionProperties);
StopWatch sw = new StopWatch();
try {
if (isDryRun()) {
ourLog.info("Dry run {} {}", next.getMigrationVersion(), next.getDescription());
@ -129,11 +131,11 @@ public class HapiMigrator {
// FIXME KHS replace with different callback probably a BaseTask consumer
myCallbacks.forEach(action -> action.handle(Event.BEFORE_EACH_MIGRATE, null));
// FIXME KHS break up
// preExecute(next);
next.execute();
// postExecute(next);
postExecute(next, sw, true);
addExecutedStatements(next.getExecutedStatements());
} catch (SQLException e) {
postExecute(next, sw, false);
throw new InternalErrorException(Msg.code(48) + e);
}
}
@ -143,6 +145,10 @@ public class HapiMigrator {
}
}
private void postExecute(BaseTask theNext, StopWatch theStopWatch, boolean theSuccess) {
myHapiMigrationStorageSvc.saveTask(theNext, Math.toIntExact(theStopWatch.getMillis()), theSuccess);
}
public void addTasks(List<BaseTask> theMigrationTasks) {
myTasks.addAll(theMigrationTasks);
}

View File

@ -6,9 +6,7 @@ import org.apache.commons.lang3.Validate;
import org.flywaydb.core.api.MigrationVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import javax.sql.DataSource;
import java.util.Date;
@ -65,19 +63,19 @@ public class HapiMigrationDao {
private Integer getHighestKey() {
String highestKeyQuery = myMigrationQueryBuilder.getHighestKeyQuery();
// FIXME KHS
RowMapper<HapiMigrationEntity> mapper = HapiMigrationEntity.newRowMapper();
List<HapiMigrationEntity> list = myJdbcTemplate.query("SELECT * FROM TEST_MIGRATION_TABLE", mapper);
return myJdbcTemplate.queryForObject(highestKeyQuery, Integer.class);
}
// FIXME KHS use this in migration
public void createMigrationTableIfRequired() {
try {
// FIXME KHS find a better way to detect
fetchMigrationVersions();
} catch (BadSqlGrammarException e) {
} catch (Exception e) {
ourLog.info("Creating table {}", myMigrationTablename);
myJdbcTemplate.execute(myMigrationQueryBuilder.createTableStatement());
String createTableStatement = myMigrationQueryBuilder.createTableStatement();
ourLog.info(createTableStatement);
myJdbcTemplate.execute(createTableStatement);
}
}
}

View File

@ -44,18 +44,26 @@ public class MigrationQueryBuilder {
myInstalledRankCol = myTable.addColumn("INSTALLED_RANK", Types.INTEGER, null);
myInstalledRankCol.notNull();
myVersionCol = myTable.addColumn("VERSION", Types.VARCHAR, HapiMigrationEntity.VERSION_MAX_SIZE);
myDescriptionCol = myTable.addColumn("DESCRIPTION", Types.VARCHAR, HapiMigrationEntity.DESCRIPTION_MAX_SIZE);
myDescriptionCol.notNull();
myTypeCol = myTable.addColumn("TYPE", Types.VARCHAR, HapiMigrationEntity.TYPE_MAX_SIZE);
myTypeCol.notNull();
myScriptCol = myTable.addColumn("SCRIPT", Types.VARCHAR, HapiMigrationEntity.SCRIPT_MAX_SIZE);
myScriptCol.notNull();
myChecksumCol = myTable.addColumn("CHECKSUM", Types.INTEGER, null);
myInstalledByCol = myTable.addColumn("INSTALLED_BY", Types.VARCHAR, HapiMigrationEntity.INSTALLED_BY_MAX_SIZE);
myInstalledByCol.notNull();
myInstalledOnCol = myTable.addColumn("INSTALLED_ON", Types.TIME, null);
myInstalledOnCol = myTable.addColumn("INSTALLED_ON", Types.TIMESTAMP, null);
myInstalledOnCol.notNull();
myExecutionTimeCol = myTable.addColumn("EXECUTION_TIME", Types.INTEGER, null);
myExecutionTimeCol.notNull();
mySuccessCol = myTable.addColumn("SUCCESS", Types.BOOLEAN, null);

View File

@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.HapiMigrationException;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
@ -176,7 +177,7 @@ public abstract class BaseTask {
ourLog.debug("Error was: {}", e.getMessage(), e);
return 0;
} else {
throw new DataAccessException(Msg.code(61) + "Failed during task " + getMigrationVersion() + ": " + e, e) {
throw new HapiMigrationException(Msg.code(61) + "Failed during task " + getMigrationVersion() + ": " + e, e) {
private static final long serialVersionUID = 8211678931579252166L;
};
}

View File

@ -9,7 +9,6 @@ import javax.sql.DataSource;
public abstract class BaseMigrationTest {
private static final String TABLE_NAME = "TEST_MIGRATION_TABLE";
private static final String TEST_DBUSER = "TEST_DBUSER";
protected static HapiMigrationDao ourHapiMigrationDao;
protected static HapiMigrationStorageSvc ourHapiMigrationStorageSvc;

View File

@ -109,7 +109,7 @@ public class SchemaMigratorTest extends BaseTest {
ImmutableList<BaseTask> taskList = ImmutableList.of(taskA, taskB, taskC, taskD);
MigrationTaskSkipper.setDoNothingOnSkippedTasks(taskList, "4_1_0.20191214.2, 4_1_0.20191214.4");
SchemaMigrator schemaMigrator = new SchemaMigrator(getUrl(), SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, getDataSource(), new Properties(), taskList, ourHapiMigrationStorageSvc);
SchemaMigrator schemaMigrator = new SchemaMigrator(getUrl(), SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, getDataSource(), new Properties(), taskList, myHapiMigrationStorageSvc);
schemaMigrator.setDriverType(getDriverType());
schemaMigrator.migrate();
@ -153,7 +153,7 @@ public class SchemaMigratorTest extends BaseTest {
@Nonnull
private SchemaMigrator createSchemaMigrator(BaseTask... tasks) {
SchemaMigrator retVal = new SchemaMigrator(getUrl(), SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, getDataSource(), new Properties(), Lists.newArrayList(tasks), ourHapiMigrationStorageSvc);
SchemaMigrator retVal = new SchemaMigrator(getUrl(), SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME, getDataSource(), new Properties(), Lists.newArrayList(tasks), myHapiMigrationStorageSvc);
retVal.setDriverType(getDriverType());
return retVal;
}

View File

@ -1,10 +1,11 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.BaseMigrationTest;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.HapiMigrationStorageSvc;
import ca.uhn.fhir.jpa.migrate.HapiMigrator;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import ca.uhn.fhir.jpa.migrate.SchemaMigrator;
import ca.uhn.fhir.jpa.migrate.dao.HapiMigrationDao;
import org.apache.commons.dbcp2.BasicDataSource;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.AfterEach;
@ -21,7 +22,7 @@ import java.util.function.Supplier;
import java.util.stream.Stream;
public abstract class BaseTest extends BaseMigrationTest {
public abstract class BaseTest {
private static final String DATABASE_NAME = "DATABASE";
private static final Logger ourLog = LoggerFactory.getLogger(BaseTest.class);
@ -30,9 +31,15 @@ public abstract class BaseTest extends BaseMigrationTest {
private String myUrl;
private HapiMigrator myMigrator;
private DriverTypeEnum.ConnectionProperties myConnectionProperties;
private HapiMigrationDao myHapiMigrationDao;
protected HapiMigrationStorageSvc myHapiMigrationStorageSvc;
static {
// required by Derby
System.setProperty("com.healthmarketscience.sqlbuilder.useBooleanLiterals", "true");
}
public static Stream<Supplier<TestDatabaseDetails>> data() {
ourLog.info("H2: {}", org.h2.Driver.class.toString());
ArrayList<Supplier<TestDatabaseDetails>> retVal = new ArrayList<>();
@ -40,6 +47,7 @@ public abstract class BaseTest extends BaseMigrationTest {
retVal.add(new Supplier<TestDatabaseDetails>() {
@Override
public TestDatabaseDetails get() {
ourLog.info("H2: {}", org.h2.Driver.class.toString());
String url = "jdbc:h2:mem:" + DATABASE_NAME + ourDatabaseUrl++;
DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(url, "SA", "SA");
BasicDataSource dataSource = new BasicDataSource();
@ -61,6 +69,8 @@ public abstract class BaseTest extends BaseMigrationTest {
retVal.add(new Supplier<TestDatabaseDetails>() {
@Override
public TestDatabaseDetails get() {
ourLog.info("Derby: {}", DriverTypeEnum.DERBY_EMBEDDED.getDriverClassName());
String url = "jdbc:derby:memory:" + DATABASE_NAME + ourDatabaseUrl++ + ";create=true";
DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.DERBY_EMBEDDED.newConnectionProperties(url, "SA", "SA");
BasicDataSource dataSource = new BasicDataSource();
@ -87,6 +97,10 @@ public abstract class BaseTest extends BaseMigrationTest {
myConnectionProperties = testDatabaseDetails.myConnectionProperties;
myDataSource = testDatabaseDetails.myDataSource;
myMigrator = testDatabaseDetails.myMigrator;
myHapiMigrationDao = new HapiMigrationDao(testDatabaseDetails.myDataSource, SchemaMigrator.HAPI_FHIR_MIGRATION_TABLENAME);
myHapiMigrationDao.createMigrationTableIfRequired();
myHapiMigrationStorageSvc = new HapiMigrationStorageSvc(myHapiMigrationDao);
}
public String getUrl() {