diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiFlywayMigrateDatabaseCommandTest.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiFlywayMigrateDatabaseCommandTest.java index c34f36e3099..5aa3777d3f4 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiFlywayMigrateDatabaseCommandTest.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/HapiFlywayMigrateDatabaseCommandTest.java @@ -1,6 +1,7 @@ package ca.uhn.fhir.cli; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import ca.uhn.fhir.jpa.migrate.JdbcUtils; import com.google.common.base.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -25,8 +26,7 @@ import java.util.List; import java.util.Map; import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; public class HapiFlywayMigrateDatabaseCommandTest { @@ -38,7 +38,7 @@ public class HapiFlywayMigrateDatabaseCommandTest { } @Test - public void testMigrate() throws IOException { + public void testMigrateFrom340() throws IOException, SQLException { File location = getLocation("migrator_h2_test_340_current"); @@ -61,7 +61,9 @@ public class HapiFlywayMigrateDatabaseCommandTest { "-n", "", "-p", "" }; + assertFalse(JdbcUtils.getTableNames(connectionProperties).contains("HFJ_RES_REINDEX_JOB")); App.main(args); + assertTrue(JdbcUtils.getTableNames(connectionProperties).contains("HFJ_RES_REINDEX_JOB")); connectionProperties.getTxTemplate().execute(t -> { JdbcTemplate jdbcTemplate = connectionProperties.newJdbcTemplate(); @@ -75,6 +77,33 @@ public class HapiFlywayMigrateDatabaseCommandTest { }); } + @Test + public void testMigrateFromEmptySchema() throws IOException, SQLException { + + File location = getLocation("migrator_h2_test_empty_current"); + + String url = "jdbc:h2:" + location.getAbsolutePath() + ";create=true"; + DriverTypeEnum.ConnectionProperties connectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(url, "", ""); + + ourLog.info("**********************************************"); + ourLog.info("Starting Migration..."); + ourLog.info("**********************************************"); + + String[] args = new String[]{ + BaseFlywayMigrateDatabaseCommand.MIGRATE_DATABASE, + "-d", "H2_EMBEDDED", + "-u", url, + "-n", "", + "-p", "" + }; + + assertFalse(JdbcUtils.getTableNames(connectionProperties).contains("HFJ_RESOURCE")); + assertFalse(JdbcUtils.getTableNames(connectionProperties).contains("HFJ_BLK_EXPORT_JOB")); + App.main(args); + assertTrue(JdbcUtils.getTableNames(connectionProperties).contains("HFJ_RESOURCE")); // Early table + assertTrue(JdbcUtils.getTableNames(connectionProperties).contains("HFJ_BLK_EXPORT_JOB")); // Late table + } + @NotNull private File getLocation(String theDatabaseName) throws IOException { File directory = new File(DB_DIRECTORY); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java index ace3fe33602..9619056eeca 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java @@ -63,10 +63,10 @@ public class CalculateHashesTask extends BaseTableColumnTask tableNames = JdbcUtils.getTableNames(getConnectionProperties()); - // This table was added a few days after hash indexes were added, so it is a reasonable indicator for whether this + // This table was added shortly after hash indexes were added, so it is a reasonable indicator for whether this // migration has already been run - if (tableNames.contains("TRM_CONCEPT_DESIG")) { - logInfo(ourLog, "The table TRM_CONCEPT_DESIG already exists. Skipping calculate hashes task."); + if (tableNames.contains("HFJ_RES_REINDEX_JOB")) { + logInfo(ourLog, "The table HFJ_RES_REINDEX_JOB already exists. Skipping calculate hashes task."); return; }