2015-10-20 48 views
2

我使用Grails 3.0.7和數據庫遷移插件。我有超級簡單的遷移,我不能去工作:Grails 3 with database-migration插件:類[com.mypackage.security.RequestMap]上的方法在Grails應用程序之外使用

databaseChangeLog = { 
    changeSet(id: '20150926BaseSecurityConfig', author: 'me') { 
     grailsChange { 
      change { 
        new RequestMap('/home', 'permitAll').save(failOnError: true, flush: true) 
      } 
     } 
    } 
} 

這就是它的全部。我有類似的遷移工作在運行Grails 2.3.7的另一個項目中,沒有任何問題。在這裏,我得到這個異常:

Caused by: java.lang.IllegalStateException: Method on class [com.mysite.security.RequestMap] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly. 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 

另外,如果我把new RequestMapBootStrap.groovy,它保存得很好。我不確定這裏有什麼問題。

+0

你能提供你的'DataSource.groovy'配置? –

+0

另外,你的hibernate插件版本是什麼? –

+0

你使用grails-3的插件嗎?請記住,在頁面'https:// grails.org/plugins /'是grails 1和2的插件。 爲grails-3準備的插件在這裏是https:// bintray.com/grails/plugins' –

回答

1

插件需要認真修改,大部分聲明不起作用。

始終使用XML和原生SQL。它完美地工作:

<?xml version="1.0" encoding="UTF-8"?> 

<databaseChangeLog 
     xmlns="http://www.liquibase.org/xml/ns/dbchangelog" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" 
     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd 
     http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"> 

    <changeSet author="me" id="201510201650_ins_data_into_foo_bar"> 
     <sql> 
      INSERT INTO foo.bar(code, name) VALUES(1, 'demo'); 
      INSERT INTO foo.bar(code, name) VALUES(2, 'demo2'); 
     </sql> 
     <rollback> 
      DELETE FROM foo.bar WHERE code BETWEEN 1 AND 2; 
     </rollback> 
    </changeSet> 

</databaseChangeLog> 
+0

是的,那就是我最終做的。不幸的是,插件仍然處於這樣糟糕的狀態。 – cloudwalker

0

我滔滔不絕地使用SQL語句進行數據庫遷移。

databaseChangeLog = { 

     changeSet(author: "me", id: "20150926BaseSecurityConfig") { 

      preConditions(onFail: 'MARK_RAN', onFailMessage: 'RequestMap already exists'){ 
       sqlCheck(expectedResult: '0', "SELECT count(*) FROM request_map WHERE url = '/home' and configAttribute = 'permitAll'") 
      } 

      sql(""" 
        INSERT INTO request_map(url, configAttribute) 
        VALUES ('/home', 'permitAll') 
      """) 
     } 
    } 
0
<?xml version="1.0" encoding="UTF-8"?>  
<databaseChangeLog 
     xmlns="http://www.liquibase.org/xml/ns/dbchangelog" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" 
     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd 
     http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"> 

    <changeSet author="me" id="20150926BaseSecurityConfig"> 
     <sql> 
      INSERT INTO request_map(url, configAttribute) 
    SELECT v.url, v.c FROM (SELECT '/home' as url, 'permitAll' as c) as v 
      WHERE NOT EXISTS(SELECT m.* FROM request_map as m 
         WHERE m.url = v.url AND m.configAttribute = v.c); 
     </sql> 
     <rollback> 
      DELETE FROM request_map WHERE url LIKE '/home' AND configAttribute LIKE 'permitAll'; 
     </rollback> 
    </changeSet> 

</databaseChangeLog> 
相關問題