如何对 SQL Server 数据库进行版本控制?

发布于 2024-07-12 03:50:00 字数 194 浏览 5 评论 0原文

我需要将版本放入 SQL Server 2005 数据库并可以从 .NET 应用程序访问这些版本。 我的想法是在数据库上使用名称为“版本”的扩展属性,当然该值将是数据库的版本。 然后我可以使用 SQL 来解决这个问题。 我的问题是这听起来像是一个好计划还是有更好的方法向 SQL Server 数据库添加版本?

假设我无法使用表来保存元数据。

I need to put versions onto a SQL Server 2005 database and have these accessible from a .NET Application. What I was thinking is using an Extended Properties on the Database with a name of 'version' and of course the value would be the version of the database. I can then use SQL to get at this. My question is does this sound like a good plan or is there a better way for adding versions to a SQL Server database?

Lets assume I am unable to use a table for holding the Metadata.

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

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

发布评论

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

评论(5

野鹿林 2024-07-19 03:50:00

我这样做:

创建一个架构表:

CREATE TABLE [dbo].[SchemaVersion](
    [Major] [int] NOT NULL,
    [Minor] [int] NOT NULL,
    [Build] [int] NOT NULL,
    [Revision] [int] NOT NULL,
    [Applied] [datetime] NOT NULL,
    [Comment] [text] NULL)

更新架构:

INSERT INTO SchemaVersion(Major, Minor, Build, Revision, Applied, Comment)
VALUES (1, 9, 1, 0, getdate(), 'Add Table to track pay status')

获取数据库架构版本:

SELECT TOP 1 Major, Minor, Build from SchemaVersion
ORDER BY Major DESC, Minor DESC, Build DESC, Revision DESC

改编自我在 编码恐怖

I do this:

Create a schema table:

CREATE TABLE [dbo].[SchemaVersion](
    [Major] [int] NOT NULL,
    [Minor] [int] NOT NULL,
    [Build] [int] NOT NULL,
    [Revision] [int] NOT NULL,
    [Applied] [datetime] NOT NULL,
    [Comment] [text] NULL)

Update Schema:

INSERT INTO SchemaVersion(Major, Minor, Build, Revision, Applied, Comment)
VALUES (1, 9, 1, 0, getdate(), 'Add Table to track pay status')

Get database Schema Version:

SELECT TOP 1 Major, Minor, Build from SchemaVersion
ORDER BY Major DESC, Minor DESC, Build DESC, Revision DESC

Adapted from what I read on Coding Horror

不交电费瞎发啥光 2024-07-19 03:50:00

我们按照您的描述使用扩展属性,并且效果非常好。

我认为有一张桌子有点过分了。 如果我想跟踪数据库中的差异,我会使用源代码管理并将所有数据库生成脚本保留在其中。

我还使用了一些 ER 图工具来帮助我跟踪数据库版本的更改。 这超出了实际应用程序的范围,但它使我能够快速看到发生了什么变化。

我认为是 CASEStudio,或者类似的东西。

We use the Extended Properties as you described it and it works really well.

I think having a table is overkill. If I want to track the differences in my databases I use source control and keep all the db generation scripts in it.

I've also used some ER diagram tools to help me keep track of changes in DB versions. This was outside the actual application but it allowed me to quickly see what changed.

I think it was CASEStudio, or something like that.

你怎么敢 2024-07-19 03:50:00

如果我正确理解您的问题(区分内部数据库版本,例如应用程序内部版本号),您可能会拥有某种 SYSVERSION 表,其中包含包含此信息的单行数据。

更方便查询。

还可以包含多列有用信息,或多行表示数据库副本升级的不同时间。

更新:好吧,如果您无法使用表来保存元数据,那么某种外部信息(硬盘驱动器上的 INFO 文件?)或扩展属性将是可行的方法。

不过,我仍然喜欢表的想法:)您始终可以使用安全性来使其只能通过自定义存储过程 get_ db_version 或其他东西来访问。

If I understand your question right (differentiating between internal database versions, like application build numbers), you could have some sort of SYSVERSION table that held a single row of data with this info.

Easier to query.

Could also contain multiple columns of useful info, or multiple rows that represent different times that copy of the database was upgraded.

Update: Well, if you can't use a table to hold the metadata, then either external info of some sort (an INFO file on the hard drive?) or extended properties would be the way to go.

I still like the table idea, though :) You could always use security to only make it accessable through a custom stored proc get_ db_version or something.

殤城〤 2024-07-19 03:50:00

最好的方法是有两个过程:一个标头用于控制插入的内容,并验证一个页脚以插入数据(无论发布是否良好)。 正文将包含您的脚本。

您需要一个包装器来封装您的脚本并记录所有信息:到目前为止的版本、已应用的脚本编号、应用日期、应用日期、发布结果“失败或成功”。

The best way to do is to have 2 procedures: one header to control what is being inserted and validations a footer to insert the data if the release is good or not. The body will contain your scripts.

You need a wrapper that will encapsulate your script and record all the info: as far release, script number been applied, applyby, applydate date, release outcome "failed or succeeded".

生活了然无味 2024-07-19 03:50:00

我正在使用类似于马特解决方案的专用表。 除此之外,数据库更改必须在对架构应用任何更改之前检查当前版本。 如果当前版本小于预期,则脚本将终止并出现致命错误。 如果当前版本大于预期,则脚本会跳过当前步骤,因为该步骤有时已经执行过。

以下是编写数据库更改脚本的完整解决方案,其中包含示例和约定:如何维护 SQL服务器数据库架构版本

I am using dedicated table similar to Matt's solution. In addition to that, database alters must check current version before applying any changes to the schema. If current version is smaller than expected, then the script terminates with fatal error. If current version is larger than expected, then script skips current step because that step has already been performed sometimes in the past.

Here is the complete solution with examples and conventions in writing database alter scripts: How to Maintain SQL Server Database Schema Version

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