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
*.jfmBuild 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.
Laying out the repository
Bringing an existing database under source control
The following steps create the baseline for a database that has never been in source control.
-
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.
-
Build and fix
Build the project, add database references for system or cross-database objects and fix any errors.
-
Commit the baseline
Commit the project to a new branch and merge it as the baseline.
-
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.
Creating the baseline
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.
| File | Why it is committed |
|---|---|
| .sqlproj | Defines the SDK version, target platform, references, SQLCMD variables and build options |
| Object .sql files | One CREATE statement per object, which is the declarative model of the schema |
| Pre- and post-deployment scripts | Reference data and data migrations that run on every publish |
| .refactorlog | Records renames so that SqlPackage renames objects instead of dropping them |
| Publish profiles (.publish.xml) | Per-environment properties and variables, without credentials |
| Test project | Unit 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.
Files in source control
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.
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.