From 29752465c87731bdc848cfa2fd65c06ac598ca4f Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Wed, 30 Oct 2019 20:27:25 -0400 Subject: [PATCH] add boolean method FliwayMigrator.migrationRequired() so we can test if we have the current version --- .../uhn/fhir/jpa/migrate/FlywayMigrator.java | 38 +++++++++++-------- .../jpa/migrate/taskdef/DropTableTest.java | 21 +++++++++- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java index 8d5421b90b6..9a7983d88ec 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigrator.java @@ -21,21 +21,14 @@ package ca.uhn.fhir.jpa.migrate; */ import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask; -import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; -import com.google.common.annotations.VisibleForTesting; import org.flywaydb.core.Flyway; -import org.flywaydb.core.api.MigrationVersion; -import org.flywaydb.core.api.migration.Context; +import org.flywaydb.core.api.MigrationInfoService; import org.flywaydb.core.api.migration.JavaMigration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; - -import static org.apache.commons.lang3.StringUtils.isBlank; public class FlywayMigrator { @@ -75,18 +68,23 @@ public class FlywayMigrator { public void migrate() { try (DriverTypeEnum.ConnectionProperties connectionProperties = myDriverType.newConnectionProperties(myConnectionUrl, myUsername, myPassword)) { - Flyway flyway = Flyway.configure() - .dataSource(myConnectionUrl, myUsername, myPassword) - .baselineOnMigrate(true) - .javaMigrations(myTasks.toArray(new JavaMigration[0])) - .load(); - for (FlywayMigration task : myTasks) { - task.setConnectionProperties(connectionProperties); - } + Flyway flyway = initFlyway(connectionProperties); flyway.migrate(); } } + private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) { + Flyway flyway = Flyway.configure() + .dataSource(myConnectionUrl, myUsername, myPassword) + .baselineOnMigrate(true) + .javaMigrations(myTasks.toArray(new JavaMigration[0])) + .load(); + for (FlywayMigration task : myTasks) { + task.setConnectionProperties(theConnectionProperties); + } + return flyway; + } + public void addTasks(List> theTasks) { theTasks.forEach(this::addTask); } @@ -118,4 +116,12 @@ public class FlywayMigrator { public boolean isNoColumnShrink() { return myNoColumnShrink; } + + public boolean migrationRequired() { + try (DriverTypeEnum.ConnectionProperties connectionProperties = myDriverType.newConnectionProperties(myConnectionUrl, myUsername, myPassword)) { + Flyway flyway = initFlyway(connectionProperties); + MigrationInfoService info = flyway.info(); + return info.pending().length > 0; + } + } } diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java index eb4fd4c16ca..232f772217c 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java @@ -5,10 +5,9 @@ import org.junit.Test; import java.sql.SQLException; -import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; public class DropTableTest extends BaseTest { @@ -58,4 +57,22 @@ public class DropTableTest extends BaseTest { assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE"))); } + @Test + public void testFlywayMigrationRequired() throws SQLException { + executeSql("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))"); + + DropTableTask task = new DropTableTask("1", "1"); + task.setTableName("SOMETABLE"); + getMigrator().addTask(task); + + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), (hasItems("SOMETABLE"))); + + assertTrue(getMigrator().migrationRequired()); + getMigrator().migrate(); + assertFalse(getMigrator().migrationRequired()); + + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE"))); + } + + }