我将如何为grails数据库迁移插件编写一个changelog.groovy,如果一系列ID尚不存在行,该插件会将行插入表中?例如。

cool_stuff表具有
id | some_other_id |

cool_stuff表中填充了数据。给定一系列cool_stuff id,即1-2000,我想:

  • 遍历id,查询cool_stuff表以查看cool_stuff id和some_other_id = 2的组合是否存在
  • 如果不存在,则插入带有cool_stuff id和some_other_id = 2
  • 的行

    最佳答案

  • Threre已在“cool_stuff”表上重新记录。
  • 您需要组合“cool_stuff.id”和“some_other_id == 2”的记录

  • 所以,你想喜欢吗?
    table of "cool_stuff"
    
    FROM:
    id  | some_other_id
    ----|---------------
    1   | 2
    2   | 1
    3   | 2
    4   | 1
    
    TO:
    id  | some_other_id
    ----|---------------
    1   | 2
    2   | 1
    3   | 2
    4   | 1
    2   | 2
    4   | 2
    

    这是正确的吗??
    我想跟随我。
    databaseChangeLog = {
        changeSet(author: "koji", id: "123456789") {
            grailsChange {
                change {
                    CoolStuff.list().findAll {
                        it.someOtherId != 2
                    }.each{
                        // save new instance
                        new CoolStuf(id: it.id, someOtherId:2).save(flush:true)
                    }
                }
            }
        }
    }
    

    10-08 08:38
    查看更多