Skip to content

Commit

Permalink
Fix #2993: improve performance of refresh method
Browse files Browse the repository at this point in the history
  • Loading branch information
DoodleBobBuffPants committed Nov 3, 2020
1 parent 7c2ff3f commit 5f4a368
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 39 deletions.
6 changes: 3 additions & 3 deletions flyway-core/src/main/java/org/flywaydb/core/Flyway.java
Expand Up @@ -211,7 +211,7 @@ public MigrateResult execute(MigrationResolver migrationResolver,
}
}

return new DbMigrate(database, schemaHistory, schemas, migrationResolver, configuration,
return new DbMigrate(database, schemaHistory, schemas[0], migrationResolver, configuration,
callbackExecutor).migrate();
}
}, true);
Expand Down Expand Up @@ -317,7 +317,7 @@ public ValidateResult execute(MigrationResolver migrationResolver, SchemaHistory
*/
private ValidateResult doValidate(Database database, MigrationResolver migrationResolver, SchemaHistory schemaHistory,
Schema[] schemas, CallbackExecutor callbackExecutor, boolean ignorePending) {
ValidateResult validateResult = new DbValidate(database, schemaHistory, schemas, migrationResolver,
ValidateResult validateResult = new DbValidate(database, schemaHistory, schemas[0], migrationResolver,
configuration, ignorePending, callbackExecutor).validate();

if (!validateResult.validationSuccessful && configuration.isCleanOnValidationError()) {
Expand Down Expand Up @@ -410,7 +410,7 @@ public RepairResult repair() throws FlywayException {
public RepairResult execute(MigrationResolver migrationResolver,
SchemaHistory schemaHistory, Database database, Schema[] schemas, CallbackExecutor callbackExecutor,
StatementInterceptor statementInterceptor) {
return new DbRepair(database, migrationResolver, schemaHistory, schemas, callbackExecutor, configuration).repair();
return new DbRepair(database, migrationResolver, schemaHistory, callbackExecutor, configuration).repair();
}
}, true);
}
Expand Down
Expand Up @@ -26,6 +26,8 @@
import org.flywaydb.core.internal.info.MigrationInfoServiceImpl;
import org.flywaydb.core.internal.schemahistory.SchemaHistory;

import java.util.Arrays;

public class DbInfo {
private final MigrationResolver migrationResolver;
private final SchemaHistory schemaHistory;
Expand All @@ -51,10 +53,11 @@ public MigrationInfoService info() {
MigrationInfoServiceImpl migrationInfoService;
try {
migrationInfoService =
new MigrationInfoServiceImpl(migrationResolver, schemaHistory, schemas, database, configuration,
new MigrationInfoServiceImpl(migrationResolver, schemaHistory, database, configuration,
configuration.getTarget(), configuration.isOutOfOrder(), configuration.getCherryPick(),
true, true, true, true);
migrationInfoService.refresh();
migrationInfoService.setAllSchemasEmpty(schemas);
} catch (FlywayException e) {
callbackExecutor.onEvent(Event.AFTER_INFO_ERROR);
throw e;
Expand Down
Expand Up @@ -71,11 +71,6 @@ public class DbMigrate {
*/
private final Schema schema;

/**
* The list of schemas managed by Flyway.
*/
private final Schema[] schemas;

/**
* The migration resolver.
*/
Expand Down Expand Up @@ -116,19 +111,18 @@ public class DbMigrate {
*
* @param database Database-specific functionality.
* @param schemaHistory The database schema history table.
* @param schemas The list of schemas managed by Flyway.
* @param schema The schema containing the schema history table.
* @param migrationResolver The migration resolver.
* @param configuration The Flyway configuration.
* @param callbackExecutor The callbacks executor.
*/
public DbMigrate(Database database,
SchemaHistory schemaHistory, Schema[] schemas, MigrationResolver migrationResolver,
SchemaHistory schemaHistory, Schema schema, MigrationResolver migrationResolver,
Configuration configuration, CallbackExecutor callbackExecutor) {
this.database = database;
this.connectionUserObjects = database.getMigrationConnection();
this.schemaHistory = schemaHistory;
this.schema = schemas[0];
this.schemas = schemas;
this.schema = schema;
this.migrationResolver = migrationResolver;
this.configuration = configuration;
this.callbackExecutor = callbackExecutor;
Expand Down Expand Up @@ -229,7 +223,7 @@ public Integer call() {
*/
private Integer migrateGroup(boolean firstRun) {
MigrationInfoServiceImpl infoService =
new MigrationInfoServiceImpl(migrationResolver, schemaHistory, schemas, database, configuration,
new MigrationInfoServiceImpl(migrationResolver, schemaHistory, database, configuration,
configuration.getTarget(), configuration.isOutOfOrder(), configuration.getCherryPick(),
true, true, true, true);
infoService.refresh();
Expand Down
Expand Up @@ -30,7 +30,6 @@
import org.flywaydb.core.internal.callback.CallbackExecutor;
import org.flywaydb.core.internal.database.base.Connection;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.info.MigrationInfoImpl;
import org.flywaydb.core.internal.info.MigrationInfoServiceImpl;
import org.flywaydb.core.internal.jdbc.ExecutionTemplateFactory;
Expand Down Expand Up @@ -94,18 +93,17 @@ public class DbRepair {
* @param database The database-specific support.
* @param migrationResolver The migration resolver.
* @param schemaHistory The schema history table.
* @param schemas The list of schemas managed by Flyway.
* @param callbackExecutor The callback executor.
*/
public DbRepair(Database database, MigrationResolver migrationResolver, SchemaHistory schemaHistory, Schema[] schemas,
public DbRepair(Database database, MigrationResolver migrationResolver, SchemaHistory schemaHistory,
CallbackExecutor callbackExecutor, Configuration configuration) {
this.database = database;
this.connection = database.getMainConnection();
this.schemaHistory = schemaHistory;
this.callbackExecutor = callbackExecutor;
this.configuration = configuration;

this.migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, schemaHistory, schemas, database, configuration,
this.migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, schemaHistory, database, configuration,
MigrationVersion.LATEST, true, configuration.getCherryPick(), true, true, true, true);

this.commandResultFactory = new CommandResultFactory();
Expand Down
Expand Up @@ -59,11 +59,6 @@ public class DbValidate {
*/
private final Schema schema;

/**
* The list of schemas managed by Flyway.
*/
private final Schema[] schemas;

/**
* The migration resolver.
*/
Expand Down Expand Up @@ -99,19 +94,18 @@ public class DbValidate {
*
* @param database The DB support for the connection.
* @param schemaHistory The database schema history table.
* @param schemas The list of schemas managed by Flyway.
* @param schema The schema containing the schema history table.
* @param migrationResolver The migration resolver.
* @param configuration The current configuration.
* @param pending Whether pending migrations are allowed.
* @param callbackExecutor The callback executor.
*/
public DbValidate(Database database, SchemaHistory schemaHistory, Schema[] schemas, MigrationResolver migrationResolver,
public DbValidate(Database database, SchemaHistory schemaHistory, Schema schema, MigrationResolver migrationResolver,
Configuration configuration, boolean pending, CallbackExecutor callbackExecutor) {
this.database = database;
this.connection = database.getMainConnection();
this.schemaHistory = schemaHistory;
this.schema = schemas[0];
this.schemas = schemas;
this.schema = schema;
this.migrationResolver = migrationResolver;
this.configuration = configuration;
this.pending = pending;
Expand Down Expand Up @@ -152,7 +146,7 @@ public Configuration getConfiguration() {
@Override
public Pair<Integer, List<ValidateOutput>> call() {
MigrationInfoServiceImpl migrationInfoService =
new MigrationInfoServiceImpl(migrationResolver, schemaHistory, schemas, database, configuration,
new MigrationInfoServiceImpl(migrationResolver, schemaHistory, database, configuration,
configuration.getTarget(),
configuration.isOutOfOrder(),
configuration.getCherryPick(),
Expand Down
Expand Up @@ -97,15 +97,10 @@ public class MigrationInfoServiceImpl implements MigrationInfoService, Operation
*/
private List<MigrationInfoImpl> migrationInfos;

/**
* The list of schemas managed by Flyway.
*/
private Schema[] schemas;

/**
* Whether all of the specified schemas are empty or not.
*/
private boolean allSchemasEmpty = true;
private Boolean allSchemasEmpty;

/**
* Creates a new MigrationInfoServiceImpl.
Expand All @@ -122,7 +117,7 @@ public class MigrationInfoServiceImpl implements MigrationInfoService, Operation
* @param future Whether future migrations are allowed.
*/
public MigrationInfoServiceImpl(MigrationResolver migrationResolver,
SchemaHistory schemaHistory, Schema[] schemas, Database database, final Configuration configuration,
SchemaHistory schemaHistory, Database database, final Configuration configuration,
MigrationVersion target, boolean outOfOrder, MigrationPattern[] cherryPick,
boolean pending, boolean missing, boolean ignored, boolean future) {
this.migrationResolver = migrationResolver;
Expand All @@ -141,7 +136,6 @@ public Configuration getConfiguration() {
this.missing = missing;
this.ignored = ignored || cherryPick != null;
this.future = future;
this.schemas = schemas;
}

/**
Expand Down Expand Up @@ -359,9 +353,6 @@ public void refresh() {
));
}

// Update whether all managed schemas are empty or not
allSchemasEmpty = Arrays.stream(schemas).allMatch(Schema::empty);

// Set output
Collections.sort(migrationInfos1);
migrationInfos = migrationInfos1;
Expand Down Expand Up @@ -616,6 +607,10 @@ public List<ValidateOutput> validate() {
return invalidMigrations;
}

public void setAllSchemasEmpty(Schema[] schemas) {
allSchemasEmpty = Arrays.stream(schemas).allMatch(Schema::empty);
}

@Override
public InfoResult getInfoResult() {
return getInfoResult(this.all());
Expand Down

0 comments on commit 5f4a368

Please sign in to comment.