From 49265dea327a4a580eb881016be7d84813a1c147 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Fri, 1 Nov 2019 17:18:27 -0400 Subject: [PATCH] added schema initialization but not used yet --- .../migrate/taskdef/InitializeSchemaTask.java | 38 +++++++++++++++++ .../BaseSchemaInitializationProvider.java | 41 +++++++++++++++++++ .../tasks/HapiFhirJpaMigrationTasks.java | 10 +++-- .../HapiFhirSchemaInitializationProvider.java | 39 ++++++++++++++++++ .../fhir/jpa/migrate/tasks/api/Builder.java | 5 +++ .../api/ISchemaInitializationProvider.java | 9 ++++ 6 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java create mode 100644 hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/BaseSchemaInitializationProvider.java create mode 100644 hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirSchemaInitializationProvider.java create mode 100644 hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/ISchemaInitializationProvider.java diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java new file mode 100644 index 00000000000..1a07137a415 --- /dev/null +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java @@ -0,0 +1,38 @@ +package ca.uhn.fhir.jpa.migrate.taskdef; + +import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class InitializeSchemaTask extends BaseTask { + private static final Logger ourLog = LoggerFactory.getLogger(InitializeSchemaTask.class); + private final ISchemaInitializationProvider mySchemaInitializationProvider; + + public InitializeSchemaTask(String theRelease, String theVersion, ISchemaInitializationProvider theSchemaInitializationProvider) { + super(theRelease, theVersion); + mySchemaInitializationProvider = theSchemaInitializationProvider; + } + + @Override + public void validate() { + // nothing + } + + @Override + public void execute() throws SQLException { + DriverTypeEnum driverType = getDriverType(); + + logInfo(ourLog, "Initializing schema for {}", driverType); + + List sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType); + + for (String nextSql : sqlStatements) { + executeSql(null, nextSql); + } + } +} diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/BaseSchemaInitializationProvider.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/BaseSchemaInitializationProvider.java new file mode 100644 index 00000000000..6bf723f0841 --- /dev/null +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/BaseSchemaInitializationProvider.java @@ -0,0 +1,41 @@ +package ca.uhn.fhir.jpa.migrate.tasks; + +import org.jetbrains.annotations.NotNull; +import ca.uhn.fhir.context.ConfigurationException; +import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; +import com.google.common.base.Charsets; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.LineIterator; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +public abstract class BaseSchemaInitializationProvider implements ISchemaInitializationProvider { + @Override + public List getSqlStatements(DriverTypeEnum theDriverType) { + List retval = new ArrayList<>(); + + String initScript; + initScript = getSchemaInitializationPath(theDriverType); + try { + InputStream sqlFileInputStream = HapiFhirSchemaInitializationProvider.class.getResourceAsStream(initScript); + if (sqlFileInputStream == null) { + throw new ConfigurationException("Schema initialization script " + initScript + " not found on classpath"); + } + LineIterator iterator = IOUtils.lineIterator(sqlFileInputStream, Charsets.UTF_8); + while (iterator.hasNext()) { + retval.add(iterator.nextLine()); + } + } catch (IOException e) { + throw new ConfigurationException("Error reading schema initialization script " + initScript, e); + } + return retval; + } + + @Nonnull + protected abstract String getSchemaInitializationPath(DriverTypeEnum theDriverType); +} diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java index 40da5a71587..3b97009241b 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java @@ -867,16 +867,18 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { private void init330() { // 20180114 - 20180329 Builder version = forVersion(VersionEnum.V3_3_0); + version.initializeSchema("20180115.0", new HapiFhirSchemaInitializationProvider()); + Builder.BuilderWithTableName hfjResource = version.onTable("HFJ_RESOURCE"); version.startSectionWithMessage("Starting work on table: " + hfjResource.getTableName()); - hfjResource.dropColumn("20180114.1", "RES_TEXT"); - hfjResource.dropColumn("20180114.2", "RES_ENCODING"); + hfjResource.dropColumn("20180115.1", "RES_TEXT"); + hfjResource.dropColumn("20180115.2", "RES_ENCODING"); Builder.BuilderWithTableName hfjResVer = version.onTable("HFJ_RES_VER"); version.startSectionWithMessage("Starting work on table: " + hfjResVer.getTableName()); - hfjResVer.modifyColumn("20180114.3", "RES_ENCODING") + hfjResVer.modifyColumn("20180115.3", "RES_ENCODING") .nullable(); - hfjResVer.modifyColumn("20180114.4", "RES_TEXT") + hfjResVer.modifyColumn("20180115.4", "RES_TEXT") .nullable(); } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirSchemaInitializationProvider.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirSchemaInitializationProvider.java new file mode 100644 index 00000000000..b5e9e17bd72 --- /dev/null +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirSchemaInitializationProvider.java @@ -0,0 +1,39 @@ +package ca.uhn.fhir.jpa.migrate.tasks; + + +import ca.uhn.fhir.context.ConfigurationException; +import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; + +import javax.annotation.Nonnull; + +public class HapiFhirSchemaInitializationProvider extends BaseSchemaInitializationProvider { + + @Nonnull + protected String getSchemaInitializationPath(DriverTypeEnum theDriverType) { + String initScript; + switch (theDriverType) { + case H2_EMBEDDED: + initScript = "/ca/uhn/hapi/fhir/jpa/docs/database/h2.sql"; + break; + case DERBY_EMBEDDED: + initScript = "/ca/uhn/hapi/fhir/jpa/docs/database/derbytenseven.sql"; + break; + case MYSQL_5_7: + case MARIADB_10_1: + initScript = "/ca/uhn/hapi/fhir/jpa/docs/database/mysql57.sql"; + break; + case POSTGRES_9_4: + initScript = "/ca/uhn/hapi/fhir/jpa/docs/database/postgresql92.sql"; + break; + case ORACLE_12C: + initScript = "/ca/uhn/hapi/fhir/jpa/docs/database/oracle12c.sql"; + break; + case MSSQL_2012: + initScript = "/ca/uhn/hapi/fhir/jpa/docs/database/sqlserver2012.sql"; + break; + default: + throw new ConfigurationException("No schema initialization script available for driver " + theDriverType); + } + return initScript; + } +} diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java index 6f9d56e11e9..a25812d697d 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java @@ -32,6 +32,11 @@ public class Builder { return this; } + public Builder initializeSchema(String theVersion, ISchemaInitializationProvider theSchemaInitializationProvider) { + mySink.addTask(new InitializeSchemaTask(myRelease, theVersion, theSchemaInitializationProvider)); + return this; + } + public Builder executeRawSql(String theVersion, DriverTypeEnum theDriver, @Language("SQL") String theSql) { mySink.addTask(new ExecuteRawSqlTask(myRelease, theVersion).addSql(theDriver, theSql)); return this; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/ISchemaInitializationProvider.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/ISchemaInitializationProvider.java new file mode 100644 index 00000000000..1f894332cd1 --- /dev/null +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/ISchemaInitializationProvider.java @@ -0,0 +1,9 @@ +package ca.uhn.fhir.jpa.migrate.tasks.api; + +import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; + +import java.util.List; + +public interface ISchemaInitializationProvider { + List getSqlStatements(DriverTypeEnum theDriverType); +}