最近我採取了一項似乎工作得很好的策略之前,我爲此奮鬥了好幾年。我生活的關鍵點:
- 數據庫並不需要從應用程序
- 所有數據庫更新腳本應該是冪等
。其結果是獨立版本,我不再產生任何版本表的種類。我只需將更改添加到可在任何給定時間應用而不會損壞數據庫的.sql文件的編號序列中。如果它使事情變得簡單,我將爲應用程序編寫一個簡單的安裝程序屏幕,以便管理員隨時運行這些腳本。
當然,這種方法確實強加在數據庫設計的幾個要求:
- 所有架構更改通過腳本完成的 - 沒有圖形用戶界面的工作。
- 必須格外小心,以確保所有鍵,約束等都被命名,以便在必要時可以通過稍後的更新腳本來引用它們。
- 所有更新腳本都應檢查現有條件。從最近的一個項目
例子:
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等..
在任何給定的時間,我都可以針對數據庫運行整個系列腳本在任何狀態下都可以使用,並且知道使用當前版本的應用程序立即可以加快速度。因爲一切都是腳本化的,所以構建一個簡單的安裝程序來執行此操作會更容易,並且它將架構更改添加到源代碼管理中根本沒有問題。
不知道你是否看到這一個,但我得到了一些合理的答案:http://stackoverflow.com/questions/2401229/database-structure-and-source-control-best-practice – Paddy 2010-05-05 15:31:30
這是否有幫助? http://msmvps.com/blogs/deborahk/archive/2010/05/02/vs-2010-database-project-an-introduction.aspx請注意,文章底部有更多鏈接。 – 2010-05-05 15:33:27
@Paddy - 感謝您的鏈接。 pdc有一個很好的解決方案,與我們正在考慮的解決方案相匹配,但腳本創建任務被外包給印度(不是我的選擇),而且其專業知識有限,因此一些更復雜的代碼可能會成爲問題。儘管如此,它仍然在桌面上。 – 2010-05-05 16:27:21