升级到 grails 3 后的 grails 数据库迁移插件问题
grails database migration plugin problems after upgrade to grails 3
我使用以前版本的 grails-database-migration 插件有一段时间了,从来没有遇到过任何大问题。然而,最近我将整个项目升级到 grails 3.0.9 并做了一些额外的开发,行为如下:
将当前产品数据库结构导入本地机器(该数据库副本没有最新的更改和新实体)
执行:grails -Dgrails.env=staging dbm-gorm-diff changlog.xml
此时我期望的是包含现有实体和新实体的所有更改的新 changlog.xml 文件。
我得到的:
- 新定义的实体自动添加到数据库中。
- changlog.xml中的改动只包括对已有表的改动,例如:
另外,如果我尝试 运行ning grails -Dgrails.env=staging run-app
ERROR grails.boot.GrailsApp - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springLiquibase_dataSource': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: liquibase.integration.spring.SpringLiquibase.createDatabase(Ljava/sql/Connection;Lliquibase/resource/ResourceAccessor;)Lliquibase/database/Database;
FAILURE: Build failed with an exception.
- What went wrong: Execution failed for task ':bootRun'.
Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java''
finished with non-zero exit value 1
...
...
- Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. | Error Failed to start server (Use --stacktrace to see the full trace)
这是我的部分 application.yml
dataSource:
pooled: true
url: jdbc:mysql://127.0.0.1:3306/triz?useUnicode=yes&characterEncoding=UTF-8
driverClassName: "com.mysql.jdbc.Driver"
jmxExport: true
username: root
password: password
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2
environments:
development:
dataSource:
dbCreate: create
# url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
staging:
dataSource:
url: jdbc:mysql://127.0.0.1:3306/triz_staging?useUnicode=yes&characterEncoding=UTF-8
和gradle.build
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
// classpath 'com.bertramlabs.plugins:less-asset-pipeline:2.6.7'
classpath "org.grails.plugins:hibernate:4.3.10.5"
classpath 'org.grails.plugins:database-migration:2.0.0.RC4'
}
}
...
...
dependencies {
...
compile 'org.liquibase:liquibase-core:3.3.2'
runtime 'org.grails.plugins:database-migration:2.0.0.RC4'
}
更新
我有另一种方法来解决这个问题:
我的计划是根据我当前的产品数据库生成一个变更日志,然后为我所做的更改生成一个差异。听起来简单明了;但是,它没有按预期进行。这是我所做的:
- 倾销产品数据库
- 删除了 liquibase 表
- 运行:
grails dbm-generate-changelog changelog-init.xml --add
此时,我希望 changelog-init.xml 包含数据库的当前状态。但是,它首先应用了基于我的模型的更改,然后尝试生成差异。最终,我得到了一个更新日志,其中包括我的整个现有数据库,其中包含从 gorm 应用的更改。
我做错了什么?
额外观察
看起来,每当我尝试 运行 任何与迁移相关的命令时,grails 都会应用之前的所有更改,甚至通过我的配置说:
staging:
dataSource:
dbCreate: ~
url: jdbc:mysql://127.0.0.1:3306/triz_staging?useUnicode=yes&characterEncoding=UTF-8
properties:
jmxEnabled: true
还尝试完全删除 dbCreate
。什么都没变...
我完成了,不知道下一步该去哪里!!!
好吧,这是一笔交易...
我不确定这是否是真正的原因,但我所做的只是将数据源配置从 application.yml
移动到 application.groovy
,然后一切恢复正常。
我很乐意听到想法。
谢谢。
我使用以前版本的 grails-database-migration 插件有一段时间了,从来没有遇到过任何大问题。然而,最近我将整个项目升级到 grails 3.0.9 并做了一些额外的开发,行为如下:
将当前产品数据库结构导入本地机器(该数据库副本没有最新的更改和新实体)
执行:
grails -Dgrails.env=staging dbm-gorm-diff changlog.xml
此时我期望的是包含现有实体和新实体的所有更改的新 changlog.xml 文件。
我得到的:
- 新定义的实体自动添加到数据库中。
- changlog.xml中的改动只包括对已有表的改动,例如:
另外,如果我尝试 运行ning grails -Dgrails.env=staging run-app
ERROR grails.boot.GrailsApp - Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springLiquibase_dataSource': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: liquibase.integration.spring.SpringLiquibase.createDatabase(Ljava/sql/Connection;Lliquibase/resource/ResourceAccessor;)Lliquibase/database/Database; FAILURE: Build failed with an exception.
- What went wrong: Execution failed for task ':bootRun'. Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 ...
...
- Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. | Error Failed to start server (Use --stacktrace to see the full trace)
这是我的部分 application.yml
dataSource:
pooled: true
url: jdbc:mysql://127.0.0.1:3306/triz?useUnicode=yes&characterEncoding=UTF-8
driverClassName: "com.mysql.jdbc.Driver"
jmxExport: true
username: root
password: password
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2
environments:
development:
dataSource:
dbCreate: create
# url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
staging:
dataSource:
url: jdbc:mysql://127.0.0.1:3306/triz_staging?useUnicode=yes&characterEncoding=UTF-8
和gradle.build
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
// classpath 'com.bertramlabs.plugins:less-asset-pipeline:2.6.7'
classpath "org.grails.plugins:hibernate:4.3.10.5"
classpath 'org.grails.plugins:database-migration:2.0.0.RC4'
}
}
...
...
dependencies {
...
compile 'org.liquibase:liquibase-core:3.3.2'
runtime 'org.grails.plugins:database-migration:2.0.0.RC4'
}
更新 我有另一种方法来解决这个问题: 我的计划是根据我当前的产品数据库生成一个变更日志,然后为我所做的更改生成一个差异。听起来简单明了;但是,它没有按预期进行。这是我所做的:
- 倾销产品数据库
- 删除了 liquibase 表
- 运行:
grails dbm-generate-changelog changelog-init.xml --add
此时,我希望 changelog-init.xml 包含数据库的当前状态。但是,它首先应用了基于我的模型的更改,然后尝试生成差异。最终,我得到了一个更新日志,其中包括我的整个现有数据库,其中包含从 gorm 应用的更改。
我做错了什么?
额外观察
看起来,每当我尝试 运行 任何与迁移相关的命令时,grails 都会应用之前的所有更改,甚至通过我的配置说:
staging:
dataSource:
dbCreate: ~
url: jdbc:mysql://127.0.0.1:3306/triz_staging?useUnicode=yes&characterEncoding=UTF-8
properties:
jmxEnabled: true
还尝试完全删除 dbCreate
。什么都没变...
我完成了,不知道下一步该去哪里!!!
好吧,这是一笔交易...
我不确定这是否是真正的原因,但我所做的只是将数据源配置从 application.yml
移动到 application.groovy
,然后一切恢复正常。
我很乐意听到想法。
谢谢。