Merge remote-tracking branch 'origin/master'

This commit is contained in:
Ken Stevens 2019-04-04 10:26:34 -04:00
commit bfd6066e99
3 changed files with 115 additions and 16 deletions

View File

@ -0,0 +1,71 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
/*-
* #%L
* HAPI FHIR JPA Server - Migration
* %%
* Copyright (C) 2014 - 2019 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import org.apache.commons.lang3.Validate;
import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class ExecuteRawSqlTask extends BaseTask<ExecuteRawSqlTask> {
private static final Logger ourLog = LoggerFactory.getLogger(ExecuteRawSqlTask.class);
private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
private List<String> myDriverNeutralSqls = new ArrayList<>();
public ExecuteRawSqlTask addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
Validate.notNull(theDriverType);
Validate.notBlank(theSql);
List<String> list = myDriverToSqls.computeIfAbsent(theDriverType, t -> new ArrayList<>());
list.add(theSql);
return this;
}
public ExecuteRawSqlTask addSql(String theSql) {
Validate.notBlank("theSql must not be null", theSql);
myDriverNeutralSqls.add(theSql);
return this;
}
@Override
public void validate() {
// nothing
}
@Override
public void execute() {
List<String> sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>());
sqlStatements.addAll(myDriverNeutralSqls);
ourLog.info("Going to execute {} SQL statements", sqlStatements.size());
for (String nextSql : sqlStatements) {
executeSql(null, nextSql);
}
}
}

View File

@ -60,7 +60,7 @@ public class BaseMigrationTasks<T extends Enum> {
return retVal;
}
protected Builder forVersion(T theVersion) {
public Builder forVersion(T theVersion) {
IAcceptsTasks sink = theTask -> {
theTask.validate();
myTasks.put(theVersion, theTask);
@ -72,7 +72,7 @@ public class BaseMigrationTasks<T extends Enum> {
void addTask(BaseTask<?> theTask);
}
protected static class Builder {
public static class Builder {
private final IAcceptsTasks mySink;
@ -92,6 +92,16 @@ public class BaseMigrationTasks<T extends Enum> {
return new BuilderAddTableRawSql(theTableName);
}
public Builder executeRawSql(@Language("SQL") String theSql) {
mySink.addTask(new ExecuteRawSqlTask().addSql(theSql));
return this;
}
public Builder executeRawSql(DriverTypeEnum theDriver, @Language("SQL") String theSql) {
mySink.addTask(new ExecuteRawSqlTask().addSql(theDriver, theSql));
return this;
}
public Builder startSectionWithMessage(String theMessage) {
Validate.notBlank(theMessage);
addTask(new LogStartSectionWithMessageTask(theMessage));

View File

@ -1,5 +1,6 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.tasks.api.BaseMigrationTasks;
import ca.uhn.fhir.jpa.model.entity.SearchParamPresent;
import ca.uhn.fhir.util.VersionEnum;
@ -69,19 +70,6 @@ public class ArbitrarySqlTaskTest extends BaseTest {
}
private static class TestUpdateTasks extends BaseMigrationTasks<VersionEnum> {
public TestUpdateTasks() {
Builder v = forVersion(VersionEnum.V3_5_0);
v
.addTableRawSql("A")
.addSql("delete from TEST_UPDATE_TASK where RES_TYPE = 'Patient'");
}
}
@Test
public void testUpdateTask() {
executeSql("create table TEST_UPDATE_TASK (PID bigint not null, RES_TYPE varchar(255), PARAM_NAME varchar(255))");
@ -90,7 +78,37 @@ public class ArbitrarySqlTaskTest extends BaseTest {
List<Map<String, Object>> rows = executeQuery("select * from TEST_UPDATE_TASK");
assertEquals(1, rows.size());
TestUpdateTasks migrator = new TestUpdateTasks();
BaseMigrationTasks<VersionEnum> migrator = new BaseMigrationTasks<VersionEnum>() {
};
migrator
.forVersion(VersionEnum.V3_5_0)
.addTableRawSql("A")
.addSql("delete from TEST_UPDATE_TASK where RES_TYPE = 'Patient'");
getMigrator().addTasks(migrator.getTasks(VersionEnum.V3_3_0, VersionEnum.V3_6_0));
getMigrator().migrate();
rows = executeQuery("select * from TEST_UPDATE_TASK");
assertEquals(0, rows.size());
}
@Test
public void testArbitrarySql() {
executeSql("create table TEST_UPDATE_TASK (PID bigint not null, RES_TYPE varchar(255), PARAM_NAME varchar(255))");
executeSql("insert into TEST_UPDATE_TASK (PID, RES_TYPE, PARAM_NAME) values (1, 'Patient', 'identifier')");
executeSql("insert into TEST_UPDATE_TASK (PID, RES_TYPE, PARAM_NAME) values (1, 'Encounter', 'identifier')");
List<Map<String, Object>> rows = executeQuery("select * from TEST_UPDATE_TASK");
assertEquals(2, rows.size());
BaseMigrationTasks<VersionEnum> migrator = new BaseMigrationTasks<VersionEnum>() {
};
migrator
.forVersion(VersionEnum.V3_5_0)
.executeRawSql(DriverTypeEnum.DERBY_EMBEDDED, "delete from TEST_UPDATE_TASK where RES_TYPE = 'Patient'")
.executeRawSql(DriverTypeEnum.DERBY_EMBEDDED, "delete from TEST_UPDATE_TASK where RES_TYPE = 'Encounter'");
getMigrator().addTasks(migrator.getTasks(VersionEnum.V3_3_0, VersionEnum.V3_6_0));
getMigrator().migrate();