sqldatabase-migrationliquibaserollbackliquibase-maven-plugin

How to set tag to changeset in SQL format to run rollbackTag Liquibase Maven?


I want to set tag to my changeset to rollback to this changeset again if I need.

So I wondered about how to set tag to changeset in SQL format.

Thanks in advance.

Here is my databasechangelog table on database.

databasechangelog table

Here is my changelog file (tag is not working, also I tried tagDatabase:"version_3.0" and didn't work neither) :

--liquibase formatted sql

--changeset yusuf:5 -tag:"version_3.0"

create table test_table2 (test_id INT, test_column VARCHAR, PRIMARY KEY (test_id))

Here is my pom.xml liquibase plugin:

    <plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>4.2.0</version>
            <configuration>
                <propertyFile>src/main/resources/liquibase.properties</propertyFile>
            </configuration>
                    <goals>
                        <goal>update</goal>
                        <goal>tag</goal>
                    </goals>
   </plugin>

Solution

  • There isn't anything to directly support doing that currently in formatted sql.

    With xml/yaml/json we have the <tag> change you can include in changesets, but formatted sql explicitly does not support those because it's focused on "the script has all the logic in it, without relying on any liquibase magic" which <tag> falls into.

    What tag is doing is just marking the last ran changeset in the databasechangelog table's tag column. So you could do something like this:

    --liquibase formatted sql
    
    --changest yusuf:5
    create table test_table2 (test_id INT, test_column VARCHAR, PRIMARY KEY (test_id))
    
    --changeset yusuf:6 -tag:"version_3.0"
    update databasechangelog t set tag='version_3.0' where .... 
    

    where that update statement is going to be something specific to your database. You could come up with the correct syntax for "where it's the row with the highest dateexecuted, orderexecured values" but that can get complex. You could get what your database uses by running the liquibase tag command with logLevel=fine and see it.

    Alternately, if you know where it's coming in your changelog file and how it's being used, you could do a more simple where id='5' and author='yusuf' and path='my/file.sql' type statement.