From a2d01686714caaa0b3d2c1518f976a5a137d157e Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 15 Jan 2019 11:30:43 -0600 Subject: [PATCH] Fix handling of sequences in migrator tool --- .../migrate/taskdef/AddIdGeneratorTask.java | 9 ++++- .../migrate/taskdef/AddTableRawSqlTask.java | 12 +++++-- .../migrate/tasks/api/BaseMigrationTasks.java | 4 +++ .../migrate/taskdef/ArbitrarySqlTaskTest.java | 33 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java index b498db1faeb..3930baa42b4 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java @@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory; import java.sql.SQLException; import java.util.Set; +import java.util.stream.Collectors; import static org.apache.commons.lang3.StringUtils.isNotBlank; @@ -78,7 +79,13 @@ public class AddIdGeneratorTask extends BaseTask { } if (isNotBlank(sql)) { - if (JdbcUtils.getSequenceNames(getConnectionProperties()).contains(myGeneratorName)) { + Set 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); return; } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java index 1da758bae16..b273992fae4 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java @@ -35,6 +35,7 @@ public class AddTableRawSqlTask extends BaseTableTask { private static final Logger ourLog = LoggerFactory.getLogger(AddTableRawSqlTask.class); private Map> myDriverToSqls = new HashMap<>(); + private List myDriverNeutralSqls = new ArrayList<>(); public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) { Validate.notNull(theDriverType); @@ -52,9 +53,11 @@ public class AddTableRawSqlTask extends BaseTableTask { return; } - List sqlStatements = myDriverToSqls.get(getDriverType()); + List sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>()); + sqlStatements.addAll(myDriverNeutralSqls); + ourLog.info("Going to create table {} using {} SQL statements", getTableName(), sqlStatements.size()); - getConnectionProperties().getTxTemplate().execute(t->{ + getConnectionProperties().getTxTemplate().execute(t -> { JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate(); for (String nextSql : sqlStatements) { @@ -65,4 +68,9 @@ public class AddTableRawSqlTask extends BaseTableTask { }); } + + public void addSql(String theSql) { + Validate.notBlank("theSql must not be null", theSql); + myDriverNeutralSqls.add(theSql); + } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java index abc4d262037..d041155d70f 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/BaseMigrationTasks.java @@ -329,6 +329,10 @@ public class BaseMigrationTasks { myTask.addSql(theDriverTypeEnum, theSql); return this; } + + public void addSql(@Language("SQL") String theSql) { + myTask.addSql(theSql); + } } public class BuilderAddTableByColumns implements IAcceptsTasks { diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTaskTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTaskTest.java index 9dbce75c8c7..da5da3f13d5 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTaskTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTaskTest.java @@ -1,6 +1,8 @@ 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.util.VersionEnum; import org.junit.Test; import java.util.List; @@ -66,4 +68,35 @@ public class ArbitrarySqlTaskTest extends BaseTest { getMigrator().migrate(); } + + private static class TestUpdateTasks extends BaseMigrationTasks { + + 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> 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()); + + } + }