Search in sources :

Example 61 with FlywayException

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

the class OracleSchema method doClean.

@Override
protected void doClean() throws SQLException {
    if (isSystem()) {
        throw new FlywayException("Clean not supported on Oracle for system schema " + database.quote(name) + "! " + "It must not be changed in any way except by running an Oracle-supplied script!");
    }
    // Disable FBA for schema tables.
    if (database.isFlashbackDataArchiveAvailable()) {
        disableFlashbackArchiveForFbaTrackedTables();
    }
    // Clean Oracle Locator metadata.
    if (database.isLocatorAvailable()) {
        cleanLocatorMetadata();
    }
    // Get existing object types in the schema.
    Set<String> objectTypeNames = getObjectTypeNames(jdbcTemplate, database, this);
    // Define the list of types to process, order is important.
    List<ObjectType> objectTypesToClean = Arrays.asList(// Types to drop.
    TRIGGER, QUEUE_TABLE, FILE_WATCHER, SCHEDULER_CHAIN, SCHEDULER_JOB, SCHEDULER_PROGRAM, SCHEDULE, RULE_SET, RULE, EVALUATION_CONTEXT, FILE_GROUP, XML_SCHEMA, MINING_MODEL, REWRITE_EQUIVALENCE, SQL_TRANSLATION_PROFILE, MATERIALIZED_VIEW, MATERIALIZED_VIEW_LOG, DIMENSION, VIEW, DOMAIN_INDEX, DOMAIN_INDEX_TYPE, TABLE, INDEX, CLUSTER, SEQUENCE, OPERATOR, FUNCTION, PROCEDURE, PACKAGE, CONTEXT, LIBRARY, TYPE, SYNONYM, JAVA_SOURCE, JAVA_CLASS, JAVA_RESOURCE, // Object types with sensitive information (passwords), skip intentionally, print warning if found.
    DATABASE_LINK, CREDENTIAL, // Unsupported types, print warning if found
    DATABASE_DESTINATION, SCHEDULER_GROUP, CUBE, CUBE_DIMENSION, CUBE_BUILD_PROCESS, MEASURE_FOLDER, // Undocumented types, print warning if found
    ASSEMBLY, JAVA_DATA);
    for (ObjectType objectType : objectTypesToClean) {
        if (objectTypeNames.contains(objectType.getName())) {
            LOG.debug("Cleaning objects of type " + objectType + " ...");
            objectType.dropObjects(jdbcTemplate, database, this);
        }
    }
    if (isDefaultSchemaForUser()) {
        jdbcTemplate.execute("PURGE RECYCLEBIN");
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ObjectType(org.flywaydb.core.internal.database.oracle.OracleSchema.ObjectType)

Example 62 with FlywayException

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

the class BooleanEvaluator method evaluateExpression.

/**
 * Evaluates a boolean expression.
 *
 * Currently only supports expressions that are 'true', 'false', 'A==B', 'A!=B'
 * and combinations of those using ( ) (precedence), && (AND), || (OR)
 *
 * @param expression The string containing the boolean expression.
 * @return The boolean value the expression evaluates to.
 */
public static boolean evaluateExpression(String expression) {
    while (expression.contains("(")) {
        String innermost = findInnermostBrackets(expression);
        expression = expression.replace("(" + innermost + ")", evaluateExpression(innermost) ? "true" : "false");
    }
    if (expression.trim().equalsIgnoreCase("true")) {
        return true;
    } else if (expression.trim().equalsIgnoreCase("false")) {
        return false;
    } else if (expression.contains("&&")) {
        String[] expressions = expression.split("&&");
        return andAll(expressions);
    } else if (expression.contains("||")) {
        String[] expressions = expression.split("\\|\\|");
        return orAll(expressions);
    } else if (expression.contains("!=")) {
        return !compareOperands("!=", expression);
    } else if (expression.contains("==")) {
        return compareOperands("==", expression);
    } else {
        throw new FlywayException("Unable to parse expression: " + expression);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException)

Example 63 with FlywayException

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

the class DbBaseline method baseline.

/**
 * Baselines the database.
 */
public BaselineResult baseline() {
    callbackExecutor.onEvent(Event.BEFORE_BASELINE);
    try {
        if (!schemaHistory.exists()) {
            schemaHistory.create(true);
            LOG.info("Successfully baselined schema with version: " + baselineVersion);
            baselineResult.successfullyBaselined = true;
            baselineResult.baselineVersion = baselineVersion.toString();
        } else {
            AppliedMigration baselineMarker = schemaHistory.getBaselineMarker();
            if (baselineMarker != null) {
                if (baselineVersion.equals(baselineMarker.getVersion()) && baselineDescription.equals(baselineMarker.getDescription())) {
                    LOG.info("Schema history table " + schemaHistory + " already initialized with (" + baselineVersion + "," + baselineDescription + "). Skipping.");
                    baselineResult.successfullyBaselined = true;
                    baselineResult.baselineVersion = baselineVersion.toString();
                } else {
                    throw new FlywayException("Unable to baseline schema history table " + schemaHistory + " with (" + baselineVersion + "," + baselineDescription + ") as it has already been baselined with (" + baselineMarker.getVersion() + "," + baselineMarker.getDescription() + ")\n" + "Need to reset your baseline? Learn more: " + FlywayDbWebsiteLinks.RESET_THE_BASELINE_MIGRATION);
                }
            } else {
                if (schemaHistory.hasSchemasMarker() && baselineVersion.equals(MigrationVersion.fromVersion("0"))) {
                    throw new FlywayException("Unable to baseline schema history table " + schemaHistory + " with version 0 as this version was used for schema creation");
                }
                if (schemaHistory.hasNonSyntheticAppliedMigrations()) {
                    throw new FlywayException("Unable to baseline schema history table " + schemaHistory + " as it already contains migrations\n" + "Need to reset your baseline? Learn more: " + FlywayDbWebsiteLinks.RESET_THE_BASELINE_MIGRATION);
                }
                if (schemaHistory.allAppliedMigrations().isEmpty()) {
                    throw new FlywayException("Unable to baseline schema history table " + schemaHistory + " as it already exists, and is empty.\n" + "Delete the schema history table with the clean command, and run baseline again.");
                }
                throw new FlywayException("Unable to baseline schema history table " + schemaHistory + " as it already contains migrations.\n" + "Delete the schema history table with the clean command, and run baseline again.\n" + "Need to reset your baseline? Learn more: " + FlywayDbWebsiteLinks.RESET_THE_BASELINE_MIGRATION);
            }
        }
    } catch (FlywayException e) {
        callbackExecutor.onEvent(Event.AFTER_BASELINE_ERROR);
        baselineResult.successfullyBaselined = false;
        throw e;
    }
    callbackExecutor.onEvent(Event.AFTER_BASELINE);
    return baselineResult;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) AppliedMigration(org.flywaydb.core.internal.schemahistory.AppliedMigration)

Example 64 with FlywayException

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

the class DbClean method clean.

public CleanResult clean() throws FlywayException {
    if (configuration.isCleanDisabled()) {
        throw new FlywayException("Unable to execute clean as it has been disabled with the 'flyway.cleanDisabled' property.");
    }
    callbackExecutor.onEvent(Event.BEFORE_CLEAN);
    CleanResult cleanResult = CommandResultFactory.createCleanResult(database.getCatalog());
    clean(cleanResult);
    callbackExecutor.onEvent(Event.AFTER_CLEAN);
    schemaHistory.clearCache();
    return cleanResult;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) CleanResult(org.flywaydb.core.api.output.CleanResult)

Example 65 with FlywayException

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

the class DbSchemas method create.

/**
 * Creates the schemas.
 *
 * @param baseline Whether to include the creation of a baseline marker.
 */
public void create(final boolean baseline) {
    callbackExecutor.onEvent(Event.CREATE_SCHEMA);
    int retries = 0;
    while (true) {
        try {
            ExecutionTemplateFactory.createExecutionTemplate(connection.getJdbcConnection(), database).execute(() -> {
                List<Schema> createdSchemas = new ArrayList<>();
                for (Schema schema : schemas) {
                    if (!schema.exists()) {
                        if (schema.getName() == null) {
                            throw new FlywayException("Unable to determine schema for the schema history table." + " Set a default schema for the connection or specify one using the defaultSchema property!");
                        }
                        LOG.debug("Creating schema: " + schema);
                        schema.create();
                        createdSchemas.add(schema);
                    } else {
                        LOG.debug("Skipping creation of existing schema: " + schema);
                    }
                }
                if (!createdSchemas.isEmpty()) {
                    schemaHistory.create(baseline);
                    schemaHistory.addSchemasMarker(createdSchemas.toArray(new Schema[0]));
                }
                return null;
            });
            return;
        } catch (RuntimeException e) {
            if (++retries >= 10) {
                throw e;
            }
            try {
                LOG.debug("Schema creation failed. Retrying in 1 sec ...");
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
            // Ignore
            }
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) Schema(org.flywaydb.core.internal.database.base.Schema) ArrayList(java.util.ArrayList)

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