use of org.flywaydb.core.api.MigrationVersion in project ArachneCentralAPI by OHDSI.
the class ApplicationContextAwareSpringJdbcMigrationResolver method extractMigrationInfo.
/**
* Extracts the migration info from this migration.
*
* @param springJdbcMigration The migration to analyse.
* @return The migration info.
*/
/* private -> testing */
ResolvedMigrationImpl extractMigrationInfo(ApplicationContextAwareSpringMigration 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;
boolean repeatable = shortName.startsWith("R");
if (shortName.startsWith("V") || repeatable) {
prefix = shortName.substring(0, 1);
} else {
throw new FlywayException("Invalid Spring 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, "__", "", repeatable);
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.MigrationVersion in project flyway by flyway.
the class MetaDataTableImpl method addAppliedMigration.
@Override
public void addAppliedMigration(AppliedMigration appliedMigration) {
dbSupport.changeCurrentSchemaTo(table.getSchema());
createIfNotExists();
MigrationVersion version = appliedMigration.getVersion();
try {
String versionStr = version == null ? null : version.toString();
// Try load an updateMetaDataTable.sql file if it exists
String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/updateMetaDataTable.sql";
ClassPathResource classPathResource = new ClassPathResource(resourceName, getClass().getClassLoader());
int installedRank = calculateInstalledRank();
if (classPathResource.exists()) {
String source = classPathResource.loadAsString("UTF-8");
Map<String, String> placeholders = new HashMap<String, String>();
// Placeholders for schema and table
placeholders.put("schema", table.getSchema().getName());
placeholders.put("table", table.getName());
// Placeholders for column values
placeholders.put("installed_rank_val", String.valueOf(installedRank));
placeholders.put("version_val", versionStr);
placeholders.put("description_val", appliedMigration.getDescription());
placeholders.put("type_val", appliedMigration.getType().name());
placeholders.put("script_val", appliedMigration.getScript());
placeholders.put("checksum_val", String.valueOf(appliedMigration.getChecksum()));
placeholders.put("installed_by_val", installedBy);
placeholders.put("execution_time_val", String.valueOf(appliedMigration.getExecutionTime() * 1000L));
placeholders.put("success_val", String.valueOf(appliedMigration.isSuccess()));
String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);
SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
sqlScript.execute(jdbcTemplate);
} else {
// Fall back to hard-coded statements
jdbcTemplate.update("INSERT INTO " + table + " (" + dbSupport.quote("installed_rank") + "," + dbSupport.quote("version") + "," + dbSupport.quote("description") + "," + dbSupport.quote("type") + "," + dbSupport.quote("script") + "," + dbSupport.quote("checksum") + "," + dbSupport.quote("installed_by") + "," + dbSupport.quote("execution_time") + "," + dbSupport.quote("success") + ")" + " VALUES (?, ?, ?, ?, ?, ?, " + installedBy + ", ?, ?)", installedRank, versionStr, appliedMigration.getDescription(), appliedMigration.getType().name(), appliedMigration.getScript(), appliedMigration.getChecksum(), appliedMigration.getExecutionTime(), appliedMigration.isSuccess());
}
LOG.debug("MetaData table " + table + " successfully updated to reflect changes");
} catch (SQLException e) {
throw new FlywaySqlException("Unable to insert row for version '" + version + "' in metadata table " + table, e);
}
}
use of org.flywaydb.core.api.MigrationVersion in project flyway by flyway.
the class FlywayMediumTest method outOfOrderInOrder.
@Test
public void outOfOrderInOrder() {
DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_out_of_order_in_order;DB_CLOSE_DELAY=-1", "sa", "", null);
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("migration/sql");
flyway.migrate();
MigrationVersion highest = null;
for (MigrationInfo migrationInfo : flyway.info().applied()) {
assertEquals(MigrationState.SUCCESS, migrationInfo.getState());
if (highest == null) {
highest = migrationInfo.getVersion();
} else {
assertTrue(migrationInfo.getVersion().compareTo(highest) > 0);
highest = migrationInfo.getVersion();
}
}
assertEquals(MigrationVersion.fromVersion("2.0"), highest);
}
use of org.flywaydb.core.api.MigrationVersion in project flyway by flyway.
the class DbMigrate method migrate.
/**
* Starts the actual migration.
*
* @return The number of successfully applied migrations.
* @throws FlywayException when migration failed.
*/
public int migrate() throws FlywayException {
try {
for (final FlywayCallback callback : configuration.getCallbacks()) {
new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupportUserObjects.changeCurrentSchemaTo(schema);
callback.beforeMigrate(connectionUserObjects);
return null;
}
});
}
StopWatch stopWatch = new StopWatch();
stopWatch.start();
int migrationSuccessCount = 0;
while (true) {
final boolean firstRun = migrationSuccessCount == 0;
boolean done = metaDataTable.lock(new Callable<Boolean>() {
@Override
public Boolean call() {
MigrationInfoServiceImpl infoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, configuration.getTarget(), configuration.isOutOfOrder(), true, true, true);
infoService.refresh();
MigrationVersion currentSchemaVersion = MigrationVersion.EMPTY;
if (infoService.current() != null) {
currentSchemaVersion = infoService.current().getVersion();
}
if (firstRun) {
LOG.info("Current version of schema " + schema + ": " + currentSchemaVersion);
if (configuration.isOutOfOrder()) {
LOG.warn("outOfOrder mode is active. Migration of schema " + schema + " may not be reproducible.");
}
}
MigrationInfo[] future = infoService.future();
if (future.length > 0) {
MigrationInfo[] resolved = infoService.resolved();
if (resolved.length == 0) {
LOG.warn("Schema " + schema + " has version " + currentSchemaVersion + ", but no migration could be resolved in the configured locations !");
} else {
int offset = resolved.length - 1;
while (resolved[offset].getVersion() == null) {
// Skip repeatable migrations
offset--;
}
LOG.warn("Schema " + schema + " has a version (" + currentSchemaVersion + ") that is newer than the latest available migration (" + resolved[offset].getVersion() + ") !");
}
}
MigrationInfo[] failed = infoService.failed();
if (failed.length > 0) {
if ((failed.length == 1) && (failed[0].getState() == MigrationState.FUTURE_FAILED) && (configuration.isIgnoreFutureMigrations() || ignoreFailedFutureMigration)) {
LOG.warn("Schema " + schema + " contains a failed future migration to version " + failed[0].getVersion() + " !");
} else {
if (failed[0].getVersion() == null) {
throw new FlywayException("Schema " + schema + " contains a failed repeatable migration (" + failed[0].getDescription() + ") !");
}
throw new FlywayException("Schema " + schema + " contains a failed migration to version " + failed[0].getVersion() + " !");
}
}
MigrationInfoImpl[] pendingMigrations = infoService.pending();
if (pendingMigrations.length == 0) {
return true;
}
boolean isOutOfOrder = pendingMigrations[0].getVersion() != null && pendingMigrations[0].getVersion().compareTo(currentSchemaVersion) < 0;
return applyMigration(pendingMigrations[0], isOutOfOrder);
}
});
if (done) {
// No further migrations available
break;
}
migrationSuccessCount++;
}
stopWatch.stop();
logSummary(migrationSuccessCount, stopWatch.getTotalTimeMillis());
for (final FlywayCallback callback : configuration.getCallbacks()) {
new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupportUserObjects.changeCurrentSchemaTo(schema);
callback.afterMigrate(connectionUserObjects);
return null;
}
});
}
return migrationSuccessCount;
} finally {
dbSupportUserObjects.restoreCurrentSchema();
}
}
use of org.flywaydb.core.api.MigrationVersion in project flyway by flyway.
the class MigrationTestCase method migrate.
@Test
public void migrate() throws Exception {
flyway.setLocations(getBasedir());
flyway.migrate();
MigrationVersion version = flyway.info().current().getVersion();
assertEquals("2.0", version.toString());
assertEquals(0, flyway.migrate());
// We should have 5 rows if we have a schema creation marker as the first entry, 4 otherwise
if (flyway.info().applied()[0].getType() == MigrationType.SCHEMA) {
assertEquals(5, flyway.info().applied().length);
} else {
assertEquals(4, flyway.info().applied().length);
}
for (MigrationInfo migrationInfo : flyway.info().applied()) {
assertChecksum(migrationInfo);
}
assertEquals(2, jdbcTemplate.queryForInt("select count(*) from all_misters"));
}
Aggregations