Search in sources :

Example 51 with ChangeSet

use of liquibase.changelog.ChangeSet in project minijax by minijax.

the class LiquibaseHelperTest method testNotIgnoreAddColumn.

@Test
public void testNotIgnoreAddColumn() {
    final AddColumnChange change = new AddColumnChange();
    final ChangeSet changeSet = new ChangeSet(null);
    changeSet.addChange(change);
    assertFalse(LiquibaseHelper.isIgnoredChangeSet(changeSet));
}
Also used : AddColumnChange(liquibase.change.core.AddColumnChange) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 52 with ChangeSet

use of liquibase.changelog.ChangeSet in project openmrs-core by openmrs.

the class DatabaseUpdater method getDatabaseChanges.

/**
 * Looks at the current liquibase-update-to-latest
 * .xml file and then checks the database to see
 * if they have been run.
 *
 * @return list of changesets that both have and haven't been run
 */
@Authorized(PrivilegeConstants.GET_DATABASE_CHANGES)
public static List<OpenMRSChangeSet> getDatabaseChanges() throws Exception {
    Database database = null;
    try {
        Liquibase liquibase = getLiquibase(CHANGE_LOG_FILE, null);
        database = liquibase.getDatabase();
        DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse(CHANGE_LOG_FILE, new ChangeLogParameters(), liquibase.getFileOpener());
        List<ChangeSet> changeSets = changeLog.getChangeSets();
        List<OpenMRSChangeSet> results = new ArrayList<>();
        for (ChangeSet changeSet : changeSets) {
            OpenMRSChangeSet omrschangeset = new OpenMRSChangeSet(changeSet, database);
            results.add(omrschangeset);
        }
        return results;
    } finally {
        try {
            if (database != null) {
                database.getConnection().close();
            }
        } catch (Exception e) {
        // pass
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) Database(liquibase.database.Database) ArrayList(java.util.ArrayList) XMLChangeLogSAXParser(liquibase.parser.core.xml.XMLChangeLogSAXParser) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) LockException(liquibase.exception.LockException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LiquibaseException(liquibase.exception.LiquibaseException) Authorized(org.openmrs.annotation.Authorized)

Example 53 with ChangeSet

use of liquibase.changelog.ChangeSet 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 54 with ChangeSet

use of liquibase.changelog.ChangeSet in project openmrs-core by openmrs.

the class DatabaseUpdater method getUnrunDatabaseChanges.

/**
 * Looks at the specified liquibase change log files and returns all changesets in the files
 * that have not been run on the database yet. If no argument is specified, then it looks at the
 * current liquibase-update-to-latest.xml file
 *
 * @param changeLogFilenames the filenames of all files to search for unrun changesets
 * @return list of change sets
 */
@Authorized(PrivilegeConstants.GET_DATABASE_CHANGES)
public static List<OpenMRSChangeSet> getUnrunDatabaseChanges(String... changeLogFilenames) {
    log.debug("Getting unrun changesets");
    Database database = null;
    try {
        if (changeLogFilenames == null) {
            throw new IllegalArgumentException("changeLogFilenames cannot be null");
        }
        // if no argument, look ONLY in liquibase-update-to-latest.xml
        if (changeLogFilenames.length == 0) {
            changeLogFilenames = new String[] { CHANGE_LOG_FILE };
        }
        List<OpenMRSChangeSet> results = new ArrayList<>();
        for (String changelogFile : changeLogFilenames) {
            Liquibase liquibase = getLiquibase(changelogFile, null);
            database = liquibase.getDatabase();
            List<ChangeSet> changeSets = liquibase.listUnrunChangeSets(CONTEXT);
            for (ChangeSet changeSet : changeSets) {
                OpenMRSChangeSet omrschangeset = new OpenMRSChangeSet(changeSet, database);
                results.add(omrschangeset);
            }
        }
        return results;
    } catch (Exception e) {
        throw new RuntimeException("Error occurred while trying to get the updates needed for the database. " + e.getMessage(), e);
    } finally {
        try {
            database.getConnection().close();
        } catch (Exception e) {
        // pass
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) ArrayList(java.util.ArrayList) ChangeSet(liquibase.changelog.ChangeSet) LockException(liquibase.exception.LockException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LiquibaseException(liquibase.exception.LiquibaseException) Authorized(org.openmrs.annotation.Authorized)

Example 55 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class HubUpdater method populateOperationChange.

private void populateOperationChange(Contexts contexts, LabelExpression labelExpression, ChangeLogIterator changeLogIterator, ListVisitor listVisitor, OperationChange operationChange) throws LiquibaseException {
    if (changeLogIterator != null) {
        changeLogIterator.run(listVisitor, new RuntimeEnvironment(database, contexts, labelExpression));
        List<ChangeSet> operationChangeSets = listVisitor.getSeenChangeSets();
        for (ChangeSet operationChangeSet : operationChangeSets) {
            operationChange.getChangeSets().add(operationChangeSet);
        }
    }
}
Also used : ChangeSet(liquibase.changelog.ChangeSet)

Aggregations

ChangeSet (liquibase.changelog.ChangeSet)75 Test (org.junit.Test)41 RanChangeSet (liquibase.changelog.RanChangeSet)13 Contexts (liquibase.Contexts)12 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)11 Database (liquibase.database.Database)11 ArrayList (java.util.ArrayList)10 Liquibase (liquibase.Liquibase)10 Change (liquibase.change.Change)9 LiquibaseException (liquibase.exception.LiquibaseException)9 DiffOutputControl (liquibase.diff.output.DiffOutputControl)7 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)7 IOException (java.io.IOException)6 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)6 DiffResult (liquibase.diff.DiffResult)6 CompareControl (liquibase.diff.compare.CompareControl)6 LabelExpression (liquibase.LabelExpression)5 Sql (liquibase.sql.Sql)5 SqlStatement (liquibase.statement.SqlStatement)5 MarkChangeSetRanStatement (liquibase.statement.core.MarkChangeSetRanStatement)5