Search in sources :

Example 16 with LockService

use of liquibase.lockservice.LockService in project liquibase by liquibase.

the class AbstractIntegrationTest method invalidIncludeDoesntBreakLiquibase.

@Test
public void invalidIncludeDoesntBreakLiquibase() throws Exception {
    if (database == null) {
        return;
    }
    Liquibase liquibase = createLiquibase(invalidReferenceChangeLog);
    try {
        liquibase.update(new Contexts());
        fail("Did not fail with invalid include");
    } catch (ChangeLogParseException ignored) {
    //expected
    }
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    assertFalse(lockService.hasChangeLogLock());
}
Also used : Liquibase(liquibase.Liquibase) LockService(liquibase.lockservice.LockService) ChangeLogParseException(liquibase.exception.ChangeLogParseException) Contexts(liquibase.Contexts) Test(org.junit.Test)

Example 17 with LockService

use of liquibase.lockservice.LockService in project liquibase by liquibase.

the class Liquibase method markNextChangeSetRan.

public void markNextChangeSetRan(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        DatabaseChangeLog changeLog = getDatabaseChangeLog();
        ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
        checkLiquibaseTables(false, changeLog, contexts, labelExpression);
        changeLog.validate(database, contexts, labelExpression);
        ChangeLogIterator logIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new CountChangeSetFilter(1));
        logIterator.run(new ChangeLogSyncVisitor(database), new RuntimeEnvironment(database, contexts, labelExpression));
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
        resetServices();
    }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException)

Example 18 with LockService

use of liquibase.lockservice.LockService in project liquibase by liquibase.

the class Liquibase method update.

public void update(Contexts contexts, LabelExpression labelExpression, Writer output, boolean checkLiquibaseTables) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), output, database);
    ExecutorService.getInstance().setExecutor(database, loggingExecutor);
    outputHeader("Update Database Script");
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        update(contexts, labelExpression, checkLiquibaseTables);
        output.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
    ExecutorService.getInstance().setExecutor(database, oldTemplate);
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LockService(liquibase.lockservice.LockService) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 19 with LockService

use of liquibase.lockservice.LockService 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)

Example 20 with LockService

use of liquibase.lockservice.LockService in project liquibase by liquibase.

the class Liquibase method tagExists.

public boolean tagExists(String tagString) throws LiquibaseException {
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
        return getDatabase().doesTagExist(tagString);
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
        }
    }
}
Also used : LockService(liquibase.lockservice.LockService)

Aggregations

LockService (liquibase.lockservice.LockService)27 IOException (java.io.IOException)10 CommandScope (liquibase.command.CommandScope)9 DatabaseConnection (liquibase.database.DatabaseConnection)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 BufferedLogService (liquibase.logging.core.BufferedLogService)8 InvalidExampleException (liquibase.snapshot.InvalidExampleException)8 LockException (liquibase.exception.LockException)7 HubChangeExecListener (liquibase.hub.listener.HubChangeExecListener)7 Connection (liquibase.hub.model.Connection)7 Operation (liquibase.hub.model.Operation)7 CompositeLogService (liquibase.logging.core.CompositeLogService)7 LiquibaseException (liquibase.exception.LiquibaseException)6 Database (liquibase.database.Database)4 ExecutorService (liquibase.executor.ExecutorService)4 InputStreamList (liquibase.resource.InputStreamList)4 Liquibase (liquibase.Liquibase)3 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)3 JdbcConnection (liquibase.database.jvm.JdbcConnection)3 ChangeLogParseException (liquibase.exception.ChangeLogParseException)3