不使用hibernate.hbm2ddl.auto,如何将所有初始架构导出到Flyway?

时间:2023-01-25 08:20:29

I am at the almost ready stage of my JEE development. With a lot of recommendation NOT to use Hibernate's hbm2ddl.auto in production, I decided to remove it.

我正处于JEE开发的近期阶段。有很多建议不要在生产中使用Hibernate的hbm2ddl.auto,我决定删除它。

So now, I found out about Flyway, which seems great for future db changes and migrations, but I am stuck at first step: I have many entities, some entities inherit from base entities. This makes the CREATE statement very complex.

所以现在,我发现了Flyway,这对于未来的数据库更改和迁移似乎很有用,但我坚持第一步:我有许多实体,一些实体继承自基础实体。这使得CREATE语句非常复杂。

What is the best practice to create the first migration file?

创建第一个迁移文件的最佳做法是什么?

Thanks!

谢谢!

4 个解决方案

#1


7  

If you've taken an "entities first" approach during development you'll need to generate the initial schema in the same way for the first live deployment: This will produce the first creation script used by Flyway and there may also need to be a second associated script for populating reference data.

如果您在开发过程中采用“实体优先”方法,则需要以相同的方式为第一次实时部署生成初始模式:这将生成Flyway使用的第一个创建脚本,并且可能还需要用于填充参考数据的第二个关联脚本。

In a nutshell, the reasons for no longer being able to use hbm2ddl.auto after the first deployment are that create will destroy existing data and update isn't reliable enough to cover all types of schema changes (as it sounds like you may already know from this SO question).

简而言之,第一次部署后不再能够使用hbm2ddl.auto的原因是create会破坏现有数据并且更新不够可靠,无法涵盖所有​​类型的架构更改(因为听起来你可能已经知道了)从这个SO问题)。

Flyway is a very useful tool but it does require a level of discipline that may not have existed during development. When going forward from the initial release, database update scripts need to be produced for Flyway that are equivalent to the changes made to the entities since the last release. There are tools (e.g. various commercial products from Redgate) that may help here: These attempt to "diff" two schemas and generate schema and/or data update scripts for getting from database A to database B. But in my experience, none of them are perfect and they don't quite reach the holy grail of enabling a completely automated approach.

Flyway是一个非常有用的工具,但它确实需要在开发过程中可能不存在的一定程度的纪律。从最初版本开始,需要为Flyway生成数据库更新脚本,这些脚本等同于自上次发布以来对实体所做的更改。有些工具(例如Redgate的各种商业产品)可能会有所帮助:这些工具试图“区分”两个模式并生成模式和/或数据更新脚本,以便从数据库A到数据库B.但根据我的经验,它们都不是是完美的,他们没有达到实现完全自动化方法的圣杯。

Arguably, the best way is an "as you go" manual approach to ensure that non-destructive update scripts are committed to source control whenever an entity change is made that affects the schema or reference data - but as already mentioned, this will require some discipline and/or documented processes for all team members to follow.

可以说,最好的方法是“随时随地”手动方法,以确保每当进行影响架构或参考数据的实体更改时,非破坏性更新脚本都会提交源代码控制 - 但如前所述,这将需要一些所有团队成员遵循的纪律和/或记录过程。

#2


1  

For the first migration file, you just need the current ddl of your database. There are many tools which can get this for you (such as the "copy ddl" option in the IntelliJ IDEA Database tool or a GUI client from your database vendor).

对于第一个迁移文件,您只需要数据库的当前ddl。有许多工具可以帮助您(例如IntelliJ IDEA数据库工具中的“copy ddl”选项或数据库供应商提供的GUI客户端)。

#3


0  

I am not sure about Flyway but there is an alternate way, you can use ant tasks for hibernate to generate or update schema.

我不确定Flyway但是有另一种方法,你可以使用ant任务来生成或更新模式。

Hope it helps.

希望能帮助到你。

#4


0  

If you build your project with Maven, you could use Hibernate maven plugin.

如果使用Maven构建项目,可以使用Hibernate maven插件。

#1


7  

If you've taken an "entities first" approach during development you'll need to generate the initial schema in the same way for the first live deployment: This will produce the first creation script used by Flyway and there may also need to be a second associated script for populating reference data.

如果您在开发过程中采用“实体优先”方法,则需要以相同的方式为第一次实时部署生成初始模式:这将生成Flyway使用的第一个创建脚本,并且可能还需要用于填充参考数据的第二个关联脚本。

In a nutshell, the reasons for no longer being able to use hbm2ddl.auto after the first deployment are that create will destroy existing data and update isn't reliable enough to cover all types of schema changes (as it sounds like you may already know from this SO question).

简而言之,第一次部署后不再能够使用hbm2ddl.auto的原因是create会破坏现有数据并且更新不够可靠,无法涵盖所有​​类型的架构更改(因为听起来你可能已经知道了)从这个SO问题)。

Flyway is a very useful tool but it does require a level of discipline that may not have existed during development. When going forward from the initial release, database update scripts need to be produced for Flyway that are equivalent to the changes made to the entities since the last release. There are tools (e.g. various commercial products from Redgate) that may help here: These attempt to "diff" two schemas and generate schema and/or data update scripts for getting from database A to database B. But in my experience, none of them are perfect and they don't quite reach the holy grail of enabling a completely automated approach.

Flyway是一个非常有用的工具,但它确实需要在开发过程中可能不存在的一定程度的纪律。从最初版本开始,需要为Flyway生成数据库更新脚本,这些脚本等同于自上次发布以来对实体所做的更改。有些工具(例如Redgate的各种商业产品)可能会有所帮助:这些工具试图“区分”两个模式并生成模式和/或数据更新脚本,以便从数据库A到数据库B.但根据我的经验,它们都不是是完美的,他们没有达到实现完全自动化方法的圣杯。

Arguably, the best way is an "as you go" manual approach to ensure that non-destructive update scripts are committed to source control whenever an entity change is made that affects the schema or reference data - but as already mentioned, this will require some discipline and/or documented processes for all team members to follow.

可以说,最好的方法是“随时随地”手动方法,以确保每当进行影响架构或参考数据的实体更改时,非破坏性更新脚本都会提交源代码控制 - 但如前所述,这将需要一些所有团队成员遵循的纪律和/或记录过程。

#2


1  

For the first migration file, you just need the current ddl of your database. There are many tools which can get this for you (such as the "copy ddl" option in the IntelliJ IDEA Database tool or a GUI client from your database vendor).

对于第一个迁移文件,您只需要数据库的当前ddl。有许多工具可以帮助您(例如IntelliJ IDEA数据库工具中的“copy ddl”选项或数据库供应商提供的GUI客户端)。

#3


0  

I am not sure about Flyway but there is an alternate way, you can use ant tasks for hibernate to generate or update schema.

我不确定Flyway但是有另一种方法,你可以使用ant任务来生成或更新模式。

Hope it helps.

希望能帮助到你。

#4


0  

If you build your project with Maven, you could use Hibernate maven plugin.

如果使用Maven构建项目,可以使用Hibernate maven插件。