我想让我的数据库处于版本控制之下。

我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。

如何对数据库应用版本控制?


当前回答

我们不存储数据库模式,而是存储对数据库的更改。我们所做的是存储模式更改,以便为数据库的任何版本构建更改脚本,并将其应用于客户的数据库。我写了一个数据库实用程序,它与我们的主应用程序一起分发,它可以读取脚本,并知道需要应用哪些更新。它还具有足够的智能,可以根据需要刷新视图和存储过程。

其他回答

典型的解决方案是根据需要转储数据库并备份这些文件。

根据您的开发平台,可能会有可用的开源插件。编写自己的代码来完成这个任务通常是相当简单的。

注意:您可能希望备份数据库转储,而不是将其放入版本控制中。在版本控制中,文件可能会迅速变大,并导致整个源代码控制系统变慢(我正在回忆CVS的恐怖故事)。

我还使用了数据库中通过数据库扩展属性家族存储的一个版本的过程。我的应用程序有脚本的每个版本的步骤(即。从1.1移动到1.2)。部署时,它会查看当前版本,然后逐个运行脚本,直到到达最后一个应用程序版本。没有脚本有直接的“最终”版本,即使部署在干净的DB上也会通过一系列升级步骤进行部署。

Now what I like to add is that I've seen two days ago a presentation on the MS campus about the new and upcoming VS DB edition. The presentation was focused specifically on this topic and I was blown out of the water. You should definitely check it out, the new facilities are focused on keeping schema definition in T-SQL scripts (CREATEs), a runtime delta engine to compare deployment schema with defined schema and doing the delta ALTERs and integration with source code integration, up to and including MSBUILD continuous integration for automated build drops. The drop will contain a new file type, the .dbschema files, that can be taken to the deployment site and a command line tool can do the actual 'deltas' and run the deployment. I have a blog entry on this topic with links to the VSDE downloads, you should check them out: http://rusanu.com/2009/05/15/version-control-and-your-database/

你可能想看看Liquibase (http://www.liquibase.org/)。即使您不使用该工具本身,它也能很好地处理数据库变更管理或重构的概念。

我建议使用比较工具为您的数据库临时设计一个版本控制系统。两个好的替代方案是xSQL模式比较和xSQL数据比较。

现在,如果您的目标是只对数据库的模式进行版本控制,那么您可以简单地使用xSQL schema Compare来生成模式的xSQL快照,并在版本控制中添加这些文件。然后,要恢复或更新到特定版本,只需将数据库的当前版本与目标版本的快照进行比较。

Also, if you want to have the data under version control as well, you can use xSQL Data Compare to generate change scripts for you database and add the .sql files in your version control. You could then execute these scripts to revert / update to any version you want. Keep in mind that for the 'revert' functionality you need to generate change scripts that, when executed, will make Version 3 the same as Version 2 and for the 'update' functionality, you need to generate change scripts that do the opposite.

最后,通过一些基本的批处理编程技能,您可以使用命令行版本的xSQL Schema Compare和xSQL Data Compare来自动化整个过程

免责声明:我隶属于xSQL。

你没有提到任何关于目标环境或约束的细节,所以这可能并不完全适用……但如果您正在寻找一种有效跟踪不断变化的DB模式的方法,并且不反对使用Ruby, ActiveRecord的迁移正适合您。

迁移使用Ruby DSL以编程方式定义数据库转换;每个转换都可以应用或(通常)回滚,允许您在任何给定的时间点跳转到您的DB模式的不同版本。定义这些转换的文件可以像任何其他源代码一样检入版本控制。

因为迁移是ActiveRecord的一部分,它们通常在全栈Rails应用程序中使用;然而,你可以用最少的努力独立于Rails使用ActiveRecord。请参阅这里,了解在Rails之外使用AR迁移的更详细处理。