diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java index ed63bc26c0b..69e3c5c6c0b 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BaseFlywayMigrateDatabaseCommand.java @@ -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 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); } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BaseMigrator.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BaseMigrator.java index b8e2c68fcad..fef2384c5cb 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BaseMigrator.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BaseMigrator.java @@ -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 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; + } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigration.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigration.java index 73a9741aa54..6c63e467d22 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigration.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/FlywayMigration.java @@ -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)) { 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 d9d560f3ea3..bbf3189c353 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 @@ -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; } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java index c4faed34720..00d0f132270 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/SchemaMigrator.java @@ -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()); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BruteForceMigrator.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/TaskOnlyMigrator.java similarity index 82% rename from hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BruteForceMigrator.java rename to hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/TaskOnlyMigrator.java index c333796a0fe..98503be3a24 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/BruteForceMigrator.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/TaskOnlyMigrator.java @@ -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> 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