11

Is it possible to use Precondition in YAML i didn't find any sources except this page http://www.liquibase.org/documentation/yaml_format.html

But I am looking for the equivalent of :

<changeSet id="addColumn-example">
  <preConditions onFail="MARK_RAN">
     <columnExists schemaName="earls" 
           tableName="category" columnName="display_name"/>
  </preConditions>
  <dropColumn columnName="display_name" schemaName="earls" tableName="category"/>
</changeSet>  

So my natural translation will be :

changeSet:
  id: addColumn-example
  author: francis
  preConditions:
    - columnExist:
      schemaName: earls
      tableName: category
      columnName: display_name                    
  changes:
    - addColumn:
      columns:
        - column:
          name: display_name
          type: varchar(100)

But i am missing onFail...

Cifren
  • 364
  • 1
  • 4
  • 15
  • Did you ever figure out an answer for this? I'm to do the same thing, but can't figure out how to do onFail: MARK_RAN. – Matt Raible Oct 03 '14 at 20:35
  • For what I remember from this issue (in 2014), it was simply not possible with YAML, I had to do it with XML. But maybe with the new version it should be working now. – Cifren Mar 17 '16 at 10:14

5 Answers5

27

this topic is poor documented, but after many tries... you can write something like this:

databaseChangeLog:
  - changeSet:
      id: 1
      author: pazfernando
      preConditions:
        - onFail: MARK_RAN
        - tableExists:
            schemaName: sa
            tableName: PROVEEDORBIENSERVICIO
      changes:
        - renameTable:
            newTableName: PROVEEDORBIENSERVICIO
            oldTableName: PROVEEDORSERVICIO
            schemaName: sa

Here is another example with the sqlCheck:

preConditions:
  - onFail: CONTINUE
  - onError: CONTINUE
  - sqlCheck:
      expectedResult: 0
      sql: select count(*) from oss_organization where Status is null
  - sqlCheck:
      expectedResult: 0
      sql: select count(*) from oss_organization where Type is null
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
pazfernando
  • 577
  • 6
  • 15
7

The following seems to work:

databaseChangeLog:
  - changeSet:
      id: 1
      author: mraible
      preConditions:
        onFail: MARK_RAN
        not:
          sequenceExists:
            schemaName: public
            sequenceName: hibernate_sequence
      changes:
      - createSequence:
          sequenceName: hibernate_sequence
Matt Raible
  • 8,187
  • 9
  • 61
  • 120
  • actually this is the correct syntax. It is a double negation. `*Exists` is returning `true`, but the `MARK_RAN` is `onFail` so the `not:` is needed to negate the response to `false` to meet `onFail` condition. Sadly there is no `onSuccess` :( – Markus Mar 20 '23 at 16:02
2

Syntax for sqlCheck condition:

databaseChangeLog:
- changeSet:
    id: changeSet-id
    author: myName
    preConditions:
      - onFail: MARK_RAN
      - sqlCheck:
          expectedResult: 1
          sql: "select count(*) from foo where some-condition"
    changes:
      - sql: "your sql script"
G.O.
  • 470
  • 1
  • 3
  • 16
1

It is probably something that didn't work with Liquibase 3.1.x but should work in the just-released 3.2.0 version. Your example changeSet should be right.

Nathan Voxland
  • 15,453
  • 2
  • 48
  • 64
0

DBMS precondition

databaseChangeLog:
  - changeSet:
      id: 1
      author: yourname
      dbms: oracle,h2
      changes:
        - sql: "your sql script"

As you can see, this is not a real precondition, but I found it to be working similar: the changeset is ignored if the database type is not matching.

Istvan Devai
  • 3,962
  • 23
  • 21