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);
}
}
}
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);
}
}
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));
}
}
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());
}
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.
}
}
}
Aggregations