// TODO: make patches 1-201 i.e db.changelog-1-201.xml skip for customers existing before introduction of liquibase
public void runLiquibaseUpdate(ISession iSession) {
iSession.getHibernateSession().doWork(connection -> {
try (var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection))) {
String masterChangelogPath = determineChangelogFilePath(iSession.getDatabaseName());
boolean isNewCustomer = isCustomerNewSinceLiquibaseIntroduction(iSession.getHibernateSession());
String firstChangelogContext = isNewCustomer ? "include-first-changelog" : null;
log.info("Running Liquibase update with context: " + (firstChangelogContext != null ? firstChangelogContext : "no context"));
Map<String, Object> scopeObjects = Map.of(
Scope.Attr.database.name(), database,
Scope.Attr.resourceAccessor.name(), new ClassLoaderResourceAccessor(getClass().getClassLoader())
);
Scope.child(scopeObjects, (Scope.ScopedRunner<?>) () -> {
CommandScope commandScope = new CommandScope("update");
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, database);
commandScope.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, masterChangelogPath);
commandScope.addArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_PARAMETERS, new ChangeLogParameters(database));
if (firstChangelogContext != null) {
commandScope.addArgumentValue(DatabaseChangelogCommandStep.CONTEXTS_ARG, firstChangelogContext);
}
commandScope.execute();
});
} catch (LiquibaseException e) {
log.error("Error running Liquibase update: " + e.getMessage(), e);
} catch (Exception e) {
log.error("Unexpected error during Liquibase update: " + e.getMessage(), e);
}
});
}
db.changelog-master.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<!-- ChangeSets before Liquibase was introduced -->
<include file="liquibase/patch/sql/mysql/db.changelog-1-201.xml" context="include-first-changelog"/>
<!-- ChangeSets after Liquibase was introduced in Release/8.2.0 -->
<include file="liquibase/patch/sql/mysql/db.changelog-202-onwards.xml"/>
</databaseChangeLog>
I want below
for new customers → both db.changelog-1-201.xml, db.changelog-202-onwards.xml should be executed
for existing customers → only db.changelog-202-onwards.xml should be executed
Everything is fine till commandScope.addArgumentValue(UpdateCommandStep.CONTEXTS_ARG, context);
. Key actually gets added
but somehow it is not behaving as we expect it
tried with labels too instead of contexts - still not resolved
commandScope.addArgumentValue(DatabaseChangelogCommandStep.LABEL_FILTER_ARG, firstChangelogLabel);
<include file="liquibase/patch/sql/mysql/db.changelog-1-201.xml" labels="include-first-changelog"/>