Search in sources :

Example 66 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class ConfigUtils method loadConfigurationFile.

/**
 * Loads the configuration from this configuration file.
 *
 * @param configFile The configuration file to load.
 * @param encoding The encoding of the configuration file.
 * @param failIfMissing Whether to fail if the file is missing.
 * @return The properties from the configuration file. An empty Map if none.
 * @throws FlywayException When the configuration file could not be loaded.
 */
public static Map<String, String> loadConfigurationFile(File configFile, String encoding, boolean failIfMissing) throws FlywayException {
    String errorMessage = "Unable to load config file: " + configFile.getAbsolutePath();
    if ("-".equals(configFile.getName())) {
        return loadConfigurationFromInputStream(System.in);
    } else if (!configFile.isFile() || !configFile.canRead()) {
        if (!failIfMissing) {
            LOG.debug(errorMessage);
            return new HashMap<>();
        }
        throw new FlywayException(errorMessage);
    }
    LOG.debug("Loading config file: " + configFile.getAbsolutePath());
    try {
        return loadConfigurationFromReader(new InputStreamReader(new FileInputStream(configFile), encoding));
    } catch (IOException | FlywayException e) {
        throw new FlywayException(errorMessage, e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException)

Example 67 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class DatabaseTypeRegister method getDatabaseTypeForConnection.

public static DatabaseType getDatabaseTypeForConnection(Connection connection) {
    DatabaseMetaData databaseMetaData = JdbcUtils.getDatabaseMetaData(connection);
    String databaseProductName = JdbcUtils.getDatabaseProductName(databaseMetaData);
    String databaseProductVersion = JdbcUtils.getDatabaseProductVersion(databaseMetaData);
    for (DatabaseType type : SORTED_DATABASE_TYPES) {
        if (type.handlesDatabaseProductNameAndVersion(databaseProductName, databaseProductVersion, connection)) {
            return type;
        }
    }
    throw new FlywayException("Unsupported Database: " + databaseProductName);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) BaseDatabaseType(org.flywaydb.core.internal.database.base.BaseDatabaseType) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 68 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class DatabaseTypeRegister method getDatabaseTypeForUrl.

public static DatabaseType getDatabaseTypeForUrl(String url) {
    List<DatabaseType> typesAcceptingUrl = getDatabaseTypesForUrl(url);
    if (typesAcceptingUrl.size() > 0) {
        if (typesAcceptingUrl.size() > 1) {
            StringBuilder builder = new StringBuilder();
            for (DatabaseType type : typesAcceptingUrl) {
                if (builder.length() > 0) {
                    builder.append(", ");
                }
                builder.append(type.getName());
            }
            LOG.debug("Multiple databases found that handle url '" + redactJdbcUrl(url) + "': " + builder);
        }
        return typesAcceptingUrl.get(0);
    } else {
        throw new FlywayException("No database found to handle " + redactJdbcUrl(url));
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) BaseDatabaseType(org.flywaydb.core.internal.database.base.BaseDatabaseType)

Example 69 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class DbRepair method repair.

/**
 * Repairs the schema history table.
 */
public RepairResult repair() {
    callbackExecutor.onEvent(Event.BEFORE_REPAIR);
    CompletedRepairActions repairActions;
    try {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        repairActions = ExecutionTemplateFactory.createExecutionTemplate(connection.getJdbcConnection(), database).execute(new Callable<CompletedRepairActions>() {

            public CompletedRepairActions call() {
                CompletedRepairActions completedActions = new CompletedRepairActions();
                completedActions.removedFailedMigrations = schemaHistory.removeFailedMigrations(repairResult, configuration.getCherryPick());
                migrationInfoService.refresh();
                completedActions.deletedMissingMigrations = deleteMissingMigrations();
                completedActions.alignedAppliedMigrationChecksums = alignAppliedMigrationsWithResolvedMigrations();
                return completedActions;
            }
        });
        stopWatch.stop();
        LOG.info("Successfully repaired schema history table " + schemaHistory + " (execution time " + TimeFormat.format(stopWatch.getTotalTimeMillis()) + ").");
        if (repairActions.deletedMissingMigrations) {
            LOG.info("Please ensure the previous contents of the deleted migrations are removed from the database, or moved into an existing migration.");
        }
        if (repairActions.removedFailedMigrations && !database.supportsDdlTransactions()) {
            LOG.info("Manual cleanup of the remaining effects of the failed migration may still be required.");
        }
    } catch (FlywayException e) {
        callbackExecutor.onEvent(Event.AFTER_REPAIR_ERROR);
        throw e;
    }
    callbackExecutor.onEvent(Event.AFTER_REPAIR);
    repairResult.setRepairActions(repairActions);
    return repairResult;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) Callable(java.util.concurrent.Callable) StopWatch(org.flywaydb.core.internal.util.StopWatch)

Example 70 with FlywayException

use of org.flywaydb.core.api.FlywayException in project flyway by flyway.

the class FileSystemScanner method scanForResources.

/**
 * Scans the FileSystem for resources under the specified location, starting with the specified prefix and ending with
 * the specified suffix.
 *
 * @param location The location in the filesystem to start searching. Subdirectories are also searched.
 * @return The resources that were found.
 */
public Collection<LoadableResource> scanForResources(Location location) {
    String path = location.getRootPath();
    LOG.debug("Scanning for filesystem resources at '" + path + "'");
    File dir = new File(path);
    if (!dir.exists()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + ".");
        }
        LOG.error("Skipping filesystem location:" + path + " (not found).");
        return Collections.emptyList();
    }
    if (!dir.canRead()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + " (not readable).");
        }
        LOG.error("Skipping filesystem location:" + path + " (not readable).");
        return Collections.emptyList();
    }
    if (!dir.isDirectory()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + " (not a directory).");
        }
        LOG.error("Skipping filesystem location:" + path + " (not a directory).");
        return Collections.emptyList();
    }
    Set<LoadableResource> resources = new TreeSet<>();
    for (String resourceName : findResourceNamesFromFileSystem(path, new File(path))) {
        boolean detectEncodingForThisResource = detectEncoding;
        if (location.matchesPath(resourceName)) {
            Charset encoding = defaultEncoding;
            String encodingBlurb = "";
            if (new File(resourceName + ".conf").exists()) {
                LoadableResource metadataResource = new FileSystemResource(location, resourceName + ".conf", defaultEncoding, false);
                SqlScriptMetadata metadata = SqlScriptMetadata.fromResource(metadataResource, null);
                if (metadata.encoding() != null) {
                    encoding = Charset.forName(metadata.encoding());
                    detectEncodingForThisResource = false;
                    encodingBlurb = " (with overriding encoding " + encoding + ")";
                }
            }
            resources.add(new FileSystemResource(location, resourceName, encoding, detectEncodingForThisResource, stream));
            LOG.debug("Found filesystem resource: " + resourceName + encodingBlurb);
        }
    }
    return resources;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) TreeSet(java.util.TreeSet) SqlScriptMetadata(org.flywaydb.core.internal.sqlscript.SqlScriptMetadata) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) Charset(java.nio.charset.Charset) FileSystemResource(org.flywaydb.core.internal.resource.filesystem.FileSystemResource) File(java.io.File)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)82 SQLException (java.sql.SQLException)22 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)14 PreparedStatement (java.sql.PreparedStatement)8 HashMap (java.util.HashMap)8 URL (java.net.URL)7 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)7 File (java.io.File)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)6 Test (org.junit.Test)6 InputStreamReader (java.io.InputStreamReader)5 Method (java.lang.reflect.Method)5 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 URLClassLoader (java.net.URLClassLoader)4