Well, to be honest, there is no a silver bullet. BUT, Liquibase itself comes with a set of awesome features such as the Liquibase Maven plugin.
Instead of writing the change log file manually, we can use the Liquibase Maven plugin to generate it and save ourselves a lot of work. I want to illustrate this by means of an example. In the early stage of the development of a new eXo add-on I am currently working on, the data model was not always stable. It had to evolve frequently as I was implementing some moving requirements. Agility is cool, but updating the change log file quickly became a painful task for me.
With my team, we decided to use Liquibase Maven plugin as a ‘Swiss Army Knife’ for several common tasks, to:
${basedir}/src/main/resources/db/changelog/myaddon.db.changelog.xml
In some situations, typically when we need to update a database schema already used in production, we can use the plugin to generate a changelog file from the differences between two existing databases:
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/tribe
tribe
root
jdbc:mysql://localhost:3306/local
com.mysql.jdbc.Driver
root
${basedir}/src/main/resources/db/changelog/
myaddon.db.changelog-master.xml
${basedir}/src/main/resources/db/changelog/myaddon.db.changelog-${maven.build.timestamp}.xml
${basedir}/src/main/resources/db/changelog/myaddon.db.changelog.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/tribe
tribe
root
jdbc:mysql://localhost:3306/tribe-ref
com.mysql.jdbc.Driver
root
true
debug
The most expected feature provided by Liquibase Maven Plugin when coupled with the Liquibase Hibernate Plugin is the way of generating the changelog from JPA entities. The goal is to generate a changelog file from the differences between an existing database (for example production) and our new persistence entities.
Note: It is important to emphasise that developers have to check the consistency of generated changesets and adapt them if necessary.
hibernate:ejb3:exo-pu?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
liquibase-hibernate comes with 3 flavours of configuration; please refer to official doc: https://github.com/liquibase/liquibase-hibernate/wiki
Note: It is worth to note that this configuration needs version 3.5.5 of liquibase-core instead of version 3.4.2.
The expected output is a changelog file as follows:
When using Liquibase as part of a build automation scenario, I think it makes sense to create a single entry point for Liquibase to manage all generated changelog files that we will call XYZ.db.changelog-master.xml. The aim is to start from this file and load all other changesets available in src/main/resources/db/changelog.
The following snippet illustrates how we use a master changelog file within our add-on:
org.javassist
javassist
${org.javassist.version}
org.liquibase
liquibase-core
${org.liquibase.version}
org.liquibase.ext
liquibase-hibernate4
${org.liquibase-hibernate4.version}
javax.validation
validation-api
${validation-api.version}
mysql
mysql-connector-java
${mysql.version}
liquibase
org.liquibase
liquibase-maven-plugin
${org.liquibase.version}
services/src/main/resources/db/changelog/myaddon.db.changelog-master.xml
services/src/main/resources/db/changelog/myaddon.db.changelog-${maven.build.timestamp}.xml
services/src/main/resources/db/changelog/myaddon.db.changelog.xml
com.mysql.jdbc.Driver
jdbc:mysql://${db.host}:${db.port}/${db.schema}
tribe
${db.username}
${db.password}
hibernate:ejb3:exo-pu?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
true
debug
org.javassist
javassist
${org.javassist.version}
org.liquibase
liquibase-core
${org.liquibase.version}
org.liquibase.ext
liquibase-hibernate4
${org.liquibase-hibernate4.version}
javax.validation
validation-api
${validation-api.version}
mysql
mysql-connector-java
${mysql.version}
org.exoplatform.commons
commons-component-common
${org.exoplatform.platform.version}
xml-apis
xml-apis
${xml-apis.version}
liquibase
localhost
3306
tribe
root
To check that it works, run :
mvn liquibase:help
To summarise what a developer needs to know in order to better use liquibase-maven-plugin through the interaction with a database (during dev lifecycle):
Note: As you may have noticed, we haven’t talked about liquibase:update option. Quite simply, this option is used to perform the schema changes listed in the changelog on the target database. For eXo projects, this use case was wrapped up within a core service called org.exoplatform.commons.persistence.impl.LiquibaseDataInitializer which is triggered at server startup.
( Your e-mail address will not be published)