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

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

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


当前回答

首先,你必须选择适合你的版本控制系统:

Centralized Version Control system - a standard system where users check out/check in before/after they work on files, and the files are being kept in a single central server Distributed Version Control system - a system where the repository is being cloned, and each clone is actually the full backup of the repository, so if any server crashes, then any cloned repository can be used to restore it After choosing the right system for your needs, you'll need to setup the repository which is the core of every version control system All this is explained in the following article: http://solutioncenter.apexsql.com/sql-server-source-control-part-i-understanding-source-control-basics/

在建立了存储库之后,如果有一个中央版本控制系统,那么就有一个工作文件夹,您可以阅读本文。它展示了如何在开发环境中使用以下命令来设置源代码控制:

通过MSSCCI提供的SQL Server Management Studio, Visual Studio和SQL Server数据工具 一个第三方工具ApexSQL源代码控制

其他回答

每个推荐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.

根据我的经验,解决方法有两个:

您需要处理开发数据库的更改,这些更改是由多个开发人员在开发期间完成的。 需要处理客户站点的数据库升级。

为了处理#1,你需要一个强大的数据库差异/合并工具。最好的工具应该能够尽可能地执行自动合并,同时允许您手动解决未处理的冲突。

完美的工具应该通过使用3-way合并算法来处理合并操作,该算法考虑到相对于BASE数据库在their数据库和MINE数据库中所做的更改。

我写了一个商业工具,为SQLite数据库提供手动合并支持,我目前正在为SQLite添加3路合并算法的支持。请登录http://www.sqlitecompare.com查看

为了处理第2点,你需要一个升级框架。

基本思想是开发一个自动升级框架,该框架知道如何从现有的SQL模式升级到更新的SQL模式,并且可以为每个现有的DB安装构建一个升级路径。

在http://www.codeproject.com/KB/database/sqlite_upgrade.aspx上查看我关于这个主题的文章,了解我所说的内容。

祝你好运

利龙·列维

查看DBGhost http://www.innovartis.co.uk/。我已经在一个自动化的方式使用了2年,它工作得很好。它允许我们的DB构建非常像Java或C构建,除了数据库。你们能明白我的心情

在迁移到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做那么多更改。它允许我们跟踪所有我们关心的对象的变化,并且允许我们在发生严重问题时重新构建完整的模式。

使用更改脚本将数据库脚本保存到版本控制中是一种好方法,这样您就可以升级您拥有的任何一个数据库。此外,您可能希望保存不同版本的模式,这样就可以创建完整的数据库,而不必应用所有的更改脚本。处理脚本应该是自动化的,这样您就不必做手工工作了。

我认为为每个开发者提供一个独立的数据库,而不是使用一个共享的数据库是很重要的。这样,开发人员就可以独立于其他开发人员创建测试用例和开发阶段。

自动化工具应该具有处理数据库元数据的方法,这些元数据可以告诉您哪些数据库处于何种开发状态,哪些表包含版本可控数据等等。