参考地址:
描述:个人理解,数据库迁移插件就是将数据库建表策略交由Liquibase来维护,默认是hibernate的。
在build.gradle中引入
buildscript { ext { grailsVersion = project.grailsVersion } repositories { 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.8.2" classpath "org.grails.plugins:hibernate4:5.0.5" //数据库迁移脚本依赖包 classpath 'org.grails.plugins:database-migration:2.0.1' } } dependencies { //mysql驱动 compile "mysql:mysql-connector-java:5.1.38" //数据库迁移所依赖包 compile 'org.grails.plugins:database-migration:2.0.1' compile 'org.liquibase:liquibase-core:3.5.3' } //指定配置文件所在位置 sourceSets { main { resources { srcDir 'grails-app/migrations' } } }
常用命令:
//创建groovy配置文件,也可以创建xml格式的配置 dbm-create-changelog changelog.groovy //创建xml数据库表配置 dbm-gorm-diff a.xml //创建数据库groovy格式的配置 dbm-gorm-diff changelog.groovy # 创建一个1.groovy的脚本,并且添加到默认的changelog.groovy中 dbm-gorm-diff 1.groovy --add dbm-update
注:配置数据库迁移后需要修改默认的application.yml中的dbCreate: none
application.yml配置需要注意把dbCreate:none
--- hibernate: cache: queries: false use_second_level_cache: true use_query_cache: false region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory' dataSource: pooled: true jmxExport: true driverClassName: com.mysql.jdbc.Driver username: root password: root environments: development: dataSource: dbCreate: none //建表交由Liquibase管理,这里设置为none url: jdbc:mysql://localhost:3306/dbc test: dataSource: dbCreate: update url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE production: dataSource: dbCreate: update url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE 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 # TRANSACTION_READ_COMMITTED --- --- grails: profile: web codegen: defaultPackage: untitled1 spring: transactionManagement: proxies: false info: app: name: '@info.app.name@' version: '@info.app.version@' grailsVersion: '@info.app.grailsVersion@' spring: groovy: template: check-template-location: false --- grails: mime: disable: accept: header: userAgents: - Gecko - WebKit - Presto - Trident types: all: '*/*' atom: application/atom+xml css: text/css csv: text/csv form: application/x-www-form-urlencoded html: - text/html - application/xhtml+xml js: text/javascript json: - application/json - text/json multipartForm: multipart/form-data pdf: application/pdf rss: application/rss+xml text: text/plain hal: - application/hal+json - application/hal+xml xml: - text/xml - application/xml urlmapping: cache: maxsize: 1000 controllers: defaultScope: singleton converters: encoding: UTF-8 views: default: codec: html gsp: encoding: UTF-8 htmlcodec: xml codecs: expression: html scriptlets: html taglib: none staticparts: none endpoints: jmx: unique-names: true
此外如果需要配置自动建表,可在grails-app/conf先创建application.groovy,添加配置
//每次启动项目自动更新,比如你创建了一个domain,等再次启动的时候会在数据库创建表 grails.plugin.databasemigration.updateOnStart = true //指定changelog.groovy文件 grails.plugin.databasemigration.updateOnStartFileName = 'changelog.groovy'
我有一个UserInfo的域类,他生成的changelog.groovy如下:
databaseChangeLog = { changeSet(author: "admin (generated)", id: "1507886702775-1") { createTable(tableName: "user_info") { column(autoIncrement: "true", name: "id", type: "BIGINT") { constraints(primaryKey: "true", primaryKeyName: "user_infoPK") } column(name: "version", type: "BIGINT") { constraints(nullable: "false") } column(name: "sex", type: "VARCHAR(255)") { constraints(nullable: "false") } column(name: "username", type: "VARCHAR(255)") { constraints(nullable: "false") } } } changeSet(author: "admin (generated)", id: "1507886702775-2") { addUniqueConstraint(columnNames: "username", constraintName: "UC_USER_INFOUSERNAME_COL", tableName: "user_info") } changeSet(author: "admin (generated)", id: "1507886702775-3") { createSequence(sequenceName: "hibernate_sequence") } }
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!