Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase on multiple databases

I have already implemented Liquibase with Maven. We are currently using a single database (db2) but now we need to add a new database to the application which will have different objects.

I've seen that i can define a new profile in maven but i couldn't find out how to differentiate which objects is being created on which database.

Is there a solution to this? Can I support 2 different databases with different objects using liquibase?

like image 410
user3202867 Avatar asked Aug 18 '14 13:08

user3202867


2 Answers

As you can see in the documentation, you can use two different executions, like this:

<plugin>
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-maven-plugin</artifactId>
  <version>3.0.5</version>
  <executions>
    <execution>
      <phase>process-resources</phase>
      <configuration>
        <changeLogFile>PATH_TO_CHANGELOG_1</changeLogFile>
        ... connection properties  ...
      </configuration>
      <goals>
        <goal>update</goal>
      </goals>
    </execution>
   <execution>
      <phase>process-resources</phase>
      <configuration>
        <changeLogFile>PATH_TO_CHANGELOG_2</changeLogFile>
        ... connection properties  ...
      </configuration>
      <goals>
        <goal>update</goal>
      </goals>
    </execution>
  </executions>
</plugin>

The only problem with this approach is that you need two different changelog.xml files, one per database.

Also, you can have preconditions in your changelog file to choose between what changeset will be processed by each database.

For example:

<changeSet id="1" author="bob">
    <preConditions onFail="MARK_RAN">
         <dbms type="oracle" />
    </preConditions>
    <comment>Comments should go after preCondition. If they are before then liquibase usually gives error.</comment>
    <dropTable tableName="oldtable"/>
</changeSet>

The onFail="MARK_RAN" makes Liquibase skip the changeset but marks it as run, so the next time it will not try again. See the customPrecondition tag in the documentation for more complex preconditions.

like image 162
Arturo Volpe Avatar answered Sep 25 '22 05:09

Arturo Volpe


You may want to have 2 separate changelogs to manage the two databases, even if they are both used by the same application.

like image 35
SteveDonie Avatar answered Sep 26 '22 05:09

SteveDonie