在使用Flyway和Hibernate的hbm2ddl的应用程序的生命周期中管理数据库模式 [英] Managing database schemas during the lifecycle of an application using Flyway together with Hibernate's hbm2ddl
问题描述
我正在开发一个Spring / Hibernate / MySql应用程序。该应用程序尚未投入生产,我目前使用Hibernate的 hbm2ddl 功能,这对于管理域上的更改非常方便。我还打算使用 Flyway 进行数据库迁移。
在未来的某个时间点,应用程序将首次投入生产这导致了我的第一个问题:
假设我的应用程序的第一个版本在生产环境中运行,我打算使用Hibernate的hbm2ddl在第二个应用程序版本上继续开发。
最好的选择是使用hbm2ddl仅进行集成测试,使用Flyway进行运行时,无论是QA测试还是生产环境。 p>
您也可以将hbmddl用作Flyway的第一个脚本的基础,但随后每次对JPA模型进行更改时,都需要手动创建新的更新脚本,这不是那个di反正很难。因为集成测试和运行时使用不同的策略,所以编写一个系统集成测试是必需的,它比较了由数据库创建的模式hbmddl和Flyway。再次,这也不难,只需要确保您与实际生产数据库(而不是内存中的集成测试)进行比较。
I am developing a Spring/Hibernate/MySql application. The application is not yet in production and I currently use the Hibernate's hbm2ddl feature which is very convenient for managing changes on the domain. I also intend to use Flyway for database migrations.
At some point in the future, the application will be put in production for the first time which leads to my first set of questions:
- What is the best practice to use for schema creation (first time the app is released into production)? Specifically, should I let Hibernate's hbm2ddl create the schema on the production database or let Flyway create the first schema using a SQL script? If the second option (i.e. Flyway) is preferable, then should I generate a SQL script from a hbm2ddl-created database?
Let's then assume I have my application's first version running in production and I intend to resume development on the second version of the application using Hibernate's hbm2ddl.
- How I am going to manage changes to the domain and especially compute the difference between version one and version two of the database schema for migrating the database during the release into production of version two?
The best trade-off is to use hbm2ddl for integration testing only and Flyway for run time, be it QA testing or the production environment.
You can use the hbmddl as the base of your first script for Flyway too, but then every time you make a change to JPA model you need to manually create a new update script, which is not that difficult anyway. This will also enable using DB specific features too.
Because the integration testing and run-time use different strategies it's mandatory to write a system integration test that compares the schemas created by both hbmddl and Flyway. Again this is not difficult either, just need yo make sure you compare against the actual production DB (not the in-memory integration testing one).
这篇关于在使用Flyway和Hibernate的hbm2ddl的应用程序的生命周期中管理数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!