Java 相当于数据库模式更改,例如 Django 的 South?
我一直在开发一个 Django 项目,使用 South 来跟踪和管理数据库架构更改。我正在使用 Google Web Toolkit 启动一个新的 Java 项目,想知道是否有等效的工具。对于那些不知道的人,这是 South 所做的:
- 自动识别对我的 Python 数据库模型的更改(添加/删除列、表等)
- 自动创建 SQL 语句以将这些更改应用到我的数据库
- 跟踪已应用的架构迁移并应用它们为了
- 允许使用 Python 代码迁移数据。例如,使用 Python split() 函数将名称字段拆分为名字和姓氏字段
我还没有决定使用我的 Java ORM,但 Hibernate 看起来是最受欢迎的。对我来说,轻松更改数据库模式的能力将是一个重要因素。
I've been working on a Django project using South to track and manage database schema changes. I'm starting a new Java project using Google Web Toolkit and wonder if there is an equivalent tool. For those who don't know, here's what South does:
- Automatically recognize changes to my Python database models (add/delete columns, tables etc.)
- Automatically create SQL statements to apply those changes to my database
- Track the applied schema migrations and apply them in order
- Allow data migrations using Python code. For example, splitting a name field into a first-name and last-name field using the Python split() function
I haven't decided on my Java ORM yet, but Hibernate looks like the most popular. For me, the ability to easily make database schema changes will be an important factor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
哇,南方听起来棒极了!我不确定有什么开箱即用的工具可以为您提供如此多的帮助,但是如果您选择 Hibernate 作为您的 ORM 解决方案,您可以构建自己的增量数据迁移套件,而不会遇到很多麻烦。
这是我在自己的项目中使用的方法,它对我来说工作了几年,并且进行了多次更新/架构更改:
在数据库中维护一个 schema_version 表,该表简单地定义了代表版本的数字您的数据库架构。如果您愿意,可以在 Hibernate 范围之外处理该表。
在代码中维护架构的“当前”版本号。
当代码中的版本号比数据库的版本号新时,您可以使用 Hibernate 的 SchemaUpdate 实用程序,它将检测任何模式添加(注意,只是添加),例如新表、列和约束。
最后,如果您愿意的话,我维护了一个“脚本”,其中的迁移步骤不仅仅是通过所需的架构版本号来标识的架构更改。例如,新列需要应用默认值或类似性质的内容。
这听起来可能需要做很多工作,尤其是当您来自一个为您处理很多工作的环境时,但是您可以使用 Hibernate 快速完成这样的设置,并且很容易添加到其中在。在那段时间里,除了添加新的迁移任务之外,我从未对我的增量更新框架进行任何更改。
希望有人能够为更加“不干涉”的方法提供一个好的答案,但我想我会分享一种对我来说非常有效的方法。
祝你好运!
Wow, South sounds pretty awesome! I'm not sure of anything out-of-the-box that will help you nearly as much as that does, however if you choose Hibernate as your ORM solution you can build your own incremental data migration suite without a lot of trouble.
Here was the approach that I used in my own project, it worked fairly well for me for over a couple of years and several updates/schema changes:
Maintain a schema_version table in the database that simply defines a number that represents the version of your database schema. This table can be handled outside of the scope of Hibernate if you wish.
Maintain the "current" version number for your schema inside your code.
When the version number in code is newer than what's the database, you can use Hibernate's SchemaUpdate utility which will detect any schema additions (NOTE, just additions) such as new tables, columns, and constraints.
Finally I maintained a "script" if you will of migration steps that were more than just schema changes that were identified by which schema version number they were required for. For instance new columns needed default values applied or something of that nature.
This may sounds like a lot of work, especially when coming from an environment that took care of a lot of it for you, but you can get a setup like this rolling pretty quickly with Hibernate and it is pretty easy to add onto as you go on. I never ended up making any changes to my incremental update framework over that time except to add new migration tasks.
Hopefully someone will come along with a good answer for a more "hands-off" approach, but I thought I'd share an approach that worked pretty well for me.
Good luck to you!
因为我正在寻找与我迄今为止所取得的成就相同的东西。
我们首先使用dbdeploy。它为您管理大部分内容,但您必须自己编写所有转换脚本!这意味着您所做的每项更改都必须在其自己的脚本中,并且您必须从头开始编写该脚本。不是很方便,但工作非常可靠。
我遇到的第二件事是liquibase。它将配置存储在一个 xml 文件中。阅读起来不是很直观,但易于管理。另外还有一个 Intellij Idea 插件。在撰写本文时,仍然存在一些小问题,但正如作者向我保证的那样,这些问题很快就会得到解决。
完美的解决方案是使用您的 java 环境。这确实是结婚的工具:D
as I'm looking for the same heres what I've achieved so far.
We first used dbdeploy. It manages the most stuff for you but you will have to write all the transition scripts by yourself! That means every change you make has to be in its own script which you will have to write from scratch. Not very handy, but works very reliable.
The second thing I encountered is liquibase. It stores the configuration in one single xml file. Not very intuitive to read, but managable. Plus there is an Intellij Idea plugin for it. At the moment of writing it still has some minor issues, but as the author assured me, they will be fixed soon.
The perfect solution would be to get south working with your java environment. That really would be a tool to marry :D
也许可以尝试 flyaway。似乎是一个不错的选择。
Maybe try flyaway. Seems like a good alternative.
我一直在考虑仅使用 django-jython 来在我们的旧版 Java 应用程序中进行数据库迁移。最新的 Jython 版本是 2.5.4rc1,但我认为我可以通过将其用于南方迁移来降低风险。
特别是因为我可以使用 inspectdb 为我生成模型。然后“无缝”地用Python替换部分Java。
I've been thinking about using
django-jython
just for db migrations in our legacy Java application. The latest Jython version is 2.5.4rc1, but I think I can mitigate the risk by just using it for South migrations.Especially since I can use inspectdb to generate the models for me. And then replace parts of the Java with Python "seamlessly".
如果您使用的是 hibernate,请查看 liquibase
http ://www.liquibase.org/databases.html
它已经存在了 10 年,所以非常可靠。它可能支持其他ORM,只需在他们的网站上进行挖掘即可。在此处查看 liquibase+hibernate 扩展:
https://github.com/liquibase/liquibase-hibernate
If you're using hibernate, then checkout liquibase
http://www.liquibase.org/databases.html
It's been around for 10 years so it's pretty solid. It may support other ORM's, just have a dig around on their website. Checkout the liquibase+hibernate extension here:
https://github.com/liquibase/liquibase-hibernate