如何升级使用ORM工具构建的数据库模式?

时间:2022-08-21 12:48:32

I'm looking for a general solution for upgrading database schema with ORM tools, like JPOX or Hibernate. How do you do it in your projects?

我正在寻找使用ORM工具升级数据库模式的通用解决方案,如JPOX或Hibernate。你是如何在你的项目中做到的?

The first solution that comes to my mind is to create my own mechanism for upgrading databases, with SQL scripts doing all the work. But in this case I'll have to remember about creating new scripts every time the object mappings are updated. And I'll still have to deal with low-level SQL queries, instead of just defining mappings and allowing the ORM tools to do all the job...

我想到的第一个解决方案是创建自己的升级数据库机制,SQL脚本完成所有工作。但在这种情况下,我必须记住每次更新对象映射时都要创建新脚本。而且我仍然需要处理低级SQL查询,而不仅仅是定义映射并允许ORM工具完成所有工作......

So the question is how to do it properly. Maybe some tools allow for simplifying this task (for example, I heard that Rails have such mechanism built-in), if so please help me decide which ORM tool to choose for my next Java project.

所以问题是如何正确地做到这一点。也许有些工具可以简化这项任务(例如,我听说Rails内置了这样的机制),如果是这样,请帮我决定为我的下一个Java项目选择哪种ORM工具。

8 个解决方案

#1


7  

LiquiBase is an interesting open source library for handling database refactorings (upgrades). I have not used it, but will definitely give it a try on my next project where I need to upgrade a db schema.

LiquiBase是一个有趣的开源库,用于处理数据库重构(升级)。我没有使用它,但肯定会尝试我需要升级数据库模式的下一个项目。

#2


2  

I don't see why ORM generated schemas are any different to other DB schemas - the problem is the same. Assuming your ORM will spit out a generation script, you can use an external tool to do the diff

我不明白为什么ORM生成的模式与其他数据库模式有任何不同 - 问题是相同的。假设您的ORM将吐出生成脚本,您可以使用外部工具来执行diff

I've not tried it but google came back with SQLCompare as one option - I'm sure there are others.

我没试过,但谷歌回来用SQLCompare作为一个选项 - 我敢肯定还有其他选择。

#3


2  

We hand code SQL update scripts and we tear down the schema and rebuild it applying the update scripts as part of our continuous build process. If any hibernate mappings do not match the schema, the build will fail.

我们处理SQL更新脚本代码,我们拆除架构并使用更新脚本重建它,作为我们持续构建过程的一部分。如果任何hibernate映射与模式不匹配,则构建将失败。

#4


1  

DbMaintain can also help here.

DbMaintain也可以在这里提供帮助。

#5


1  

You can check this feature comparison of some database schema upgrade tools.

您可以检查某些数据库架构升级工具的此功能比较。

A comparison of the number of questions in SOW of some of those tools:

比较其中一些工具的SOW问题数量:

  • mybatis (1049 questions tagged)
  • mybatis(1049个问题)
  • Liquibase (663 questions tagged)
  • Liquibase(663个问题)
  • Flyway (400 questions tagged)
  • Flyway(标有400个问题)
  • DBDeploy (24 questions tagged).
  • DBDeploy(标记24个问题)。

#6


0  

I think your best bet is to use an ORM-tool that includes database migration like SubSonic:

我认为最好的办法是使用包含SubSonic等数据库迁移的ORM工具:

http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/

http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/

#7


0  

We ended up making update scripts each time we changed the database. So there's a script from version 10 to 11, from 11 to 12, etc.. Then we can run any consecutive set of scripts to skip from some existing version to the new version. We stored the existing version in the database so we could detect this upon startup.

我们每次更改数据库时都会创建更新脚本。所以有一个从版本10到11,从11到12等的脚本。然后我们可以运行任何连续的脚本集来从一些现有版本跳到新版本。我们将现有版本存储在数据库中,以便我们可以在启动时检测到它。

Yes this involved database-specific code! One of the main problems with Hibernate!

是的,这涉及特定于数据库的代码! Hibernate的主要问题之一!

#8


0  

When working with Hibernate, I use an installer class that runs from the command-line and has options for creating database schema, inserting base data, and dynamically updating the database schema using SchemaUpdate. I find it to be extremely useful. It also gives me a place to put one-off scripts that will be run when a new version is launched to, for example, populate a new field in an existing DB table.

使用Hibernate时,我使用从命令行运行的安装程序类,并具有创建数据库模式,插入基础数据和使用SchemaUpdate动态更新数据库模式的选项。我发现它非常有用。它还为我提供了一个放置一次性脚本的地方,这些脚本将在启动新版本时运行,例如,在现有数据库表中填充新字段。

#1


7  

LiquiBase is an interesting open source library for handling database refactorings (upgrades). I have not used it, but will definitely give it a try on my next project where I need to upgrade a db schema.

LiquiBase是一个有趣的开源库,用于处理数据库重构(升级)。我没有使用它,但肯定会尝试我需要升级数据库模式的下一个项目。

#2


2  

I don't see why ORM generated schemas are any different to other DB schemas - the problem is the same. Assuming your ORM will spit out a generation script, you can use an external tool to do the diff

我不明白为什么ORM生成的模式与其他数据库模式有任何不同 - 问题是相同的。假设您的ORM将吐出生成脚本,您可以使用外部工具来执行diff

I've not tried it but google came back with SQLCompare as one option - I'm sure there are others.

我没试过,但谷歌回来用SQLCompare作为一个选项 - 我敢肯定还有其他选择。

#3


2  

We hand code SQL update scripts and we tear down the schema and rebuild it applying the update scripts as part of our continuous build process. If any hibernate mappings do not match the schema, the build will fail.

我们处理SQL更新脚本代码,我们拆除架构并使用更新脚本重建它,作为我们持续构建过程的一部分。如果任何hibernate映射与模式不匹配,则构建将失败。

#4


1  

DbMaintain can also help here.

DbMaintain也可以在这里提供帮助。

#5


1  

You can check this feature comparison of some database schema upgrade tools.

您可以检查某些数据库架构升级工具的此功能比较。

A comparison of the number of questions in SOW of some of those tools:

比较其中一些工具的SOW问题数量:

  • mybatis (1049 questions tagged)
  • mybatis(1049个问题)
  • Liquibase (663 questions tagged)
  • Liquibase(663个问题)
  • Flyway (400 questions tagged)
  • Flyway(标有400个问题)
  • DBDeploy (24 questions tagged).
  • DBDeploy(标记24个问题)。

#6


0  

I think your best bet is to use an ORM-tool that includes database migration like SubSonic:

我认为最好的办法是使用包含SubSonic等数据库迁移的ORM工具:

http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/

http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/

#7


0  

We ended up making update scripts each time we changed the database. So there's a script from version 10 to 11, from 11 to 12, etc.. Then we can run any consecutive set of scripts to skip from some existing version to the new version. We stored the existing version in the database so we could detect this upon startup.

我们每次更改数据库时都会创建更新脚本。所以有一个从版本10到11,从11到12等的脚本。然后我们可以运行任何连续的脚本集来从一些现有版本跳到新版本。我们将现有版本存储在数据库中,以便我们可以在启动时检测到它。

Yes this involved database-specific code! One of the main problems with Hibernate!

是的,这涉及特定于数据库的代码! Hibernate的主要问题之一!

#8


0  

When working with Hibernate, I use an installer class that runs from the command-line and has options for creating database schema, inserting base data, and dynamically updating the database schema using SchemaUpdate. I find it to be extremely useful. It also gives me a place to put one-off scripts that will be run when a new version is launched to, for example, populate a new field in an existing DB table.

使用Hibernate时,我使用从命令行运行的安装程序类,并具有创建数据库模式,插入基础数据和使用SchemaUpdate动态更新数据库模式的选项。我发现它非常有用。它还为我提供了一个放置一次性脚本的地方,这些脚本将在启动新版本时运行,例如,在现有数据库表中填充新字段。