Search in sources :

Example 6 with Resource

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

the class ClassPathScannerSmallTest method scanForResources.

@Test
public void scanForResources() throws Exception {
    Resource[] resources = classPathScanner.scanForResources(new Location("classpath:migration/sql"), "V", ".sql");
    assertEquals(4, resources.length);
    assertEquals("migration/sql/V1.1__View.sql", resources[0].getLocation());
    assertEquals("migration/sql/V1_2__Populate_table.sql", resources[1].getLocation());
    assertEquals("migration/sql/V1__First.sql", resources[2].getLocation());
    assertEquals("migration/sql/V2_0__Add_foreign_key_and_super_mega_humongous_padding_to_exceed_the_maximum_column_length_in_the_metadata_table.sql", resources[3].getLocation());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) Location(org.flywaydb.core.internal.util.Location) Test(org.junit.Test) DB2MigrationMediumTest(org.flywaydb.core.internal.dbsupport.db2.DB2MigrationMediumTest)

Example 7 with Resource

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

the class ClassUtilsSmallTest method addDirectoryToClasspath.

/**
     * Tests dynamically adding a directory to the classpath.
     */
@Test
public void addDirectoryToClasspath() throws Exception {
    assertFalse(new ClassPathResource("pkg/runtime.conf", getClassLoader()).exists());
    String folder = new ClassPathResource("dynamic", getClassLoader()).getLocationOnDisk();
    ClassUtils.addJarOrDirectoryToClasspath(folder);
    assertTrue(new ClassPathResource("pkg/runtime.conf", getClassLoader()).exists());
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:pkg"), "run", ".conf");
    assertEquals("pkg/runtime.conf", resources[0].getLocation());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 8 with Resource

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

the class ClassUtilsSmallTest method addJarToClasspath.

/**
     * Tests dynamically adding a jar file to the classpath.
     */
@Test
public void addJarToClasspath() throws Exception {
    assertFalse(new ClassPathResource("db/migration/V1__Initial_structure.sql.sql", getClassLoader()).exists());
    assertFalse(ClassUtils.isPresent("org.flywaydb.sample.migration.V1_2__Another_user", getClassLoader()));
    String jar = new ClassPathResource("flyway-sample.jar", getClassLoader()).getLocationOnDisk();
    assertTrue(new File(jar).isFile());
    ClassUtils.addJarOrDirectoryToClasspath(jar);
    assertTrue(new ClassPathResource("db/migration/V1__Initial_structure.sql", getClassLoader()).exists());
    assertTrue(ClassUtils.isPresent("org.flywaydb.sample.migration.V1_2__Another_user", getClassLoader()));
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:db/migration"), "V1__", ".sql");
    assertEquals("db/migration/V1__Initial_structure.sql", resources[0].getLocation());
    Class<?>[] classes = new ClassPathScanner(getClassLoader()).scanForClasses(new Location("classpath:org/flywaydb/sample/migration"), SpringJdbcMigration.class);
    assertEquals("org.flywaydb.sample.migration.V1_2__Another_user", classes[0].getName());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) File(java.io.File) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 9 with Resource

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

the class ClassUtilsSmallTest method addDirectoryToClasspathDefaultPackage.

/**
     * Tests dynamically adding a directory to the default package of classpath.
     */
@Test
public void addDirectoryToClasspathDefaultPackage() throws Exception {
    assertFalse(new ClassPathResource("runtime.conf", getClassLoader()).exists());
    String folder = new ClassPathResource("dynamic/pkg2", getClassLoader()).getLocationOnDisk();
    ClassUtils.addJarOrDirectoryToClasspath(folder);
    assertTrue(new ClassPathResource("funtime.properties", getClassLoader()).exists());
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:"), "fun", ".properties");
    assertEquals("funtime.properties", resources[1].getLocation());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 10 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)

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