Search in sources :

Example 21 with ClassPathResource

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

Example 22 with ClassPathResource

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);
        }
    }
}
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 23 with ClassPathResource

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);
    }
}
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 24 with ClassPathResource

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");
}
Also used : ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)

Example 25 with ClassPathResource

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"));
}
Also used : Properties(java.util.Properties) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Aggregations

ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)31 Test (org.junit.Test)21 SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)17 SqlStatement (org.flywaydb.core.internal.dbsupport.SqlStatement)11 File (java.io.File)4 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)4 Resource (org.flywaydb.core.internal.util.scanner.Resource)4 ClassPathScanner (org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner)4 BeforeClass (org.junit.BeforeClass)4 FlywayException (org.flywaydb.core.api.FlywayException)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)2 Schema (org.flywaydb.core.internal.dbsupport.Schema)2 Location (org.flywaydb.core.internal.util.Location)2 AfterClass (org.junit.AfterClass)2 InputStreamReader (java.io.InputStreamReader)1