Liquibase on multiple databases

2019-04-10 12:05发布

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?

3条回答
Juvenile、少年°
2楼-- · 2019-04-10 12:40

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

查看更多
三岁会撩人
3楼-- · 2019-04-10 12:49

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-04-10 12:56

As Arturo says you can have 2 or more execution-nodes, but you must give every execution-node a seperate id.

                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>3.0.5</version>
                    <executions>
                        <execution>
                            <id>db1-update</id>
                            <phase>process-resources</phase>
                            <configuration>
                                <changeLogFile>src/main/resources/org/liquibase/db1.xml</changeLogFile>
                                <driver>org.postgresql.Driver</driver>
                                <url>jdbc:postgresql://localhost/db1</url>
                                <username>..</username>
                                <password>..</password>
                            </configuration>
                            <goals>
                                <goal>update</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>db2-update</id>
                            <phase>process-resources</phase>
                            <configuration>
                                <changeLogFile>src/main/resources/org/liquibase/db2.xml</changeLogFile>
                                <driver>org.postgresql.Driver</driver>
                                <url>jdbc:postgresql://localhost/db2</url>
                                <username>...</username>
                                <password>...</password>
                            </configuration>
                            <goals>
                                <goal>update</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>db3-update</id>
                            <phase>process-resources</phase>
                            <configuration>
                                <changeLogFile>src/main/resources/org/liquibase/db3.xml</changeLogFile>
                                <driver>org.postgresql.Driver</driver>
                                <url>jdbc:postgresql://localhost/db3</url>
                                <username>...</username>
                                <password>...</password>
                            </configuration>
                            <goals>
                                <goal>update</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
查看更多
登录 后发表回答