use of org.flywaydb.core.internal.util.PlaceholderReplacer 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.internal.util.PlaceholderReplacer in project flyway by flyway.
the class SqlScriptSmallTest method parsePlaceholderComments.
@Test
public void parsePlaceholderComments() {
String source = "${drop_view} \"SOME_VIEW\" IF EXISTS;\n" + "CREATE ${or_replace} VIEW \"SOME_VIEW\";\n";
Map<String, String> placeholders = new HashMap<String, String>();
placeholders.put("drop_view", "--");
placeholders.put("or_replace", "OR REPLACE");
PlaceholderReplacer placeholderReplacer = new PlaceholderReplacer(placeholders, "${", "}");
List<SqlStatement> sqlStatements = sqlScript.parse(placeholderReplacer.replacePlaceholders(source));
assertNotNull(sqlStatements);
assertEquals(1, sqlStatements.size());
SqlStatement sqlStatement = sqlStatements.get(0);
assertEquals(2, sqlStatement.getLineNumber());
assertEquals("CREATE OR REPLACE VIEW \"SOME_VIEW\"", sqlStatement.getSql());
}
use of org.flywaydb.core.internal.util.PlaceholderReplacer in project flyway by flyway.
the class CompositeMigrationResolverSmallTest method resolveMigrationsMultipleLocations.
@Test
public void resolveMigrationsMultipleLocations() {
FlywayConfigurationForTests config = FlywayConfigurationForTests.create();
PlaceholderReplacer placeholderReplacer = new PlaceholderReplacer(new HashMap<String, String>(), "${", "}");
MigrationResolver migrationResolver = new CompositeMigrationResolver(null, new Scanner(Thread.currentThread().getContextClassLoader()), config, new Locations("migration/subdir/dir2", "migration.outoforder", "migration/subdir/dir1"), placeholderReplacer, new MyCustomMigrationResolver());
Collection<ResolvedMigration> migrations = migrationResolver.resolveMigrations();
List<ResolvedMigration> migrationList = new ArrayList<ResolvedMigration>(migrations);
assertEquals(4, migrations.size());
assertEquals("First", migrationList.get(0).getDescription());
assertEquals("Late arrivals", migrationList.get(1).getDescription());
assertEquals("Virtual Migration", migrationList.get(2).getDescription());
assertEquals("Add foreign key", migrationList.get(3).getDescription());
}
Aggregations