Search in sources :

Example 1 with ShouldRunChangeSetFilter

use of liquibase.changelog.filter.ShouldRunChangeSetFilter in project openmrs-core by openmrs.

the class DatabaseUpdater method executeChangelog.

/**
 * This code was borrowed from the liquibase jar so that we can call the given callback
 * function.
 *
 * @param changeLogFile the file to execute
 * @param contexts the liquibase changeset context
 * @param userInput answers given by the user
 * @param callback the function to call after every changeset
 * @param cl {@link ClassLoader} to use to find the changeLogFile (or null to use
 *            {@link OpenmrsClassLoader})
 * @return A list of messages or warnings generated by the executed changesets
 * @throws Exception
 */
public static List<String> executeChangelog(String changeLogFile, String contexts, Map<String, Object> userInput, ChangeSetExecutorCallback callback, ClassLoader cl) throws Exception {
    final class OpenmrsUpdateVisitor extends UpdateVisitor {

        private ChangeSetExecutorCallback callback;

        private int numChangeSetsToRun;

        public OpenmrsUpdateVisitor(Database database, ChangeSetExecutorCallback callback, int numChangeSetsToRun) {
            super(database);
            this.callback = callback;
            this.numChangeSetsToRun = numChangeSetsToRun;
        }

        @Override
        public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database) throws LiquibaseException {
            if (callback != null) {
                callback.executing(changeSet, numChangeSetsToRun);
            }
            super.visit(changeSet, databaseChangeLog, database);
        }
    }
    if (cl == null) {
        cl = OpenmrsClassLoader.getInstance();
    }
    log.debug("Setting up liquibase object to run changelog: " + changeLogFile);
    Liquibase liquibase = getLiquibase(changeLogFile, cl);
    int numChangeSetsToRun = liquibase.listUnrunChangeSets(contexts).size();
    Database database = null;
    LockService lockHandler = null;
    try {
        database = liquibase.getDatabase();
        lockHandler = LockService.getInstance(database);
        lockHandler.waitForLock();
        ResourceAccessor openmrsFO = new ClassLoaderFileOpener(cl);
        ResourceAccessor fsFO = new FileSystemResourceAccessor();
        DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse(changeLogFile, new ChangeLogParameters(), new CompositeResourceAccessor(openmrsFO, fsFO));
        changeLog.setChangeLogParameters(liquibase.getChangeLogParameters());
        changeLog.validate(database);
        ChangeLogIterator logIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new DbmsChangeSetFilter(database));
        database.checkDatabaseChangeLogTable(true, changeLog, new String[] { contexts });
        logIterator.run(new OpenmrsUpdateVisitor(database, callback, numChangeSetsToRun), database);
    } catch (LiquibaseException e) {
        throw e;
    } finally {
        try {
            lockHandler.releaseLock();
        } catch (Exception e) {
            log.error("Could not release lock", e);
        }
        try {
            database.getConnection().close();
        } catch (Exception e) {
        // pass
        }
    }
    return updateWarnings;
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) ShouldRunChangeSetFilter(liquibase.changelog.filter.ShouldRunChangeSetFilter) UpdateVisitor(liquibase.changelog.visitor.UpdateVisitor) LockService(liquibase.lockservice.LockService) ContextChangeSetFilter(liquibase.changelog.filter.ContextChangeSetFilter) XMLChangeLogSAXParser(liquibase.parser.core.xml.XMLChangeLogSAXParser) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) LockException(liquibase.exception.LockException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LiquibaseException(liquibase.exception.LiquibaseException) Liquibase(liquibase.Liquibase) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ChangeLogIterator(liquibase.changelog.ChangeLogIterator) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) Database(liquibase.database.Database) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) LiquibaseException(liquibase.exception.LiquibaseException) DbmsChangeSetFilter(liquibase.changelog.filter.DbmsChangeSetFilter) ChangeSet(liquibase.changelog.ChangeSet)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Liquibase (liquibase.Liquibase)1 ChangeLogIterator (liquibase.changelog.ChangeLogIterator)1 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)1 ChangeSet (liquibase.changelog.ChangeSet)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 ContextChangeSetFilter (liquibase.changelog.filter.ContextChangeSetFilter)1 DbmsChangeSetFilter (liquibase.changelog.filter.DbmsChangeSetFilter)1 ShouldRunChangeSetFilter (liquibase.changelog.filter.ShouldRunChangeSetFilter)1 UpdateVisitor (liquibase.changelog.visitor.UpdateVisitor)1 Database (liquibase.database.Database)1 LiquibaseException (liquibase.exception.LiquibaseException)1 LockException (liquibase.exception.LockException)1 LockService (liquibase.lockservice.LockService)1 XMLChangeLogSAXParser (liquibase.parser.core.xml.XMLChangeLogSAXParser)1 CompositeResourceAccessor (liquibase.resource.CompositeResourceAccessor)1 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)1 ResourceAccessor (liquibase.resource.ResourceAccessor)1