DAC Usage1:Data-tier Application Overview

Data-tier application 是SQL Server 用于database scheme Migration的管理工具,跟DAC相关的artifact 有两种:dacpac 和 bacpac,分别用于encapsulate 数据库的Schema 和 data,但是我在SQL Server 2012 上没有看到bacpac 相关的feature,估计跟我的SQL Editon 有关,对于这个feature,我还是非常期待的,尽管不知道好不好用。

A data-tier application (DAC) is a logical database management entity that defines all of the SQL Server objects - like tables, views, and instance objects, including logins – associated with a user’s database. A DAC is a self-contained unit of SQL Server database deployment that enables data-tier developers and database administrators to package SQL Server objects into a portable artifact called a DAC package, also known as a DACPAC.

A BACPAC is a related artifact that encapsulates the database schema as well as the data stored in the database. Import a data-tier application (DAC) file – a .bacpac file – to create a copy of the original database, with the data, on a new instance of the Database Engine, or to Azure SQL Database. Export-import operations can be combined to migrate a DAC or database between instances, or to create a logical backup, such as creating an on-premise copy of a database deployed in SQL Database.

 

Benefits of Data-tier Applications                             

The lifecycle of most database applications involves developers and DBAs sharing and exchanging scripts and ad hoc integration notes for application update and maintenance activities. While this is acceptable for a small number of databases, it quickly becomes unscalable once databases grow in number, size, and complexity.

A DAC is a database lifecycle management and productivity tool that enables declarative database development to simplify deployment and management. A developer can author a database in SQL Server Data Tool database project and then build the database into a DACPAC for handoff to a DBA. The DBA can deploy the DAC using SQL Server Management Studio to a test or production instance of SQL Server or Windows Azure SQL Database. Alternatively, the DBA can use the DACPAC to upgrade a previously deployed database using SQL Server Management Studio. To complete the lifecycle, the DBA can extract the database into a DACPAC and hand it off to a developer to either reflect test or production adjustments, or to enable further database design changes in response to changes in the application.

The advantage of a DAC-driven deployment over a script driven exercise is that the tool helps the DBA with identifying and validating behaviors from different source and target databases. During upgrades, the tool warns the DBA if the upgrade might cause data loss, and also provide an upgrade plan. The DBA can evaluate the plan and then utilize the  tool to proceed with the upgrade.

DAC’s also support versioning to help the developer and the DBA maintain and manage the database lineage through its lifecycle.

DAC Concepts                                 

A DAC simplifies the development, deployment, and management of data-tier elements that support an application:

  • A data-tier application (DAC) is a logical database management entity that defines all SQL Server objects - such as tables, views, and instance objects – associated with a user’s database. It is a self-contained unit of SQL Server database deployment that enables data-tier developers and DBAs to package SQL Server objects into a portable artifact called a DAC package, or .dacpac file.

  • For a SQL Server database to be treated as a DAC, it must be registered – either explicitly by a user operation, or implicitly by one of the DAC operations. When a database is registered, the DAC version and other properties are recorded as part of the metadata of the database. Conversely, a database can also be unregistered and have its DAC properties removed.

  • In general, DAC tools are capable of reading DACPAC files generated by DAC tools from previous SQL Server versions, and can also deploy DACPAC’s to previous versions of SQL Server. However, DAC tools from earlier versions cannot read DACPAC files generated by DAC tools from later versions. Specifically:

    • DAC operations were introduced in SQL Server 2008 R2. In addition to SQL Server 2008 R2 databases, the tools support generation of DACPAC files from SQL Server 2008, SQL Server 2005 and SQL Server 2000 databases.

    • In addition to SQL Server 2012 databases, the tools shipped with SQL Server 2012 can read DACPAC files generated by DAC tools shipped with SQL Server 2008 R2. This includes databases from SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005, but not SQL Server 2000.

    • DAC tools from SQL Server 2008 R2 cannot read DACPAC files generated by tools from SQL Server 2012.

  • A DACPAC is a Windows file with a .dacpac extension. The file supports an open format consisting of multiple XML sections representing details of the DACPAC origin, the objects in the database, and other characteristics. An advanced user can unpack the file using the DacUnpack.exe utility that ships with the product to inspect each section more closely.

  • The user must be a member of the dbmanager role or assigned CREATE DATABASE permissions to create a database, including creating a database by deploying a DAC package. The user must be a member of the dbmanager role, or have been assigned DROP DATABASE permissions to drop a database.

DAC Operations                                 

A DAC supports the following operations:

  • EXTRACT – the user can extract a database into a DACPAC.

  • DEPLOY – the user can deploy a DACPAC to a host server. When the deployment is done from a manageability tool like SQL Server Management Studio or the Management Portal for SQL Azure, the resulting database in the host server is implicitly registered as a data-tier application.

  • REGISTER – the user can register a database as a data-tier application.

  • UNREGISTER – a database previously registered as a DAC can be unregistered.

  • UPGRADE – a database can be upgraded using a DACPAC. Upgrade is supported even on databases that are not previously registered as data-tier applications, but as a consequence of the upgrade, the database will be implicitly registered.

Backup Package (.bacpac)                             
A BACPAC is an artifact that encapsulates the database schema as well as the data stored in the database. The BACPAC is a Windows file with a .bacpac extension. Similar to the DACPAC, the BACPAC file format is open – the schema contents of the BACPAC are identical to that of the DACPAC. The data is stored in JSON format.

DACPAC and BACPAC are similar but they target different scenarios. A DACPAC is focused on capturing and deploying schema, including upgrading an existing database. The primary use case for a DACPAC is to deploy a tightly defined schema to development, test, and then production environments, and the reverse: capturing production’s schema and applying it to back to test and development environments.

A BACPAC, on the other hand, is focused on capturing schema and data. A BACPAC is the logical equivalent of a database backup and cannot be used to upgrade existing databases. The primary use case for a BACPAC is to move a database from one server to another - or from a local server to the cloud - and archiving an existing database in an open format.

A BACPAC supports two main operations:

  • EXPORT– The user can export the schema and the data of a database to a BACPAC.

  • IMPORT – The user can import the schema and the data into a new database in the host server.

Both these capabilities are supported by the database management tools: Server Management Studio, the Management Portal for SQL Azure, and the DACFx API.

 

参考文档:

MSDN Data-tier Applications

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

利用DAC(Data-tier Application)实现数据库结构迁移

你可能感兴趣的:(DAC Usage1:Data-tier Application Overview)