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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations