在Liquibase前提条件中不是空的sqlCheck

时间:2015-06-02 08:05:57

标签: liquibase

我想知道是否有一种简单的方法来创建liquibase前置条件检查特定值是NULL还是NOT NULL。我的方法不起作用。

我创建了以下测试变更集:

<?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.1.xsd">

   <changeSet id="1" author="test">
      <createTable tableName="test_table">
         <column name="id" type="int">
            <constraints primaryKey="true" nullable="false" />
         </column>
         <column name="test_text" type="varchar(50)">
            <constraints nullable="true" />
         </column>
      </createTable>
   </changeSet>

   <changeSet id="2" author="test">
      <insert tableName="test_table">
         <column name="id" value="1" />
         <column name="test_text" value="test1" />
      </insert>
      <insert tableName="test_table">
         <column name="id" value="2" />
         <column name="test_text" value="null" />
      </insert>
   </changeSet>

   <changeSet id="3" author="test">
      <preConditions onFail="HALT">
         <sqlCheck expectedResult="test1">select test_text from test_table where id=1</sqlCheck>
      </preConditions>
      <insert tableName="test_table">
         <column name="id" value="3" />
         <column name="test_text" value="foo" />
      </insert>
   </changeSet>

   <changeSet id="4" author="test">
      <preConditions onFail="HALT">
         <sqlCheck expectedResult="NULL">select test_text from test_table where id=2</sqlCheck>
      </preConditions>
      <insert tableName="test_table">
         <column name="id" value="4" />
         <column name="test_text" value="bar" />
      </insert>
   </changeSet>

</databaseChangeLog>

一切正常,除了变更集4,它没有出现令人惊讶的错误消息&#34;没有从SQL Precondition&#34;返回的行。但是有数据:

$ psql -U test -c 'select * from test_table'
 id | test_text 
----+-----------
  1 | test1
  2 | 
  3 | foo
(3 rows)

似乎结果被解释为空。实际上,它返回一行,其中一列包含NULL值。任何想法如何使这个工作?

我在Ubuntu 14.04上使用Liquibase 3.3.5和PostgreSQL 9.3。

这是完整输出(信息级别):

$ ./liquibase --driver=org.postgresql.Driver  --url="jdbc:postgresql://localhost:5432/test" --username=test --password="" --changeLogFile=/tmp/test.changeset.xml  --logLevel=info update
Liquibase Home is not set.
INFO 02.06.15 09:57: liquibase: Successfully acquired change log lock
INFO 02.06.15 09:57: liquibase: Creating database history table with name: public.databasechangelog
INFO 02.06.15 09:57: liquibase: Reading from public.databasechangelog
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::1::test: Table test_table created
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::1::test: ChangeSet /tmp/test.changeset.xml::1::test ran successfully in 15ms
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::2::test: New row inserted into test_table
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::2::test: New row inserted into test_table
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::2::test: ChangeSet /tmp/test.changeset.xml::2::test ran successfully in 10ms
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::3::test: New row inserted into test_table
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::3::test: ChangeSet /tmp/test.changeset.xml::3::test ran successfully in 3ms
SEVERE 02.06.15 09:57: liquibase: /tmp/test.changeset.xml: /tmp/test.changeset.xml::4::test: Change Set /tmp/test.changeset.xml::4::test failed.  Error: Migration failed for change set /tmp/test.changeset.xml::4::test:
     Reason: 
          /tmp/test.changeset.xml : No rows returned from SQL Precondition

liquibase.exception.MigrationFailedException: Migration failed for change set /tmp/test.changeset.xml::4::test:
     Reason: 
          /tmp/test.changeset.xml : No rows returned from SQL Precondition

    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:485)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:43)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73)
    at liquibase.Liquibase.update(Liquibase.java:200)
    at liquibase.integration.commandline.Main.doMigration(Main.java:1044)
    at liquibase.integration.commandline.Main.run(Main.java:175)
    at liquibase.integration.commandline.Main.main(Main.java:94)
Caused by: liquibase.exception.PreconditionFailedException: Preconditions Failed
    at liquibase.precondition.core.AndPrecondition.check(AndPrecondition.java:51)
    at liquibase.precondition.core.PreconditionContainer.check(PreconditionContainer.java:201)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:471)
    ... 6 more
INFO 02.06.15 09:57: liquibase: /tmp/test.changeset.xml::4::test: Successfully released change log lock
Unexpected error running Liquibase: Preconditions Failed

SEVERE 02.06.15 09:57: liquibase: /tmp/test.changeset.xml::4::test: Preconditions Failed
liquibase.exception.MigrationFailedException: Migration failed for change set /tmp/test.changeset.xml::4::test:
     Reason: 
          /tmp/test.changeset.xml : No rows returned from SQL Precondition

    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:485)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:43)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73)
    at liquibase.Liquibase.update(Liquibase.java:200)
    at liquibase.integration.commandline.Main.doMigration(Main.java:1044)
    at liquibase.integration.commandline.Main.run(Main.java:175)
    at liquibase.integration.commandline.Main.main(Main.java:94)
Caused by: liquibase.exception.PreconditionFailedException: Preconditions Failed
    at liquibase.precondition.core.AndPrecondition.check(AndPrecondition.java:51)
    at liquibase.precondition.core.PreconditionContainer.check(PreconditionContainer.java:201)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:471)
    ... 6 more

1 个答案:

答案 0 :(得分:2)

可能不是最简单或最优雅的方式,但您可以使用<customPrecondition>

您可以编写自己的类来实现CustomPrecondition

查看此示例类:ExampleCustomPrecondition作为参考。

您手头有一个Database对象,可以自己查询数据库并抛出{ "_id" : { "$oid" : "50906d7fa3c412bb040eb577" }, "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb578" }, "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb579" }, "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb57a" }, "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb57b" }, "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb57c" }, "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb57d" }, "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 } { "_id" : { "$oid" : "50906d7fa3c412bb040eb57e" }, "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }

(或者创建一个Jira-Ticket来提交更改此行为的请求或添加实现CustomPreconditionFailedException preCondition检查的方法。)

相关问题