add boolean method FliwayMigrator.migrationRequired() so we can test if we have the current version
This commit is contained in:
parent
d29e112e26
commit
29752465c8
|
@ -21,21 +21,14 @@ package ca.uhn.fhir.jpa.migrate;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
|
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.Flyway;
|
||||||
import org.flywaydb.core.api.MigrationVersion;
|
import org.flywaydb.core.api.MigrationInfoService;
|
||||||
import org.flywaydb.core.api.migration.Context;
|
|
||||||
import org.flywaydb.core.api.migration.JavaMigration;
|
import org.flywaydb.core.api.migration.JavaMigration;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static org.apache.commons.lang3.StringUtils.isBlank;
|
|
||||||
|
|
||||||
public class FlywayMigrator {
|
public class FlywayMigrator {
|
||||||
|
|
||||||
|
@ -75,16 +68,21 @@ public class FlywayMigrator {
|
||||||
|
|
||||||
public void migrate() {
|
public void migrate() {
|
||||||
try (DriverTypeEnum.ConnectionProperties connectionProperties = myDriverType.newConnectionProperties(myConnectionUrl, myUsername, myPassword)) {
|
try (DriverTypeEnum.ConnectionProperties connectionProperties = myDriverType.newConnectionProperties(myConnectionUrl, myUsername, myPassword)) {
|
||||||
|
Flyway flyway = initFlyway(connectionProperties);
|
||||||
|
flyway.migrate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Flyway initFlyway(DriverTypeEnum.ConnectionProperties theConnectionProperties) {
|
||||||
Flyway flyway = Flyway.configure()
|
Flyway flyway = Flyway.configure()
|
||||||
.dataSource(myConnectionUrl, myUsername, myPassword)
|
.dataSource(myConnectionUrl, myUsername, myPassword)
|
||||||
.baselineOnMigrate(true)
|
.baselineOnMigrate(true)
|
||||||
.javaMigrations(myTasks.toArray(new JavaMigration[0]))
|
.javaMigrations(myTasks.toArray(new JavaMigration[0]))
|
||||||
.load();
|
.load();
|
||||||
for (FlywayMigration task : myTasks) {
|
for (FlywayMigration task : myTasks) {
|
||||||
task.setConnectionProperties(connectionProperties);
|
task.setConnectionProperties(theConnectionProperties);
|
||||||
}
|
|
||||||
flyway.migrate();
|
|
||||||
}
|
}
|
||||||
|
return flyway;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTasks(List<BaseTask<?>> theTasks) {
|
public void addTasks(List<BaseTask<?>> theTasks) {
|
||||||
|
@ -118,4 +116,12 @@ public class FlywayMigrator {
|
||||||
public boolean isNoColumnShrink() {
|
public boolean isNoColumnShrink() {
|
||||||
return myNoColumnShrink;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,9 @@ import org.junit.Test;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.contains;
|
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
import static org.hamcrest.core.IsNot.not;
|
import static org.hamcrest.core.IsNot.not;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class DropTableTest extends BaseTest {
|
public class DropTableTest extends BaseTest {
|
||||||
|
|
||||||
|
@ -58,4 +57,22 @@ public class DropTableTest extends BaseTest {
|
||||||
assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE")));
|
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")));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue