我觉得我的商店有一个漏洞,因为我们没有一个可靠的过程来控制数据库模式更改的版本。我们做了很多备份,所以我们或多或少地得到了保护,但以这种方式依赖于最后一道防线是一种糟糕的做法。

令人惊讶的是,这似乎是一个共同的主线。与我交谈过的许多商店都忽略了这个问题,因为他们的数据库不会经常更改,他们基本上只是尽量做到一丝不苟。

不过,我知道这个故事是怎么发展的。这只是时间问题,迟早会出问题,会有东西丢失。

在这方面有什么最佳实践吗?你有哪些行之有效的策略?


当前回答

这对我来说也一直是一个很大的烦恼——似乎对开发数据库进行快速更改,保存它(忘记保存更改脚本)太容易了,然后你就卡在那里了。您可以撤消刚才所做的并重新做它以创建更改脚本,当然,如果您愿意的话,也可以从头开始编写它,尽管这将花费大量的时间来编写脚本。

A tool that I have used in the past that has helped with this some is SQL Delta. It will show you the differences between two databases (SQL server/Oracle I believe) and generate all the change scripts necessary to migrate A->B. Another nice thing it does is show all the differences between database content between the production (or test) DB and your development DB. Since more and more apps store configuration and state that is crucial to their execution in database tables, it can be a real pain to have change scripts that remove, add, and alter the proper rows. SQL Delta shows the rows in the database just like they would look in a Diff tool - changed, added, deleted.

一个很好的工具。以下是链接: http://www.sqldelta.com/

其他回答

必须阅读将数据库置于版本控制之下。查看K. Scott Allen的系列帖子。

When it comes to version control, the database is often a second or even third-class citizen. From what I've seen, teams that would never think of writing code without version control in a million years-- and rightly so-- can somehow be completely oblivious to the need for version control around the critical databases their applications rely on. I don't know how you can call yourself a software engineer and maintain a straight face when your database isn't under exactly the same rigorous level of source control as the rest of your code. Don't let this happen to you. Get your database under version control.

我们维护由ER工具(PowerAMC)生成的DDL(有时是DML)脚本。

我们有一个shell脚本工作台,它重命名以主干分支上的数字开头的脚本。 提交每个脚本并标记bugzilla编号。

这些脚本在需要时与应用程序代码合并到发布分支中。

我们有一个记录脚本及其状态的表。 在部署工具的每次安装中,每个脚本都按顺序执行,并记录在此表中。

任何数据库接口代码都应该进入版本控制(存储过程、函数等)。

对于结构和数据,这是一个判断。我个人对我的数据库保持一个干净的结构模板,但由于它们的大小,不将它们存储在版本控制中。但是将它存储在版本控制中是非常有益的,即使对于只有历史记录也是如此。

在我们的业务中,我们使用数据库更改脚本。运行脚本时,它的名称存储在数据库中,并且不会再次运行,除非删除该行。脚本是基于日期、时间和代码分支命名的,因此可以控制执行。

在脚本在实际环境中运行之前,已经完成了大量的测试,所以一般来说,“oopsies”只发生在开发数据库上。

Visual Studio中的新数据库项目提供了源代码控制和更改脚本。

他们有一个很好的工具,可以比较数据库,并可以生成一个脚本,将一个数据库的模式转换为另一个数据库,或者更新一个数据库中的数据以匹配另一个数据库。

db模式被“分解”以创建许多许多小的.sql文件,每个DDL命令一个描述db。

+tom


附加信息2008-11-30

作为一名开发人员,我在过去的一年里一直在使用它,我真的很喜欢它。它可以很容易地比较我的开发工作和生产工作,并生成用于发布的脚本。我不知道它是否缺少dba在“企业级”项目中所需要的特性。

因为模式被“分解”到sql文件中,所以源代码控制可以正常工作。

One gotcha is that you need to have a different mindset when you use a db project. The tool has a "db project" in VS, which is just the sql, plus an automatically generated local database which has the schema and some other admin data -- but none of your application data, plus your local dev db that you use for app data dev work. You rarely are aware of the automatically generated db, but you have to know its there so you can leave it alone :). This special db is clearly recognizable because it has a Guid in its name,

VS DB项目在将其他团队成员所做的DB更改集成到您的本地项目/相关DB方面做得很好。但是您需要采取额外的步骤,将项目模式与本地dev db模式进行比较,并应用mods。这是有道理的,但一开始似乎很尴尬。

DB项目是一个非常强大的工具。它们不仅生成脚本,而且可以立即应用它们。请确保不要用它破坏您的生产数据库。;)

我真的很喜欢VS DB项目,我希望在我所有的DB项目中使用这个工具。

+tom