Search in sources :

Example 16 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript in project flyway by flyway.

the class MetaDataTableImpl method update.

@Override
public void update(MigrationVersion version, String description, Integer checksum) {
    clearCache();
    LOG.info("Repairing metadata for version " + version + " (Description: " + description + ", Checksum: " + checksum + ")  ...");
    // Try load an update.sql file if it exists
    String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/update.sql";
    ClassPathResource resource = new ClassPathResource(resourceName, getClass().getClassLoader());
    if (resource.exists()) {
        String source = resource.loadAsString("UTF-8");
        Map<String, String> placeholders = new HashMap<String, String>();
        // Placeholders for column names
        placeholders.put("schema", table.getSchema().getName());
        placeholders.put("table", table.getName());
        // Placeholders for column values
        placeholders.put("version_val", version.toString());
        placeholders.put("description_val", description);
        placeholders.put("checksum_val", String.valueOf(checksum));
        String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);
        new SqlScript(sourceNoPlaceholders, dbSupport).execute(jdbcTemplate);
    } else {
        try {
            jdbcTemplate.update("UPDATE " + table + " SET " + dbSupport.quote("description") + "='" + description + "' , " + dbSupport.quote("checksum") + "=" + checksum + " WHERE " + dbSupport.quote("version") + "='" + version + "'");
        } catch (SQLException e) {
            throw new FlywaySqlException("Unable to repair metadata table " + table + " for version " + version, e);
        }
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) 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 17 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript 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 18 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript in project flyway by flyway.

the class SqlScriptFlywayCallback method execute.

private void execute(String key, Connection connection) {
    SqlScript sqlScript = scripts.get(key);
    if (sqlScript != null) {
        LOG.info("Executing SQL callback: " + key);
        sqlScript.execute(new JdbcTemplate(connection, 0));
    }
}
Also used : SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) JdbcTemplate(org.flywaydb.core.internal.dbsupport.JdbcTemplate)

Example 19 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript in project flyway by flyway.

the class MySQLSqlScriptSmallTest method multiLineCommentDirective.

@Test
public void multiLineCommentDirective() throws Exception {
    String source = "/*!50001 CREATE ALGORITHM=UNDEFINED */\n" + "/*!50013 DEFINER=`user`@`%` SQL SECURITY DEFINER */\n" + "/*!50001 VIEW `viewname` AS select `t`.`id` AS `someId`,`t`.`name` AS `someName` from `someTable` `t` where `t`.`state` = 0 */;\n";
    SqlScript sqlScript = new SqlScript(source, new MySQLDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(1, sqlStatements.size());
    assertEquals(1, sqlStatements.get(0).getLineNumber());
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) Test(org.junit.Test)

Example 20 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript in project flyway by flyway.

the class SQLServerMigrationTestCase method msDBToolsNotCleared.

@Test
public void msDBToolsNotCleared() throws Exception {
    Schema schema = dbSupport.getOriginalSchema();
    new SqlScript(new ClassPathResource("migration/dbsupport/sqlserver/createMSDBTools.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8"), dbSupport).execute(jdbcTemplate);
    try {
        final String queryObjectCount = "SELECT COUNT(*) from sys.all_objects";
        int initialObjectsCount = jdbcTemplate.queryForInt(queryObjectCount);
        schema.clean();
        int finalObjectCount = jdbcTemplate.queryForInt(queryObjectCount);
        assertEquals("Cleaning the schema must not delete MS DB Tools objects.", initialObjectsCount, finalObjectCount);
    } finally {
        try {
            new SqlScript(new ClassPathResource("migration/dbsupport/sqlserver/dropMSDBTools.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8"), dbSupport).execute(jdbcTemplate);
        } catch (Exception e) {
        // Swallow to prevent override of test raised exception.
        }
    }
}
Also used : Schema(org.flywaydb.core.internal.dbsupport.Schema) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) FlywaySqlScriptException(org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException) Test(org.junit.Test)

Aggregations

SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)26 Test (org.junit.Test)21 SqlStatement (org.flywaydb.core.internal.dbsupport.SqlStatement)19 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)17 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)4 FlywayException (org.flywaydb.core.api.FlywayException)3 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)2 Schema (org.flywaydb.core.internal.dbsupport.Schema)2 Callable (java.util.concurrent.Callable)1 MigrationVersion (org.flywaydb.core.api.MigrationVersion)1 JdbcTemplate (org.flywaydb.core.internal.dbsupport.JdbcTemplate)1 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)1