Search in sources :

Example 1 with Location

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

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

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

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

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

the class ClassPathScannerSmallTest method scanForClassesJarFile.

@Test
public void scanForClassesJarFile() throws Exception {
    Class<?>[] classes = classPathScanner.scanForClasses(new Location("classpath:org/mockito/internal/creation"), MockSettings.class);
    assertTrue(Arrays.asList(classes).contains(MockSettingsImpl.class));
}
Also used : MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Location(org.flywaydb.core.internal.util.Location) Test(org.junit.Test) DB2MigrationMediumTest(org.flywaydb.core.internal.dbsupport.db2.DB2MigrationMediumTest)

Aggregations

Location (org.flywaydb.core.internal.util.Location)29 Test (org.junit.Test)20 DB2MigrationMediumTest (org.flywaydb.core.internal.dbsupport.db2.DB2MigrationMediumTest)11 Resource (org.flywaydb.core.internal.util.scanner.Resource)11 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)7 FileSystemResource (org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource)6 ArrayList (java.util.ArrayList)5 Test (org.testng.annotations.Test)5 Scanner (org.flywaydb.core.internal.util.scanner.Scanner)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 SqlMigrationResolver (org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver)2 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Hashing (com.google.common.hash.Hashing)1 ByteSource (com.google.common.io.ByteSource)1 File (java.io.File)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1