Grails中的数据库迁移有什么好的工作流程? [英] What is a good workflow for database migration in Grails?

查看:164
本文介绍了Grails中的数据库迁移有什么好的工作流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数据库迁移 grails插件进行数据库迁移。当我第一次启动我的Grails应用程序时,会自动创建所有数据库表。在我的DataSource.groovy中的生产设置是:

 生产{


dataSource {
dbCreate =update
url =jdbc:mysql:// localhost / myapp?useUnicode = yes& characterEncoding = UTF-8
username =test
password =test
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
properties {
validationQuery =select 1
testWhileIdle = true
timeBetweenEvictionRunsMillis = 60000
}


在我的config.groovy中,我设置了

  grails.plugin.databasemigration.updateOnStart = true 
grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy' ]

当我将属性添加到我的域类时,我需要调整changelog文件。
在这种情况下执行数据库迁移的最佳方式是什么?当我添加或删除列时,我必须执行的步骤是什么? c $ c> dbcreate 指令是不建议用于生产用途:


您也可以完全删除 dbCreate 设置,推荐一旦你的模式相对稳定,并且当你的应用程序和数据库被部署到生产环境中。


所以请记住,你会需要删除这个(或者设置为'none')。




初始基准工作流程




  1. 定义当前状态

  2. 从更改日志创建数据库或标记为最新

  3. 设置配置选项

第一步是获取 changelog 来反映当前状态。如果你有一个现有的数据库,你想用它来定义基线。否则,使用GORM来定义表。



这些命令将为您的数据库生成一个基线。此外,我选择使用groovy DSL格式而不是liquibase XML,因为可读性

现有数据库



如果您已经有一个包含数据的生产数据库,棘手。您将需要从您的grails环境访问数据库或其副本。如果您操作副本,则需要将更新应用回生产环境(并可能将其作为计划中断进行管理)。



该命令是:

  grails [环境] dbm-generate-changelog changelog.groovy 

...其中 environment 可选地指定数据库定义为的dev / test / prod / custom环境。



接下来,将数据库标记为与更新日志相符的'最新':

然后将数据库重新应用到生产环境中。 grails [environment] dbm-changelog-sync 

,如果有必要的话。



新数据库



如果您没有现有的数据库(或者不care):

  grails dbm-generate-gorm-changelog changelog.groovy 

然后,从changelog创建数据库:

  grails [环境] dbm-update 



配置



您已经正确得到了选项设置:

pre $ grails.plugin.databasemigration.updateOnStart = true
grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy']

这些选项仅表示插件会尝试将更改应用于数据库在应用程序启动时。
$ b




开发工作流程




  1. 对域进行更改
  2. 生成更改日志标识差异
  3. (备份和)更新数据库


  4. 所以现在你已经有了一个最新的数据库,并且你正在粉碎对域类的更改,添加新的并更改验证属性。



    每次要记录更改时,都希望将您的GORM类与数据库中存在的类进行比较,并创建一个新的更改日志文件以记录差异:

      grails [环境] dbm-gorm-diff [有意义的名称] .groovy --add 
    $ b $ environment 是您要比较的数据库,有意义的名称应以某种方式反映所应用的更改(可能是JIRA问题密钥,或版本号或说明)。



    - 添加标志将插入一个 include 语句在 changelog.groovy



    已配置 updateOnStart ,那么你就完成了!否则,要手动处理更新,请重用命令:

      grails [环境] dbm-update 






    RTFM




    • 插件文档 - 入门指南

    • 插件文档 - 一般用法

    • Confile上面的答案指向一个很好的教程,详细介绍手动更改变更日志的情况。
    • Liquibase文档 - 变更集(使用XML格式,但对理解概念很有用)

    I want to use the database-migration grails plugin for database migration. When I start my Grails app the first time all the database tables are created automatically. The production setting in my DataSource.groovy is:

    production {
    
    
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost/myapp?useUnicode=yes&characterEncoding=UTF-8"
            username = "test"
            password = "test"
            dialect = org.hibernate.dialect.MySQL5InnoDBDialect
            properties {
               validationQuery = "select 1"
               testWhileIdle = true
               timeBetweenEvictionRunsMillis = 60000
            }
        }
    }
    

    In my config.groovy I set:

    grails.plugin.databasemigration.updateOnStart = true
    grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy']
    

    When I add properties to my domain classes I need to adjust the changelog file. What is the best way to do database migration in this case? What are the steps I have to do when I add or remove columns?

    解决方案

    As you're probably aware, the dbcreate directive is not recommended for production use:

    You can also remove the dbCreate setting completely, which is recommended once your schema is relatively stable and definitely when your application and database are deployed in production.

    So keep in mind that you will need to remove this (or set to 'none').


    Initial Baseline Workflow

    1. Define current state
    2. Create database from change log or mark as up-to-date
    3. Set config options

    The first step is to get the changelog to reflect the current state. If you've got an existing database, you want to use that to define the baseline. Otherwise, use GORM to define the tables.

    These commands will generate a baseline for your database. Also I choose to use the groovy DSL format rather than liquibase XML, because readability.

    Existing Database

    If you've got a production database with data already, its a little bit tricky. You will need to access the database or a copy of it from your grails environment. If you manipulate a copy, you will need to apply the updates back to your production (and potentially manage it as a planned outage).

    The command is:

    grails [environment] dbm-generate-changelog changelog.groovy
    

    ...where environment optionally specifies the dev/test/prod/custom environment the database is defined as.

    Following that, mark the database as 'up-to-date' with regards to the changelog:

    grails [environment] dbm-changelog-sync
    

    Then reapply the database to production, if neccesary.

    New Database

    If you don't have an existing database (or don't care):

    grails dbm-generate-gorm-changelog changelog.groovy
    

    Then, to create the database from the changelog:

    grails [environment] dbm-update
    

    Configuration

    You've already correctly got the options set:

    grails.plugin.databasemigration.updateOnStart = true
    grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy']
    

    These options simply mean that the plugin will attempt to apply the changes to the database when the application starts.


    Development Workflow

    1. Make changes to domains
    2. Generate changelog identifying differences
    3. (Backup and) Update the database

    So now you've got a database up-to-date, and you're smashing out changes to the domain classes, adding new ones and changing validation properties.

    Each time you want to record your changes, you want to compare your GORM classes to what exists in the database, and create a new changelog file to record the difference:

    grails [environment] dbm-gorm-diff [meaningful name].groovy --add
    

    Here environment is the database you are comparing against, and meaningful name should reflect in some way the change being applied (perhaps a JIRA issue key, or a version number, or a description).

    The --add flag will insert an include statement in changelog.groovy.

    If you've configured updateOnStart, then you're done! Otherwise, to manually process the update, reuse the command:

    grails [environment] dbm-update
    


    RTFM

    • Plugin documentation - Getting Started
    • Plugin documentation - General Usage
    • Confile's answer above points to a good tutorial that goes into detail about manual changes to changelogs
    • Liquibase documentation - Changesets (Uses the XML format, but useful for understanding concepts)

    这篇关于Grails中的数据库迁移有什么好的工作流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆