Out of the Shadows: Explore a Database Projects’ Inner Workings using SSDT Schema Compare
For a well-balanced project.

Out of the Shadows: Explore a Database Projects’ Inner Workings using SSDT Schema Compare

Overview

In Jungian psychology, the concept of the shadow is often symbolized as the hidden side of a person's psyche. A codebase is not all that different; it is just as complex in its own way, so it requires exploration and understanding.

By acknowledging and examining those complexities, a developer can form a more complete and balanced understanding of “the shadow” within their solution. SQL Server Data Tools (SSDT) aids in this journey of self-discovery, so to speak.

Schema Compare, a powerful comparison tool available in SSDT, can reveal hidden or lesser-known aspects of a project’s code, such as a "tiny" change to a query or an unknown dependency between environments.

While the utility offers a broad range of benefits – among them, maintaining source control, ensuring consistent deployments, and upholding code standards – a practical use is known to assist in streamlining production support issues.

When a release goes sideways, for example, the core comparison feature produces a line-by-line diffing between lower and live environments. Developers can then use the results to quickly identify the schema modifications that could be causing the issue.

This type of exploration of what lies beneath ultimately reduces risk in the development lifecycles.

With its’ ability to help us learn about our codebase on a deeper level, it is no wonder that Schema Compare can have an almost transformative effect on some of those who use it. Great potential here for learning and improvement, and an inviting journey.

What will you find?

/**/

This article will demonstrate how to create a database schema comparison file in a Visual Studio SQL Server Database project.

Caveats

A few things to know before you go.

  • Handle with care. Ensure that the comparison is configured in the right direction. It is not uncommon to apply changes in unwanted places. More detail in the guide below.
  • No standalone installer is available for SQL Server Data Tools, a prerequisite. Use Visual Studio Installer.
  • Project or database only. This only works with Visual Studio projects and databases.
  • Each database, their own. There is no “global” schema comparison. Each database requires its’ own schema comparison.

Not so bad.

Prerequisites

Basic system requirements:

  • Visual Studio. Community Edition is free and supports this functionality. Get it here.
  • SQL Server Data Tools. The Schema Compare feature can be installed from the Visual Studio Installer. Read more about the product here.
  • SQL Server databases. The objects to be compared in this guide. Two are needed. A repo clone on the local machine can be used as well.

Not too terrible.

No alt text provided for this image




Steps to Compare Schemas in a Visual Studio Project

Comparing schemas follows a general process in Visual Studio.

  1. Open Visual Studio.
  2. Create or open a SQL Server Database project.
  3. Right-click on the project in the Solution Explorer. Select Add then New Item and Schema Compare.

No alt text provided for this image

A Schema Compare tab will appear.

The tab is divided into two major areas: Source on the left, Target on the right.

No alt text provided for this image

Source = Development SQL database

Target = Test SQL database

We would like to see the differences between the Development and Test databases, with Development being compared against Test.

4. In the dropdown, click Select Source.

5. Specify the development database or local project in the next dialog.

No alt text provided for this image

6. Click Browse to set up a new database connection. Select History to browse existing connections.

No alt text provided for this image

7. Click Connect to establish the Source connection.

The dropdown will populate with the selected source.

No alt text provided for this image

8. ?Do the same for the Target.

No alt text provided for this image

9. Click the wheel icon in the tab. Through this window you can specify the items that you want to include and exclude.

No alt text provided for this image
No alt text provided for this image

The first tab, General, concern comparison settings. Click Reset to use default settings.

Lots of stuff to unpack here, as they say!

No alt text provided for this image

The second tab, Object Types, handles the inclusions/exclusions of the database things to be compared.

No alt text provided for this image

Application-scoped objects relate to the project and database itself. Non-application-scoped objects feature administrative and configuration objects are the server-level, so logins, credentials, routes, etc.

It is worth pointing out the commands available in the command bar, which make Schema Compare quite a reliable companion when managing project updates.

No alt text provided for this image


10. Click the Compare button once the source and target databases have been selected and the compare options are set.

No alt text provided for this image

The comparison may take a minute or two, depending on the size of the database.

The comparison results will be displayed in the same window.

The juice: each item can be accessed to view a line-by-line comparison.

No alt text provided for this image

A few items to note:

  • The button in between Source and Target dropdowns determines the comparison direction. Click to change the direction.
  • Unchecking the boxes in the middle will further exclude items from the update, even if there are differences. This fine-tuning affords manual selection and greater control.
  • Three major categories exist: Add, Delete and Change. A Change is represented by a pencil; Delete is a big red X and Add is a green plus sign.
  • Comparison direction matters.

If Development, for example, contains a table that the Test db does not, a “+” appears. This is a new object.

No alt text provided for this image

Reversing the comparison will display a “-“ sign, as the Test environment does not contain the table object, therefore registering it as delete candidate.

No alt text provided for this image

At this point, we are ready to update the database. Use caution when clicking the Update button, as this applies the changes from the source database or project to the target database. It is indeed possible that you may make changes to the wrong place.

Before doing that, create backups in case something goes sideways.

No alt text provided for this image
"Well, it worked before you broke it..."

11. Click Update once ready.

No alt text provided for this image

Allow a few moments for the update to process.

No alt text provided for this image
This may take a second, hang on.

After the process has completed, you do not need to update the database – this operation has done everything for you, unless an error is returned.

12.?Save the Schema Compare file.

No alt text provided for this image

A file is created with each schema compare operation. The extension is .scmp. A new file does not need to be recreated every time a comparison needs to happen. Once saved, will be located within the database project within the solution.

All you need to do is open the comparison file within the Solution Explorer, confirm source, target and comparison direction and click Compare.

And that’s it. Compare one database to another, or a project folder to database and reveal how they differ with minimal effort.

/**/

Next Steps and Suggestions

A few suggestions.

  • Save the Schema Compare file for later use. Save time The .scmp extension file does not need to be recreated every time a comparison needs to happen. The schema comparison file, once saved, will be located within the database project within the solution.
  • Create backups. Before making any changes, create backups in case there is an error or accidental update.
  • Experiment in a sandbox. If this is your first time doing this, use a sandbox or similar environment that is meant for exploration.
  • Try it yourself. Create your own schema comparison within a SQL Server Database project. Create and destroy objects and projects in a sandbox to your heart's content.

Simple and practical. Find what works for you and your situation but always use caution when updating your target.

References

要查看或添加评论,请登录

社区洞察

其他会员也浏览了