From 469873aff7a573d9d894bc4e8ff2f6118bdd04b8 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 29 Nov 2023 09:16:41 -0500 Subject: [PATCH] Add migrator to uhnfhirtest (#5508) * Add migrator to uhnfhirtest * Fix spotless * Test fix --- .../HapiFlywayMigrateDatabaseCommandTest.java | 10 ++-- .../tasks/HapiFhirJpaMigrationTasks.java | 49 ++++++++++-------- .../ca/uhn/fhirtest/config/CommonConfig.java | 6 +++ .../uhn/fhirtest/config/TestAuditConfig.java | 2 +- .../uhn/fhirtest/config/TestDstu2Config.java | 2 +- .../uhn/fhirtest/config/TestDstu3Config.java | 2 +- .../ca/uhn/fhirtest/config/TestR4BConfig.java | 2 +- .../ca/uhn/fhirtest/config/TestR4Config.java | 2 +- .../ca/uhn/fhirtest/config/TestR5Config.java | 2 +- .../migrate/FhirTestAutoMigrator.java | 51 +++++++++++++++++++ 10 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/migrate/FhirTestAutoMigrator.java 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 cebbf9c9a8b..31b74d0b5ab 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 @@ -5,11 +5,14 @@ import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.jpa.migrate.SchemaMigrator; import ca.uhn.fhir.jpa.migrate.dao.HapiMigrationDao; import ca.uhn.fhir.jpa.migrate.entity.HapiMigrationEntity; +import ca.uhn.fhir.jpa.util.RandomTextUtils; import ca.uhn.fhir.system.HapiSystemProperties; import com.google.common.base.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; @@ -35,10 +38,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +@TestMethodOrder(MethodOrderer.MethodName.class) public class HapiFlywayMigrateDatabaseCommandTest { private static final Logger ourLog = LoggerFactory.getLogger(HapiFlywayMigrateDatabaseCommandTest.class); - public static final String DB_DIRECTORY = "target/h2_test"; + private final String myDbDirectory = "target/h2_test/" + RandomTextUtils.newSecureRandomAlphaNumericString(5); static { HapiSystemProperties.enableTestMode(); @@ -252,12 +256,12 @@ public class HapiFlywayMigrateDatabaseCommandTest { @Nonnull private File getLocation(String theDatabaseName) throws IOException { - File directory = new File(DB_DIRECTORY); + File directory = new File(myDbDirectory); if (directory.exists()) { FileUtils.deleteDirectory(directory); } - return new File(DB_DIRECTORY + "/" + theDatabaseName); + return new File(myDbDirectory + "/" + theDatabaseName); } private void seedDatabase340(DriverTypeEnum.ConnectionProperties theConnectionProperties) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java index a0f718dceb8..f480b23ed6e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java @@ -977,27 +977,34 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { // Ugh. Only oracle supports using IDX_TAG_DEF_TP_CD_SYS to enforce this constraint. The others will // create another index. // For Sql Server, should change the index to be unique with include columns. Do this in 6.1 - tagTable.dropIndex("20220429.8", "IDX_TAGDEF_TYPESYSCODE"); - Map addTagDefConstraint = new HashMap<>(); - addTagDefConstraint.put( - DriverTypeEnum.H2_EMBEDDED, - "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)"); - addTagDefConstraint.put( - DriverTypeEnum.MARIADB_10_1, - "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)"); - addTagDefConstraint.put( - DriverTypeEnum.MSSQL_2012, - "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)"); - addTagDefConstraint.put( - DriverTypeEnum.MYSQL_5_7, - "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)"); - addTagDefConstraint.put( - DriverTypeEnum.ORACLE_12C, - "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)"); - addTagDefConstraint.put( - DriverTypeEnum.POSTGRES_9_4, - "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, TAG_SYSTEM)"); - version.executeRawSql("20220429.9", addTagDefConstraint); + // tagTable.dropIndex("20220429.8", "IDX_TAGDEF_TYPESYSCODE"); + // Map addTagDefConstraint = new HashMap<>(); + // addTagDefConstraint.put( + // DriverTypeEnum.H2_EMBEDDED, + // "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, + // TAG_SYSTEM)"); + // addTagDefConstraint.put( + // DriverTypeEnum.MARIADB_10_1, + // "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, + // TAG_SYSTEM)"); + // addTagDefConstraint.put( + // DriverTypeEnum.MSSQL_2012, + // "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, + // TAG_SYSTEM)"); + // addTagDefConstraint.put( + // DriverTypeEnum.MYSQL_5_7, + // "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, + // TAG_SYSTEM)"); + // addTagDefConstraint.put( + // DriverTypeEnum.ORACLE_12C, + // "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, + // TAG_SYSTEM)"); + // addTagDefConstraint.put( + // DriverTypeEnum.POSTGRES_9_4, + // "ALTER TABLE HFJ_TAG_DEF ADD CONSTRAINT IDX_TAGDEF_TYPESYSCODE UNIQUE (TAG_TYPE, TAG_CODE, + // TAG_SYSTEM)"); + // version.executeRawSql("20220429.9", addTagDefConstraint); + version.addNop("20220429.9"); } // Fix for https://github.com/hapifhir/hapi-fhir-jpaserver-starter/issues/328 diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java index b28a9367462..1d84422e1a2 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java @@ -23,6 +23,7 @@ import ca.uhn.fhir.storage.interceptor.balp.IBalpAuditEventSink; import ca.uhn.fhirtest.ScheduledSubscriptionDeleter; import ca.uhn.fhirtest.interceptor.AnalyticsInterceptor; import ca.uhn.fhirtest.joke.HolyFooCowInterceptor; +import ca.uhn.fhirtest.migrate.FhirTestAutoMigrator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -131,6 +132,11 @@ public class CommonConfig { return new FhirTestBalpAuditContextServices(); } + @Bean + public FhirTestAutoMigrator migrator() { + return new FhirTestAutoMigrator(); + } + public static boolean isLocalTestMode() { return "true".equalsIgnoreCase(System.getProperty("testmode.local")); } diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestAuditConfig.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestAuditConfig.java index bd6c6690f16..03e838b3296 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestAuditConfig.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestAuditConfig.java @@ -120,7 +120,7 @@ public class TestAuditConfig { } extraProperties.put("hibernate.format_sql", "false"); extraProperties.put("hibernate.show_sql", "false"); - extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.hbm2ddl.auto", "none"); extraProperties.put("hibernate.jdbc.batch_size", "20"); extraProperties.put("hibernate.cache.use_query_cache", "false"); extraProperties.put("hibernate.cache.use_second_level_cache", "false"); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java index 7d957240c90..a3e4f203b2a 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java @@ -138,7 +138,7 @@ public class TestDstu2Config { } extraProperties.put("hibernate.format_sql", "false"); extraProperties.put("hibernate.show_sql", "false"); - extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.hbm2ddl.auto", "none"); extraProperties.put("hibernate.jdbc.batch_size", "20"); extraProperties.put("hibernate.cache.use_query_cache", "false"); extraProperties.put("hibernate.cache.use_second_level_cache", "false"); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu3Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu3Config.java index da6b07ac91b..0e4e285b9a2 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu3Config.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu3Config.java @@ -138,7 +138,7 @@ public class TestDstu3Config { } extraProperties.put("hibernate.format_sql", "false"); extraProperties.put("hibernate.show_sql", "false"); - extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.hbm2ddl.auto", "none"); extraProperties.put("hibernate.jdbc.batch_size", "20"); extraProperties.put("hibernate.cache.use_query_cache", "false"); extraProperties.put("hibernate.cache.use_second_level_cache", "false"); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4BConfig.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4BConfig.java index 454faeab90a..bafbdc2daf3 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4BConfig.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4BConfig.java @@ -136,7 +136,7 @@ public class TestR4BConfig { } extraProperties.put("hibernate.format_sql", "false"); extraProperties.put("hibernate.show_sql", "false"); - extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.hbm2ddl.auto", "none"); extraProperties.put("hibernate.jdbc.batch_size", "20"); extraProperties.put("hibernate.cache.use_query_cache", "false"); extraProperties.put("hibernate.cache.use_second_level_cache", "false"); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4Config.java index f95782ccc96..1c62b7a5225 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4Config.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR4Config.java @@ -139,7 +139,7 @@ public class TestR4Config { } extraProperties.put("hibernate.format_sql", "false"); extraProperties.put("hibernate.show_sql", "false"); - extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.hbm2ddl.auto", "none"); extraProperties.put("hibernate.jdbc.batch_size", "20"); extraProperties.put("hibernate.cache.use_query_cache", "false"); extraProperties.put("hibernate.cache.use_second_level_cache", "false"); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR5Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR5Config.java index 2870a925c32..e255f2627cb 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR5Config.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestR5Config.java @@ -148,7 +148,7 @@ public class TestR5Config { } extraProperties.put("hibernate.format_sql", "false"); extraProperties.put("hibernate.show_sql", "false"); - extraProperties.put("hibernate.hbm2ddl.auto", "update"); + extraProperties.put("hibernate.hbm2ddl.auto", "none"); extraProperties.put("hibernate.jdbc.batch_size", "20"); extraProperties.put("hibernate.cache.use_query_cache", "false"); extraProperties.put("hibernate.cache.use_second_level_cache", "false"); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/migrate/FhirTestAutoMigrator.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/migrate/FhirTestAutoMigrator.java new file mode 100644 index 00000000000..0de60267015 --- /dev/null +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/migrate/FhirTestAutoMigrator.java @@ -0,0 +1,51 @@ +package ca.uhn.fhirtest.migrate; + +import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import ca.uhn.fhir.jpa.migrate.HapiMigrationStorageSvc; +import ca.uhn.fhir.jpa.migrate.MigrationTaskList; +import ca.uhn.fhir.jpa.migrate.SchemaMigrator; +import ca.uhn.fhir.jpa.migrate.dao.HapiMigrationDao; +import ca.uhn.fhir.jpa.migrate.tasks.HapiFhirJpaMigrationTasks; +import ca.uhn.fhir.util.VersionEnum; +import ca.uhn.fhirtest.config.CommonConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Properties; +import java.util.Set; +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +public class FhirTestAutoMigrator { + + private static final Logger ourLog = LoggerFactory.getLogger(FhirTestAutoMigrator.class); + public static final String MIGRATION_TABLENAME = "MIGRATIONS"; + + @Autowired + private DataSource myDataSource; + + @PostConstruct + public void run() { + DriverTypeEnum driver; + if (CommonConfig.isLocalTestMode()) { + driver = DriverTypeEnum.H2_EMBEDDED; + } else { + driver = DriverTypeEnum.POSTGRES_9_4; + } + + HapiMigrationDao hapiMigrationDao = new HapiMigrationDao(myDataSource, driver, MIGRATION_TABLENAME); + HapiMigrationStorageSvc hapiMigrationStorageSvc = new HapiMigrationStorageSvc(hapiMigrationDao); + + MigrationTaskList tasks = new HapiFhirJpaMigrationTasks(Set.of()).getAllTasks(VersionEnum.values()); + + SchemaMigrator schemaMigrator = new SchemaMigrator( + "HAPI FHIR", MIGRATION_TABLENAME, myDataSource, new Properties(), tasks, hapiMigrationStorageSvc); + schemaMigrator.setDriverType(driver); + + ourLog.info("About to run migration..."); + schemaMigrator.createMigrationTableIfRequired(); + schemaMigrator.migrate(); + ourLog.info("Migration complete"); + } +}