如何在Spring Security和默认DB,H2中使用Grails DB Migration?

时间:2015-05-23 10:29:27

标签: grails spring-security h2

我遇到了 Grails数据库迁移插件生成的异常。

JIRA中,它说这是一个不会修复的错误。

但是,我想将 Spring Security 与默认数据库 H2 一起使用,因为它很容易用来制作一个简单的应用程序。

除了将数据库更改为 MYSQL ORACLE 之外,我该如何避免此错误?

更新

在JIRA中的解决方案之后,我在User.groovy中将发生错误的列的名称password更改为passwd

User.groovy的一部分如下所示。

    String username
    String password
    static transients = ['springSecurityService']
    static constraints = {
        username blank: false, unique: true
        password blank: false
    }
    static mapping = {
        password column: '`passwd`'
    }

运行命令gradle grails-dbm-gorm-diff -PgrailsArgs="2015-5-23-first-diff.groovy --add"后,我收到如下错误。

| Starting dbm-gorm-diff
liquibase.exception.DatabaseException: org.h2.jdbc.JdbcSQLException: 列 "PASSWD" が見つかりません
Column "PASSWD" not found; SQL statement:
SELECT passwd FROM USER WHERE 1 = 0 [42122-176]
    at liquibase.snapshot.jvm.JdbcDatabaseSnapshotGenerator.createSnapshot(JdbcDatabaseSnapshotGenerator.java:251)
    at liquibase.snapshot.DatabaseSnapshotGeneratorFactory.createSnapshot(DatabaseSnapshotGeneratorFactory.java:69)
    at liquibase.diff.Diff.compare(Diff.java:71)
    at grails.plugin.databasemigration.GormDiff.compare(GormDiff.groovy:45)
    at grails.plugin.databasemigration.ScriptUtils.createAndPrintFixedDiff(ScriptUtils.groovy:244)
    at DbmGormDiff$_run_closure1_closure2_closure3.doCall(DbmGormDiff:53)
    at grails.plugin.databasemigration.MigrationUtils.executeInSession(MigrationUtils.groovy:133)
    at DbmGormDiff$_run_closure1_closure2.doCall(DbmGormDiff:50)
    at grails.plugin.databasemigration.ScriptUtils.executeAndWrite(ScriptUtils.groovy:104)
    at DbmGormDiff$_run_closure1.doCall(DbmGormDiff:49)
    at org.grails.launcher.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:57)
    at org.grails.launcher.context.DelegatingGrailsLaunchContext.launch(DelegatingGrailsLaunchContext.java:409)
    at org.grails.launcher.ReflectiveGrailsLauncher.launch(ReflectiveGrailsLauncher.java:32)
    at org.grails.launcher.InProcessGrailsLauncher.launch(InProcessGrailsLauncher.java:27)
    at org.grails.launcher.Main.run(Main.java:62)
    at org.grails.launcher.Main.main(Main.java:38)
Caused by: org.h2.jdbc.JdbcSQLException: 列 "PASSWD" が見つかりません
Column "PASSWD" not found; SQL statement:
SELECT passwd FROM USER WHERE 1 = 0 [42122-176]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:344)
    at org.h2.message.DbException.get(DbException.java:178)
    at org.h2.message.DbException.get(DbException.java:154)
    at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:148)
    at org.h2.command.dml.Select.prepare(Select.java:831)
    at org.h2.command.Parser.prepareCommand(Parser.java:248)
    at org.h2.engine.Session.prepareLocal(Session.java:442)
    at org.h2.engine.Session.prepareCommand(Session.java:384)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1188)
    at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:75)
    at liquibase.snapshot.jvm.JdbcDatabaseSnapshotGenerator.isColumnAutoIncrement(JdbcDatabaseSnapshotGenerator.java:842)
    at liquibase.snapshot.jvm.JdbcDatabaseSnapshotGenerator.readColumns(JdbcDatabaseSnapshotGenerator.java:369)
    at liquibase.snapshot.jvm.JdbcDatabaseSnapshotGenerator.createSnapshot(JdbcDatabaseSnapshotGenerator.java:244)
    ... 15 more
:grails-dbm-gorm-diff FAILED

2 个答案:

答案 0 :(得分:4)

如JIRA所述,解决方案如下:

  

这是由逃避密码的反引号造成的。在   生成的用户类。 '密码'在某些人中是保留的词   数据库因此默认情况下脚本会将其转义,因为它不是a   保留字它没有效果。所以要么删除"密码列:   ' password'"如果它不是保留字,或将其改为a   没有反引号的非保留字,如果是,例如, "密码栏:   ' passwd的'"

这意味着您需要修改User域类。

更新您的域类应如下所示:

String username
String password
static transients = ['springSecurityService']
static constraints = {
    username blank: false, unique: true
    password blank: false
}
static mapping = {
    // NOTICE THE FOLLOWING FIELD DOES NOT HAVE BACKTICKS!
    password column: 'passwd'
}

答案 1 :(得分:0)

我也遇到了这个问题,但是能够通过改变来修复它:

grails test dbm-gorm-diff ...

SELECT TIMESTAMP(your_ts_str + "01") AS ts

我的开发环境(默认)设置为使用H2数据库,但我的测试环境(此命令所针对的环境)设置为使用postgres。