Liquibase on multiple databases

喜欢而已 提交于 2019-11-30 05:24:43

问题


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?


回答1:


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




回答2:


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.




回答3:


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>


来源:https://stackoverflow.com/questions/25364023/liquibase-on-multiple-databases

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