Search in sources :

Example 1 with Scanner

use of org.flywaydb.core.internal.util.scanner.Scanner 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 Scanner

use of org.flywaydb.core.internal.util.scanner.Scanner 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 Scanner

use of org.flywaydb.core.internal.util.scanner.Scanner in project flyway by flyway.

the class MigrationTestCase method assertChecksum.

/**
     * Compares the DB checksum to the classpath checksum of this migration.
     *
     * @param migrationInfo
     *            The migration to check.
     */
protected void assertChecksum(MigrationInfo migrationInfo) {
    SqlMigrationResolver sqlMigrationResolver = new SqlMigrationResolver(dbSupport, new Scanner(Thread.currentThread().getContextClassLoader()), new Location(getBasedir()), PlaceholderReplacer.NO_PLACEHOLDERS, FlywayConfigurationForTests.create());
    List<ResolvedMigration> migrations = sqlMigrationResolver.resolveMigrations();
    for (ResolvedMigration migration : migrations) {
        if (migration.getVersion().toString().equals(migrationInfo.getVersion().toString())) {
            assertEquals("Wrong checksum for " + migrationInfo.getScript(), migration.getChecksum(), migrationInfo.getChecksum());
        }
    }
}
Also used : Scanner(org.flywaydb.core.internal.util.scanner.Scanner) SqlMigrationResolver(org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) Location(org.flywaydb.core.internal.util.Location)

Example 4 with Scanner

use of org.flywaydb.core.internal.util.scanner.Scanner in project flyway by flyway.

the class SqlMigrationResolverMediumTest method resolveMigrations.

@Test
public void resolveMigrations() throws Exception {
    @SuppressWarnings("ConstantConditions") String path = URLDecoder.decode(getClass().getClassLoader().getResource("migration/subdir").getPath(), "UTF-8");
    SqlMigrationResolver sqlMigrationResolver = new SqlMigrationResolver(null, new Scanner(Thread.currentThread().getContextClassLoader()), new Location("filesystem:" + new File(path).getPath()), PlaceholderReplacer.NO_PLACEHOLDERS, FlywayConfigurationForTests.create());
    Collection<ResolvedMigration> migrations = sqlMigrationResolver.resolveMigrations();
    assertEquals(3, migrations.size());
    List<ResolvedMigration> migrationList = new ArrayList<ResolvedMigration>(migrations);
    assertEquals("1", migrationList.get(0).getVersion().toString());
    assertEquals("1.1", migrationList.get(1).getVersion().toString());
    assertEquals("2.0", migrationList.get(2).getVersion().toString());
    assertEquals("dir1/V1__First.sql", migrationList.get(0).getScript());
    assertEquals("V1_1__Populate_table.sql", migrationList.get(1).getScript());
    assertEquals("dir2/V2_0__Add_foreign_key.sql", migrationList.get(2).getScript());
}
Also used : Scanner(org.flywaydb.core.internal.util.scanner.Scanner) ArrayList(java.util.ArrayList) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) File(java.io.File) Location(org.flywaydb.core.internal.util.Location) Test(org.junit.Test)

Example 5 with Scanner

use of org.flywaydb.core.internal.util.scanner.Scanner in project flyway by flyway.

the class CompositeMigrationResolverSmallTest method resolveMigrationsMultipleLocations.

@Test
public void resolveMigrationsMultipleLocations() {
    FlywayConfigurationForTests config = FlywayConfigurationForTests.create();
    PlaceholderReplacer placeholderReplacer = new PlaceholderReplacer(new HashMap<String, String>(), "${", "}");
    MigrationResolver migrationResolver = new CompositeMigrationResolver(null, new Scanner(Thread.currentThread().getContextClassLoader()), config, new Locations("migration/subdir/dir2", "migration.outoforder", "migration/subdir/dir1"), placeholderReplacer, new MyCustomMigrationResolver());
    Collection<ResolvedMigration> migrations = migrationResolver.resolveMigrations();
    List<ResolvedMigration> migrationList = new ArrayList<ResolvedMigration>(migrations);
    assertEquals(4, migrations.size());
    assertEquals("First", migrationList.get(0).getDescription());
    assertEquals("Late arrivals", migrationList.get(1).getDescription());
    assertEquals("Virtual Migration", migrationList.get(2).getDescription());
    assertEquals("Add foreign key", migrationList.get(3).getDescription());
}
Also used : Scanner(org.flywaydb.core.internal.util.scanner.Scanner) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) 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)

Aggregations

Scanner (org.flywaydb.core.internal.util.scanner.Scanner)6 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)5 MigrationResolver (org.flywaydb.core.api.resolver.MigrationResolver)3 Location (org.flywaydb.core.internal.util.Location)3 Test (org.junit.Test)3 SqlMigrationResolver (org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver)2 Locations (org.flywaydb.core.internal.util.Locations)2 File (java.io.File)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 FlywayException (org.flywaydb.core.api.FlywayException)1 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)1 SqlScriptFlywayCallback (org.flywaydb.core.internal.callback.SqlScriptFlywayCallback)1 DbRepair (org.flywaydb.core.internal.command.DbRepair)1 DbSupport (org.flywaydb.core.internal.dbsupport.DbSupport)1 Schema (org.flywaydb.core.internal.dbsupport.Schema)1 MetaDataTable (org.flywaydb.core.internal.metadatatable.MetaDataTable)1 MetaDataTableImpl (org.flywaydb.core.internal.metadatatable.MetaDataTableImpl)1 CompositeMigrationResolver (org.flywaydb.core.internal.resolver.CompositeMigrationResolver)1