简体   繁体   English

使用Yaml格式的liquibase插入行

[英]Insert rows using liquibase in yaml format

I am trying to define a changeSet to insert two rows in a table using liquibase. 我正在尝试定义一个changeSet以使用liquibase在表中插入两行。 In order to do this, I wrote the following changeSet: 为了做到这一点,我编写了以下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. 当我启动Spring Boot应用程序时,将执行变更集,但不会插入行。

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. 在DATABASECHANGELOG表中,我发现一个原始的说法是已执行迁移,但是描述为“ Empty”,以表示liquibase无法看到迁移中的任何更改。

How do I fix my yaml in order to be able to insert those two rows? 如何修复我的Yaml,以便能够插入这两行?


PS I've managed to solve my problem embedding SQL statements instead of using a liquibase insert. PS我设法解决了嵌入SQL语句而不是使用liquibase插入的问题。

- 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. 这可行,但是我仍然想知道如何正确定义liquibase插入。 Thank you. 谢谢。

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. 首先,关于您的特定问题-我认为您的.yml格式不正确,尽管它可能只是stackoverflow格式。 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 请注意,整个变更集内容从- changeSet块的开头缩进
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? 顺便说一句,我希望您不要忘记文件顶部的databaseChangeLog块?

Something about troubleshooting liquibase issues: 有关解决liquibase问题的方法:

(Credit to this post ) 此职位的积分

First, as Mattias B mentioned, there is truly helpful "updateSQL" option. 首先,正如Mattias B提到的那样,有一个非常有用的“ updateSQL”选项。 I used CLI , 我使用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 您可以将几乎所有选项移至file,例如

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 或者,如果您将文件命名为liquibase.properties您甚至可以跳过--defaultsFile,因此它只是

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 其次,如果您在运行程序或CLI中均未看到任何错误,则几乎可以确定问题是.yml格式错误

Hope it helps! 希望能帮助到你!

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. 我建议使用liquibase“ updateSQL”,以通过显示生成的SQL来跟踪问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM