Merge pull request #1630 from jamesagnew/ks-20191212-dryrun

added dry-run output to both Flyway and TaskOnly migrator
This commit is contained in:
James Agnew 2019-12-12 14:47:12 -05:00 committed by GitHub
commit d9d47bb419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 7 deletions

View File

@ -21,9 +21,9 @@ package ca.uhn.fhir.cli;
*/
import ca.uhn.fhir.jpa.migrate.BaseMigrator;
import ca.uhn.fhir.jpa.migrate.BruteForceMigrator;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.FlywayMigrator;
import ca.uhn.fhir.jpa.migrate.TaskOnlyMigrator;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
@ -124,8 +124,9 @@ public abstract class BaseFlywayMigrateDatabaseCommand<T extends Enum> extends B
boolean outOfOrderPermitted = theCommandLine.hasOption(BaseFlywayMigrateDatabaseCommand.OUT_OF_ORDER_PERMITTED);
BaseMigrator migrator;
if (dontUseFlyway) {
migrator = new BruteForceMigrator();
if (dontUseFlyway || dryRun) {
// Flyway dryrun is not available in community edition
migrator = new TaskOnlyMigrator();
} else {
migrator = new FlywayMigrator(myMigrationTableName);
}

View File

@ -23,7 +23,9 @@ package ca.uhn.fhir.jpa.migrate;
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
import org.flywaydb.core.api.MigrationInfoService;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public abstract class BaseMigrator {
@ -35,6 +37,7 @@ public abstract class BaseMigrator {
private String myConnectionUrl;
private String myUsername;
private String myPassword;
private List<BaseTask.ExecutedStatement> myExecutedStatements = new ArrayList<>();
public abstract void migrate();
@ -97,4 +100,26 @@ public abstract class BaseMigrator {
public void setOutOfOrderPermitted(boolean theOutOfOrderPermitted) {
myOutOfOrderPermitted = theOutOfOrderPermitted;
}
public void addExecutedStatements(List theExecutedStatements) {
myExecutedStatements.addAll(theExecutedStatements);
}
protected StringBuilder buildExecutedStatementsString() {
StringBuilder statementBuilder = new StringBuilder();
String lastTable = null;
for (BaseTask.ExecutedStatement next : myExecutedStatements) {
if (!Objects.equals(lastTable, next.getTableName())) {
statementBuilder.append("\n\n-- Table: ").append(next.getTableName()).append("\n");
lastTable = next.getTableName();
}
statementBuilder.append(next.getSql()).append(";\n");
for (Object nextArg : next.getArguments()) {
statementBuilder.append(" -- Arg: ").append(nextArg).append("\n");
}
}
return statementBuilder;
}
}

View File

@ -70,13 +70,14 @@ public class FlywayMigration implements JavaMigration {
}
@Override
public void migrate(Context theContext) throws Exception {
public void migrate(Context theContext) {
myTask.setDriverType(myFlywayMigrator.getDriverType());
myTask.setDryRun(myFlywayMigrator.isDryRun());
myTask.setNoColumnShrink(myFlywayMigrator.isNoColumnShrink());
myTask.setConnectionProperties(myConnectionProperties);
try {
myTask.execute();
myFlywayMigrator.addExecutedStatements(myTask.getExecutedStatements());
} catch (SQLException e) {
String description = myTask.getDescription();
if (isBlank(description)) {

View File

@ -72,6 +72,10 @@ public class FlywayMigrator extends BaseMigrator {
Flyway flyway = initFlyway(connectionProperties);
flyway.repair();
flyway.migrate();
if (isDryRun()) {
StringBuilder statementBuilder = buildExecutedStatementsString();
ourLog.info("SQL that would be executed:\n\n***********************************\n{}***********************************", statementBuilder);
}
} catch (Exception e) {
throw e;
}

View File

@ -100,7 +100,7 @@ public class SchemaMigrator {
private BaseMigrator newMigrator() {
BaseMigrator migrator;
if (myDontUseFlyway) {
migrator = new BruteForceMigrator();
migrator = new TaskOnlyMigrator();
migrator.setDriverType(myDriverType);
migrator.setConnectionUrl(myDataSource.getUrl());
migrator.setUsername(myDataSource.getUsername());

View File

@ -35,9 +35,9 @@ import java.util.Optional;
* This class is an alternative to {@link FlywayMigrator). It doesn't use Flyway, but instead just
* executes all tasks.
*/
public class BruteForceMigrator extends BaseMigrator {
public class TaskOnlyMigrator extends BaseMigrator {
private static final Logger ourLog = LoggerFactory.getLogger(BruteForceMigrator.class);
private static final Logger ourLog = LoggerFactory.getLogger(TaskOnlyMigrator.class);
private List<BaseTask<?>> myTasks = new ArrayList<>();
@Override
@ -53,10 +53,16 @@ public class BruteForceMigrator extends BaseMigrator {
try {
ourLog.info("Executing task of type: {}", next.getClass().getSimpleName());
next.execute();
addExecutedStatements(next.getExecutedStatements());
} catch (SQLException e) {
throw new InternalErrorException(e);
}
}
if (isDryRun()) {
StringBuilder statementBuilder = buildExecutedStatementsString();
ourLog.info("SQL that would be executed:\n\n***********************************\n{}***********************************", statementBuilder);
}
}
@Override