Prepstellar

DP-700 · Workspace Lifecycle Management

29 cards

SQL Database Source Control

Swipe, scroll or use ← →
  1. Turn a live database into project code

    A database project is the database expressed as code: the definitions of its objects, kept in a repository so they can be reviewed, compared, and rebuilt. A SQL database in Fabric has Git integration that produces this project for you, so a team can track the definitions of its database objects over time.

    The starting point is a workspace connected to an Azure DevOps or GitHub remote repository. The repository connection applies at the workspace level, and a single branch of the repository is associated with that workspace. How to connect a workspace and how Git integration works in general belong to the Git integration lesson. Here the subject is what happens to the database.

    The integration works in two directions, and they do different things.

    Direction What Fabric does
    Commit Reads the object definitions from the live database and writes them as code to the remote repository.
    Update Validates the code in the repository, then applies a differential change to the database.
    1 / 29
  2. Turn a live database into project code

    To commit, create or change objects with any tool (the Fabric SQL editor, SQL Server Management Studio, or the MSSQL extension for Visual Studio Code), open the Source control panel, select the checkbox next to the database, and select Commit.

    In the repository, the database is a folder named <yourdatabase>.SQLDatabase. Inside it, each object is a .sql file. A table, for example, is a file such as dbo/Tables/MyTable.sql that holds its CREATE TABLE statement:

    CREATE TABLE dbo.MyTable
    (
        Id INT PRIMARY KEY,
        ExampleColumn NVARCHAR(50)
    );
    

    The file holds the definition of the table, not its rows. As development continues, including edits to objects that already exist, the same commit step keeps the code in step with the database, and the repository shows the history of every object.

    2 / 29
  3. Quick check

    A team creates tables directly in a test database and commits the database from the Source control panel. What reaches the repository?

    1. AThe rows of each table, exported as data files

      Commit captures the definitions of the objects. The rows that the tables contain stay in the database.

    2. BThe object definitions, written as code files

      Right. The service reads the object definitions from the live database and writes them to the remote repository, one .sql file per object.

    3. CA backup of the database that Update can restore

      There is no backup in the repository. Update builds the code and applies only the differences to the database.

    3 / 29

  4. Read the SQL project file, do not write it

    Next to the object files, the repository holds a SQL project file that contains metadata about the database. That file is what lets Fabric bring more functionality into source control and deployment pipelines.

    Fabric generates and updates the project file automatically. Avoid manual edits: whatever you write there is overwritten on the next commit from Fabric. Lasting changes belong in object files or in the supported scripts.

    One addition has a specific purpose. To build the SQL project on your own machine with SQL tools such as SQL Server Management Studio or the SQL projects extension for Visual Studio Code, you can add a reference to the master.dacpac file in the project file.

    4 / 29
  5. Read the SQL project file, do not write it

    Fabric adds three things to the generated project file.

    What Fabric adds Effect
    .sharedQueries excluded from the build The scripts stay tracked in source control without affecting database model validation.
    Pre-deployment and post-deployment scripts The scripts from the .sharedQueries folder are registered in the project.
    System object references A package reference to master.dacpac, set up automatically with no action required.

    The exclusion is the one to understand. The queries in the Shared Queries folder are versioned like every other file, but the build does not treat them as database objects. They are scripts to run, not part of the database model, so a query there cannot break model validation.

    5 / 29
  6. Quick check

    A developer clones the repository and wants to build the generated SQL project on a laptop with the SQL projects extension. Which manual change to the project file has a supported purpose?

    1. AAdding a reference to the master.dacpac file

      Right. To build the project locally with SQL tools, you can add a reference to master.dacpac. Other manual edits are overwritten on the next commit from Fabric.

    2. BListing each table file, so that the build can find the tables

      Fabric generates and updates the project file with the metadata it needs. Lasting changes belong in object files, not in a hand-written list.

    3. CRemoving the exclusion of .sharedQueries, so that the queries are validated too

      The exclusion is there on purpose. Shared queries are scripts to run, not part of the database model, so the build must leave them out.

    6 / 29

  7. Work code-first in Visual Studio Code

    The project can also be driven from the other end: write the code first and let Fabric apply it. This path suits developers who prefer Visual Studio Code, who already have applications that use SQL projects, or who have more advanced CI/CD requirements.

    The setup has three steps:

    1. Install Visual Studio Code with the MSSQL and SQL projects extensions.
    2. Create a new SQL database in the workspace and commit it without adding any objects. This step adds the empty SQL project and the SQL database item metadata to the repository.
    3. Clone the repository to your machine and open the folder. The branch associated with the workspace might not be the default one, so switch to it to see the <yourdatabase>.SQLDatabase folder.
    7 / 29
  8. Work code-first in Visual Studio Code

    Then add .sql files for the objects you want, inside the folder structure of the database.

    Before you commit, build the project from the Database Projects view. The build validates the database model, so a syntax error shows up on your machine. After a successful build, commit the files with the source control view of Visual Studio Code or another Git client, push the commit to the remote repository, and check that the files appear in Azure DevOps or GitHub.

    Back in Fabric, the Source control panel might already show that there are pending changes from Git. Select Update all to apply the code from the SQL project to the database.

    The local build is optional in the sense that nothing forces it. Its value is timing: Fabric builds the project again during the update, so an error is always found, but a local build finds it before the commit reaches the branch that the whole team uses.

    8 / 29
  9. Quick check

    In the code-first flow, why does a developer first commit a new SQL database that has no objects?

    1. ASo that Fabric copies the sample rows into the repository

      A commit writes object definitions and project metadata. Table rows are not part of it.

    2. BSo that the first Update all can run without a build

      Every update from source control includes a build of the SQL project. Committing an empty database does not remove that step.

    3. CSo that the empty SQL project and its metadata reach Git

      Right. The empty commit creates the project and the item metadata in the repository, so the developer has a folder to clone and add .sql files to.

    9 / 29

  10. Keep your progress in the app

    That’s 3 of 11 quick checks. In the app they stay answered, and every lesson remembers where you left off.

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store
    Keep reading here
  11. Follow the update into the database

    Updating a SQL database in Fabric from source control combines two operations: a SQL project build and a SqlPackage publish.

    The build comes first. It validates the syntax of the SQL files and generates a .dacpac file, a package that describes the database model the code adds up to. The publish operation then determines the changes necessary to update the database to match the .dacpac file.

    A build turns the SQL project into a dacpac, and the publish step applies only the differences between that package and the database.buildpublish the differencesSQL project.sql filesPackage.dacpacTargetdatabase
    The package describes the model. The publish compares it with the target database and applies only the changes needed to match it.

    Two consequences follow from that order.

    First, validation happens before anything changes in the database. The .dacpac exists only if the SQL files describe a valid model.

    Second, the update is differential. The publish works out the difference between the model in the .dacpac and the objects that exist in the database, and it changes only those. An object that did not change is left alone. Once the update completes, connect to the database with any tool and the objects you added to the SQL project are visible.

    10 / 29
  12. Quick check

    During an update from source control, what does the SqlPackage publish step do with the .dacpac file?

    1. AIt replaces the database with a new one built from the package

      The publish is differential. It does not recreate the database, and objects that did not change are left alone.

    2. BIt works out and applies the changes that make the database match it

      Right. The publish determines the changes necessary to update the database so that it matches the model in the .dacpac file.

    3. CIt checks the syntax of each .sql file and reports the errors

      Checking the syntax is the job of the build, which runs first and produces the .dacpac file.

    11 / 29

  13. Read the publish options that Fabric applies

    When you run SqlPackage yourself, you choose its options. In Fabric you do not: because the interface is streamlined, Fabric applies a fixed set of four options to the publish operation of every update.

    /p:ScriptDatabaseOptions = false
    /p:DoNotAlterReplicatedObjects = false
    /p:IncludeTransactionalScripts = true
    /p:GenerateSmartDefaults = true
    

    Each line is a SqlPackage property, written with /p: followed by the property name and its value. Three of them explain behavior you can observe.

    Option Value in Fabric What it means
    GenerateSmartDefaults true The publish automatically provides a default value when it updates a table that contains data with a column that does not allow null values.
    IncludeTransactionalScripts true The publish uses transactional statements where possible.
    ScriptDatabaseOptions false This option decides whether target database properties are set or updated as part of the publish. With false, the publish leaves them alone.
    12 / 29
  14. Read the publish options that Fabric applies

    Here is how to use that table.

    Suppose a table already holds rows and you add a column that does not allow null values. The existing rows have no value for it. With GenerateSmartDefaults set to true, the publish supplies a default value automatically, so the update can add the column.

    ScriptDatabaseOptions set to false matches a rule you will meet in a moment: database-level settings are not carried by an update from source control.

    These values are not the defaults of the tool. On its own, SqlPackage defaults GenerateSmartDefaults and IncludeTransactionalScripts to false, and ScriptDatabaseOptions and DoNotAlterReplicatedObjects to true. Fabric reverses all four.

    13 / 29
  15. Quick check

    Fabric publishes with /p:ScriptDatabaseOptions = false. What does that value tell you about an update from source control?

    1. AThe update does not set or change the properties of the target database

      Right. ScriptDatabaseOptions decides whether target database properties are set or updated as part of the publish, and Fabric turns it off.

    2. BThe update skips the build and publishes the .sql files directly

      Every update builds the SQL project first. This option only concerns database properties during the publish.

    3. CThe update runs without transactional statements

      Transactional statements are controlled by IncludeTransactionalScripts, which Fabric sets to true.

    14 / 29

  16. Handle an update that fails or shows Uncommitted

    Two things can surprise you right after you select Update all. They look similar and need opposite reactions.

    The update fails. If a change to the SQL project has a syntax error or uses a feature that Fabric does not support, the database update fails. Fabric does not repair the code. You must manually revert the change in source control before you can continue. Until then, the branch holds code that cannot be applied, and that affects everyone who updates from it.

    15 / 29
  17. Handle an update that fails or shows Uncommitted

    The update succeeds, but the database shows Uncommitted. This can happen immediately after the update, even though nobody changed the database.

    The reason is how Git integration decides the status. It does a direct comparison of all the file content generated for an item definition, and some unintentional differences are possible. One example is inline attributes on columns: the file you wrote and the file Fabric generates for the same table can differ in form.

    Nothing is wrong with the database. The fix is to commit back to source control from the Fabric web interface, which syncs the definition in the repository with what a commit operation generates.

    Symptom after Update all Cause Reaction
    The update fails Syntax error or unsupported feature in the code Revert the change manually in source control
    The update works, the item shows Uncommitted Differences in the generated file content Commit from Fabric
    16 / 29
  18. Quick check

    Two developers each run Update all. For Ana the update fails on a file with invalid syntax. For Ben it succeeds, but the database shows Uncommitted. What should each one do?

    1. AAna commits from Fabric, and Ben reverts his own change in source control

      This is the swap. A failed update needs the bad change reverted in source control, and an Uncommitted status after a good update is cleared with a commit from Fabric.

    2. BBoth run Update all again until the status changes

      Running the update again applies the same code. Ana's file is still invalid, and Ben's database still differs from the repository files in form.

    3. CAna reverts the change in source control, and Ben commits from Fabric

      Right. Invalid code must be reverted manually in source control. An Uncommitted status right after a successful update only needs a commit from Fabric.

    17 / 29

  19. Know what the project does not carry

    A database project describes objects. Two things are outside it.

    Database-level settings are the first. Settings such as collation and compatibility level are not included in the source control and deployment pipelines integration. For database settings that you can set with T-SQL after database creation, you can modify the database with scripts after deployment.

    Table data is the second. When Fabric creates a database from the code in the repository, for example in a workspace created with Branch out to new workspace, the database contains the objects that were checked into source control, and its tables are new and empty. The definitions travel and the rows do not. Any data the new environment needs has to be loaded, or, if it is a small controlled set, managed with a script as the next section shows.

    18 / 29
  20. Quick check

    A team expects a new environment built from the repository to match the source database exactly. Which two things will be missing?

    1. AThe stored procedures and the views

      Procedures and views are object definitions, and object definitions are exactly what the project carries.

    2. BThe SQL project file and the Shared Queries scripts

      Both are in the repository. Fabric generates the project file and tracks the Shared Queries scripts in source control.

    3. CThe table rows and database-level settings such as collation

      Right. The tables arrive new and empty, and settings such as collation and compatibility level are not included in the integration.

    19 / 29

  21. Control static data with a post-deployment script

    Pre-deployment and post-deployment scripts attach your own T-SQL to a deployment. The same capability applies to deployment pipelines, so one script covers both delivery paths: an update from source control and a deployment pipeline deployment.

    The standard case is a lookup table with a known set of values that do not change often, such as a dbo.Colors table that an application uses. Create a new query in the SQL database editor in Fabric and add a MERGE statement that manages the contents of the table:

    MERGE dbo.Colors AS target
    USING (VALUES (1, 'Red'), (2, 'Green'), (3, 'Blue')) AS source (Id, Name)
    ON target.Id = source.Id
    WHEN MATCHED THEN
        UPDATE SET Name = source.Name
    WHEN NOT MATCHED BY TARGET THEN
        INSERT (Id, Name) VALUES (source.Id, source.Name)
    WHEN NOT MATCHED BY SOURCE THEN
        DELETE;
    

    Read it clause by clause. The list of values is the wanted state. A row that exists in both places gets its name updated. A row missing from the table is inserted. A row in the table that is not in the list is deleted. Running the statement twice gives the same result, which is what a script that runs on every deployment needs.

    20 / 29
  22. Control static data with a post-deployment script

    Registering the query takes two steps. Rename it to something recognizable, such as Post-Deployment-StaticData.sql, and move it to Shared Queries. Then open the ... menu of the query and select Set as Post-deployment Script.

    From that point, the query runs automatically as part of any update from source control or deployment pipeline deployment. Nobody has to remember to run it.

    Because pre-deployment and post-deployment scripts are included in the SQL project, there are two ways to change the rows later: edit the query in the Fabric query editor and commit, or edit the file locally with Visual Studio Code or other SQL project tools and commit there. Either way, the change to the data is a change to code that the team can review.

    21 / 29
  23. Quick check

    In the MERGE script for dbo.Colors, the value list holds Red, Green, and Blue. The table in the test database also holds a row for Yellow. What does the script do with Yellow on the next deployment?

    1. AIt keeps the row, because the script only inserts and updates

      The statement has a third clause, WHEN NOT MATCHED BY SOURCE THEN DELETE, which handles rows that are not in the value list.

    2. BIt deletes the row

      Right. Yellow exists in the table but not in the value list, so the WHEN NOT MATCHED BY SOURCE clause deletes it.

    3. CIt stops the deployment and reports a conflict

      MERGE does not treat an extra row as an error. Each row falls into one of the three clauses, and this one falls into the delete clause.

    22 / 29

  24. Branch, review, merge, then update

    The project makes team development possible, and the sequence has one step that people forget.

    Branch out to new workspace, on the Branches tab of the Source control menu, asks for the names of a branch and a workspace. It creates the branch in the repository, populated with the committed contents of the branch you are branching from, and it creates the workspace in Fabric. When the database creation completes, the new database contains the objects from the repository, with new and empty tables.

    A developer changes objects in that workspace and commits them with the Commit button. In Azure DevOps or GitHub, a pull request from the secondary branch to the primary branch shows the changes in the database code for review.

    23 / 29
  25. Branch, review, merge, then update

    Completing the pull request updates source control. The database in the primary workspace is not changed.

    To change the primary database, update the primary workspace from source control with the Update button on the Source control panel. That separate step is useful: someone decides when reviewed code becomes the primary database.

    Step Where it happens What changes
    Branch out to new workspace Fabric A new branch and a new workspace with empty tables
    Commit Fabric, branch workspace The secondary branch
    Complete the pull request Azure DevOps or GitHub The primary branch
    Update Fabric, primary workspace The primary database
    24 / 29
  26. Quick check

    A pull request from a feature branch into the primary branch was completed an hour ago. A tester connects to the database in the primary workspace and cannot find the new stored procedure. Why?

    1. AThe pull request changed the primary branch, and the workspace was not updated

      Right. Completing a pull request updates source control only. The primary database changes when someone selects Update in the primary workspace.

    2. BThe stored procedure was left in the feature workspace, because merges only move tables

      A merge moves all the code of the branch, including procedures. The code is in the primary branch and has not been applied yet.

    3. CThe merge takes several hours to reach the database

      There is no delayed automatic sync. The database changes when the workspace is updated from source control.

    25 / 29

  27. Publish a dacpac with SqlPackage yourself

    Fabric runs SqlPackage for you during an update, but SqlPackage is also a command-line tool, and the same vocabulary appears in both places.

    The SqlPackage publish operation incrementally updates the schema of a target database to match the structure of a source. With a .dacpac file as the source, the command names the action, the source file, and the target:

    SqlPackage /Action:Publish /SourceFile:"C:\AdventureWorksLT.dacpac" /TargetConnectionString:"<connection string of the target database>"
    
    Part Role
    /Action:Publish The operation to perform.
    /SourceFile The .dacpac file to publish.
    /TargetConnectionString The database that receives the changes.
    /p: An option for the publish, as a property name and a value.

    For an existing database, the publish action requires the db_owner permission.

    This is also why the local build matters in the code-first flow: the build is what produces the .dacpac file that a publish needs, whether Fabric runs the publish or you do.

    26 / 29
  28. Quick check

    A developer wants to publish a compiled database model from the command line. Which part of the SqlPackage command points to the .dacpac file?

    1. A/TargetConnectionString

      The target connection string identifies the database that receives the changes, not the package.

    2. B/Action:Publish

      The action tells SqlPackage which operation to run. It does not say where the package is.

    3. C/SourceFile

      Right. /SourceFile names the .dacpac file that is the source of the publish.

    27 / 29

  29. Key takeaways

    • The project file belongs to Fabric: Fabric generates and updates the SQL project file, and a manual edit is overwritten on the next commit from Fabric.
    • Shared Queries are tracked, not built: the .sharedQueries folder stays in source control and is excluded from the project build.
    • Build locally before you push: a local build finds a syntax error before the commit reaches the shared branch. A failed update must be reverted manually in source control.
    • Uncommitted after a good update is normal: commit back from Fabric to sync the generated files.
    • GenerateSmartDefaults is on: the publish provides a default value when a column that does not allow null values is added to a table that contains data.
    • Definitions travel, data and database settings do not: a branched database has empty tables, and collation or compatibility level need scripts after deployment.
    • Static rows go in a post-deployment script: a MERGE query in Shared Queries, set as a post-deployment script, runs on every update from source control and every deployment pipeline deployment.
    • Merging is not publishing: the primary database changes only when the primary workspace is updated from source control.
    28 / 29
  30. Quick check

    Which statement about a SQL database under Fabric source control is correct?

    1. ACompleting a pull request applies the merged code to the database of the primary workspace

      Completing a pull request updates source control. The primary database changes when the primary workspace is updated.

    2. BA manual edit to the generated project file is kept by Fabric on later commits

      Fabric generates and updates the project file, so a manual edit is overwritten on the next commit from Fabric.

    3. CAn update builds the project into a dacpac and publishes only the differences

      Right. The build validates the code and produces the dacpac, and the SqlPackage publish applies the changes needed to match it.

    29 / 29

  31. 11 quick checks · then the test

    In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.

    Take this lesson’s test → Next lesson: Warehouse Database Projects

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store

The whole course, on your computer and your phone

Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.

Opens in your browser. Nothing to install.

Start studying

On your phone, with the same account

Get it on Google Play Download on the App Store

“This app is the ‘One Lake’ for preparing the certification exams. Really worth it.”

Abdelhak Y. Google Play review