Liquibase rollback when maven build failed - spring

How to rollback liquibase changeset automatically when maven build failed due to some reason like any test failed or some other reason.
I am using below configuration in liquibase.
<changeSet id="1" author="Manish">
<createTable tableName="person2">
<column name="address" type="varchar(255)"/>
</createTable>
</changeSet>
Is there a way to rollback this if build failed?
Thanks!

Yes, you can rollback a changeset. For that you will need to do 2 things as below:
Add a rollback code in the changeset. Refer to the video in this document to learn how you can add the rollback code.
You will need to run the rollback command from one of the Maven goals. You can learn more about the Maven goals on this document: https://docs.liquibase.com/tools-integrations/maven/commands/maven-rollback.html

Related

mvn liquibase rollback returns type incompatibility error

My Spring Boot api utilizes liquibase and I'm trying to roll back the last liquibase update.
I've tried a couple different commands (followed by my db credentials on the same line):
mvn liquibase:rollback -Dliquibase.rollbackCount=1
and also:
mvn liquibase:rollback -Dliquibase.rollbackDate=2022-11-08
For each, the mvn execution starts out promising, with Successfully acquired change lock
But then each ends up with this error:
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.4.1:rollback (default-cli) on project tve: A type incompatibility occurred while executing org.liquibase:liquibase-maven-plugin:3.4.1:rollback: java.time.LocalDateTime cannot be cast to java.lang.String
Any idea what I'm doing wrong here?
I notice that the most recent rows in my databasechangelog table show a liquibase version of 4.5.0, while all the older ones show 3.5.4. Perhaps that's somehow related to this error?
As it turns out, I was able to resolve this by changing my liquibase-maven-plugin version from 3.4.1 to 4.5.0.
Not exactly sure what the issue was or why that resolved it, but perhaps this may help someone else.

Liquibase customPrecondition causes maven plugin to fail

We have our liquibase scripts setup in a maven project and we are using the liquibase maven plugin to execute the scripts on deploy of our software.
We need to ensure that only the liquibase scripts relevant to the version of the software being deployed are run.
I have created a custom PreCondition in liquibase for each changeset as follows:
<preConditions onFail="CONTINUE">
<customPrecondition className="class.CompatibleVersion">
<param name="version" value="1.0.0"/>
</customPrecondition>
</preConditions>
This preCondition throws a CustomPreconditionFailedException when the precondition version parameter is > deployedVersion.
The precondition is working unfortunately whenever it throws the CustomPreconditionFailedException instead of skipping the change set (onFail="CONTINUE") the maven plugin fails with the following:
Error setting up or running Liquibase: Migration failed for change set src/database/changelog/db.changelog-1.0.1.xml::1.0.1_script1::fred.smith:
[ERROR] Reason:
[ERROR] db.changelog-master.xml : liquibase.precondition.CustomPreconditionWrapper#2add4d24 : liquibase.exception.CustomPreconditionFailedException: Change set version : 1.0.1 incompatible with deploy version : 1.0.0. Skipping change set. Precondition Error
This is the exception I expect it to throw but I am expecting the change set to be skipped due to the onFail="CONTINUE".
What am I missing ?
Thanks.

Command to update only one ChangeSet : Mvn liquibase command?

Normally to execute all the change set in a file : we use command :
mvn liquibase:update
What should I use If I want to execute to 'specific' Changeset out of many ?
Probably the best way to do this is to use labels on your changesets, and then see if you can get the maven plugin to do a deploy with labels. Labels are a fairly new feature, and it may be that the Maven plugin will need to be updated to provide support for labels. It appears that the Liquibase Maven plugin is currently using Liquibase 3.2, and labels were introduced in 3.3.

Maven liquibase plugin

I am new to Maven and Liquibase. For the past couple of days, I have been playing around with liquibase without Maven. I have tried generating changelog file with a new database and an existing database. Now I am trying to implement Maven and Liquibase using the plugin.
What is the advantange of running Liquibase with Maven from the command prompt rather than just running Liquibase from the command prompt?
Once I generate the pom.xml file and have src folder ready for my current project. I have the maven liquibase plugin in the pom file and liquibase.properties in resources. Will the changeLog file automatically get created?
The reason you would want to use Liquibase through Maven is to better fit with the rest of your build configuration. If you have your project set up with Maven and your CI servers using Maven it is usually easiest to run Liquibase through Maven as well to keep things simpler for you.
There is a generateChangeLog command that you can run either through Maven or through the command line which will generate a changelog file for you based on an existing database. This is often helpful when first starting to use Liquibase.
Once you have your changelog file, you would run liqubase update through the command line or Maven to apply your changelog to a database. As you need to make changes, the recommended approach is to add new changeSets to your changelog file then run liquibase update. This tends to be safer and more consistent than making a change to the database then running liquibase diffChangeLog to capture the change back to the changelog file.

How can I retrieve ant property into teamcity

I am configuring teamcity project for ant. I know I can retrieve maven build number from pom.xml using ${maven.build.number} into teamcity. I am not getting how to retrieve the same from ant xml. I have configured a property in ant file for version.
<project xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property name="version" value="1.0.0.16"/>
Teamcity provides a large number of properties that are automatically made available to your ANT build (via System properties). In your specific case the Teamcity build number is available as:
<property name="version" value="${build.number}"/>
There are dozens of other properties you can make available as well, consult the Teamcity Docs

Resources