How can I use Grails DB Migration with Spring Security and the default DB, H2?

别说谁变了你拦得住时间么 提交于 2019-12-24 03:27:05

问题


I'm running into an exception generated by the Grails Database Migration plugin.

In JIRA, it says this is a bug won't fixed.

But, I want to use Spring Security with the default DB, H2, because it's easy to use to make a simple application.

How can I avoid this bug except changing DB to MYSQL or ORACLE?



UPDATE

Following the solution in JIRA, I changed the name of the column which has made the error, password, to passwd in User.groovy.

A part of User.groovy shown below.

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

After I run the command gradle grails-dbm-gorm-diff -PgrailsArgs="2015-5-23-first-diff.groovy --add", I got error like below.

| 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

回答1:


As stated in the JIRA the solution is as follows:

This is caused by the backticks that escape the 'password' field in the generated user class. 'password' is a reserved word in some databases so the script escapes it by default since if it's not a reserved word it has no effect. So either remove "password column: 'password'" if it isn't a reserved word, or change it to a non-reserved word without a backtick if it is, e.g. "password column: 'passwd'"

This means you need to edit your User domain class.

Update Your domain class should look like this:

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'
}



回答2:


I just ran into this problem as well, but was able to fix it by changing:

grails dbm-gorm-diff ...

to

grails test dbm-gorm-diff ...

My development environment (default) is set to use an H2 database, but my test environment (the one this command was intended for) was set to use postgres.



来源:https://stackoverflow.com/questions/30411603/how-can-i-use-grails-db-migration-with-spring-security-and-the-default-db-h2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!