From 828e6ebe9f3bc328b0b36c20e1b583af9f34f5ae Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Sat, 17 Sep 2022 15:21:18 -0400 Subject: [PATCH] down to 16 failed tests --- .../fhir/jpa/migrate/taskdef/BaseTest.java | 4 ++-- .../jpa/migrate/HapiMigrationException.java | 5 +++++ .../jpa/migrate/HapiMigrationStorageSvc.java | 8 ++++++++ .../ca/uhn/fhir/jpa/migrate/HapiMigrator.java | 10 ++++++++-- .../jpa/migrate/dao/HapiMigrationDao.java | 12 +++++------ .../migrate/dao/MigrationQueryBuilder.java | 10 +++++++++- .../fhir/jpa/migrate/taskdef/BaseTask.java | 3 ++- .../fhir/jpa/migrate/BaseMigrationTest.java | 1 - .../fhir/jpa/migrate/SchemaMigratorTest.java | 4 ++-- .../fhir/jpa/migrate/taskdef/BaseTest.java | 20 ++++++++++++++++--- 10 files changed, 58 insertions(+), 19 deletions(-) diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java index ebe3e5bbabc..f4970ea8ee1 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java @@ -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); } diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationException.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationException.java index b5f51d3ba50..6582932ecb2 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationException.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationException.java @@ -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); + } } diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationStorageSvc.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationStorageSvc.java index ce821e1fa63..7b92f0f33d2 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationStorageSvc.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrationStorageSvc.java @@ -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); + } } diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrator.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrator.java index f67b215ec64..3f2b247e485 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrator.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/HapiMigrator.java @@ -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 theMigrationTasks) { myTasks.addAll(theMigrationTasks); } diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/HapiMigrationDao.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/HapiMigrationDao.java index ac2eed1ddaa..4ebf1796e92 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/HapiMigrationDao.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/HapiMigrationDao.java @@ -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 mapper = HapiMigrationEntity.newRowMapper(); - List 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); } } } diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/MigrationQueryBuilder.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/MigrationQueryBuilder.java index a366a5b8291..1bf35983f75 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/MigrationQueryBuilder.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/dao/MigrationQueryBuilder.java @@ -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); diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java index 2935f466c35..ffe350ed93c 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java @@ -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; }; } diff --git a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/BaseMigrationTest.java b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/BaseMigrationTest.java index fa0552b8693..96c6ed8a211 100644 --- a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/BaseMigrationTest.java +++ b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/BaseMigrationTest.java @@ -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; diff --git a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java index ef5c187f07a..aa8a392c496 100644 --- a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java +++ b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/SchemaMigratorTest.java @@ -109,7 +109,7 @@ public class SchemaMigratorTest extends BaseTest { ImmutableList 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; } diff --git a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java index c7c8ae1e946..efa2feb1369 100644 --- a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java +++ b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTest.java @@ -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> data() { - ourLog.info("H2: {}", org.h2.Driver.class.toString()); ArrayList> retVal = new ArrayList<>(); @@ -40,6 +47,7 @@ public abstract class BaseTest extends BaseMigrationTest { retVal.add(new Supplier() { @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() { @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() {