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