Search in sources :

Example 6 with PlaceholderReplacer

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);
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) MigrationVersion(org.flywaydb.core.api.MigrationVersion) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)

Example 7 with PlaceholderReplacer

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());
}
Also used : HashMap(java.util.HashMap) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) Test(org.junit.Test)

Example 8 with PlaceholderReplacer

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());
}
Also used : Scanner(org.flywaydb.core.internal.util.scanner.Scanner) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) Locations(org.flywaydb.core.internal.util.Locations) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) Test(org.junit.Test)

Aggregations

PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)8 HashMap (java.util.HashMap)6 SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)4 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)4 SQLException (java.sql.SQLException)3 FlywayException (org.flywaydb.core.api.FlywayException)2 MigrationVersion (org.flywaydb.core.api.MigrationVersion)2 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)2 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 Test (org.junit.Test)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 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1