add boolean method FliwayMigrator.migrationRequired() so we can test if we have the current version

This commit is contained in:
Ken Stevens 2019-10-30 20:27:25 -04:00
parent d29e112e26
commit 29752465c8
2 changed files with 41 additions and 18 deletions

View File

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

View File

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