在负责任的编程和版本控制范围内,我想开始对我的数据库更改进行版本控制,特别是因为我正在开发我的数据库实例,然后将其转移到生产环境中。我还没有发现任何对我来说真正有意义的事情。我使用 Visual Studio 2010 Pro 作为我的 IDE。是否有文档可以使此过程变得简单并能够相对轻松地检测到数据库的更改?或者我应该在我的工作流程中进行哪些更改以使其更容易?
3 回答
我过去成功完成此类事情的一种方法是通过Sql Source Control。Visual Studio 不为您提供此功能。
或者,您可以使用 SSMS 为您生成数据库脚本并将其保存为文件;然后您可以签入脚本。您可以选择是在一个文件中生成整个 DB 脚本,还是逐个对象生成。同步部分必须由您通过在生产中执行脚本来完成。总之,一场噩梦。
Redgate 还提供 Sql Compare,非常适合同步数据库。如果您或您的公司负担得起,请查看他们的产品。
We use our own DB solution in-house which brings all the tools required for proper DB versioning. While I realize that it may not be a perfect solution for everyone, I invite you to have a look at it (it is open-source): bsn ModuleStore
The versioning aspect is as follows: the tool can script out the SQL semi-automatically, and it does reformat the source code to be in an uniform format. The files will therefore always be identical for the same source, no matter of when and by whom something has been scripted; this therefore works nicely with non-locking source control systems (especially SVN, Git or Mercurial).
The reformat puts all statements in the same form (e.g. optional keywords such as AS
, INNER
, OUTER
etc. are dealt with), scripts everything to the "dbo" schema (even if it was in a different one), puts all identifiers into the square braces ([something]
), uppercases all reserved words, does the indentation etc.
Besides versioning, the runtime part of the tool can diff the running DB and the CREATE scripts (DB source code) and apply updates automatically for all non-destructive changes (e.g. updating indexes, constraints, views, stored procedures, triggers, custom types, new tables etc.). Destructuve changes have to be scriped manually (table changes which then usually require data transformations). The runtime will make sure that all updates are performed in a transaction and rollback if the resulting DB doesn't match the CREATE scripts, therefore you get the safety of knowing that the DB is exactly on the version required by the application, even if it has been tampered with manually.
Also, multiple "modules" can be used in a single database. Each module is stored as a schema and independent of other schemas, thereby making it possible to add or remove modules from one single DB, and avoiding the need to create multiple databases for different parts of the application. Also, the use of schemas to do this makes sure that there are no name collisions.
It may be worth noting that the toolset has no dependency to the SMO, it is autonomous.
将您的数据库脚本保存在 SVN。这是参考 如何使用 SVN Tortoise
或者
将数据库脚本保存在 VSS。这是参考 什么是VSS?我们如何使用它?
在这两种情况下,您都可以跟踪所做的更改,以便将来您可以检查以版本形式保存的历史记录。
您也可以使用 Red Gate 产品
编辑
你如何找出改变了什么?
使用比较功能检查以前版本中所做的更改。
如何将更改应用到实时数据库服务器?
从服务器下载最新文件。
我希望您没有在合并脚本中对表使用 Drop 语句。因为它将从表中删除所有记录。
将针对 Stored Pro、View、Function 等进行删除语句。
请注意,您必须使用下面提到的行动计划在生产服务器上运行完整的最新数据库脚本文件
1. Remove Drop Statement for Schema DDL
2. Add Drop/Create Statements for Stored Proc/Views
3. Include Alter statements DML of schema.
希望这肯定会对您有所帮助。