consolidate providers

This commit is contained in:
Ken Stevens 2019-11-01 21:35:11 -04:00
parent 7066f35e49
commit 3dc2770962
5 changed files with 40 additions and 50 deletions

View File

@ -1,5 +1,6 @@
package ca.uhn.fhir.jpa.migrate;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.lang3.Validate;
@ -70,6 +71,34 @@ public enum DriverTypeEnum {
return myDriverClassName;
}
public String getSchemaFilename() {
String retval;
switch (this) {
case H2_EMBEDDED:
retval = "h2.sql";
break;
case DERBY_EMBEDDED:
retval = "derbytenseven.sql";
break;
case MYSQL_5_7:
case MARIADB_10_1:
retval = "mysql57.sql";
break;
case POSTGRES_9_4:
retval = "postgresql92.sql";
break;
case ORACLE_12C:
retval = "oracle12c.sql";
break;
case MSSQL_2012:
retval = "sqlserver2012.sql";
break;
default:
throw new ConfigurationException("No schema initialization script available for driver " + this);
}
return retval;
}
public static DriverTypeEnum fromDriverClassName(String theDriverClassName) {
for (DriverTypeEnum driverTypeEnum : DriverTypeEnum.values()) {
if (driverTypeEnum.myDriverClassName.equals(theDriverClassName)) {

View File

@ -106,7 +106,7 @@ public class FlywayMigrator {
private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) {
// FIXME KHS instantiate from database, not this other stuff
// FIXME KHS ensure we have a default schema
// FIXME KHS succeeds from zero. But then second time fails with error. It's as though the flyway db isn't persisting.... or maybe a checksum issue...?
Flyway flyway = Flyway.configure()
// FIXME KHS required?
// .schemas(myDefaultSchema)

View File

@ -867,7 +867,7 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
private void init330() { // 20180114 - 20180329
Builder version = forVersion(VersionEnum.V3_3_0);
version.initializeSchema("20180115.0", new HapiFhirSchemaInitializationProvider());
version.initializeSchema("20180115.0", new SchemaInitializationProvider("/ca/uhn/hapi/fhir/jpa/docs/database/"));
Builder.BuilderWithTableName hfjResource = version.onTable("HFJ_RESOURCE");
version.startSectionWithMessage("Starting work on table: " + hfjResource.getTableName());

View File

@ -1,39 +0,0 @@
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;
}
}

View File

@ -1,28 +1,31 @@
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 {
public class SchemaInitializationProvider implements ISchemaInitializationProvider {
private final String mySchemaFileClassPath;
public SchemaInitializationProvider(String theSchemaFileClassPath) {
mySchemaFileClassPath = theSchemaFileClassPath;
}
@Override
public List<String> getSqlStatements(DriverTypeEnum theDriverType) {
List<String> retval = new ArrayList<>();
String initScript;
initScript = getSchemaInitializationPath(theDriverType);
initScript = mySchemaFileClassPath + "/" + theDriverType.getSchemaFilename();
try {
InputStream sqlFileInputStream = HapiFhirSchemaInitializationProvider.class.getResourceAsStream(initScript);
InputStream sqlFileInputStream = SchemaInitializationProvider.class.getResourceAsStream(initScript);
if (sqlFileInputStream == null) {
throw new ConfigurationException("Schema initialization script " + initScript + " not found on classpath");
}
@ -38,7 +41,4 @@ public abstract class BaseSchemaInitializationProvider implements ISchemaInitial
}
return retval;
}
@Nonnull
protected abstract String getSchemaInitializationPath(DriverTypeEnum theDriverType);
}