added schema initialization but not used yet

This commit is contained in:
Ken Stevens 2019-11-01 17:18:27 -04:00
parent 5f294f335d
commit 49265dea32
6 changed files with 138 additions and 4 deletions

View File

@ -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<InitializeSchemaTask> {
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<String> sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType);
for (String nextSql : sqlStatements) {
executeSql(null, nextSql);
}
}
}

View File

@ -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<String> getSqlStatements(DriverTypeEnum theDriverType) {
List<String> 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);
}

View File

@ -867,16 +867,18 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
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();
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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<String> getSqlStatements(DriverTypeEnum theDriverType);
}