使用 LIQUIBASE WITHOUT CHANGELOGS 时如何执行多个插入
How to execute multiple inserts when using LIQUIBASE WITHOUT CHANGELOGS
根据 this 文档
Liquibase
可以在给定路径中执行多个 sql
文件而不需要任何更改日志。但是,当我使用以下插入内容创建文件时
insert into address (id, line1, line2) values (1, '121 Main Ave', null);
insert into address (id, line1, line2) values (2, '662 Broadway', 'Suite 3317');
insert into address (id, line1, line2) values (3, '412 Riverview', null);
我收到以下错误
Invalid sql
syntax
Liquibase 无法识别您的 sql 文件。
在 sql 文件之上添加这两行:
--liquibase formatted sql
--changeset {authorName}:{id}
根据您的意愿更改 authorName 和 id。您也可以在 changelog.xml 文件中执行类似的操作:
<changeSet author="authorName" id=”id”>
<sqlFile path="insertcommands.sql"/>
</changeSet>
在这种情况下,您无需将 insertcommands.sql 文件置于顶部
--liquibase formatted sql
--changeset {authorName}:{id}
就像你之前做的那样。
PS - 在 liquibase-3.4 和 mysql5.5
上测试
您可以将 loadData(或 loadUpdate 用于 table 中的部分更改)用于此类目的:
<loadData encoding="UTF-8"
file="[path to csv]"
separator=";"
tableName="ADDRESS"/>
并提供包含以下内容的 CSV 文件:
id;line1;line2
1;121 Main Ave;null
2;662 Broadway;Suite 3317
3;412 Riverview;null
有两种写多查询的方法
1) 使用 <sqlFile>
标签。
path : 所有插入查询写入的文件路径
<changeSet author="liquibase-docs" id="sqlFile-example">
<sqlFile encoding="utf8" path="filepathsql"relativeToChangelogFile="true" splitStatements="true"stripComments="true"/>
</changeSet>`
注意:需要为多个查询写入属性 splitStatements=true
http://www.liquibase.org/documentation/changes/sql_file.html
2) <sql>
标签
<changeSet author="liquibase-docs" id="sql-example">
<comment>insert queries</comment>
<sql dbms="h2, oracle"
splitStatements="true"
stripComments="true">
insert into address (id, line1, line2) values (1, "121 Main Ave", null);
insert into address (id, line1, line2) values (2, "662 Broadway", "Suite 3317");
insert into address (id, line1, line2) values (3, "412 Riverview", null);
</sql>
</changeSet>
根据 this 文档
Liquibase
可以在给定路径中执行多个 sql
文件而不需要任何更改日志。但是,当我使用以下插入内容创建文件时
insert into address (id, line1, line2) values (1, '121 Main Ave', null);
insert into address (id, line1, line2) values (2, '662 Broadway', 'Suite 3317');
insert into address (id, line1, line2) values (3, '412 Riverview', null);
我收到以下错误
Invalid
sql
syntax
Liquibase 无法识别您的 sql 文件。 在 sql 文件之上添加这两行:
--liquibase formatted sql
--changeset {authorName}:{id}
根据您的意愿更改 authorName 和 id。您也可以在 changelog.xml 文件中执行类似的操作:
<changeSet author="authorName" id=”id”>
<sqlFile path="insertcommands.sql"/>
</changeSet>
在这种情况下,您无需将 insertcommands.sql 文件置于顶部
--liquibase formatted sql
--changeset {authorName}:{id}
就像你之前做的那样。
PS - 在 liquibase-3.4 和 mysql5.5
上测试您可以将 loadData(或 loadUpdate 用于 table 中的部分更改)用于此类目的:
<loadData encoding="UTF-8"
file="[path to csv]"
separator=";"
tableName="ADDRESS"/>
并提供包含以下内容的 CSV 文件:
id;line1;line2
1;121 Main Ave;null
2;662 Broadway;Suite 3317
3;412 Riverview;null
有两种写多查询的方法
1) 使用 <sqlFile>
标签。
path : 所有插入查询写入的文件路径
<changeSet author="liquibase-docs" id="sqlFile-example">
<sqlFile encoding="utf8" path="filepathsql"relativeToChangelogFile="true" splitStatements="true"stripComments="true"/>
</changeSet>`
注意:需要为多个查询写入属性 splitStatements=true
http://www.liquibase.org/documentation/changes/sql_file.html
2) <sql>
标签
<changeSet author="liquibase-docs" id="sql-example">
<comment>insert queries</comment>
<sql dbms="h2, oracle"
splitStatements="true"
stripComments="true">
insert into address (id, line1, line2) values (1, "121 Main Ave", null);
insert into address (id, line1, line2) values (2, "662 Broadway", "Suite 3317");
insert into address (id, line1, line2) values (3, "412 Riverview", null);
</sql>
</changeSet>