Skip to main content

Automatic SQL Server Schema Migrations with Atlas

Microsoft SQL Server is a relational database management system that has been one of the prominent enterprise-grade data solutions for decades. Commonly used by enterprises, SQL Server can efficiently handle growing amounts of data and users, making it easy to scale.

However, managing a large database schema in SQL Server can be challenging due to the complexity of related data structures and the need for coordinated schema changes across multiple teams and applications.

Enter: Atlas​

Atlas helps developers manage their database schema as code, abstracting away the intricacies of database schema management. With Atlas, users provide the desired state of the database schema and Atlas automatically plans the required migrations.

In this guide, we will dive into setting up Atlas for SQL Server schema migration and walk through both of the available workflows:

WorkflowWhat Atlas doesCommand
DeclarativeDiffs your desired schema against the live database and applies the plan directlyatlas schema apply
VersionedWrites a timestamped migration file to review, commit, and deploy through CI/CDatlas migrate diff

Either way the schema is a file in your repository, written in HCL or plain SQL. It covers tables, indexes, foreign keys and constraints, views and materialized views, stored procedures, functions, triggers, sequences, alias and table types, full-text indexes, and temporal tables: see the SQL Server driver documentation for the full list.

Prerequisites​

  1. Docker
  2. Atlas installed on your machine:

To download and install the latest release of the Atlas CLI, simply run the following in your terminal:

curl -sSf https://atlasgo.sh | sh
  1. Optional, for coding agents: the atlas-onboard and atlas-mssql skills. One command installs both, with the other Atlas skills:

Install the skills from a terminal. It installs the Atlas plugin, with all its skills, for every project on this machine.

claude plugin marketplace add ariga/atlas
claude plugin install atlas@ariga

Then start the onboarding skill in Claude Code. See Claude Code with Atlas for the other ways to use the skills.

/atlas:atlas-onboard

Logging in to Atlas​

SQL Server support is available to Pro users. To use this feature, run atlas login.

To use SQL Server with Atlas, you'll need to log in to Atlas. If it's your first time, you will be prompted to create both an account and a workspace (organization):

atlas login

Getting Started​

Let's start by spinning up a local database using Docker:

docker run --rm -d --platform linux/amd64 --name atlas-demo -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=P@ssw0rd0995' -p 1433:1433 mcr.microsoft.com/mssql/server:2022-latest

For this example, we will begin with a minimal database containing a users table with ID as the primary key.

CREATE TABLE users (
ID bigint PRIMARY KEY,
name varchar(255) NOT NULL
);

To create this in our local database, run the following command:

docker exec -it atlas-demo /opt/mssql-tools18/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd0995' -C -Q "CREATE TABLE users (ID bigint PRIMARY KEY, name varchar(255) NOT NULL);"

Inspecting our Database​

The atlas schema inspect command takes a URL and outputs its schema definition. In this guide, we will demonstrate this flow using both the Atlas DDL (default) and SQL formats for the output.

Azure SQL with Microsoft Entra ID

To connect to Azure SQL Database or Azure SQL Managed Instance without a password, use the azuresql:// scheme with fedauth=ActiveDirectoryDefault. Atlas gets a token from your Azure credentials, such as az login or a managed identity:

azuresql://<instance>.database.windows.net?fedauth=ActiveDirectoryDefault&database=master

See IAM Authentication for the atlas.hcl setup.

To inspect our locally-running SQL Server instance, use the -u flag and write the output to a file named schema.hcl:

atlas schema inspect -u "sqlserver://SA:P@ssw0rd0995@localhost:1433?database=master&mode=database" > schema.hcl

Open the schema.hcl file to view the Atlas schema that describes our database.

schema.hcl
table "users" {
schema = schema.dbo
column "ID" {
null = false
type = bigint
}
column "name" {
null = false
type = varchar(255)
}
primary_key "PK__users__3214EC279C70B1D1" {
columns = [column.ID]
}
}
schema "dbo" {
}

This first block represents a table resource with ID and name columns. The schema field references the dbo schema that is defined in the block below. The primary_key sub-block establishes the ID column as the primary key for the table. SQL Server named the key PK__users__3214EC279C70B1D1 because the CREATE TABLE statement did not name it. Your key gets a different suffix: use the name from your own schema.hcl in the examples below.

Atlas mimics the syntax of the database that the user is working against. In this case, the type for the ID column is bigint, and the name column's is varchar(255).

info

For in-depth details on the atlas schema inspect command, covering aspects like inspecting specific schemas, handling multiple schemas concurrently, excluding tables, and more, see Inspecting Existing Database Schemas.

To generate a visual representation of our schema as an Entity Relationship Diagram (ERD) on Atlas Cloud, we can add the -w flag to the inspect command:

atlas schema inspect -u "sqlserver://SA:P@ssw0rd0995@localhost:1433?database=master&mode=database" -w

Entity relationship diagram of SQL Server schema (users)

Declarative Migrations​

Using the declarative approach, users define the desired state of their database schema as code. Atlas then inspects the target database's current state and calculates an execution plan to reach the desired state.

We will start off by making a change to our schema file by adding a repos table:

schema.hcl
table "users" {
schema = schema.dbo
column "ID" {
null = false
type = bigint
}
column "name" {
null = false
type = varchar(255)
}
primary_key "PK__users__3214EC279C70B1D1" {
columns = [column.ID]
}
}
table "repos" {
schema = schema.dbo
column "ID" {
null = false
type = bigint
}
column "name" {
null = false
type = varchar(255)
}
column "owner_id" {
null = false
type = bigint
}
primary_key {
columns = [column.ID]
}
foreign_key "fk_repo_owner" {
columns = [column.owner_id]
ref_columns = [table.users.column.ID]
on_update = NO_ACTION
on_delete = NO_ACTION
}
}
schema "dbo" {
}

Now that our desired state has changed, to apply these changes to our database, Atlas will plan a migration for us by running the atlas schema apply command:

atlas schema apply \
-u "sqlserver://SA:P@ssw0rd0995@localhost:1433?database=master&mode=database" \
--to file://schema.hcl \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

Approve the proposed changes, and that's it! You have successfully run a declarative migration.

Applying approved migration (1 statement in total):

-- create "repos" table
-> CREATE TABLE [dbo].[repos] (
[ID] bigint NOT NULL,
[name] varchar(255) NOT NULL,
[owner_id] bigint NOT NULL,
CONSTRAINT [PK_repos] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [fk_repo_owner] FOREIGN KEY ([owner_id]) REFERENCES [dbo].[users] ([ID]) ON UPDATE NO ACTION ON DELETE NO ACTION
)
-- ok (12.957875ms)

-------------------------
-- 13.041875ms
-- 1 migration
-- 1 sql statement

The plan creates the repos table and its foreign key. The users table already matches the desired state, so Atlas leaves it unchanged.

info

For a more detailed description of the atlas schema apply command, see Declarative Schema Migrations.

To ensure that the changes have been made to the schema, let's run the inspect command with the -w flag once more and view the ERD:

Entity relationship diagram of SQL Server schema (users and repos)

Pushing schemas to Atlas​

Similar to how Docker images are pushed to Docker Hub, you can push your schema to Atlas Cloud for versioning, collaboration, and deployment:

atlas schema push app \
--url file://schema.hcl \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

Once pushed, Atlas prints a URL to the schema. You can then apply it to any database using the schema URL in --to:

atlas schema apply \
-u "sqlserver://SA:P@ssw0rd0995@localhost:1433?database=master&mode=database" \
--to "atlas://app" \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

This workflow allows you to manage your schema centrally and deploy it to multiple environments without having the schema files locally.

For more advanced workflows, you can use atlas schema plan to pre-plan and review migrations before applying them. This enables teams to plan, lint, and review changes during the PR stage, edit generated SQL if needed, and ensure no human intervention is required during deployment.

Versioned Migrations​

Alternatively, there is the versioned migrations workflow, sometimes called change-based migrations, where each migration is saved in source control to be evaluated during code review and applied later. Users still benefit from Atlas intelligently planning migrations for them, but they are not automatically applied.

Creating the first migration​

When using the versioned migration workflow, our database state is managed by a migration directory. The migration directory holds all of the migration files created by Atlas, and the sum of all files in lexicographical order represents the current state of the database.

To create our first migration file, we will run the atlas migrate diff command with the necessary parameters:

  • --dir: The URL to the migration directory, file://migrations by default.
  • --to: The URL of the desired state. A state can be specified using a database URL, an HCL or SQL schema definition, or another migration directory.
  • --dev-url: A URL to a Dev Database that will be used to compute the diff.
info

The examples below use the docker:// driver. To use a SQL Server instance you run yourself as the dev database, see Providing Your Own Dev Database. A database-scoped connection (?mode=database) requires the db_owner role.

atlas migrate diff initial \
--to file://schema.hcl \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

Run ls migrations, and you'll notice that Atlas has automatically created a migration directory for us, as well as two files:

-- Create "users" table
CREATE TABLE [dbo].[users] (
[ID] bigint NOT NULL,
[name] varchar(255) NOT NULL,
CONSTRAINT [PK__users__3214EC279C70B1D1] PRIMARY KEY CLUSTERED ([ID] ASC)
);
-- Create "repos" table
CREATE TABLE [dbo].[repos] (
[ID] bigint NOT NULL,
[name] varchar(255) NOT NULL,
[owner_id] bigint NOT NULL,
CONSTRAINT [PK_repos] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [fk_repo_owner] FOREIGN KEY ([owner_id]) REFERENCES [dbo].[users] ([ID]) ON UPDATE NO ACTION ON DELETE NO ACTION
);

The migration file represents the current state of our database, and the sum file is used by Atlas to maintain the integrity of the migration directory.

Pushing migration directories to Atlas​

Now that we have our first migration, we can push the migration directory to Atlas Cloud, much like how Docker images are pushed to Docker Hub.

Let's name our new migration project app and run atlas migrate push:

atlas migrate push app \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

Migration Directory created with atlas migrate push

Once the migration directory is pushed, Atlas prints a URL to the created directory, similar to the one shown in the image above.

Applying migrations​

Once our app migration directory has been pushed, we can apply it to a database from any CD platform without necessarily having our directory there.

The atlas-demo database already has the users and repos tables from the declarative section, so let's create a second, empty database using Docker to resemble a local environment, this time on port 1434:

docker run --rm -d --platform linux/amd64 --name atlas-local -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=P@ssw0rd0995' -p 1434:1433 mcr.microsoft.com/mssql/server:2022-latest

Next, we'll create a simple Atlas configuration file (atlas.hcl) to store the settings for our local environment:

atlas.hcl
env "local" {
url = "sqlserver://SA:P@ssw0rd0995@localhost:1434?database=master&mode=database"
migration {
dir = "atlas://app"
}
}

Run atlas migrate apply with the --env flag to instruct Atlas to select the environment configuration from the atlas.hcl file:

atlas migrate apply --env local

After applying the migration, you should receive a link to the deployment and the database where the migration was applied. Here's an example of what it should look like:

Migration deployment report created with atlas migrate apply

Generating another migration​

After applying the first migration, it's time to update our schema definition file and tell Atlas to generate another migration.

Let's make two changes to our schema:

  • Add a new description column to our repos table
  • Add a new commits table
schema.hcl
table "users" {
schema = schema.dbo
column "ID" {
null = false
type = bigint
}
column "name" {
null = false
type = varchar(255)
}
primary_key "PK__users__3214EC279C70B1D1" {
columns = [column.ID]
}
}
table "repos" {
schema = schema.dbo
column "ID" {
null = false
type = bigint
}
column "name" {
null = false
type = varchar(255)
}
column "owner_id" {
null = false
type = bigint
}
column "description" {
null = true
type = varchar(-1)
}
primary_key {
columns = [column.ID]
}
foreign_key "fk_repo_owner" {
columns = [column.owner_id]
ref_columns = [table.users.column.ID]
on_update = NO_ACTION
on_delete = NO_ACTION
}
}
table "commits" {
schema = schema.dbo
column "ID" {
null = false
type = bigint
}
column "message" {
null = false
type = varchar(255)
}
column "repo_id" {
null = false
type = bigint
}
column "author_id" {
null = false
type = bigint
}
primary_key {
columns = [column.ID]
}
foreign_key "fk_commit_author" {
columns = [column.author_id]
ref_columns = [table.users.column.ID]
on_update = NO_ACTION
on_delete = NO_ACTION
}
foreign_key "fk_repo_commit" {
columns = [column.repo_id]
ref_columns = [table.repos.column.ID]
on_update = NO_ACTION
on_delete = NO_ACTION
}
}
schema "dbo" {
}

Next, run the atlas migrate diff command again:

atlas migrate diff add_commits \
--to file://schema.hcl \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

Run ls migrations, and you'll notice that a new migration file has been generated.

20261129113555_add_commits.sql
-- Modify "repos" table
ALTER TABLE [dbo].[repos] ADD [description] varchar(MAX) NULL;
-- Create "commits" table
CREATE TABLE [dbo].[commits] (
[ID] bigint NOT NULL,
[message] varchar(255) NOT NULL,
[repo_id] bigint NOT NULL,
[author_id] bigint NOT NULL,
CONSTRAINT [PK_commits] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [fk_commit_author] FOREIGN KEY ([author_id]) REFERENCES [dbo].[users] ([ID]) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT [fk_repo_commit] FOREIGN KEY ([repo_id]) REFERENCES [dbo].[repos] ([ID]) ON UPDATE NO ACTION ON DELETE NO ACTION
);

If you run atlas migrate push again, you can observe the new file on the migration directory page.

atlas migrate push app \
--dev-url "docker://sqlserver/2022-latest/dev?mode=database"

Migration Directory created with atlas migrate push

Finally, run atlas migrate apply again to apply the new migration to the database:

atlas migrate apply --env local
Migrating to version 20261129113555 from 20261129113421 (1 migrations in total):

-- migrating version 20261129113555
-> ALTER TABLE [dbo].[repos] ADD [description] varchar(MAX) NULL
-> CREATE TABLE [dbo].[commits] (
[ID] bigint NOT NULL,
[message] varchar(255) NOT NULL,
[repo_id] bigint NOT NULL,
[author_id] bigint NOT NULL,
CONSTRAINT [PK_commits] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [fk_commit_author] FOREIGN KEY ([author_id]) REFERENCES [dbo].[users] ([ID]) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT [fk_repo_commit] FOREIGN KEY ([repo_id]) REFERENCES [dbo].[repos] ([ID]) ON UPDATE NO ACTION ON DELETE NO ACTION
)
-- ok (16.150459ms)

-------------------------
-- 35.262583ms
-- 1 migration
-- 2 sql statements

Atlas runs only 20261129113555_add_commits.sql. The initial migration was applied in the previous step, so Atlas skips it.

Next Step: Set Up CI/CD​

Once you have your migration directory set up, the next step is to integrate Atlas into your CI/CD pipeline. Atlas provides native integrations for popular platforms:

Wrapping Up​

In this guide, we demonstrated how to set up Atlas to manage your SQL Server database schema. We covered both declarative and versioned migration workflows, and showed how to generate migrations, push them to an Atlas workspace, and apply them to your databases.

To keep going:

Have questions? Feedback? Message our team on Intercom.