Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert rows using liquibase in yaml format

I am trying to define a changeSet to insert two rows in a table using liquibase. In order to do this, I wrote the following changeSet:

- changeSet:
  id: 1.0/7
  author: stivlo
  changes:
    -insert:
        tableName: my_table
        columns:
        - column:
            name: id
            value: "1"
        - column:
            name: name
            value: "One"
    -insert:
        tableName: my_table
        columns:
        - column:
            name: id
            value: "2"
        - column:
            name: name
            value: "Two"

When I start my Spring Boot application, the changeset is executed, but the rows are not inserted.

In DATABASECHANGELOG table I find a raw saying that the migration was executed, but the description is "Empty", as to signify that liquibase could not see any changes in the migration.

How do I fix my yaml in order to be able to insert those two rows?


P.S. I've managed to solve my problem embedding SQL statements instead of using a liquibase insert.

- changeSet:
  id: 1.0/7
  author: stivlo
  changes:
    - sql:
        sql: insert into my_table (id, name) values (1, "One")
    - sql:
        sql: insert into my_table (id, name) values (2, "Two")

This works, but I am still interested to know how to properly define a liquibase insert. Thank you.

like image 745
stivlo Avatar asked May 02 '15 19:05

stivlo


2 Answers

I've struggled with the same issue and i want to share some thoughts.
First of all, about your particular issue - i think you had a bit malformed .yml, though it may be just stackoverflow formatting. It should look like this:

- changeSet:
    id: 1.0/7
    author: stivlo
    comment: "Some comments go here"
    changes:
     - insert:
         tableName: my_table
         columns:
         - column:
             name: id
             value: "1"
         - column:
             name: name
             value: "One"
     - insert:
         tableName: my_table
         columns:
         - column:
             name: id
             value: "2"
         - column:
             name: name
             value: "Two"

Please notice that whole changeset content is indented from the beginning of the - changeSet block
Also all - marks should be followed by whitespaces, otherwise you either see nothing or error in some cases
Btw i hope you didn't forget databaseChangeLog block on top of the file?

Something about troubleshooting liquibase issues:

(Credit to this post)

First, as Mattias B mentioned, there is truly helpful "updateSQL" option. I used CLI,

  java -jar liquibase.jar --driver=org.h2.Driver --classpath=h2-1.3.170.jar --changeLogFile=yourchangelogpath/changelog.yaml --url=jdbc:h2:D:/Variaciok/db/variation --username=sa --password= updateSQL

You can move almost all options to file , like

driver: org.h2.Driver
classpath: h2-1.3.170.jar
changeLogFile: yourchangelogpath/changelog.yaml
url: jdbc:h2:D:/db/variation
username: sa
password:

so your command look like

java -jar liquibase.jar --defaultsFile=our_database.properties updateSQL

or if you named file liquibase.properties you can even skip --defaultsFile so it is just

java -jar liquibase.jar updateSQL

Second, if you are not seeing any errors either in your runner or in CLI, ther problem is almost certain is malformed .yml

Hope it helps!

like image 160
alex_java_kotlin Avatar answered Sep 19 '22 09:09

alex_java_kotlin


I know its a old post but other people might find this usefull

I would suggest using liquibase "updateSQL" in order to track down the issue by showing the generated SQL.

like image 40
Mattias B Avatar answered Sep 21 '22 09:09

Mattias B