我想让我的数据库处于版本控制之下。
我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。
如何对数据库应用版本控制?
我想让我的数据库处于版本控制之下。
我总是希望在那里至少有一些数据(如alumb提到的:用户类型和管理员)。我还经常需要大量生成的测试数据来进行性能度量。
如何对数据库应用版本控制?
当前回答
我还使用了数据库中通过数据库扩展属性家族存储的一个版本的过程。我的应用程序有脚本的每个版本的步骤(即。从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/
其他回答
这是围绕发展的“难题”之一。据我所知,没有完美的解决方案。
如果只需要存储数据库结构而不需要存储数据,则可以将数据库导出为SQL查询。(在企业管理器中:右键单击数据库->生成SQL脚本。我建议在选项卡上设置“每个对象创建一个文件”),然后您可以将这些文本文件提交到svn,并使用svn的diff和日志功能。
我将此与一个Batch脚本绑定在一起,该脚本采用一对参数并设置数据库。我还添加了一些额外的查询,输入默认数据,如用户类型和管理用户。(如果你想要更多关于这方面的信息,张贴一些东西,我可以把脚本放在可以访问的地方)
如果你需要保留所有的数据,我建议保留一个数据库备份,并使用Redgate (http://www.red-gate.com/)产品进行比较。它们并不便宜,但它们值每一分钱。
版本控制数据库的另一种选择是使用版本控制数据库,现在有几个这样的数据库。
https://www.dolthub.com/blog/2021-09-17-database-version-control/
这些产品没有在其他类型的数据库上应用版本控制——它们是自己的数据库引擎,支持版本控制操作。因此,您需要迁移到它们,或者在它们的基础上开始构建。
我编写了其中一个,DoltDB,它结合了MySQL和Git的接口。看看这里:
https://github.com/dolthub/dolt
在Red Gate,我们提供了一个工具,SQL Source Control,它使用SQL Compare技术将数据库与TFS或SVN存储库链接起来。该工具集成到SSMS中,让您像往常一样工作,只是现在允许提交对象。
对于基于迁移的方法(更适合于自动化部署),我们提供了SQL Change Automation(以前称为ReadyRoll),它作为Visual Studio项目创建和管理一组增量脚本。
在SQL源代码控制中,可以指定静态数据表。它们作为INSERT语句存储在源代码控制中。
如果您正在讨论测试数据,我们建议您要么使用工具生成测试数据,要么通过您定义的部署后脚本生成测试数据,要么简单地将生产备份恢复到开发环境。
关于这个主题,Martin Fowler写了一篇我最喜欢的文章http://martinfowler.com/articles/evodb.html。我选择不像alumb和其他人建议的那样将模式转储放在版本控制下,因为我想要一种简单的方法来升级我的生产数据库。
对于一个web应用程序,我将有一个生产数据库实例,我使用两种技术:
数据库升级脚本
一个序列数据库升级脚本,其中包含将模式从版本N移动到版本N+1所需的DDL。(这些会进入你的版本控制系统。)一个_version_history_表,类似于
create table VersionHistory (
Version int primary key,
UpgradeStart datetime not null,
UpgradeEnd datetime
);
每次运行与新版本对应的升级脚本时获取一个新条目。
这可以确保很容易查看数据库模式的版本,并且数据库升级脚本只运行一次。同样,这些不是数据库转储。相反,每个脚本表示从一个版本迁移到下一个版本所需的更改。它们是应用于生产数据库以“升级”它的脚本。
开发人员沙箱同步
备份、清理和收缩生产数据库的脚本。在每次升级到生产DB后运行此命令。 在开发人员的工作站上恢复(并在必要时调整)备份的脚本。每个开发人员在每次升级到生产DB后都会运行此脚本。
警告:我的自动化测试运行在模式正确但空的数据库上,因此这个建议不能完全满足您的需求。
我还使用了数据库中通过数据库扩展属性家族存储的一个版本的过程。我的应用程序有脚本的每个版本的步骤(即。从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/