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

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

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


当前回答

版本控制数据库的另一种选择是使用版本控制数据库,现在有几个这样的数据库。

https://www.dolthub.com/blog/2021-09-17-database-version-control/

这些产品没有在其他类型的数据库上应用版本控制——它们是自己的数据库引擎,支持版本控制操作。因此,您需要迁移到它们,或者在它们的基础上开始构建。

我编写了其中一个,DoltDB,它结合了MySQL和Git的接口。看看这里:

https://github.com/dolthub/dolt

其他回答

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

我同意ESV的答案,正是出于这个原因,我开始了一个小项目,以帮助维护一个非常简单的文件中的数据库更新,然后可以在一个很长的源代码之外进行维护。它允许对开发人员以及UAT和生产进行轻松的更新。该工具适用于SQL Server和MySQL。

项目的一些特点:

允许模式更改 允许值树填充 允许单独的测试数据插入,例如。UAT 允许回滚选项(不是自动的) 维护对SQL server和MySQL的支持 有能力导入现有的数据库到版本控制与一个简单的命令(SQL server仅…仍在开发MySQL)

请查看代码以获得更多信息。

Red Gate的SQL Compare产品不仅允许您进行对象级比较,并从中生成更改脚本,还允许您将数据库对象导出到按对象类型组织的文件夹层次结构中,其中一个[objectname]。这些目录中每个对象的SQL创建脚本。对象类型层次结构是这样的:

\功能 \安全 \安全\角色 \ \安全模式 \安全\用户 \存储过程 \表

如果在进行更改后将脚本转储到相同的根目录,则可以使用此目录更新SVN repo,并单独保存每个对象的运行历史记录。

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

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

为了处理#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上查看我关于这个主题的文章,了解我所说的内容。

祝你好运

利龙·列维

我还使用了数据库中通过数据库扩展属性家族存储的一个版本的过程。我的应用程序有脚本的每个版本的步骤(即。从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/