Fix handling of sequences in migrator tool

This commit is contained in:
James Agnew 2019-01-15 11:30:43 -06:00
parent a584e15251
commit a2d0168671
4 changed files with 55 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank;
@ -78,7 +79,13 @@ public class AddIdGeneratorTask extends BaseTask<AddIdGeneratorTask> {
} }
if (isNotBlank(sql)) { if (isNotBlank(sql)) {
if (JdbcUtils.getSequenceNames(getConnectionProperties()).contains(myGeneratorName)) { Set<String> sequenceNames =
JdbcUtils.getSequenceNames(getConnectionProperties())
.stream()
.map(String::toLowerCase)
.collect(Collectors.toSet());
ourLog.debug("Currently have sequences: {}", sequenceNames);
if (sequenceNames.contains(myGeneratorName.toLowerCase())) {
ourLog.info("Sequence {} already exists - No action performed", myGeneratorName); ourLog.info("Sequence {} already exists - No action performed", myGeneratorName);
return; return;
} }

View File

@ -35,6 +35,7 @@ public class AddTableRawSqlTask extends BaseTableTask<AddTableRawSqlTask> {
private static final Logger ourLog = LoggerFactory.getLogger(AddTableRawSqlTask.class); private static final Logger ourLog = LoggerFactory.getLogger(AddTableRawSqlTask.class);
private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>(); private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
private List<String> myDriverNeutralSqls = new ArrayList<>();
public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) { public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
Validate.notNull(theDriverType); Validate.notNull(theDriverType);
@ -52,9 +53,11 @@ public class AddTableRawSqlTask extends BaseTableTask<AddTableRawSqlTask> {
return; return;
} }
List<String> sqlStatements = myDriverToSqls.get(getDriverType()); List<String> sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>());
sqlStatements.addAll(myDriverNeutralSqls);
ourLog.info("Going to create table {} using {} SQL statements", getTableName(), sqlStatements.size()); ourLog.info("Going to create table {} using {} SQL statements", getTableName(), sqlStatements.size());
getConnectionProperties().getTxTemplate().execute(t->{ getConnectionProperties().getTxTemplate().execute(t -> {
JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate(); JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate();
for (String nextSql : sqlStatements) { for (String nextSql : sqlStatements) {
@ -65,4 +68,9 @@ public class AddTableRawSqlTask extends BaseTableTask<AddTableRawSqlTask> {
}); });
} }
public void addSql(String theSql) {
Validate.notBlank("theSql must not be null", theSql);
myDriverNeutralSqls.add(theSql);
}
} }

View File

@ -329,6 +329,10 @@ public class BaseMigrationTasks<T extends Enum> {
myTask.addSql(theDriverTypeEnum, theSql); myTask.addSql(theDriverTypeEnum, theSql);
return this; return this;
} }
public void addSql(@Language("SQL") String theSql) {
myTask.addSql(theSql);
}
} }
public class BuilderAddTableByColumns implements IAcceptsTasks { public class BuilderAddTableByColumns implements IAcceptsTasks {

View File

@ -1,6 +1,8 @@
package ca.uhn.fhir.jpa.migrate.taskdef; package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.tasks.api.BaseMigrationTasks;
import ca.uhn.fhir.jpa.model.entity.SearchParamPresent; import ca.uhn.fhir.jpa.model.entity.SearchParamPresent;
import ca.uhn.fhir.util.VersionEnum;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
@ -66,4 +68,35 @@ public class ArbitrarySqlTaskTest extends BaseTest {
getMigrator().migrate(); getMigrator().migrate();
} }
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))");
executeSql("insert into TEST_UPDATE_TASK (PID, RES_TYPE, PARAM_NAME) values (1, 'Patient', 'identifier')");
List<Map<String, Object>> rows = executeQuery("select * from TEST_UPDATE_TASK");
assertEquals(1, rows.size());
TestUpdateTasks migrator = new TestUpdateTasks();
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());
}
} }