我想让我的数据库处于版本控制之下。
我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。
如何对数据库应用版本控制?
我想让我的数据库处于版本控制之下。
我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。
如何对数据库应用版本控制?
当前回答
每个推荐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.
其他回答
如果您有一个小型数据库,并且希望对整个数据库进行版本化,那么这个批处理脚本可能会有所帮助。它将MSSQL数据库MDF文件分离、压缩并检入Subversion。
如果您主要想要版本化您的模式,并且只有少量的引用数据,您可以使用SubSonic Migrations来处理它。这样做的好处是您可以轻松地向上或向下迁移到任何特定的版本。
您还可以考虑迁移解决方案。它们允许您在c#代码中指定数据库模式,并使用MSBuild上下滚动数据库版本。
我目前正在使用DbUp,它工作得很好。
我们不存储数据库模式,而是存储对数据库的更改。我们所做的是存储模式更改,以便为数据库的任何版本构建更改脚本,并将其应用于客户的数据库。我写了一个数据库实用程序,它与我们的主应用程序一起分发,它可以读取脚本,并知道需要应用哪些更新。它还具有足够的智能,可以根据需要刷新视图和存储过程。
这很简单。
当基础项目准备就绪时,您必须创建完整的数据库脚本。此脚本提交给SVN。这是第一个版本。 之后,所有开发人员创建更改脚本(ALTER…,新表,sprocs等)。 当您需要当前版本时,您应该执行所有新的更改脚本。 当应用程序发布到生产环境时,你会回到1(当然,那将是连续版本)。
Nant将帮助您执行这些更改脚本。:)
记住。只要有纪律,一切都能顺利进行。每次提交数据库更改时,代码中相应的函数也会被提交。
在迁移到x64平台后,我们需要对SQL数据库进行版本调整,而旧版本在迁移过程中出现了问题。我们写了一个c#应用程序,使用sqlmo将所有SQL对象映射到一个文件夹:
Root ServerName DatabaseName Schema Objects Database Triggers* .ddltrigger.sql Functions ..function.sql Security Roles Application Roles .approle.sql Database Roles .role.sql Schemas* .schema.sql Users .user.sql Storage Full Text Catalogs* .fulltext.sql Stored Procedures ..proc.sql Synonyms* .synonym.sql Tables ..table.sql Constraints ...chkconst.sql ...defconst.sql Indexes ...index.sql Keys ...fkey.sql ...pkey.sql ...ukey.sql Triggers ...trigger.sql Types User-defined Data Types ..uddt.sql XML Schema Collections* ..xmlschema.sql Views ..view.sql Indexes ...index.sql Triggers ...trigger.sql
然后,应用程序将新编写的版本与存储在SVN中的版本进行比较,如果存在差异,则更新SVN。 我们确定每晚运行一次流程就足够了,因为我们没有对SQL做那么多更改。它允许我们跟踪所有我们关心的对象的变化,并且允许我们在发生严重问题时重新构建完整的模式。