How can i creat properly an external liquibase-changeSet-xml if i already have a changelog.xml?



<?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"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.9.xsd">

    
    <changeSet id="sameAsOriginalChangelog" author="xy">
        <addColumn tableName="table_to_modify">
            <column name="new_column"
                    type="varchar(255)">
                <constraints nullable="true"/>
            </column>
        </addColumn>
    </changeSet>
</databaseChangeLog>

I already have a changelog.xml to this table, and I have to update the the table with an extra xml changelog file, will it work?

Answer

The root of all Liquibase changes is the changelog file. Liquibase uses a changelog to sequentially list all changes made to your database. Think of it as a ledger. It is a file that contains a record of all your database changes (changesets). Liquibase uses this changelog record to audit your database and execute any changes that are not yet applied to your database.

Please read more about liquibase changelogs here. From your question, I think you want to apply more than one changes to your DB which means you are planning to have multiple changesets with targeted DB changes. To achieve this you can follow following 2 ways:

1. Defining multiple changesets inside single changelog file – In this approach you can define multiple changesets in your changelog file itself and all these changes should get executed.

<?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:pro="http://www.liquibase.org/xml/ns/pro"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.0.xsd
      http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd">  
    <changeSet id="1" author="bob">  
        <comment>A sample change log</comment>  
        <createTable/> 
    </changeSet>  
    <changeSet id="2" author="bob" runAlways="true">  
        <alterTable/>  
    </changeSet>  
    <changeSet id="3" author="alice" failOnError="false" dbms="oracle">
        <alterTable/>  
    </changeSet>  
    <changeSet id="4" author="alice" failOnError="false" dbms="!oracle">
        <alterTable/>  
    </changeSet>  

</databaseChangeLog>

Read more about liquibase changesets here

2. Including path to external changeset in your parent changelog file – In this approach you can create a separate changeset.xml file with the changes you want to be applied on your DB and just include it in your changelog file like below :

<?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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
    <include file="com/example/news/news.changelog.sql"/>
    <include file="com/example/directory/directory.changelog.sql"/>
</databaseChangeLog>

You can also use includeAll to include all your changesets located inside a directory without having to include each changeset specifically. Read more about includeAll here and Read more about include tag here



Source: stackoverflow