Liquibase on multiple databases

半城伤御伤魂 提交于 2019-11-30 22:18:39

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

Arturo Volpe

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.

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!