Search in sources :

Example 16 with DatabaseChangeLog

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

the class ValidatingVisitorTest method visit_successful.

@Test
public void visit_successful() throws Exception {
    CreateTableChange change1 = new CreateTableChange();
    change1.setTableName("table1");
    ColumnConfig column1 = new ColumnConfig();
    change1.addColumn(column1);
    column1.setName("col1");
    column1.setType("int");
    CreateTableChange change2 = new CreateTableChange();
    change2.setTableName("table2");
    ColumnConfig column2 = new ColumnConfig();
    change2.addColumn(column2);
    column2.setName("col2");
    column2.setType("int");
    changeSet1.addChange(change1);
    changeSet2.addChange(change2);
    ValidatingVisitor handler = new ValidatingVisitor(new ArrayList<RanChangeSet>());
    handler.visit(changeSet1, new DatabaseChangeLog(), new MockDatabase(), null);
    handler.visit(changeSet2, new DatabaseChangeLog(), new MockDatabase(), null);
    assertTrue(handler.validationPassed());
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) CreateTableChange(liquibase.change.core.CreateTableChange) MockDatabase(liquibase.database.core.MockDatabase) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) RanChangeSet(liquibase.changelog.RanChangeSet) Test(org.junit.Test)

Example 17 with DatabaseChangeLog

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

the class XMLChangeLogSAXParserTest method testIgnoreDuplicateChangeSets.

@Test
public void testIgnoreDuplicateChangeSets() throws ChangeLogParseException, Exception {
    XMLChangeLogSAXParser xmlParser = new XMLChangeLogSAXParser();
    DatabaseChangeLog changeLog = xmlParser.parse("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/master.changelog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
    final List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    new ChangeLogIterator(changeLog).run(new ChangeSetVisitor() {

        @Override
        public Direction getDirection() {
            return Direction.FORWARD;
        }

        @Override
        public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
            changeSets.add(changeSet);
        }
    }, new RuntimeEnvironment(new MockDatabase(), new Contexts(), new LabelExpression()));
    Assert.assertEquals(8, changeSets.size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(0).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(1).toString());
    Assert.assertEquals(1, changeSets.get(1).getContexts().getContexts().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(2).toString());
    Assert.assertEquals(1, changeSets.get(2).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(3).toString());
    Assert.assertEquals(2, changeSets.get(3).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(4).toString());
    Assert.assertEquals(1, changeSets.get(4).getDbmsSet().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog1.xml::1::testuser", changeSets.get(5).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog3.xml::1::testuser", changeSets.get(6).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog2.xml::1::testuser", changeSets.get(7).toString());
}
Also used : ChangeSetVisitor(liquibase.changelog.visitor.ChangeSetVisitor) RuntimeEnvironment(liquibase.RuntimeEnvironment) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) ArrayList(java.util.ArrayList) Contexts(liquibase.Contexts) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogIterator(liquibase.changelog.ChangeLogIterator) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) MockDatabase(liquibase.sdk.database.MockDatabase) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) MockDatabase(liquibase.sdk.database.MockDatabase) LiquibaseException(liquibase.exception.LiquibaseException) ChangeSetFilterResult(liquibase.changelog.filter.ChangeSetFilterResult) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 18 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog 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 19 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog 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 DatabaseChangeLog

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

the class YamlChangeLogParser method parse.

@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
    Yaml yaml = new Yaml(new SafeConstructor());
    try (InputStream changeLogStream = resourceAccessor.openStream(null, physicalChangeLogLocation)) {
        if (changeLogStream == null) {
            throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist");
        }
        Map parsedYaml = parseYamlStream(physicalChangeLogLocation, yaml, changeLogStream);
        if ((parsedYaml == null) || parsedYaml.isEmpty()) {
            throw new ChangeLogParseException("Empty file " + physicalChangeLogLocation);
        }
        DatabaseChangeLog changeLog = new DatabaseChangeLog(physicalChangeLogLocation);
        Object rootList = parsedYaml.get("databaseChangeLog");
        if (rootList == null) {
            throw new ChangeLogParseException("Could not find databaseChangeLog node");
        }
        if (!(rootList instanceof List)) {
            throw new ChangeLogParseException("databaseChangeLog does not contain a list of entries. Each changeSet must begin ' - changeSet:'");
        }
        for (Object obj : (List) rootList) {
            if (obj instanceof Map) {
                if (((Map) obj).containsKey("property")) {
                    Map property = (Map) ((Map) obj).get("property");
                    ContextExpression context = new ContextExpression((String) property.get("context"));
                    Labels labels = new Labels((String) property.get("labels"));
                    Boolean global = getGlobalParam(property);
                    if (property.containsKey("name")) {
                        Object value = property.get("value");
                        if (value != null) {
                            // TODO: not nice...
                            value = value.toString();
                        }
                        changeLogParameters.set((String) property.get("name"), (String) value, context, labels, (String) property.get("dbms"), global, changeLog);
                    } else if (property.containsKey("file")) {
                        loadChangeLogParametersFromFile(changeLogParameters, resourceAccessor, changeLog, property, context, labels, global);
                    }
                }
                if (((Map) obj).containsKey("changeLogId")) {
                    String changeLogId = (String) ((Map) obj).get("changeLogId");
                    changeLog.setChangeLogId(changeLogId);
                }
            }
        }
        replaceParameters(parsedYaml, changeLogParameters, changeLog);
        changeLog.setChangeLogParameters(changeLogParameters);
        ParsedNode databaseChangeLogNode = new ParsedNode(null, "databaseChangeLog");
        databaseChangeLogNode.setValue(rootList);
        changeLog.load(databaseChangeLogNode, resourceAccessor);
        return changeLog;
    } catch (ChangeLogParseException e) {
        throw e;
    } catch (Exception e) {
        throw new ChangeLogParseException("Error parsing " + physicalChangeLogLocation, e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) InputStream(java.io.InputStream) ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) Yaml(org.yaml.snakeyaml.Yaml) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) IOException(java.io.IOException) ChangeLogParseException(liquibase.exception.ChangeLogParseException) LiquibaseException(liquibase.exception.LiquibaseException) ChangeLogParseException(liquibase.exception.ChangeLogParseException) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor)

Aggregations

DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)23 ChangeSet (liquibase.changelog.ChangeSet)11 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)6 RanChangeSet (liquibase.changelog.RanChangeSet)6 LiquibaseException (liquibase.exception.LiquibaseException)6 IOException (java.io.IOException)5 CreateTableChange (liquibase.change.core.CreateTableChange)5 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)5 Database (liquibase.database.Database)5 MockDatabase (liquibase.database.core.MockDatabase)5 ChangeLogParseException (liquibase.exception.ChangeLogParseException)4 ContextExpression (liquibase.ContextExpression)3 Labels (liquibase.Labels)3 Liquibase (liquibase.Liquibase)3 ColumnConfig (liquibase.change.ColumnConfig)3 CommandExecutionException (liquibase.exception.CommandExecutionException)3 CompositeResourceAccessor (liquibase.resource.CompositeResourceAccessor)3 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)3 ResourceAccessor (liquibase.resource.ResourceAccessor)3