use of org.flywaydb.core.api.FlywayException 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());
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class JdbcMigrationResolver method extractMigrationInfo.
/**
* Extracts the migration info from this migration.
*
* @param jdbcMigration The migration to analyse.
* @return The migration info.
*/
/* private -> testing */
ResolvedMigrationImpl extractMigrationInfo(JdbcMigration jdbcMigration) {
Integer checksum = null;
if (jdbcMigration instanceof MigrationChecksumProvider) {
MigrationChecksumProvider checksumProvider = (MigrationChecksumProvider) jdbcMigration;
checksum = checksumProvider.getChecksum();
}
MigrationVersion version;
String description;
if (jdbcMigration instanceof MigrationInfoProvider) {
MigrationInfoProvider infoProvider = (MigrationInfoProvider) jdbcMigration;
version = infoProvider.getVersion();
description = infoProvider.getDescription();
if (!StringUtils.hasText(description)) {
throw new FlywayException("Missing description for migration " + version);
}
} else {
String shortName = ClassUtils.getShortName(jdbcMigration.getClass());
String prefix;
if (shortName.startsWith("V") || shortName.startsWith("R")) {
prefix = shortName.substring(0, 1);
} else {
throw new FlywayException("Invalid Jdbc migration class name: " + jdbcMigration.getClass().getName() + " => ensure it starts with V or R," + " or implement org.flywaydb.core.api.migration.MigrationInfoProvider for non-default naming");
}
Pair<MigrationVersion, String> info = MigrationInfoHelper.extractVersionAndDescription(shortName, prefix, "__", "");
version = info.getLeft();
description = info.getRight();
}
ResolvedMigrationImpl resolvedMigration = new ResolvedMigrationImpl();
resolvedMigration.setVersion(version);
resolvedMigration.setDescription(description);
resolvedMigration.setScript(jdbcMigration.getClass().getName());
resolvedMigration.setChecksum(checksum);
resolvedMigration.setType(MigrationType.JDBC);
return resolvedMigration;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class JdbcMigrationResolver method resolveMigrations.
@Override
public List<ResolvedMigration> resolveMigrations() {
List<ResolvedMigration> migrations = new ArrayList<ResolvedMigration>();
if (!location.isClassPath()) {
return migrations;
}
try {
Class<?>[] classes = scanner.scanForClasses(location, JdbcMigration.class);
for (Class<?> clazz : classes) {
JdbcMigration jdbcMigration = ClassUtils.instantiate(clazz.getName(), scanner.getClassLoader());
ConfigurationInjectionUtils.injectFlywayConfiguration(jdbcMigration, configuration);
ResolvedMigrationImpl migrationInfo = extractMigrationInfo(jdbcMigration);
migrationInfo.setPhysicalLocation(ClassUtils.getLocationOnDisk(clazz));
migrationInfo.setExecutor(new JdbcMigrationExecutor(jdbcMigration));
migrations.add(migrationInfo);
}
} catch (Exception e) {
throw new FlywayException("Unable to resolve Jdbc Java migrations in location: " + location + " (" + e.getMessage() + ")", e);
}
Collections.sort(migrations, new ResolvedMigrationComparator());
return migrations;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class SpringJdbcMigrationResolver method extractMigrationInfo.
/**
* Extracts the migration info from this migration.
*
* @param springJdbcMigration The migration to analyse.
* @return The migration info.
*/
/* private -> testing */
ResolvedMigrationImpl extractMigrationInfo(SpringJdbcMigration springJdbcMigration) {
Integer checksum = null;
if (springJdbcMigration instanceof MigrationChecksumProvider) {
MigrationChecksumProvider checksumProvider = (MigrationChecksumProvider) springJdbcMigration;
checksum = checksumProvider.getChecksum();
}
MigrationVersion version;
String description;
if (springJdbcMigration instanceof MigrationInfoProvider) {
MigrationInfoProvider infoProvider = (MigrationInfoProvider) springJdbcMigration;
version = infoProvider.getVersion();
description = infoProvider.getDescription();
if (!StringUtils.hasText(description)) {
throw new FlywayException("Missing description for migration " + version);
}
} else {
String shortName = ClassUtils.getShortName(springJdbcMigration.getClass());
String prefix;
if (shortName.startsWith("V") || shortName.startsWith("R")) {
prefix = shortName.substring(0, 1);
} else {
throw new FlywayException("Invalid Jdbc migration class name: " + springJdbcMigration.getClass().getName() + " => ensure it starts with V or R," + " or implement org.flywaydb.core.api.migration.MigrationInfoProvider for non-default naming");
}
Pair<MigrationVersion, String> info = MigrationInfoHelper.extractVersionAndDescription(shortName, prefix, "__", "");
version = info.getLeft();
description = info.getRight();
}
ResolvedMigrationImpl resolvedMigration = new ResolvedMigrationImpl();
resolvedMigration.setVersion(version);
resolvedMigration.setDescription(description);
resolvedMigration.setScript(springJdbcMigration.getClass().getName());
resolvedMigration.setChecksum(checksum);
resolvedMigration.setType(MigrationType.SPRING_JDBC);
return resolvedMigration;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class SpringJdbcMigrationResolver method resolveMigrations.
@Override
public Collection<ResolvedMigration> resolveMigrations() {
List<ResolvedMigration> migrations = new ArrayList<ResolvedMigration>();
if (!location.isClassPath()) {
return migrations;
}
try {
Class<?>[] classes = scanner.scanForClasses(location, SpringJdbcMigration.class);
for (Class<?> clazz : classes) {
SpringJdbcMigration springJdbcMigration = ClassUtils.instantiate(clazz.getName(), scanner.getClassLoader());
ConfigurationInjectionUtils.injectFlywayConfiguration(springJdbcMigration, configuration);
ResolvedMigrationImpl migrationInfo = extractMigrationInfo(springJdbcMigration);
migrationInfo.setPhysicalLocation(ClassUtils.getLocationOnDisk(clazz));
migrationInfo.setExecutor(new SpringJdbcMigrationExecutor(springJdbcMigration));
migrations.add(migrationInfo);
}
} catch (Exception e) {
throw new FlywayException("Unable to resolve Spring Jdbc Java migrations in location: " + location, e);
}
Collections.sort(migrations, new ResolvedMigrationComparator());
return migrations;
}
Aggregations