Search in sources :

Example 1 with Resource

use of org.flywaydb.core.internal.util.scanner.Resource in project che by eclipse.

the class CustomSqlMigrationResolver method resolveSqlMigrations.

private List<ResolvedMigration> resolveSqlMigrations() throws IOException, SQLException {
    LOG.info("Searching for sql scripts in locations {}", Arrays.toString(flywayConfiguration.getLocations()));
    final Map<Location, List<Resource>> allResources = finder.findResources(flywayConfiguration);
    LOG.debug("Found scripts: {}", allResources);
    final Map<String, Map<String, SqlScript>> scriptsInDir = new HashMap<>();
    for (Location location : allResources.keySet()) {
        final List<Resource> resources = allResources.get(location);
        for (Resource resource : resources) {
            final SqlScript newScript = scriptsCreator.createScript(location, resource);
            if (!scriptsInDir.containsKey(newScript.dir)) {
                scriptsInDir.put(newScript.dir, new HashMap<>(4));
            }
            final Map<String, SqlScript> existingScripts = scriptsInDir.get(newScript.dir);
            final SqlScript existingScript = existingScripts.get(newScript.name);
            if (existingScript == null) {
                existingScripts.put(newScript.name, newScript);
            } else if (Objects.equals(existingScript.vendor, newScript.vendor)) {
                throw new FlywayException(format("More than one script with name '%s' is registered for " + "database vendor '%s', script '%s' conflicts with '%s'", newScript.name, existingScript.vendor, newScript, existingScript));
            } else if (vendorName.equals(newScript.vendor)) {
                existingScripts.put(newScript.name, newScript);
            }
        }
    }
    final Map<MigrationVersion, ResolvedMigration> migrations = new HashMap<>();
    for (SqlScript script : scriptsInDir.values().stream().flatMap(scripts -> scripts.values().stream()).collect(toList())) {
        final ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
        migration.setVersion(versionResolver.resolve(script, flywayConfiguration));
        migration.setScript(script.resource.getLocation());
        migration.setPhysicalLocation(script.resource.getLocationOnDisk());
        migration.setType(MigrationType.SQL);
        migration.setDescription(script.name);
        migration.setChecksum(ByteSource.wrap(script.resource.loadAsBytes()).hash(Hashing.crc32()).asInt());
        migration.setExecutor(new SqlMigrationExecutor(dbSupport, script.resource, placeholderReplacer, flywayConfiguration.getEncoding()));
        if (migrations.put(migration.getVersion(), migration) != null) {
            throw new FlywayException("Two migrations with the same version detected");
        }
    }
    return new ArrayList<>(migrations.values());
}
Also used : Arrays(java.util.Arrays) FlywayException(org.flywaydb.core.api.FlywayException) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) Resource(org.flywaydb.core.internal.util.scanner.Resource) HashMap(java.util.HashMap) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) BaseMigrationResolver(org.flywaydb.core.api.resolver.BaseMigrationResolver) ArrayList(java.util.ArrayList) Location(org.flywaydb.core.internal.util.Location) MigrationVersion(org.flywaydb.core.api.MigrationVersion) SQLException(java.sql.SQLException) Map(java.util.Map) ByteSource(com.google.common.io.ByteSource) Logger(org.slf4j.Logger) SqlMigrationExecutor(org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) Collection(java.util.Collection) IOException(java.io.IOException) MigrationType(org.flywaydb.core.api.MigrationType) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) Resource(org.flywaydb.core.internal.util.scanner.Resource) ArrayList(java.util.ArrayList) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) SqlMigrationExecutor(org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor) MigrationVersion(org.flywaydb.core.api.MigrationVersion) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.flywaydb.core.internal.util.Location)

Example 2 with Resource

use of org.flywaydb.core.internal.util.scanner.Resource in project che by eclipse.

the class SqlScriptCreatorTest method createsScript.

@Test
public void createsScript() throws Exception {
    final Location location = new Location("filesystem:schema");
    final Resource resource = new FileSystemResource("schema/5.0.0-M7/v1__init.sql");
    final SqlScriptCreator scriptsCreator = new SqlScriptCreator();
    final SqlScript script = scriptsCreator.createScript(location, resource);
    assertEquals(script.name, "v1__init.sql");
    assertEquals(script.location, location);
    assertEquals(script.dir, "5.0.0-M7");
    assertEquals(script.resource.getLocation(), resource.getLocation());
    assertNull(script.vendor);
}
Also used : FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource) Resource(org.flywaydb.core.internal.util.scanner.Resource) FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource) Location(org.flywaydb.core.internal.util.Location) Test(org.testng.annotations.Test)

Example 3 with Resource

use of org.flywaydb.core.internal.util.scanner.Resource in project che by eclipse.

the class SqlScriptCreatorTest method failsToCreateResourceWhenPathIsInvalid.

@Test(expectedExceptions = FlywayException.class)
public void failsToCreateResourceWhenPathIsInvalid() throws Exception {
    final Location location = new Location("filesystem:schema");
    final Resource resource = new FileSystemResource("schema/v1__init.sql");
    new SqlScriptCreator().createScript(location, resource);
}
Also used : FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource) Resource(org.flywaydb.core.internal.util.scanner.Resource) FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource) Location(org.flywaydb.core.internal.util.Location) Test(org.testng.annotations.Test)

Example 4 with Resource

use of org.flywaydb.core.internal.util.scanner.Resource in project che by eclipse.

the class SqlScriptCreatorTest method createsVendorScript.

@Test
public void createsVendorScript() throws Exception {
    final Location location = new Location("filesystem:schema");
    final Resource resource = new FileSystemResource("schema/5.0.0-M7/postgresql/v1__init.sql");
    final SqlScriptCreator scriptsCreator = new SqlScriptCreator();
    final SqlScript script = scriptsCreator.createScript(location, resource);
    assertEquals(script.name, "v1__init.sql");
    assertEquals(script.location, location);
    assertEquals(script.dir, "5.0.0-M7");
    assertEquals(script.resource.getLocation(), resource.getLocation());
    assertEquals(script.vendor, "postgresql");
}
Also used : FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource) Resource(org.flywaydb.core.internal.util.scanner.Resource) FileSystemResource(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource) Location(org.flywaydb.core.internal.util.Location) Test(org.testng.annotations.Test)

Example 5 with Resource

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

the class SqlMigrationResolver method scanForMigrations.

private void scanForMigrations(List<ResolvedMigration> migrations, String prefix, String separator, String suffix) {
    for (Resource resource : scanner.scanForResources(location, prefix, suffix)) {
        String filename = resource.getFilename();
        if (isSqlCallback(filename, suffix)) {
            continue;
        }
        Pair<MigrationVersion, String> info = MigrationInfoHelper.extractVersionAndDescription(filename, prefix, separator, suffix);
        ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
        migration.setVersion(info.getLeft());
        migration.setDescription(info.getRight());
        migration.setScript(extractScriptName(resource));
        migration.setChecksum(calculateChecksum(resource, resource.loadAsString(configuration.getEncoding())));
        migration.setType(MigrationType.SQL);
        migration.setPhysicalLocation(resource.getLocationOnDisk());
        migration.setExecutor(new SqlMigrationExecutor(dbSupport, resource, placeholderReplacer, configuration));
        migrations.add(migration);
    }
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) Resource(org.flywaydb.core.internal.util.scanner.Resource) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Aggregations

Resource (org.flywaydb.core.internal.util.scanner.Resource)17 Location (org.flywaydb.core.internal.util.Location)11 Test (org.junit.Test)11 DB2MigrationMediumTest (org.flywaydb.core.internal.dbsupport.db2.DB2MigrationMediumTest)7 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)4 ClassPathScanner (org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner)4 FileSystemResource (org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource)3 Test (org.testng.annotations.Test)3 File (java.io.File)2 HashSet (java.util.HashSet)2 MigrationVersion (org.flywaydb.core.api.MigrationVersion)2 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)2 AfterClass (org.junit.AfterClass)2 BeforeClass (org.junit.BeforeClass)2 Hashing (com.google.common.hash.Hashing)1 ByteSource (com.google.common.io.ByteSource)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1