Search in sources :

Example 1 with Patch

use of org.alfresco.repo.admin.patch.Patch in project alfresco-repository by Alfresco.

the class SchemaBootstrap method checkSchemaPatchScripts.

/**
 * Check that the necessary scripts have been executed against the database
 */
private void checkSchemaPatchScripts(Connection connection, List<SchemaUpgradeScriptPatch> scriptPatches, boolean apply) throws Exception {
    // first check if there have been any applied patches
    int appliedPatchCount = countAppliedPatches(connection);
    if (appliedPatchCount == 0) {
        // and patches will not have been applied yet
        return;
    }
    ensureCurrentClusterMemberIsBootstrapping(connection);
    // Retrieve the first installed schema number
    int installedSchema = getInstalledSchemaNumber(connection);
    nextPatch: for (SchemaUpgradeScriptPatch patch : scriptPatches) {
        final String patchId = patch.getId();
        final String scriptUrl = patch.getScriptUrl();
        // Check if any of the alternative patches were executed
        List<Patch> alternatives = patch.getAlternatives();
        for (Patch alternativePatch : alternatives) {
            String alternativePatchId = alternativePatch.getId();
            boolean alternativeSucceeded = didPatchSucceed(connection, alternativePatchId, true);
            if (alternativeSucceeded) {
                continue nextPatch;
            }
        }
        // check if the script was successfully executed
        boolean wasSuccessfullyApplied = didPatchSucceed(connection, patchId, false);
        if (wasSuccessfullyApplied) {
            // with the patch bean present.
            continue;
        } else if (!patch.applies(installedSchema)) {
            // Patch does not apply to the installed schema number
            continue;
        } else if (!apply) {
            // the script was not run and may not be run automatically
            throw AlfrescoRuntimeException.create(ERR_SCRIPT_NOT_RUN, scriptUrl);
        }
        // it wasn't run and it can be run now
        executeScriptUrl(connection, scriptUrl);
    }
}
Also used : SchemaUpgradeScriptPatch(org.alfresco.repo.admin.patch.impl.SchemaUpgradeScriptPatch) List(java.util.List) ArrayList(java.util.ArrayList) SchemaUpgradeScriptPatch(org.alfresco.repo.admin.patch.impl.SchemaUpgradeScriptPatch) AppliedPatch(org.alfresco.repo.admin.patch.AppliedPatch) Patch(org.alfresco.repo.admin.patch.Patch) Savepoint(java.sql.Savepoint)

Example 2 with Patch

use of org.alfresco.repo.admin.patch.Patch in project alfresco-repository by Alfresco.

the class SchemaBootstrap method updateSchema.

/**
 * Builds the schema from scratch or applies the necessary patches to the schema.
 */
private boolean updateSchema(Connection connection) throws Exception {
    boolean create = false;
    try {
        final int numberOfPatchesApplied = countAppliedPatches(connection);
        if (logger.isInfoEnabled()) {
            logger.info("Applied patches detected: " + numberOfPatchesApplied);
        }
    } catch (NoSchemaException e) {
        create = true;
    }
    // Get the dialect
    final Dialect dialect = this.dialect;
    String dialectStr = dialect.getClass().getSimpleName();
    if (create) {
        long start = System.currentTimeMillis();
        // execute pre-create scripts (not patches)
        for (String scriptUrl : this.preCreateScriptUrls) {
            executeScriptUrl(connection, scriptUrl);
        }
        // execute post-create scripts (not patches)
        for (String scriptUrl : this.postCreateScriptUrls) {
            executeScriptUrl(connection, scriptUrl);
        }
        if (logger.isInfoEnabled()) {
            logger.info("Creating Alfresco tables took " + (System.currentTimeMillis() - start) + " ms");
        }
    } else {
        long start = System.currentTimeMillis();
        // Execute any pre-auto-update scripts
        checkSchemaPatchScripts(connection, preUpdateScriptPatches, true);
        // Execute any post-auto-update scripts
        checkSchemaPatchScripts(connection, postUpdateScriptPatches, true);
        if (logger.isInfoEnabled()) {
            logger.info("Checking and patching Alfresco tables took " + (System.currentTimeMillis() - start) + " ms");
        }
    }
    ensureCurrentClusterMemberIsBootstrapping(connection);
    // Initialise Activiti DB, using an unclosable connection
    boolean activitiTablesExist = checkActivitiTablesExist(connection);
    if (logger.isInfoEnabled()) {
        logger.info("Activiti tables need to be " + (activitiTablesExist ? "checked for patches" : " created"));
    }
    if (!activitiTablesExist) {
        long start = System.currentTimeMillis();
        ensureCurrentClusterMemberIsBootstrapping(connection);
        // Activiti DB updates are performed as patches in alfresco, only give
        // control to activiti when creating new one.
        initialiseActivitiDBSchema(new UnclosableConnection(connection));
        // ALF-18996: Upgrade from 3.4.12 to 4.2.0 fails: Activiti tables have not been bootstrapped
        // The Activiti bootstrap is effectively doing the work of all the other patches,
        // which should be considered complete.
        int installedSchemaNumber = getInstalledSchemaNumber(connection);
        for (Patch activitiScriptPatch : updateActivitiScriptPatches) {
            AppliedPatch appliedPatch = new AppliedPatch();
            appliedPatch.setId(activitiScriptPatch.getId());
            appliedPatch.setDescription(activitiScriptPatch.getDescription());
            appliedPatch.setFixesFromSchema(activitiScriptPatch.getFixesFromSchema());
            appliedPatch.setFixesToSchema(activitiScriptPatch.getFixesToSchema());
            appliedPatch.setTargetSchema(activitiScriptPatch.getTargetSchema());
            appliedPatch.setAppliedToSchema(installedSchemaNumber);
            appliedPatch.setAppliedToServer("UNKNOWN");
            // the date applied
            appliedPatch.setAppliedOnDate(new Date());
            appliedPatch.setSucceeded(true);
            appliedPatch.setWasExecuted(false);
            appliedPatch.setReport("Placeholder for Activiti bootstrap at schema " + installedSchemaNumber);
            appliedPatchDAO.createAppliedPatch(appliedPatch);
        }
        if (logger.isInfoEnabled()) {
            logger.info("Creating Activiti tables took " + (System.currentTimeMillis() - start) + " ms");
        }
    } else {
        long start = System.currentTimeMillis();
        // Execute any auto-update scripts for Activiti tables
        checkSchemaPatchScripts(connection, updateActivitiScriptPatches, true);
        // verify that all Activiti patches have been applied correctly
        checkSchemaPatchScripts(connection, updateActivitiScriptPatches, false);
        if (logger.isInfoEnabled()) {
            logger.info("Checking and patching Activiti tables took " + (System.currentTimeMillis() - start) + " ms");
        }
    }
    if (!create) {
        long start = System.currentTimeMillis();
        // verify that all patches have been applied correctly
        // check scripts
        checkSchemaPatchScripts(connection, preUpdateScriptPatches, false);
        // check scripts
        checkSchemaPatchScripts(connection, postUpdateScriptPatches, false);
        if (logger.isInfoEnabled()) {
            logger.info("Checking that all patches have been applied took " + (System.currentTimeMillis() - start) + " ms");
        }
    }
    return create;
}
Also used : MySQLInnoDBDialect(org.alfresco.repo.domain.dialect.MySQLInnoDBDialect) Oracle9Dialect(org.alfresco.repo.domain.dialect.Oracle9Dialect) SQLServerDialect(org.alfresco.repo.domain.dialect.SQLServerDialect) PostgreSQLDialect(org.alfresco.repo.domain.dialect.PostgreSQLDialect) MySQLClusterNDBDialect(org.alfresco.repo.domain.dialect.MySQLClusterNDBDialect) Dialect(org.alfresco.repo.domain.dialect.Dialect) SchemaUpgradeScriptPatch(org.alfresco.repo.admin.patch.impl.SchemaUpgradeScriptPatch) AppliedPatch(org.alfresco.repo.admin.patch.AppliedPatch) Patch(org.alfresco.repo.admin.patch.Patch) Savepoint(java.sql.Savepoint) Date(java.util.Date) AppliedPatch(org.alfresco.repo.admin.patch.AppliedPatch)

Aggregations

Savepoint (java.sql.Savepoint)2 AppliedPatch (org.alfresco.repo.admin.patch.AppliedPatch)2 Patch (org.alfresco.repo.admin.patch.Patch)2 SchemaUpgradeScriptPatch (org.alfresco.repo.admin.patch.impl.SchemaUpgradeScriptPatch)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Dialect (org.alfresco.repo.domain.dialect.Dialect)1 MySQLClusterNDBDialect (org.alfresco.repo.domain.dialect.MySQLClusterNDBDialect)1 MySQLInnoDBDialect (org.alfresco.repo.domain.dialect.MySQLInnoDBDialect)1 Oracle9Dialect (org.alfresco.repo.domain.dialect.Oracle9Dialect)1 PostgreSQLDialect (org.alfresco.repo.domain.dialect.PostgreSQLDialect)1 SQLServerDialect (org.alfresco.repo.domain.dialect.SQLServerDialect)1