暗夜行者 发表于 2023-9-14 12:00:24

SQL版本控制方法

关于SO的SQL版本控制和网络上的大量资源,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。这些脚本中的某些脚本可能是重复的(例如,添加了列,但随后又对其进行了更改以更改数据类型)。由于某些客户端DB可能非常大,因此我们试图避免这种重复,因此这些更改可能会花费比必要时间更长的时间。
一些组织将仅在每个版本级别保留完整的数据库构建脚本,然后使用SQL
Compare之类的工具将数据库升级到其中一个版本。这里的问题是,混合DML脚本可能是一个问题。想象一下一个场景,我添加一列,使用DML脚本填充该列,然后在更高版本中更改该列名。
也许有一些混合解决方案?也许我只是要求太多?任何想法或建议,将不胜感激。
如果主持人认为这更适合作为社区Wiki,请告诉我。
谢谢!
               
解决方案:
               


                我为此苦苦挣扎了几年,最近才采取了一项效果很好的策略。我生活的重点:
无需从应用程序独立对数据库进行版本控制
所有数据库更新脚本均应幂等

结果,我不再创建任何类型的版本表。我只是将更改添加到编号的.sql文件序列中,这些更改可以在任何给定时间应用而不会破坏数据库。如果使事情变得容易,我将为该应用程序编写一个简单的安装程序屏幕,以允许管理员在需要时运行这些脚本。
当然,此方法确实对数据库设计有一些要求:
所有架构更改均通过脚本完成-无需GUI工作。
必须格外小心,以确保所有键,约束等都被命名,以便以后如有必要,可以在以后的更新脚本中引用它们。
所有更新脚本都应检查现有条件。

来自最近项目的示例:
001.sql:
if object_id(N'dbo.Registrations') is null
begin
    create table dbo.Registrations
    (
                        uniqueidentifier not null,
                     nvarchar(50)   null,
                     nvarchar(50)   null,
                     nvarchar(50)   not null,
                  nvarchar(50)   not null,
                nvarchar(100)    not null,
                   nvarchar(50)   not null,
                  nvarchar(50)   not null,
                  nvarchar(200)    not null,
               nvarchar(50)   not null,
                  nvarchar(50)   not null,
               nchar(2)         not null,
                nvarchar(10)   not null,
         nvarchar(10)   not null,
      nvarchar(10)   not null,
             nvarchar(10)   not null,
                int            not null,
                   nvarchar(20)   not null,
                   bit            not null,
                  bit            not null,
                     datetime         not null,
                  datetime         not null,
                     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 = 'ix_registrations_created')
    create clustered index ix_registrations_created
      on dbo.Registrations(Created);
if not exists (select 1 from sysindexes where = 'ix_registrations_email')
    create index ix_registrations_email
      on dbo.Registrations(EmailAddress);
if not exists (select 1 from sysindexes where = '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等…
在任何给定的时间,我都可以在任何状态下对数据库运行整个脚本系列,并且知道可以立即使用当前版本的应用程序来加快运行速度。因为所有内容都是脚本编写的,所以构建一个简单的安装程序来执行此操作要容易得多,并且将模式更改添加到源代码管理中完全没有问题。
页: [1]
查看完整版本: SQL版本控制方法