DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade

一,Introduce

Extract DAC 是从现存的DB中创建DAC,抽取DB Object的definition 和 与之相关的实例级别的元素,比如Login,以及Login 和User之间的关系。

The extraction process creates a DAC package file that contains definitions of the database objects and their related instance-level elements. For example, a DAC package file contains the database tables, stored procedures, views, and users, along with the logins that map to the database users.

 

Register DAC是将DAC包含的object definition 和 related instance-level elements 注册到msdb系统数据库中,从视图 msdb.dbo.sysdac_instances 查看注册的DAC Instance,从表 msdb.[dbo].[sysdac_history_internal] 查看注册的历史记录,后缀是internal,不要修改这些表的记录。

Registration process builds a data-tier application (DAC) definition that describes the objects in an existing database, and register the DAC definition in the msdb system database.

 

Deploy DAC是在SQL Server 实例级别上使用DAC包含的object definition 和 related instance-level elements创建新的DB

deploy a data-tier application (DAC) from a DAC package to an existing instance of the Database Engine or SQL Database by using a wizard or a PowerShell script. The deployment process registers a DAC instance by storing the DAC definition in the msdb system database, creates a database, and then populates the database with all the database objects defined in the DAC.

 

Upgrade DAC是指对现存DB的object definition 和 related instance-level elements 进行upgrade,进行db schema 版本升级。

A DAC upgrade is an in-place process that alters the schema of the existing database to match the schema defined in a new version of the DAC. The new version of the DAC is supplied in a DAC package file.

二,Usage Example

1,Create Data

Use DAC_Study
go

Create Table dbo.dt_1
(
id int,
name varchar(10),
CreateDate datetime)
go

insert into dbo.dt_1
DEFAULT values;
go 11


create view vw_dt
as

select id,name,CreateDate
from dbo.dt_1

go


create procedure dbo.usp_get_ID_Name
as
begin

    select id,name
    from dbo.dt_1
end
go

create function dbo.udf_GetDate( @id int)
returns DateTime
begin
    
    declare @dt datetime

    select @dt=CreateDate
    from dbo.dt_1

    return @dt
end
go


2, Extract a DAC

选中一个DB,点击Tasks->Extract Data-tier Application...,打开Extract DAC Wizard

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第1张图片

从Left Pane 可以看出,Extract DAC主要分为Set Properties,Validation 和 build Package 三步。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第2张图片

Step1, Set DAC Properties

Application name 属性必须填写正确,用于标识DAC。

These properties are used to identify the DAC and help distinguish it from others.

Application Name- This name identifies the DAC. It can be different than the name of the DAC package file and should describe your application.

Overwrite existing file - Select this check box to replace the DAC package file if one already exists with the same name.

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第3张图片

Step2,Validation and Summary

对属性设置的Validation 和 Summary

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第4张图片

Step3, build Package

创建DAC,在Right Pane中有Result 字段,用以 indicate 创建DAC的结果。

创建DAC成功之后,会在Specified Folder下生成 DAC_Study.dacpac 文件。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第5张图片

 

3,unpack DAC

unpack DAC 就是拆包,打开dacpac 文件,查看文件的内容。

Use the Unpack Data-tier Application dialog box to unzip the scripts and files from a data-tier application (DAC) package. The scripts and files are placed in a folder where they can be reviewed before the package is used to deploy the DAC into a production system. The contents of one DAC can also be compared with the contents of another package unpacked to another folder.

 

右键点击dacpac文件,弹出Unpack...,选择拆分文件所存放的folder。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第6张图片

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第7张图片

拆解之后,共有四个文件,能够打开逐一查看。

A Transact-SQL script that contains the statements for creating the objects defined in the DAC.

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第8张图片

4,Register DAC

在目标实例上创建一个空的DB,DB 不一定和Extract的DB 同名,这里创建的Empty DB是DAC_Test,点击Task->Register as Data-tier Application...,弹出Register DAC Wizard

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第9张图片

 

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第10张图片

 

Step1, Set Properties

Application Name 属性必须填写正确,必须和Extract DAC时填写的Application Name 相同。

Application name. - A string that specifies the name used to identify the DAC defintion, the field is been populated with the database name.

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第11张图片

Step2,Vaildation and Summary

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第12张图片

Step3, Register DAC

Register DAC,并查看Register的结果。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第13张图片

Step4, 从MSDB system DB中查看Register DAC的结果

从视图 msdb.dbo.sysdac_instances 查看注册的DAC Instance,从表 msdb.[dbo].[sysdac_history_internal] 查看注册的历史记录,后缀是internal,不要修改这些表的记录。

5,Deploy DAC

在一个SQL Server 实例上Deploy DAC,使用DAC包含的Schema创建新的DB。

You can deploy a data-tier application (DAC) from a DAC package to an existing instance of the Database Engine or SQL Database by using a wizard or a PowerShell script. The deployment process registers a DAC instance by storing the DAC definition in the msdb system database。

Step1,右击DataBases,弹出快捷菜单,点击 Deploy Data-tier Application...,弹出Deploy DAC Wizard。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第14张图片

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第15张图片

Step2,Select package

 DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第16张图片

Step3,Configure deployment properties

name 是新的Database name,这个name 属性可以随便输入。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第17张图片

 

Step4,Summary

DAC 部署信息汇总,Application Name 是从dacpac文件中读取的。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第18张图片

Step5,Deploy DAC

部署DAC,生成一个新的DB,最终注册Metadata。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第19张图片

 

三,Upgrade DAC

Upgrade DAC 是对现存DB Schema的更新和升级,前提是必须有一个DB,而Deploy DAC是创建新的DB。

A DAC upgrade is an in-place process that alters the schema of the existing database to match the schema defined in a new version of the DAC. The new version of the DAC is supplied in a DAC package file.

选中注册的DB,点击 Tasks->Upgrade Data-tier Application...,打开 Upgrader DAC Wizard

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第20张图片

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第21张图片

Step1,Select package

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第22张图片

Step2,Detect Change

Use this page reports the results of the wizards check for changes made to the database that make it's schema different than the schema definition stored in the DAC metadata in msdb. For example, if CREATE, ALTER, or DROP statements have been used to add, change, or remove objects from the database after the DAC was originally deployed. The page first displays a progress bar, and then reports the results of the analysis.

Detecting change, this may take a few minutes - Displays a progress bar as the wizard checks for differences between the current schema of the database and the objects in the DAC definition.

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第23张图片

Step3,Option

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第24张图片

Step4,Review Upgrade Plan

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第25张图片

Step5,Summary

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第26张图片

Step6,Upgrade DAC

查看Action,Upgrade DAC 就是一个Deployment 的过程,最终Register Metadata。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第27张图片

四,Check Generation

打开 DAC_Test DB,查看产生的Object,可以看到,通过Extract,Register 和 Upgrade,实现DB Schema的Migration。

DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade_第28张图片

推荐阅读:

Chapter 3: Data-Tier Applications

参考文档:

Data-tier Applications

将数据库迁移到SQL Azure的三种工具

你可能感兴趣的:(DAC Usage2:通过Extract,Register,Deploy 和 Upgrade DAC,实现DB Schema的Migration和Upgrade)