我想让我的数据库处于版本控制之下。
我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。
如何对数据库应用版本控制?
我想让我的数据库处于版本控制之下。
我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。
如何对数据库应用版本控制?
当前回答
Red Gate的SQL Compare产品不仅允许您进行对象级比较,并从中生成更改脚本,还允许您将数据库对象导出到按对象类型组织的文件夹层次结构中,其中一个[objectname]。这些目录中每个对象的SQL创建脚本。对象类型层次结构是这样的:
\功能 \安全 \安全\角色 \ \安全模式 \安全\用户 \存储过程 \表
如果在进行更改后将脚本转储到相同的根目录,则可以使用此目录更新SVN repo,并单独保存每个对象的运行历史记录。
其他回答
你没有提到任何关于目标环境或约束的细节,所以这可能并不完全适用……但如果您正在寻找一种有效跟踪不断变化的DB模式的方法,并且不反对使用Ruby, ActiveRecord的迁移正适合您。
迁移使用Ruby DSL以编程方式定义数据库转换;每个转换都可以应用或(通常)回滚,允许您在任何给定的时间点跳转到您的DB模式的不同版本。定义这些转换的文件可以像任何其他源代码一样检入版本控制。
因为迁移是ActiveRecord的一部分,它们通常在全栈Rails应用程序中使用;然而,你可以用最少的努力独立于Rails使用ActiveRecord。请参阅这里,了解在Rails之外使用AR迁移的更详细处理。
这是围绕发展的“难题”之一。据我所知,没有完美的解决方案。
如果只需要存储数据库结构而不需要存储数据,则可以将数据库导出为SQL查询。(在企业管理器中:右键单击数据库->生成SQL脚本。我建议在选项卡上设置“每个对象创建一个文件”),然后您可以将这些文本文件提交到svn,并使用svn的diff和日志功能。
我将此与一个Batch脚本绑定在一起,该脚本采用一对参数并设置数据库。我还添加了一些额外的查询,输入默认数据,如用户类型和管理用户。(如果你想要更多关于这方面的信息,张贴一些东西,我可以把脚本放在可以访问的地方)
如果你需要保留所有的数据,我建议保留一个数据库备份,并使用Redgate (http://www.red-gate.com/)产品进行比较。它们并不便宜,但它们值每一分钱。
每个推荐RedGate工具的人都要加1分,还有额外的建议和警告。
SqlCompare also has a decently documented API: so you can, for instance, write a console app which syncs your source controlled scripts folder with a CI integration testing database on checkin, so that when someone checks in a change to the schema from their scripts folder it's automatically deployed along with the matching application code change. This helps close the gap with developers who are forgetful about propagating changes in their local db up to a shared development DB (about half of us, I think :) ).
A caveat is that with a scripted solution or otherwise, the RedGate tools are sufficiently smooth that it's easy to forget about SQL realities underlying the abstraction. If you rename all the columns in a table, SqlCompare has no way to map the old columns to the new columns and will drop all the data in the table. It will generate warnings but I've seen people click past that. There's a general point here worth making, I think, that you can only automate DB versioning and upgrade so far - the abstractions are very leaky.
我们使用DBGhost来管理SQL数据库。然后,您将脚本放在版本控制中构建一个新数据库,它将构建一个新数据库,或者将任何现有数据库升级到版本控制中的模式。这样,您就不必担心创建更改脚本(尽管您仍然可以这样做,例如,如果您想更改列的数据类型并需要转换数据)。
我建议使用比较工具为您的数据库临时设计一个版本控制系统。两个好的替代方案是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。