gpt4 book ai didi

sql - SQL版本控制方法

转载 作者:行者123 更新时间:2023-12-03 22:30:51 30 4
gpt4 key购买 nike

关于SO的SQL版本控制和Web上的大量资源,SO上存在几个问题,但是我找不到能够完全覆盖我要做的事情。

首先,我在这里谈论一种方法。我熟悉各种源代码控制应用程序,也熟悉Red Gate的SQL Compare等工具,而且我知道如何编写一个应用程序来自动检查源代码控制系统中的内容。如果有一种工具在提供全新方法方面特别有用,或者具有有用和罕见的功能,那就太好了,但是对于上面提到的任务,我已经设定好了。

我要满足的要求是:


数据库模式和查找表数据已版本化
对用于较大表的数据修复的DML脚本进行了版本控制
可以将服务器从版本N升级到版本N + X,其中X不一定总是1
代码在版本控制系统中没有重复-例如,如果我在表中添加一列,则不必确保更改同时在创建脚本和更改脚本中进行
系统需要支持具有不同版本的应用程序的多个客户端(试图将它们全部升级到1或2个发行版之内,但尚未发行)


一些组织将增量更改脚本保留在其版本控制中,并且要从版本N升级到N + 3,您必须为N-> N + 1然后N + 1-> N + 2然后N + 2-> N +运行脚本3。这些脚本中的某些脚本可能是重复的(例如,添加了一个列,但随后又对其进行了更改以更改数据类型)。由于某些客户端数据库可能非常大,因此我们试图避免这种重复,因此这些更改可能会花费比必要时间更长的时间。

一些组织将只在每个版本级别保留完整的数据库构建脚本,然后使用SQL Compare之类的工具将数据库升级到其中一个版本。这里的问题是混合DML脚本可能是一个问题。设想一个场景,我添加一列,使用DML脚本填充所述列,然后在更高版本中更改该列名称。

也许有一些混合解决方案?也许我只是要求太多?任何想法或建议将不胜感激。

如果主持人认为这更适合作为社区Wiki,请告诉我。

谢谢!

最佳答案

我为此苦苦挣扎了几年,最近才采取了一项效果很好的策略。我生活的重点:


无需从应用程序独立对数据库进行版本控制
所有数据库更新脚本均应幂等


结果,我不再创建任何类型的版本表。我只是将更改添加到编号的.sql文件序列中,这些更改可以在任何给定时间应用而不会破坏数据库。如果使事情变得容易,我将为该应用程序编写一个简单的安装程序屏幕,以允许管理员在需要时运行这些脚本。

当然,此方法确实对数据库设计有一些要求:


所有架构更改均通过脚本完成-无需GUI工作。
必须格外小心,以确保所有键,约束等都被命名,以便以后如有必要,可以在以后的更新脚本中引用它们。
所有更新脚本都应检查现有条件。


最近项目的示例:

001.sql:

if object_id(N'dbo.Registrations') is null 
begin
create table dbo.Registrations
(
[Id] uniqueidentifier not null,
[SourceA] nvarchar(50) null,
[SourceB] nvarchar(50) null,
[Title] nvarchar(50) not null,
[Occupation] nvarchar(50) not null,
[EmailAddress] nvarchar(100) not null,
[FirstName] nvarchar(50) not null,
[LastName] nvarchar(50) not null,
[ClinicName] nvarchar(200) not null,
[ClinicAddress] nvarchar(50) not null,
[ClinicCity] nvarchar(50) not null,
[ClinicState] nchar(2) not null,
[ClinicPostal] nvarchar(10) not null,
[ClinicPhoneNumber] nvarchar(10) not null,
[ClinicPhoneExtension] nvarchar(10) not null,
[ClinicFaxNumber] nvarchar(10) not null,
[NumberOfVets] int not null,
[IpAddress] nvarchar(20) not null,
[MailOptIn] bit not null,
[EmailOptIn] bit not null,
[Created] datetime not null,
[Modified] datetime not null,
[Deleted] datetime null
);
end

if not exists(select 1 from information_schema.table_constraints where constraint_name = 'pk_registrations')
alter table dbo.Registrations add
constraint pk_registrations primary key nonclustered (Id);

if not exists (select 1 from sysindexes where [name] = 'ix_registrations_created')
create clustered index ix_registrations_created
on dbo.Registrations(Created);

if not exists (select 1 from sysindexes where [name] = 'ix_registrations_email')
create index ix_registrations_email
on dbo.Registrations(EmailAddress);

if not exists (select 1 from sysindexes where [name] = 'ix_registrations_email')
create index ix_registrations_name_and_clinic
on dbo.Registrations (FirstName,
LastName,
ClinicName);


002.sql

/**********************************************************************
The original schema allowed null for these columns, but we don't want
that, so update existing nulls and change the columns to disallow
null values
*********************************************************************/

update dbo.Registrations set SourceA = '' where SourceA is null;
update dbo.Registrations set SourceB = '' where SourceB is null;
alter table dbo.Registrations alter column SourceA nvarchar(50) not null;
alter table dbo.Registrations alter column SourceB nvarchar(50) not null;

/**********************************************************************
The client wanted to modify the signup form to include a fax opt-in
*********************************************************************/

if not exists
(
select 1
from information_schema.columns
where table_schema = 'dbo'
and table_name = 'Registrations'
and column_name = 'FaxOptIn'
)
alter table dbo.Registrations
add FaxOptIn bit null
constraint df_registrations_faxoptin default 0;


003.sql,004.sql等...

在任何给定的时间,我都可以在任何状态下对数据库运行整个脚本系列,并且知道可以立即使用当前版本的应用程序来加快运行速度。因为所有内容都是脚本编写的,所以构建一个简单的安装程序来执行此操作要容易得多,并且将模式更改添加到源代码管理中完全没有问题。

关于sql - SQL版本控制方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2774587/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com