Insert rows using liquibase in yaml format
Asked Answered
W

3

12

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.

Winwaloe answered 2/5, 2015 at 19:35 Comment(0)
G
10

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 looks 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, the problem is almost certainly in malformed .yml

Hope it helps!

Gaillardia answered 8/9, 2016 at 17:3 Comment(0)
C
1

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.

Chronicles answered 29/1, 2016 at 8:49 Comment(2)
This should be a comment.Incarnate
@ρяσѕρєяK I think user does not have enough mod points to make commentsHomosexuality
R
0

Try to set ddl-auto to none

spring.jpa.hibernate.ddl-auto=none
Rupiah answered 13/7, 2024 at 21:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.