use of liquibase.changelog.ChangeLogParameters in project liquibase by liquibase.
the class ChangeLogPropertyDefinedPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
ChangeLogParameters changeLogParameters = changeLog.getChangeLogParameters();
if (changeLogParameters == null) {
throw new PreconditionFailedException("No Changelog properties were set", changeLog, this);
}
Object propertyValue = changeLogParameters.getValue(property, changeLog);
if (propertyValue == null) {
throw new PreconditionFailedException("Changelog property '" + property + "' was not set", changeLog, this);
}
if ((value != null) && !propertyValue.toString().equals(value)) {
throw new PreconditionFailedException("Expected changelog property '" + property + "' to have a value of '" + value + "'. Got '" + propertyValue + "'", changeLog, this);
}
}
use of liquibase.changelog.ChangeLogParameters 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());
}
use of liquibase.changelog.ChangeLogParameters 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
}
}
}
use of liquibase.changelog.ChangeLogParameters 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;
}
use of liquibase.changelog.ChangeLogParameters in project liquibase by liquibase.
the class DeactivateChangelogCommandStep method parseChangeLogFile.
private DatabaseChangeLog parseChangeLogFile(String changeLogFile) throws LiquibaseException {
ResourceAccessor resourceAccessor = Scope.getCurrentScope().getResourceAccessor();
ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser(changeLogFile, resourceAccessor);
ChangeLogParameters changeLogParameters = new ChangeLogParameters();
return parser.parse(changeLogFile, changeLogParameters, resourceAccessor);
}
Aggregations