结合Hibernate的自动模式创建和数据库版本控制 [英] Combine Hibernate's automatic schema creation and database versioning

查看:107
本文介绍了结合Hibernate的自动模式创建和数据库版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,当应用程序第一次在机器上运行时,Hibernate会创建我所有的表模式。这很好地工作。

现在我想知道Hibernate是否有某种机制来保持数据库处于版本控制之下,也就是说,当我运行一个Hibernate时,Hibernate会知道如何将一个模式迁移到另一个模式我的应用程序的不同版本和Hibernate从旧版本中找到不同的数据库模式?我以某种方式认为这应该是可能的,因为Hibernate可以读取存在的模式并可以将模式与映射描述进行比较。然而,我不知道如何告诉Hibernate迁移旧数据,就像在创建变更脚本时一样。 Liquibase / Flyway。

我可能没有使用Google搜索正确的东西,因为Hibernate和版本控制将向您展示审计和字段版本管理方面的大量内容,但我正在考虑更多Liquibase / Flyway类型的版本控制。我从来没有考虑过这两者,但由于Hibernate不会创建更新脚本,而是直接操纵数据库,所以我不知道如何让这两者合作。



这是第一次让Hibernate创建我的模式而不是写自己的脚本。我这样做是为了利用 Hibernate Envers 手动创建脚本的过程。也许我错过了一些明显的东西。感谢您对此事的任何意见!

更新:我今天要和Flyway的开发者谈谈,他告诉我他不会知道一个好的解决方案。也许什么都没有?

解决方案

我们的Java / Hibernate项目有同样的问题,并且不需要任何代码重复工作。 Hibernate的更新功能根本不可靠,LiquidBase更好但不是100%的傻瓜证明。最后,我们开发了一个简单的脚本来管理以下过程:
$ b $ ol

  • 当前数据库模式始终由Hibernate生成,
    直接针对DEV数据库。

  • 前一个数据库模式是由一系列LiquiBase更改集合生成的
    。每次需要
    迁移时,在
    previous和current数据库之间调用LiquiBasediff函数(两个实际数据库,是,这样更可靠地
    ),生成一个新的LiquiBase变更集。

  • 这个
    更改集需要手动查看。所有更改集在源代码控制中保持

  • 通过应用所有LiquiBase更改集,生产数据库的模式



      $ {LIQB_COMMAND} $ {PREV_DB_OPTIONS} --changeLogFile = $ {LIQB_CHGLOG_FILE_NEW} \ 
    diffChangeLog \
    --referenceUsername = $ {DEV_DB_USER} \
    --referencePassword = $ {DEV_DB_PWD} \
    --referenceDriver = com .mysql.jdbc.Driver \
    --referenceUrl = $ {DEV_DB_URL}

    方式,我们的迁移过程是相当可靠的,你不写两次架构代码。在XML中对生成的变更集进行了手动审查,但大多数情况下没有问题,并且确实比手动编写模式更改操作更容易。


    I have an application where Hibernate creates all my table schemata whenever the application is run on a machine for the first time. This works nicely.

    Now I was however wondering if Hibernate has some sort of mechanism to keep the database under version control, i.e. will Hibernate know how to migrate one schema to another when I run a different version of my application and Hibernate finds a different database schema from an older version present? I somehow think that this should be possible, considering that Hibernate can read the existent schema and could compare the schema to the mapping description. I would however not know how I could tell Hibernate to migrate old data as when creating change scripts with e.g. Liquibase / Flyway.

    I might not have googled the right things since Hibernate and versioning will show you a lot of hits on auditing and field versioning but I am thinking more in terms of Liquibase / Flyway kind of versioning. I never considered the both together but since Hibernate does not create update scripts but directly manipulates the database, I would not know how to make the both work together.

    This is the first time I let Hibernate create my schema instead of writing my own scripts. I do this in order to make use of Hibernate Envers what makes the manual script creation extra tedious. Maybe I am missing something obvious. Thanks for any input on this matter!

    Update: I got to talk to the developer of Flyway today and he told me that he would not know of a good solution. Maybe there is nothing?

    解决方案

    We had the same problem with our Java/Hibernate project, and did not want any code duplication effort. Hibernate "update" feature is not reliable at all, LiquidBase is better but not 100% fool proof either. In the end we developed a simple script to manage the following process:

    1. The "current" database schema is always generated by Hibernate, against a DEV database directly.
    2. A "previous" database schema is generated by a series of LiquiBase change sets.
    3. Every time a migration is needed, a LiquiBase "diff" function is invoked between the "previous" and "current" databases (two actual databases, yes, more reliably that way), generating a new LiquiBase change set.
    4. This change set needs to be manually reviewed. All change sets are kept in source code control.
    5. The PRODUCTION database has its schema generated by applying all the LiquiBase change sets.

    A key command in our script reads like the following:

    ${LIQB_COMMAND} ${PREV_DB_OPTIONS} --changeLogFile=${LIQB_CHGLOG_FILE_NEW}  \
        diffChangeLog \
                    --referenceUsername=${DEV_DB_USER} \
                    --referencePassword=${DEV_DB_PWD} \
                    --referenceDriver=com.mysql.jdbc.Driver \
                    --referenceUrl=${DEV_DB_URL}
    

    This way, our migration process is quite reliable and you don't write the schema code twice. There is a manual review of the generated change set in XML, but most of the time there is no problem, and it sure is much easier than manually writing the schema change operations.

    这篇关于结合Hibernate的自动模式创建和数据库版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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