如何在 Liquibase 中使用一个 ChangeLog 到另一个 ChangeLog?

How to use one ChangeLog inside another in Liquibase?

我有两个 changeLog,其中 changeset 可以创建两个表。

具有创建人员的变更集的变更日志

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

       <changeSet id="1" author="nvoxland">
        <createTable tableName="person">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="firstname" type="varchar(50)" />
            <column name="lastname" type="varchar(50)">
                <constraints nullable="false" />
            </column>
            <column name="state" type="char(2)" />
            <column name="district" type="char(2)" />
            <column name="city" type="char(2)" />
         </createTable>
        </changeSet>
</databaseChangeLog>

和创建护照的变更日志

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

       <changeSet id="2" author="nvoxland">
        <createTable tableName="passport">
            <column name="passportNo" type="int">
                <constraints primaryKey="true" nullable="false" />
            </column>
         </createTable>
        </changeSet>

</databaseChangeLog>

我想在一个单独的变更日志中创建一个新的变更集,它可以使用上面的两个变更日志,并在个人和护照之间添加一个外键约束。

像这样

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

       <changeSet id="3" author="nvoxland">
        import  changeset 1 & 2
        add foreign-key between person & passport 
        </changeSet>
</databaseChangeLog>

这可以使用 Liquibase 吗?请提出可能的方法。

如果我答对了你的问题,你可以使用 master-changelog.xml 文件并在那里指定所有 changelog 文件。

您的 master-changelog 可能如下所示:

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <include file="changelog/changelog_file_1.xml" relativeToChangelogFile="true"/>
    <include file="changelog/changelog_file_2.xml" relativeToChangelogFile="true"/>
    <include file="changelog/changelog_file_3.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

changelog 个文件将按照提供的顺序执行,因此第三个 changelog 将已经拥有所有可用的所需数据。

因此在您的 changelog_file_3.xml 中您将有一个 changeSet<addForeignKeyConstraint>

顺便说一下,您不需要为每个 changeset.

创建单独的 changelog