Search in sources :

Example 1 with MigrationResolver

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

the class CompositeMigrationResolverSmallTest method skipDefaultResolvers.

@Test
public void skipDefaultResolvers() {
    FlywayConfigurationForTests config = FlywayConfigurationForTests.create();
    config.setSkipDefaultResolvers(true);
    MigrationResolver migrationResolver = new CompositeMigrationResolver(null, new Scanner(Thread.currentThread().getContextClassLoader()), config, new Locations("migration/outoforder", "org/flywaydb/core/internal/resolver/jdbc/dummy"), PlaceholderReplacer.NO_PLACEHOLDERS);
    Collection<ResolvedMigration> migrations = migrationResolver.resolveMigrations();
    assertTrue(migrations.isEmpty());
}
Also used : Scanner(org.flywaydb.core.internal.util.scanner.Scanner) Locations(org.flywaydb.core.internal.util.Locations) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) Test(org.junit.Test)

Example 2 with MigrationResolver

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

the class Flyway method execute.

/**
     * Executes this command with proper resource handling and cleanup.
     *
     * @param command The command to execute.
     * @param <T>     The type of the result.
     * @return The result of the command.
     */
/*private -> testing*/
<T> T execute(Command<T> command) {
    T result;
    VersionPrinter.printVersion();
    Connection connectionMetaDataTable = null;
    try {
        if (dataSource == null) {
            throw new FlywayException("Unable to connect to the database. Configure the url, user and password!");
        }
        connectionMetaDataTable = JdbcUtils.openConnection(dataSource);
        DbSupport dbSupport = DbSupportFactory.createDbSupport(connectionMetaDataTable, !dbConnectionInfoPrinted);
        dbConnectionInfoPrinted = true;
        LOG.debug("DDL Transactions Supported: " + dbSupport.supportsDdlTransactions());
        if (schemaNames.length == 0) {
            Schema currentSchema = dbSupport.getOriginalSchema();
            if (currentSchema == null) {
                throw new FlywayException("Unable to determine schema for the metadata table." + " Set a default schema for the connection or specify one using the schemas property!");
            }
            setSchemas(currentSchema.getName());
        }
        if (schemaNames.length == 1) {
            LOG.debug("Schema: " + schemaNames[0]);
        } else {
            LOG.debug("Schemas: " + StringUtils.arrayToCommaDelimitedString(schemaNames));
        }
        Schema[] schemas = new Schema[schemaNames.length];
        for (int i = 0; i < schemaNames.length; i++) {
            schemas[i] = dbSupport.getSchema(schemaNames[i]);
        }
        Scanner scanner = new Scanner(classLoader);
        MigrationResolver migrationResolver = createMigrationResolver(dbSupport, scanner);
        if (!skipDefaultCallbacks) {
            Set<FlywayCallback> flywayCallbacks = new LinkedHashSet<FlywayCallback>(Arrays.asList(callbacks));
            flywayCallbacks.add(new SqlScriptFlywayCallback(dbSupport, scanner, locations, createPlaceholderReplacer(), this));
            callbacks = flywayCallbacks.toArray(new FlywayCallback[flywayCallbacks.size()]);
        }
        for (FlywayCallback callback : callbacks) {
            ConfigurationInjectionUtils.injectFlywayConfiguration(callback, this);
        }
        MetaDataTable metaDataTable = new MetaDataTableImpl(dbSupport, schemas[0].getTable(table), installedBy);
        if (metaDataTable.upgradeIfNecessary()) {
            new DbRepair(dbSupport, connectionMetaDataTable, schemas[0], migrationResolver, metaDataTable, callbacks).repairChecksumsAndDescriptions();
            LOG.info("Metadata table " + table + " successfully upgraded to the Flyway 4.0 format.");
        }
        result = command.execute(connectionMetaDataTable, migrationResolver, metaDataTable, dbSupport, schemas, callbacks);
    } finally {
        JdbcUtils.closeConnection(connectionMetaDataTable);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FlywayException(org.flywaydb.core.api.FlywayException) Scanner(org.flywaydb.core.internal.util.scanner.Scanner) MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) Schema(org.flywaydb.core.internal.dbsupport.Schema) Connection(java.sql.Connection) DbRepair(org.flywaydb.core.internal.command.DbRepair) MetaDataTableImpl(org.flywaydb.core.internal.metadatatable.MetaDataTableImpl) SqlScriptFlywayCallback(org.flywaydb.core.internal.callback.SqlScriptFlywayCallback) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) SqlScriptFlywayCallback(org.flywaydb.core.internal.callback.SqlScriptFlywayCallback) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) CompositeMigrationResolver(org.flywaydb.core.internal.resolver.CompositeMigrationResolver)

Example 3 with MigrationResolver

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

the class FlywaySmallTest method configure.

@Test
public void configure() {
    Properties properties = new Properties();
    properties.setProperty("flyway.user", "sa");
    properties.setProperty("flyway.password", "");
    properties.setProperty("flyway.url", "jdbc:h2:mem:flyway_test;DB_CLOSE_DELAY=-1");
    properties.setProperty("flyway.driver", "org.h2.Driver");
    final Flyway flyway = new Flyway();
    flyway.configure(properties);
    assertNotNull(flyway.getDataSource());
    flyway.execute(new Flyway.Command<Void>() {

        public Void execute(Connection connectionMetaDataTable, MigrationResolver migrationResolver, MetaDataTable metaDataTable, DbSupport dbSupport, Schema[] schemas, FlywayCallback[] flywayCallbacks) {
            assertEquals("PUBLIC", flyway.getSchemas()[0]);
            return null;
        }
    });
}
Also used : FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) Schema(org.flywaydb.core.internal.dbsupport.Schema) Connection(java.sql.Connection) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) Properties(java.util.Properties) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) MyCustomMigrationResolver(org.flywaydb.core.internal.resolver.MyCustomMigrationResolver) Test(org.junit.Test)

Example 4 with MigrationResolver

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

the class Flyway method info.

/**
     * <p>Retrieves the complete information about all the migrations including applied, pending and current migrations with
     * details and status.</p>
     * <img src="https://flywaydb.org/assets/balsamiq/command-info.png" alt="info">
     *
     * @return All migrations sorted by version, oldest first.
     * @throws FlywayException when the info retrieval failed.
     */
public MigrationInfoService info() {
    return execute(new Command<MigrationInfoService>() {

        public MigrationInfoService execute(final Connection connectionMetaDataTable, MigrationResolver migrationResolver, MetaDataTable metaDataTable, final DbSupport dbSupport, final Schema[] schemas, FlywayCallback[] flywayCallbacks) {
            try {
                for (final FlywayCallback callback : flywayCallbacks) {
                    new TransactionTemplate(connectionMetaDataTable).execute(new Callable<Object>() {

                        @Override
                        public Object call() throws SQLException {
                            dbSupport.changeCurrentSchemaTo(schemas[0]);
                            callback.beforeInfo(connectionMetaDataTable);
                            return null;
                        }
                    });
                }
                MigrationInfoServiceImpl migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, target, outOfOrder, true, true, true);
                migrationInfoService.refresh();
                for (final FlywayCallback callback : flywayCallbacks) {
                    new TransactionTemplate(connectionMetaDataTable).execute(new Callable<Object>() {

                        @Override
                        public Object call() throws SQLException {
                            dbSupport.changeCurrentSchemaTo(schemas[0]);
                            callback.afterInfo(connectionMetaDataTable);
                            return null;
                        }
                    });
                }
                return migrationInfoService;
            } finally {
                dbSupport.restoreCurrentSchema();
            }
        }
    });
}
Also used : MigrationInfoServiceImpl(org.flywaydb.core.internal.info.MigrationInfoServiceImpl) MigrationInfoService(org.flywaydb.core.api.MigrationInfoService) MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) Schema(org.flywaydb.core.internal.dbsupport.Schema) Connection(java.sql.Connection) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) Callable(java.util.concurrent.Callable) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) SqlScriptFlywayCallback(org.flywaydb.core.internal.callback.SqlScriptFlywayCallback) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) CompositeMigrationResolver(org.flywaydb.core.internal.resolver.CompositeMigrationResolver)

Example 5 with MigrationResolver

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

the class CompositeMigrationResolverSmallTest method collectMigrations.

/**
     * Checks that migrations are properly collected, eliminating all exact duplicates.
     */
@Test
public void collectMigrations() {
    MigrationResolver migrationResolver = new MigrationResolver() {

        public List<ResolvedMigration> resolveMigrations() {
            List<ResolvedMigration> migrations = new ArrayList<ResolvedMigration>();
            migrations.add(createTestMigration(MigrationType.SPRING_JDBC, "1", "Description", "Migration1", 123));
            migrations.add(createTestMigration(MigrationType.SPRING_JDBC, "1", "Description", "Migration1", 123));
            migrations.add(createTestMigration(MigrationType.SQL, "2", "Description2", "Migration2", 1234));
            return migrations;
        }
    };
    Collection<MigrationResolver> migrationResolvers = new ArrayList<MigrationResolver>();
    migrationResolvers.add(migrationResolver);
    Collection<ResolvedMigration> migrations = CompositeMigrationResolver.collectMigrations(migrationResolvers);
    assertEquals(2, migrations.size());
}
Also used : ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) Test(org.junit.Test)

Aggregations

MigrationResolver (org.flywaydb.core.api.resolver.MigrationResolver)7 Test (org.junit.Test)5 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)4 Connection (java.sql.Connection)3 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)3 DbSupport (org.flywaydb.core.internal.dbsupport.DbSupport)3 Schema (org.flywaydb.core.internal.dbsupport.Schema)3 MetaDataTable (org.flywaydb.core.internal.metadatatable.MetaDataTable)3 Scanner (org.flywaydb.core.internal.util.scanner.Scanner)3 SqlScriptFlywayCallback (org.flywaydb.core.internal.callback.SqlScriptFlywayCallback)2 CompositeMigrationResolver (org.flywaydb.core.internal.resolver.CompositeMigrationResolver)2 Locations (org.flywaydb.core.internal.util.Locations)2 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 Callable (java.util.concurrent.Callable)1 FlywayException (org.flywaydb.core.api.FlywayException)1 MigrationInfoService (org.flywaydb.core.api.MigrationInfoService)1 DbRepair (org.flywaydb.core.internal.command.DbRepair)1 MigrationInfoServiceImpl (org.flywaydb.core.internal.info.MigrationInfoServiceImpl)1 MetaDataTableImpl (org.flywaydb.core.internal.metadatatable.MetaDataTableImpl)1