首页 > 解决方案 > 将存储过程的嵌入式 sql 文件用作 Liquibase 变更集的实际示例

问题描述

我可以举一个关于如何将嵌入式 sql 文件用于存储过程的示例吗?应该使用带有 --changeset 语法的变更日志中的 sql 嵌入文件。

我试过查看文档,它显示了如何编写变更集,但不是特别针对存储过程,而且我没有看到实际的源 sql 文件。

标签: liquibase

解决方案


带有存储过程的示例嵌入式 sql 文件:

DROP FUNCTION IF EXISTS split_token;
//

CREATE FUNCTION split_token(txt text, delimiter_text VARCHAR(255), token_index INT UNSIGNED) RETURNS TEXT DETERMINISTIC
BEGIN
/*! Return substring by index in delimited text */
IF CHAR_LENGTH(delimiter_text) = ''
THEN return SUBSTRING(txt, token_index, 1);
ELSE return SUBSTRING_INDEX(SUBSTRING_INDEX(txt, delimiter_text, token_index),delimiter_text,-1);
END IF;
END
//

DROP FUNCTION IF EXISTS split_token2;
//

CREATE FUNCTION split_token2(txt text, delimiter_text VARCHAR(255), token_index INT UNSIGNED) RETURNS TEXT DETERMINISTIC
BEGIN
/*! Return substring by index in delimited text */
IF CHAR_LENGTH(delimiter_text) = ''
THEN return SUBSTRING(txt, token_index, 1);
ELSE return SUBSTRING_INDEX(SUBSTRING_INDEX(txt, delimiter_text, token_index),delimiter_text,-1);
END IF;
END

回滚脚本如下所示:

DROP FUNCTION IF EXISTS split_token;
//

DROP FUNCTION IF EXISTS split_token2;

变更日志如下所示:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

 <!-- <changeSet author="SteveZ" id="external-sql+rollback-script-example" context="QA" labels="Jira1000">
   <sqlFile dbms="mysql" splitStatements="true" endDelimiter="//" stripComments="true" path="objects/function/split_token.sql"/>
   <rollback>
    <sqlFile dbms="mysql" splitStatements="true" endDelimiter="//" stripComments="true" path="objects/function/split_token_rollback.sql"/>
   </rollback>
 </changeSet> -->
  ....
</databaseChangeLog>

推荐阅读