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

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

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


当前回答

您还可以考虑迁移解决方案。它们允许您在c#代码中指定数据库模式,并使用MSBuild上下滚动数据库版本。

我目前正在使用DbUp,它工作得很好。

其他回答

这是围绕发展的“难题”之一。据我所知,没有完美的解决方案。

如果只需要存储数据库结构而不需要存储数据,则可以将数据库导出为SQL查询。(在企业管理器中:右键单击数据库->生成SQL脚本。我建议在选项卡上设置“每个对象创建一个文件”),然后您可以将这些文本文件提交到svn,并使用svn的diff和日志功能。

我将此与一个Batch脚本绑定在一起,该脚本采用一对参数并设置数据库。我还添加了一些额外的查询,输入默认数据,如用户类型和管理用户。(如果你想要更多关于这方面的信息,张贴一些东西,我可以把脚本放在可以访问的地方)

如果你需要保留所有的数据,我建议保留一个数据库备份,并使用Redgate (http://www.red-gate.com/)产品进行比较。它们并不便宜,但它们值每一分钱。

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

这很简单。

当基础项目准备就绪时,您必须创建完整的数据库脚本。此脚本提交给SVN。这是第一个版本。 之后,所有开发人员创建更改脚本(ALTER…,新表,sprocs等)。 当您需要当前版本时,您应该执行所有新的更改脚本。 当应用程序发布到生产环境时,你会回到1(当然,那将是连续版本)。

Nant将帮助您执行这些更改脚本。:)

记住。只要有纪律,一切都能顺利进行。每次提交数据库更改时,代码中相应的函数也会被提交。

每个推荐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数据库。然后,您将脚本放在版本控制中构建一个新数据库,它将构建一个新数据库,或者将任何现有数据库升级到版本控制中的模式。这样,您就不必担心创建更改脚本(尽管您仍然可以这样做,例如,如果您想更改列的数据类型并需要转换数据)。