2013-04-30 50 views
2

我們開始在Grails服務器啓動時運行Liquibase遷移。我們希望在數據源上使用dbCreate ='validate',以確保數據庫和對象模型保持同步。我們的數據源配置目前看起來是這樣的:驗證遷移後的GORM模型

development { 
    dataSource_dbm { 
     dbCreate = '' 
     url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;MODE=Oracle" 
     username = 'sa' 
     password = '' 
    } 
    dataSource { 
     dbCreate = 'validate' 
     url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;MODE=Oracle" 
     username = 'sa' 
     password = '' 
    } 
} 

grails.plugin.databasemigration.dbm.updateOnStart = true 
grails.plugin.databasemigration.dbm.updateOnStartFileNames = ['changelog.groovy'] 
我們Config.groovy中

。這導致一些錯誤,在啓動時:

Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'transactionManager': 
Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory': 
Invocation of init method failed; 
nested exception is org.hibernate.HibernateException: 
Missing table: ... 

看起來好像是創建的默認數據源和liquibase遷移之前應用的dbCreate的政策正在運行。

如果我們註釋掉第二個dataSource,我們會看到所有的遷移確實在啓動時應用。

有沒有辦法配置我們的數據源或databasemigration插件,以便遷移在驗證之前運行?或者我們不得不放棄服務器啓動時的驗證,並使用dbCreate =''的數據源,並依賴運行時錯誤來解決問題?

+0

同步能力不是數據庫遷移插件的最終目標嗎?摘錄'[這裏](http://grails.org/doc/latest/guide/single.html#dataSource)'建議從開始使用插件或Liquibase時使用'dbCreate'移動,並且當你有一個相對穩定的架構。相反,我們在插件中提供了'before'和'after'回調函數來處理'database'而不是'datasource'。 – dmahapatro 2013-04-30 21:17:16

回答

1

您可以自己驗證模式,就像schema-export腳本可以讓您捕獲Hibernate在使用「create-drop」時運行的SQL一樣。如果你

import org.hibernate.tool.hbm2ddl.SchemaValidator 

def grailsApplication 

.... 

def ctx = grailsApplication.mainContext 
def sessionFactoryFactory = ctx.getBean('&sessionFactory') 
def sessionFactory = ctx.getBean('sessionFactory') 
def configuration = sessionFactoryFactory.configuration 
def settings = sessionFactory.settings 

def validator = new SchemaValidator(configuration, settings) 
validator.validate() 

這可能使一個很好的補充插件:如果你有機會到春節ApplicationContext(這裏我用的依賴注入的grailsApplication豆得到它),那麼你可以自舉運行此在http://jira.grails.org/browse/GPDATABASEMIGRATION創建一個增強請求我會考慮在即將發佈的版本中添加它。