Create a Visual Studio Database project, host in DevOps Git-repo and deploy with pipelines
Learn to create a Visual Studio Database project, host it in a DevOps Git repo, and deploy it using Azure DevOps pipelines.
Introduction
I wrote some time ago a similar article...
But I missed to include precisely what I'm about to cover with this, properly create a Visual Studio Database Project within a MSFT DevOps Git-repo, I will also include how to import the schema of an existing Azure Database.
Setting the stage
Here's the scenario: your are part of a developer team working on an application (e.g. power bi report, webapp, pipeline, or any other) that uses a backend database to support your it (e.g. it's data source, configuration); the database is hosted on cloud in the Azure platform, and it is an Azure Database and its schema it has been developed via SQL Server Manager Studio (SSMS).
Currently there are tables, keys, foreign keys (relations), stored procedures, but they are not versioned-controlled, so, it's quite common for the team to have integration issues during day-to-day work and deployment to upper environments is expensive in time & effort as we need to detect schema differences and manually fix those using scripts... sounds familiar π€?
If so, I have two great news for you ... first is that a much better way to this and two, I'm about to show you how ππ
Tools and services used...
- Source SQL Database (Azure or On-prem)
- Target Azure SQL Database
- Azure Key Vault
- Visual Studio with SQL Server Data Tools
- Azure DevOps services
Solution
Assuming that you have a MSFT DevOps Git-repo and you have it locally cloned via Azure Visual Studio 2022 (or higher), then your VS would look something like this...
Add .gitignore file for Visual Studio
As described, this is to "Ignore Visual Studio temporary files, build results, and files generated by popular Visual Studio add-ons" you can either download a copy of the file from VisualStudio.gitignore or just copy its content as I did.
Create the database project
From Visual studio, click File > New > Project... and search the templates for SQL Server Database Project, select and click Next
Next we will have to Configure the new database project, but I think a quick notes with regards to the difference between the Project and the Solution.
- Projects are represents a single software component or module, such as a library, application, or service; they contain source code files, configuration settings, and build instructions
- Solution is a container that holds one or more related projects, they facilitate managing multiple projects together as a cohesive whole
With this in mind, hope it would make sense the following configuration...
Outcome should base somehow similar to this...
Import schema from an existing database
Simple, let's just connect to an existing database an import it's schema, click
Select the database you would like to use as a source and, if otherwise required, configure Import Setting as shown below.
Outcome shouldn't be a surprise, right? The database schema was imported to the project!
Configure the project's to be Azure-deployment compatible
Go to the database project Properties (right click > Properties) and select as Target Platform: Microsoft Azure SQL Database
Deploy using a pipelines
On the next part of the article, I'll explain just the most relevant parts of the code
In the example shared in the code, I'm using IaC to create the report's back-end database along it's with the SQL Server Logic server, notice that I'm assigning an Entra's ID security group (1) to be the server's Administrator (2)(3).
I created group SQLServerAdministrators manually and added as member the security principal representing my AzDO Service Connection (1)
The result is that SQL Logic server admin is the security group SQLServerAdministrators and by proxy, pipeline via its service connection has admin privileges to deploy the DACPAC
We need to Build the database project (1), this will create a DACPAC (Data-tier Application Package) and create an Artifact (2)
We then use the SqlAzureDacpacDeployment yaml activity (1) to deploy the DACPAC towards any target database
DevOps pipeline (2) will do the rest ππ€ that is, applying a Data-tier Application Package (DACPAC) to a target database
Provisioning Database Security
Visit this article where I explain in detail how to do it π
Conclusion
DACPAC deployment ensures consistent schema changes across databases, simplifying database management and promoting automation via...
- Consistency: Ensures consistent schema across environments (development, testing, production).
- Version Control: DACPACs can be versioned and tracked in source control.
- Automated Deployment: Easily deploy schema changes without manual scripts.
- Safety: Deployment scripts are generated, allowing review before execution.
Call to action
I explained the code, now is your turn to ...