I am migrating an existing database to be managed by liquibase.
I have the changelog already.
the application is a spring boot application and I updated the pom with liquibase-core
and set up the spring liquibase properties.
I tested this against an empty database and works as expected and liquibase executes the changelog on application startup and maintains the DATABASECHANGELOG
tables.
Now the issue that I am facing is that the database already exist in all other environments and I just want to baseline the changelog against these environments on the first application run with liquibase migration done.
I am aware that the liquibase changelogsync
command is what I need and I am aware of the maven plugin that can execute it.
However, the maven plugin is not my first preference as I do not want to bring in another component and was hoping if there is a way to execute a changelogsync
on application startup like how the liquibase-core executes the liquibase on app startup
and control that with a spring application property.
I did a lot of research on this but only found answers using the maven plugin.
CodePudding user response:
I brought down the source for liquibase-core
and found there are helper methods already in there to execute a changelogsync
.
Basically the foundation of all CLI and plugins is the java based liquibase-core module so there are public methods to do all the possible tasks. They are simply not exposed to the spring module.
I tried the below changelogsync method and it seems to do what needs to be done.
CodePudding user response:
There are 2 independent issues in your topic.
- Sync the existing databases with the new change log file. Liquibase must not try to run those changes on existing database, only on the new one.
- Trigger liquibase each time (not only once), when the application starts up. Liquibase using
DATABASECHANGELOG
table identifies, what changes have been applied. You do not need to try it run only once.
For the first issue, you have 2 options:
1.1 Use changeLogSync
or changeLogSyncSQL
. It is reasonable only if you have a single client.
1.2 A better approach, adopt the new change log file (each statement) with preConditions
, like:
<preConditions onFail="MARK_RAN"><not><tableExists tableName="settings"/></not></preConditions>
- To trigger liquibase with Spring Boot during application startup, there are 2 articles on the official site: