Search in sources :

Example 1 with MigrationExecutor

use of org.flywaydb.core.api.resolver.MigrationExecutor in project flyway by flyway.

the class MyCustomMigrationResolver method resolveMigrations.

@Override
public List<ResolvedMigration> resolveMigrations() {
    List<ResolvedMigration> resolvedMigrations = new ArrayList<ResolvedMigration>();
    resolvedMigrations.add(new ResolvedMigration() {

        @Override
        public MigrationVersion getVersion() {
            return MigrationVersion.fromVersion("1.9");
        }

        @Override
        public String getDescription() {
            return "Virtual Migration";
        }

        @Override
        public String getScript() {
            return "VirtualScript 1.9";
        }

        @Override
        public Integer getChecksum() {
            return 19;
        }

        @Override
        public MigrationType getType() {
            return MigrationType.CUSTOM;
        }

        @Override
        public String getPhysicalLocation() {
            return "virtual://loaction";
        }

        @Override
        public MigrationExecutor getExecutor() {
            return new MigrationExecutor() {

                @Override
                public void execute(Connection connection) {
                    System.out.println("Executed !");
                }

                @Override
                public boolean executeInTransaction() {
                    return true;
                }
            };
        }
    });
    return resolvedMigrations;
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) ArrayList(java.util.ArrayList) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) Connection(java.sql.Connection) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) MigrationType(org.flywaydb.core.api.MigrationType)

Example 2 with MigrationExecutor

use of org.flywaydb.core.api.resolver.MigrationExecutor in project flyway by flyway.

the class DbMigrate method applyMigration.

/**
     * Applies this migration to the database. The migration state and the execution time are updated accordingly.
     *
     * @param migration    The migration to apply.
     * @param isOutOfOrder If this migration is being applied out of order.
     * @return The result of the migration.
     */
private Boolean applyMigration(final MigrationInfoImpl migration, boolean isOutOfOrder) {
    MigrationVersion version = migration.getVersion();
    final MigrationExecutor migrationExecutor = migration.getResolvedMigration().getExecutor();
    final String migrationText;
    if (version != null) {
        migrationText = "schema " + schema + " to version " + version + " - " + migration.getDescription() + (isOutOfOrder ? " [out of order]" : "") + (migrationExecutor.executeInTransaction() ? "" : " [non-transactional]");
    } else {
        migrationText = "schema " + schema + " with repeatable migration " + migration.getDescription() + (migrationExecutor.executeInTransaction() ? "" : " [non-transactional]");
    }
    LOG.info("Migrating " + migrationText);
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    try {
        if (migrationExecutor.executeInTransaction()) {
            new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    doMigrate(migration, migrationExecutor, migrationText);
                    return null;
                }
            });
        } else {
            try {
                doMigrate(migration, migrationExecutor, migrationText);
            } catch (SQLException e) {
                throw new FlywaySqlException("Unable to apply migration", e);
            }
        }
    } catch (FlywayException e) {
        String failedMsg = "Migration of " + migrationText + " failed!";
        if (dbSupport.supportsDdlTransactions() && migrationExecutor.executeInTransaction()) {
            LOG.error(failedMsg + " Changes successfully rolled back.");
        } else {
            LOG.error(failedMsg + " Please restore backups and roll back database and code!");
            stopWatch.stop();
            int executionTime = (int) stopWatch.getTotalTimeMillis();
            AppliedMigration appliedMigration = new AppliedMigration(version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), executionTime, false);
            metaDataTable.addAppliedMigration(appliedMigration);
        }
        throw e;
    }
    stopWatch.stop();
    int executionTime = (int) stopWatch.getTotalTimeMillis();
    AppliedMigration appliedMigration = new AppliedMigration(version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), executionTime, true);
    metaDataTable.addAppliedMigration(appliedMigration);
    return false;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) SQLException(java.sql.SQLException) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) StopWatch(org.flywaydb.core.internal.util.StopWatch) MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration)

Example 3 with MigrationExecutor

use of org.flywaydb.core.api.resolver.MigrationExecutor in project flyway by flyway.

the class SampleResolver method resolveMigrations.

@Override
public Collection<ResolvedMigration> resolveMigrations() {
    return Arrays.asList((ResolvedMigration) new ResolvedMigration() {

        @Override
        public MigrationVersion getVersion() {
            return MigrationVersion.fromVersion("2.0");
        }

        @Override
        public String getDescription() {
            return "Custom Resolved";
        }

        @Override
        public String getScript() {
            return "R1";
        }

        @Override
        public Integer getChecksum() {
            return 20;
        }

        @Override
        public MigrationType getType() {
            return MigrationType.CUSTOM;
        }

        @Override
        public String getPhysicalLocation() {
            return null;
        }

        @Override
        public MigrationExecutor getExecutor() {
            return new MigrationExecutor() {

                @Override
                public void execute(Connection connection) throws SQLException {
                    Statement statement = null;
                    try {
                        statement = connection.createStatement();
                        statement.execute("INSERT INTO test_user (name) VALUES ('Resolvix')");
                    } finally {
                        JdbcUtils.closeStatement(statement);
                    }
                }

                @Override
                public boolean executeInTransaction() {
                    return true;
                }
            };
        }
    });
}
Also used : Statement(java.sql.Statement) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) Connection(java.sql.Connection) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration)

Example 4 with MigrationExecutor

use of org.flywaydb.core.api.resolver.MigrationExecutor in project killbill by killbill.

the class DbMigrateWithDryRun method applyMigration.

private void applyMigration(final int installedRnk, final MigrationInfoImpl migration) {
    final MigrationVersion version = migration.getVersion();
    final String migrationText;
    if (version != null) {
        migrationText = "schema " + schema + " to version " + version + " - " + migration.getDescription();
    } else {
        migrationText = "schema " + schema + " with repeatable migration " + migration.getDescription();
    }
    LOG.info("Migrating " + migrationText);
    // PIERRE: override the executor to capture the SQL
    final FileSystemResource sqlScriptResource = new FileSystemResource(migration.getResolvedMigration().getPhysicalLocation());
    final MigrationExecutor migrationExecutor = new CapturingSqlMigrationExecutor(sqlStatements, dbSupport, sqlScriptResource, placeholderReplacer, encoding);
    try {
        doMigrate(migration, migrationExecutor, migrationText);
    } catch (final SQLException e) {
        throw new FlywayException("Unable to apply migration", e);
    }
    final AppliedMigration appliedMigration = new AppliedMigration(installedRnk, version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), null, null, -1, true);
    metaDataTableForDryRun.addAppliedMigration(appliedMigration);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) MigrationVersion(org.flywaydb.core.api.MigrationVersion) SQLException(java.sql.SQLException) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource)

Aggregations

MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)4 MigrationVersion (org.flywaydb.core.api.MigrationVersion)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 FlywayException (org.flywaydb.core.api.FlywayException)2 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)2 AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)2 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 MigrationType (org.flywaydb.core.api.MigrationType)1 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)1 StopWatch (org.flywaydb.core.internal.util.StopWatch)1 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)1 FileSystemResource (org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource)1