JITHIN_PATHROSE
JITHIN_PATHROSE

Reputation: 1396

How to use one ChangeLog inside another in Liquibase?

I have two changeLog which has changesets to create two tables .

The changelog having changeset to create person

<?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>

and the changelog to create passport

<?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>

I want to create a new changeset in a separate changelog which can use the above two changelog and add a foreignkey constraint between person and passport.

Something like this

<?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>

Is this possible using Liquibase? Please suggest the possible ways.

Upvotes: 1

Views: 334

Answers (1)

htshame
htshame

Reputation: 7330

If I get your question right, you can use master-changelog.xml file and specify all your changelog files there.

Your master-changelog may look like this:

<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 files will be executed in the provided order, so the third changelog will already have all the needed data available.

So in your changelog_file_3.xml you'll have a changeSet with <addForeignKeyConstraint>.

By the way, you don't need to create separate changelog for each changeset.

Upvotes: 2

Related Questions