use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class Flyway method doValidate.
/**
* Performs the actual validation. All set up must have taken place beforehand.
*
* @param connectionMetaDataTable The database connection for the metadata table.
* @param dbSupport The database-specific support.
* @param migrationResolver The migration resolver;
* @param metaDataTable The metadata table.
* @param schemas The schemas managed by Flyway.
* @param pending Whether pending migrations are ok.
*/
private void doValidate(Connection connectionMetaDataTable, DbSupport dbSupport, MigrationResolver migrationResolver, MetaDataTable metaDataTable, Schema[] schemas, FlywayCallback[] flywayCallbacks, boolean pending) {
String validationError = new DbValidate(connectionMetaDataTable, dbSupport, metaDataTable, schemas[0], migrationResolver, target, outOfOrder, pending, ignoreMissingMigrations, ignoreFutureMigrations, flywayCallbacks).validate();
if (validationError != null) {
if (cleanOnValidationError) {
new DbClean(connectionMetaDataTable, dbSupport, metaDataTable, schemas, flywayCallbacks, cleanDisabled).clean();
metaDataTable.clearCache();
} else {
throw new FlywayException("Validate failed: " + validationError);
}
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class AbstractFlywayMojo method getConfigFileProperties.
/**
* Retrieve the properties from the config file (if specified).
*/
private Properties getConfigFileProperties() throws IOException {
Properties properties = new Properties();
String configFileProp = System.getProperty("flyway.configFile");
if (configFileProp != null) {
configFile = new File(configFileProp);
if (!configFile.isAbsolute()) {
configFile = new File(mavenProject.getBasedir(), configFileProp);
}
}
if (configFile == null) {
File file = new File(mavenProject.getBasedir(), "flyway.properties");
if (file.isFile() && file.canRead()) {
configFile = file;
} else {
log.debug("flyway.properties not found. Skipping.");
return properties;
}
} else if (!configFile.canRead() || !configFile.isFile()) {
throw new FlywayException("Unable to read config file: " + configFile.getAbsolutePath());
}
properties.load(new InputStreamReader(new FileInputStream(configFile), encoding));
return properties;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class DriverDataSource method getConnectionFromDriver.
/**
* Build properties for the Driver, including the given user and password (if any),
* and obtain a corresponding Connection.
*
* @param username the name of the user
* @param password the password to use
* @return the obtained Connection
* @throws SQLException in case of failure
* @see java.sql.Driver#connect(String, java.util.Properties)
*/
protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
Properties properties = new Properties(this.defaultProperties);
if (username != null) {
properties.setProperty("user", username);
}
if (password != null) {
properties.setProperty("password", password);
}
properties.putAll(additionalProperties);
Connection connection = driver.connect(url, properties);
if (connection == null) {
throw new FlywayException("Unable to connect to " + DatabaseTypeRegister.redactJdbcUrl(url));
}
connection.setAutoCommit(autoCommit);
return connection;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class PostgreSQLCopyParsedStatement method execute.
@Override
public Results execute(JdbcTemplate jdbcTemplate) {
// #2355: Use reflection to ensure this works in cases where the PostgreSQL driver classes were loaded in a
// child URLClassLoader instead of the system classloader.
Object baseConnection;
Object copyManager;
Method copyManagerCopyInMethod;
try {
Connection connection = jdbcTemplate.getConnection();
ClassLoader classLoader = connection.getClass().getClassLoader();
Class<?> baseConnectionClass = classLoader.loadClass("org.postgresql.core.BaseConnection");
baseConnection = connection.unwrap(baseConnectionClass);
Class<?> copyManagerClass = classLoader.loadClass("org.postgresql.copy.CopyManager");
Constructor<?> copyManagerConstructor = copyManagerClass.getConstructor(baseConnectionClass);
copyManagerCopyInMethod = copyManagerClass.getMethod("copyIn", String.class, Reader.class);
copyManager = copyManagerConstructor.newInstance(baseConnection);
} catch (Exception e) {
throw new FlywayException("Unable to find PostgreSQL CopyManager class", e);
}
Results results = new Results();
try {
try {
Long updateCount = (Long) copyManagerCopyInMethod.invoke(copyManager, getSql(), new StringReader(copyData));
results.addResult(new Result(updateCount, null, null, getSql()));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new SQLException("Unable to execute COPY operation", e);
}
} catch (SQLException e) {
jdbcTemplate.extractErrors(results, e);
}
return results;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class TransactionalExecutionTemplate method execute.
/**
* Executes this callback within a transaction.
*
* @param callback The callback to execute.
* @return The result of the transaction code.
*/
@Override
public <T> T execute(Callable<T> callback) {
boolean oldAutocommit = true;
try {
oldAutocommit = connection.getAutoCommit();
connection.setAutoCommit(false);
T result = callback.call();
connection.commit();
return result;
} catch (Exception e) {
RuntimeException rethrow;
if (e instanceof SQLException) {
rethrow = new FlywaySqlException("Unable to commit transaction", (SQLException) e);
} else if (e instanceof RuntimeException) {
rethrow = (RuntimeException) e;
} else {
rethrow = new FlywayException(e);
}
if (rollbackOnException) {
try {
LOG.debug("Rolling back transaction...");
connection.rollback();
LOG.debug("Transaction rolled back");
} catch (SQLException se) {
LOG.error("Unable to rollback transaction", se);
}
} else {
try {
connection.commit();
} catch (SQLException se) {
LOG.error("Unable to commit transaction", se);
}
}
throw rethrow;
} finally {
try {
connection.setAutoCommit(oldAutocommit);
} catch (SQLException e) {
LOG.error("Unable to restore autocommit to original value for connection", e);
}
}
}
Aggregations