生成用于 SQL Server 数据库版本控制的数据库脚本

发布于 2024-12-29 07:51:34 字数 206 浏览 1 评论 0原文

在负责任的编程和版本控制的范围内,我想开始对我的数据库更改进行版本控制,特别是因为我正在数据库实例上进行开发,然后将其转移到生产环境中。对于如何做到这一点,我还没有找到任何对我来说真正有意义的事情。我使用 Visual Studio 2010 Pro 作为我的 IDE。是否有一个文档可以使此过程变得简单并且能够相对轻松地检测数据库的更改?或者我应该在工作流程中进行哪些更改才能使这一切变得更容易?

In the scope of responsible programming and versioning, I would like to start to version my database changes especially since I am developing on my database instance then moving it to production. I haven't found any thing that truly makes sense to me on how to do this. I am using Visual Studio 2010 Pro as my IDE. Is there a document that makes this process simple and able to detect changes to the database with relative ease? Or what should I change in my workflow to make this easier?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的鱼塘能养鲲 2025-01-05 07:51:34

我过去成功完成此类事情的一种方法是通过 Sql 源代码控制。 Visual Studio 不为您提供此功能。

或者,您可以使用 SSMS 为您生成数据库脚本并将其保存为文件;然后你可以签入脚本。您可以选择是在一个文件中生成整个数据库脚本,还是逐个对象地生成。同步部分必须由您通过在生产中执行脚本来完成。总之,这是一场彻底的噩梦

Redgate 还提供 Sql Compare,这对于同步数据库非常有用。如果您或您的公司负担得起,请看看他们的产品。

One way that I've successfully done this sort of thing in the past, is via Sql Source Control. Visual Studio does not offer this functionality for you.

Alternatively, you can use SSMS to generate the Database scripts for you and save it as a file; then you can check in the script. You would chose whether you generate the whole DB script in one file or whether you do it on an object by object basis. The syncing part will have to be done by you by executing your scripts in production. In conclusion a total nightmare.

Redgate also offers Sql Compare, which is great for syncing databases. Take a look at their products if you or your company can afford them.

叶落知秋 2025-01-05 07:51:34

我们使用自己的内部数据库解决方案,它提供了正确的数据库版本控制所需的所有工具。虽然我意识到这可能不是每个人的完美解决方案,但我邀请您看一下它(它是开源的):bsn ModuleStore

版本控制方面如下:该工具可以半自动编写 SQL 脚本,并且可以将源代码重新格式化为统一格式。因此,对于同一来源,文件始终是相同的,无论脚本何时由何人编写;因此,这可以很好地与非锁定源代码控制系统(尤其是 SVN、Git 或 Mercurial)配合使用。

重新格式化将所有语句置于相同的形式(例如,处理诸如 ASINNEROUTER 等可选关键字),对所有内容进行脚本化到“dbo”模式(即使它位于不同的模式中),将所有标识符放入方括号([something])中,将所有保留字大写,进行缩进等。

除了版本控制之外,该工具的运行时部分可以比较正在运行的数据库和 CREATE 脚本(数据库源代码),并自动对所有非破坏性更改应用更新(例如更新索引、约束、视图、存储过程、触发器、自定义类型、新表等)。破坏性更改必须手动编写(表更改通常需要数据转换)。运行时将确保所有更新都在事务中执行,如果生成的数据库与 CREATE 脚本不匹配,则回滚,因此您可以安全地知道数据库完全符合应用程序所需的版本,即使它已被手动篡改。

此外,多个“模块”可以在单个数据库中使用。每个模块都存储为模式并独立于其他模式,从而可以从单个数据库添加或删除模块,并避免需要为应用程序的不同部分创建多个数据库。此外,使用模式来执行此操作可确保不存在名称冲突。

值得注意的是,该工具集不依赖于 SMO,它是自主的。

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.

黑白记忆 2025-01-05 07:51:34

将您的数据库脚本保存在 SVN 上。这是参考 如何使用 SVN Tortoise

将数据库脚本保存在 VSS 中。这是参考 什么是 VSS ?我们如何使用它?

在这两种情况下,您都可以跟踪所做的更改,以便将来您可以检查以版本形式保存的历史记录。

您还可以使用 Red Gate 产品

编辑

如何找出发生了变化的内容?

使用比较功能来检查以前版本中所做的更改。

如何将更改应用到实时数据库服务器?

从服务器下载最新的文件。

我希望您没有在合并脚本中使用表的 Drop 语句。因为它将删除表中的所有记录。

将针对 Stored Pro、View、Function 等执行 Drop 语句。

请注意,您必须在生产服务器上运行完整的最新数据库脚本文件,并执行下面提到的操作计划

1. Remove Drop Statement for Schema DDL
2. Add Drop/Create Statements for Stored Proc/Views
3. Include Alter statements DML of schema.

希望这一定会对您有所帮助。

在此处输入图像描述

Save Your Database scripts at SVN. Here is the Refernce How to use SVN Tortoise

OR

Save your database script at VSS. Here is the reference What is VSS ? How can we use that ?

In both cases you can keep track of the changes done so that in future you can check the history which in saved in the form of versions.

You can use Red Gate product also

EDIT

How do you pull out what what has changed?

Use comparison feature to check the changes made in the previous versions.

How do I apply the changes to the live database server?

Download the latest file from server.

I hope you are not using the Drop statements for the Table in your consolidated script. As it will delete all records from the table.

Drop statements will take place for Stored Pro, View, Function etc.

Please note that you have to run the complete latest database script file on the production server with below mentioned action plans

1. Remove Drop Statement for Schema DDL
2. Add Drop/Create Statements for Stored Proc/Views
3. Include Alter statements DML of schema.

Hope this will definitely help you.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文