Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

liquibase maven plugin multiple changeLogFile

I'm using liquibase maven plugin to update the database changes via jenkins automated builds.

I have this in my pom.xml

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.4.2</version>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.5</version>
        </dependency>
    </dependencies>
    <configuration>
        <changeLogFile>${basedir}/src/main/resources/schema.sql</changeLogFile>
        <changeLogFile>${basedir}/src/main/resources/data.sql</changeLogFile>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://${db.url}</url>
        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
    </configuration>
</plugin>

I need to run schema.sql before data.sql. When I run this locally it works. When I run it via jenkins the schema changeLogFile executes second, so in order to make it work I reversed the commads.

Question: What's the order of execution? Am I doing something wrong?

like image 747
Alkis Kalogeris Avatar asked Feb 07 '23 11:02

Alkis Kalogeris


1 Answers

The official goal documentation specify that only one entry is foreseen:

changeLogFile:
Specifies the change log file to use for Liquibase.

  • Type: java.lang.String
  • Required: No
  • Expression: ${liquibase.changeLogFile}

You can add further entries, but they will be ignored and maven will not complain: it doesn't validate plugin configuration' content, it cannot, because that part is up to the plugin and not known upfront by maven. That is, is generic.

To ensure a deterministic order and have two changeLogFile executed, you should specify several plugin executions as following:

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.4.2</version>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.5</version>
        </dependency>
    </dependencies>
    <configuration>
        <changeLogFile>${basedir}/src/main/resources/schema.sql</changeLogFile>
        <changeLogFile>${basedir}/src/main/resources/data.sql</changeLogFile>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://${db.url}</url>
        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
    </configuration>
    <executions>
        <execution>
            <id>update-schema</id>
            <phase>process-resources</phase>
            <goals>
                <goal>update</goal>
            </goals>
            <configuration>
                <changeLogFile>${basedir}/src/main/resources/schema.sql</changeLogFile>
            </configuration>
        </execution>
        <execution>
            <id>update-data</id>
            <phase>process-resources</phase>
            <goals>
                <goal>update</goal>
            </goals>
            <configuration>
                <changeLogFile>${basedir}/src/main/resources/data.sql</changeLogFile>
            </configuration>
        </execution>
    </executions>
</plugin>

Note: we are specifying a common configuration for all executions outside of the executions section, then per each execution we are only defining the additional configuration, which is every time the different file.

The deterministic order is guaranteed by Maven: for the same plugin, for the same phase, the order of declaration in the POM will be respected.

However, this executions will be part of your build now as part of the process-resources phase, which is probably not what you want. So in this case, better to move it to a profile as following:

<profiles>
    <profile>
        <id>liquibase-executions</id>
        <build>
            <defaultGoal>process-resources</defaultGoal>
            <plugins>
                <!-- MOVE HERE liquibase plugin configuration and executions -->
            </plugins>
        </build>
    </profile>
</profiles>

And then execute the following (according also to your comment):

mvn -Pliquibase-executions -Ddb.url=IP:PORT/DB -Dliquibase.username=USERNAME
like image 140
A_Di-Matteo Avatar answered Feb 12 '23 10:02

A_Di-Matteo