如果我已经有了 changelog.xml,我该如何正确创建外部 liquibase-changeSet-xml?

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>

我已经有了这个 table 的 changelog.xml,我必须用一个额外的 xml 更新日志文件更新 table,它可以工作吗?

据我了解,您有现有的变更日志并且您想要包含一些其他的变更日志文件。您可以通过在当前更改日志文件的末尾添加以下条目来实现此目的:

<include file="classpath:/path/to/changelog/additional-liquibase-changelog.xml"/>

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.

请阅读有关 liquibase 更新日志的更多信息 here。 根据您的问题,我认为您想对数据库应用多个更改,这意味着您计划拥有多个具有目标数据库更改的变更集。为此,您可以采用以下两种方式:

1.在单个变更日志文件中定义多个变更集 - 在这种方法中,您可以在变更日志文件本身中定义多个变更集,并且应该执行所有这些变更。

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

阅读更多关于 liquibase changesets here

2。在父变更日志文件中包含外部变更集的路径 - 在这种方法中,您可以创建一个单独的 changeset.xml 文件,其中包含您想要应用到数据库的变更,并且只是 include 它在您的更改日志文件如下所示:

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

您还可以使用 includeAll 来包含目录中的所有变更集,而不必专门包含每个变更集。详细了解 includeAll here and Read more about include tag here