database - 我可以从 Liquibase LoadUpdateData XML 标记中跳过主键子句吗
问题描述
我的表结构如下
CREATE TABLE domain_stat (
domain_stat bpchar(2) NOT NULL,
des_domain_stat varchar(40) NULL,
cde_domain_stat_catg bpchar(1) NOT NULL,
date_crea timestamp NOT NULL,
id_crea_user bpchar(8) NOT NULL,
date_updt timestamp NOT NULL,
id_updt_user bpchar(8) NOT NULL
);
因此,在这种情况下不涉及主键,甚至我的实际数据也确实包含重复的行。
问题是通过使用 liquibase 语法将数据加载到表中,我必须primarykey
强制添加子句。有什么办法可以跳过主键。或添加复合键的任何替代方法,以便可以将完整行标识为唯一行。
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<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">
<changeSet author="liquibase-docs"
id="loadUpdateData-example" context="!prod">
<loadUpdateData encoding="UTF-8"
file="config/liquibase/data/domain_stat.csv" onlyUpdate="false" primaryKey="id"
quotchar="'" separator="," tableName="domain_stat">
</loadUpdateData>
</changeSet>
</databaseChangeLog>
解决方案
我找到了解决这个问题的方法。因为我的桌子上没有主键。我创建了一个可执行bat
文件,并使用 Postgres\COPY
指令将 CSV 文件数据加载到相应的表中。
bat 文件为
set PGPASSWORD=root
set DB_HOST_NAME="localhost"
set DB_PORT="5434"
set DB_USER="postgres"
set DB_NAME="db_dev"
echo "Importing accessory Data"
psql -h %DB_HOST_NAME% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "\COPY "dev".p_table FROM ../data/p_data.csv delimiter ',' csv HEADER NULL AS 'null';"
我的 liquibase XML 文件看起来像
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<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">
<changeSet author="liquibase-docs" context="!prod"
id="executeCommand-example">
<executeCommand executable="import-data.bat" />
</changeSet>
</databaseChangeLog>
然后将此文件导入主更改日志,这对我有用!
推荐阅读
- visual-studio-code - 我的 TailWind CSS Intellisense 插件无法在我的 VSCode 上运行
- android - 字符串 SVG 到 Google 地图标记
- python - Python中字符串中至少出现三次的连续字符的正则表达式
- apache-spark - Spark 在运行期间用千兆字节的小文件填充临时目录
- php - Laravel 更改时区
- c# - 如何在C#中保存userInput以自动调整难度
- c# - C# compre 两个类结构
- mysql - 有条件地删除 MySQL 数据库中的行
- jenkins - 根据之前的作业状态运行 jenkins 作业
- html - 我的网页在重新加载时花费了太多时间