2016-04-12 19 views
0

grailsデータベース移行プラグインのchangelog.groovyを作成して、idsの範囲に行が存在しない場合は、テーブルに行を挿入する方法を教えてください。例えば。grailsデータベース移行プラグイン - 行を条件付きで挿入する方法

cool_stuffテーブルには があります。 some_other_id |

cool_stuffテーブルにデータが入力されます。 cool_stuff IDの範囲、1所与 - 2000、私がしたい:

  1. 反復IDSを介して、それはdoesnの場合cool_stuff IDとsome_other_id = 2の組合せは、
  2. が存在するかどうかcool_stuffテーブルを照会「T "はcool_stuff" テーブルにrecoreds既にThrereある
+0

これは、changelog.groovyではなくchangelog.xmlで行うことができますか? – ddelponte

答えて

0
  1. 2 =、存在cool_stuff IDを持つ行を挿入しsome_other_id。
  2. あなたは "cool_stuff.id" と "some_other_id == 2"

ので、あなたは次のようにしたいですかという記録のconbinationが必要ですか?

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) 
       } 
      } 
     } 
    } 
} 
関連する問題