【问题标题】:Liquibase formatted SQL changelogs and multiple filesLiquibase 格式的 SQL 更改日志和多个文件
【发布时间】:2018-04-06 07:17:22
【问题描述】:

我们开始在 spring-boot 上为我们的应用程序使用 liquibase。其中一项要求使用普通的 sql 作为 liquibase。我们有许多用于初始化数据库的 sql 文件。我检查了文档https://www.liquibase.org/documentation/sql_format.html,但没有找到如何创建更改日志 sql 文件的层次结构的信息。 Spring Boot 属性liquibase.change-log 等待单个文件。我试图用,; 分隔文件名,总是从spring-boot 得到错误找不到更改日志位置... 所以我的问题:
如何声明执行另一个或多个“sql 格式”文件??
类似于 xml 等效项:
<include file="second_changelog.sql"/>
<include file="third_changelog.sql"/>


无效示例first_changelog.sql :
--liquibase formatted sql --changeset author_1:1 UPDATE [dbo].[customers] SET name='HD_1' WHERE id = '11'; --import file=second_changelog.sql --import file=third_changelog.sql

PS。请不要建议xml,因为我只需要SQL

【问题讨论】:

    标签: java spring spring-boot liquibase


    【解决方案1】:

    您可以使用一个包含对普通 sql 的多个引用的 XML 文件(详情请参阅https://www.liquibase.org/documentation/changes/sql_file.html

    例子:

    <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.3.xsd">
    
        <changeSet id="init" author="author">
            <sqlFile encoding="utf8" path="first_changelog.sql"/>
            <sqlFile encoding="utf8" path="second_changelog.sql"/>
        </changeSet>
    

    【讨论】:

    • 谢谢,但由于要求不接受。我只需要普通的 sql 更改日志文件。
    【解决方案2】:

    所以,

    您似乎不想使用一个大格式的 SQL 变更日志,其中每个变更集都写入文件中。相反,如果您想拥有指向的单独 SQL 文件(使用普通 SQL),则需要有一个仅触碰一次的变更日志,如下所示:

    <changeLog><includeAll path="/path/to/your/sql/directory"></changeLog>
    

    使用includeAll告诉 Liquibase 拉入您指向的目录中的所有 SQL 文件,就好像它们是单独的变更集一样。见:http://www.liquibase.org/2015/09/liquibase-without-changelogs.html

    【讨论】:

      【解决方案3】:

      功能请求仍处于打开状态,解决方案为“尚无计划” 请查看以下链接以获取更多讨论: http://forum.liquibase.org/topic/can-we-include-a-file-in-rollback

      Jira 中的功能请求 ID: https://liquibase.jira.com/browse/CORE-1637

      【讨论】:

        【解决方案4】:

        我使用了下面的结构,@Yuriy 的回答会将 sql 文件作为更改集(不是 sql 文件中定义的更改集)。

        <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.3.xsd">
          <include file="changelog.sql"/>
        <include file="changelog2.sql"/>
        </databaseChangeLog>
        

        进一步的 changelog.sql(s) 如下所示。这样 liquibase 也会考虑 sql 变更集。

        --liquibase formatted sql
        
        --changeset nvoxland:1
        
        CREATE TABLE department_1   
        (    
             DeptID int NOT NULL PRIMARY KEY CLUSTERED  
           , DeptName varchar(50) NOT NULL  
           , ManagerID INT  NULL  
           , ParentDeptID int NULL  
           , SysStartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL  
           , SysEndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL  
           , PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)     
        )    
        WITH (SYSTEM_VERSIONING = ON)   
        ;  
        --rollback drop table department_1;
        

        【讨论】:

          【解决方案5】:

          使用主 XML 文件从多个文件加载 sql 变更集。

          <?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">
              <includeAll path="sql/"/>
          </databaseChangeLog>
          

          现在您可以将所有 .sql 文件存储在 sql 目录中。甚至它也会从子目录中读取。

          【讨论】:

            猜你喜欢
            • 2020-02-12
            • 1970-01-01
            • 1970-01-01
            • 2015-01-27
            • 1970-01-01
            • 1970-01-01
            • 2020-02-16
            • 1970-01-01
            • 2019-04-15
            相关资源
            最近更新 更多