我在liquibase中有回滚,例如:
<changeSet author="..." id="...">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">
...
</sqlCheck>
</preConditions>
<delete tableName="A">
<where>ID = 'house'</where>
</delete>
<rollback>
<insert tableName="A">
<column name="ID" value="house" />
<column name="TITLE" value="a" />
<column name="TYPE" value="b" />
<column name="VER" valueNumeric="c" />
</insert>
</rollback>
</changeSet>
此变更集在3个不同的xml文件中运行,每个文件都相同。
我需要提供自定义sql查询以进行回滚,因为它可能引发将相同值插入DB的异常,因此我需要知道具有给定值的wheter ID是否存在。
我做了这样的事情:
<rollback>
<sql dbms="oracle">
insert into A(ID,TITLE,TYPE,VER)
VALUES(house,a,b,c)
where not exists (select ID from A where ID = 'house')
</sql>
<sql dbms="postgresql">
...
</sql>
</rollback>
我做了类似的事情,但是没有用。 (对于postgres,我也需要这样做)我该怎么做?我只需要检查“ A”表中是否存在具有给定值的ID。
答案 0 :(得分:1)
使用SELECT
作为源,而不是values
子句:
对于Postgres:
INSERT INTO A(ID,TITLE,TYPE,VER)
select 'house','a','b','c'
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house');
对于Oracle:
INSERT INTO A(ID,TITLE,TYPE,VER)
select 'house','a','b','c'
from dual
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house');
答案 1 :(得分:0)
INSERT INTO A(ID,TITLE,TYPE,VER)
VALUES(house,a,b,c)
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house')
应该为postgres工作