use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.
the class MetaDataTableImpl method createIfNotExists.
/**
* Creates the metatable if it doesn't exist, upgrades it if it does.
*/
private void createIfNotExists() {
int retries = 0;
while (!table.exists()) {
if (retries == 0) {
LOG.info("Creating Metadata table: " + table);
}
try {
String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/createMetaDataTable.sql";
String source = new ClassPathResource(resourceName, getClass().getClassLoader()).loadAsString("UTF-8");
Map<String, String> placeholders = new HashMap<String, String>();
placeholders.put("schema", table.getSchema().getName());
placeholders.put("table", table.getName());
String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);
final SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
sqlScript.execute(jdbcTemplate);
LOG.debug("Metadata table " + table + " created.");
} catch (FlywayException e) {
if (++retries >= 10) {
throw e;
}
try {
LOG.debug("Metadata table creation failed. Retrying in 1 sec ...");
Thread.sleep(1000);
} catch (InterruptedException e1) {
// Ignore
}
}
}
}
use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource 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.util.scanner.classpath.ClassPathResource 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.scanner.classpath.ClassPathResource in project flyway by flyway.
the class VersionPrinter method printVersion.
/**
* Prints the Flyway version.
*/
public static void printVersion() {
if (printed) {
return;
}
printed = true;
String version = new ClassPathResource("org/flywaydb/core/internal/version.txt", VersionPrinter.class.getClassLoader()).loadAsString("UTF-8");
LOG.info("Flyway " + version + " by Boxfuse");
}
use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.
the class MainClassLoaderSmallTest method loadConfigurationFile.
@Test
public void loadConfigurationFile() throws Exception {
Properties properties = new Properties();
properties.put("existing", "still there!");
properties.put("override", "loses :-(");
String filename = new ClassPathResource("test.properties", getClassLoader()).getLocationOnDisk();
String[] args = new String[] { "-configFile=" + filename, "-configFileEncoding=UTF-8" };
Main.loadConfiguration(properties, args);
assertEquals(4, properties.size());
assertEquals("still there!", properties.getProperty("existing"));
assertEquals("räbbit 123", properties.getProperty("roger"));
assertEquals("wins :-)", properties.getProperty("override"));
}
Aggregations