Lesson 1 of 4 Source Control and Deployment Pipelines

Source Control for SQL Projects

This lesson explains how to organize SQL projects in Git and bring an existing database under source control.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe a repository layout for SQL projects.
  • Explain how to baseline an existing database.
  • Identify how SQL database in Fabric integrates with Git.

Repository layout

A SQL project usually lives in the same repository as the application it serves, so a feature's schema and code change together in one pull request. Inside the project, files are organized by schema and object type, for example Sales/Tables/Orders.sql and Sales/StoredProcedures/usp_CreateOrder.sql, with post-deployment and data scripts in a Scripts folder, publish profiles per environment and tests in a separate project.

# .gitignore for SQL projects
bin/
obj/
*.user
*.dbmdl
*.jfm

Build output and user-specific files are excluded. Publish profiles can be committed if they contain only properties and variables, never passwords or connection strings with credentials.

Scenario questions

Laying out the repository

Exam skills 2.3.4

Choose how to organize SQL project files.

Log in to try this activity Interactive activities are free with an account.

Bringing an existing database under source control

The following steps create the baseline for a database that has never been in source control.

  1. Create the project from the database

    Use the SQL Database Projects extension's create-from-database option, or SqlPackage Extract with the option to produce files, to generate one file per object.

  2. Build and fix

    Build the project, add database references for system or cross-database objects and fix any errors.

  3. Commit the baseline

    Commit the project to a new branch and merge it as the baseline.

  4. Change only through the project

    From then on, all schema changes are made in the project and deployed by the pipeline; direct changes are treated as drift.

Put in order

Creating the baseline

Exam skills 2.3.4

Put the steps for bringing an existing database under source control in order.

Log in to try this activity Interactive activities are free with an account.

SQL database in Fabric and Git

Fabric workspaces can connect to a Git repository in Azure DevOps or GitHub. A SQL database in Fabric is then represented in the repository as a SQL project, and changes flow in both directions: commits from the workspace, and updates from Git into the workspace. This allows the same pull request and pipeline practices for Fabric-hosted databases.

Files that must be committed

A deployment can only reproduce what the repository contains, so some files that look like tooling are essential. The following table lists what belongs in source control for a SQL project and why.

FileWhy it is committed
.sqlprojDefines the SDK version, target platform, references, SQLCMD variables and build options
Object .sql filesOne CREATE statement per object, which is the declarative model of the schema
Pre- and post-deployment scriptsReference data and data migrations that run on every publish
.refactorlogRecords renames so that SqlPackage renames objects instead of dropping them
Publish profiles (.publish.xml)Per-environment properties and variables, without credentials
Test projectUnit tests that the pipeline runs after deploying to a test database

The refactor log is the file most often lost. If a developer renames a column with the refactor tooling but the .refactorlog file is not committed, the pipeline's build has no record of the rename, so SqlPackage plans a drop and an add, and the deployment either loses the column's data or is stopped by BlockOnPossibleDataLoss. Environment-specific values, such as a linked server name or a storage URL, belong in SQLCMD variables with values supplied by publish profiles or the pipeline, so the object files stay identical in every environment.

Match the pairs

Files in source control

Exam skills 2.3.4

Match each committed file with why it matters.

Log in to try this activity Interactive activities are free with an account.
Worked example

One change, two layers

A feature adds a column to the Orders table and an API endpoint that returns it. In the past, the API was deployed before the schema change, causing errors. How should the repository and changes be organized?

Keep the SQL project and the API in one repository and make both changes in one pull request, with a pipeline that deploys the database before the API. The review sees the whole feature, and the deployment order is enforced. Separate repositories with independent pipelines make it harder to keep the two layers in step, and manual scripts bypass review.

Check yourself

Which folders of a SQL project are excluded from Git?

Show answer

bin and obj, which hold build output.

Exam Tips

Remember for the exam

  • One file per object, organized by schema and type; exclude bin and obj.
  • Baseline an existing database by creating a project from it.
  • After the baseline, change the schema only through the project.
  • SQL database in Fabric can sync with Git as a SQL project.