Skip to main content

Automatic SQLite Schema Migrations with Atlas

SQLite is an embedded relational database that stores the entire database in a single file. It runs in-process with the application, with no server to operate.

SQLite's ALTER TABLE supports only a few operations. Changing a column's type, nullability, or default, or altering a constraint, requires creating a new table, copying the rows, and swapping the tables, while keeping foreign keys intact.

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, including the table rebuilds SQLite needs.

In this guide, we will set up Atlas as a SQLite migration tool 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, columns, indexes, foreign keys, and constraints. Atlas Pro adds views and triggers: see the SQLite driver documentation for the full list.

Prerequisites​

  1. The SQLite command-line tool (sqlite3), used to create the example database
  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 skill. One command installs it, 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

Unlike server databases, SQLite does not need Docker for this guide. The databases are local files, and the dev database is an in-memory SQLite database: sqlite://dev?mode=memory.

Logging in to Atlas​

SQLite support is open source, but this guide also uses Atlas Pro features: generating ERDs and pushing schemas and migration directories to Atlas Cloud. To follow every step, log in to Atlas, which starts a free trial. If it's your first time, you will be prompted to create both an account and a workspace (organization):

atlas login

Getting Started​

Create a new project directory and run every command in this guide from it:

mkdir atlas-sqlite-demo && cd atlas-sqlite-demo

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

CREATE TABLE `users` (
`id` integer NOT NULL,
`name` text NOT NULL,
PRIMARY KEY (`id`)
);

To create this table in a new database file named demo.db, run the following command:

sqlite3 demo.db "CREATE TABLE \`users\` (\`id\` integer NOT NULL, \`name\` text NOT NULL, PRIMARY KEY (\`id\`));"

Inspecting our Database​

The atlas schema inspect command takes a URL and outputs its schema definition. A SQLite URL points to the database file: sqlite://demo.db. In this guide, we will demonstrate this flow using both the Atlas DDL (default) and SQL formats for the output.

To inspect the demo.db database, use the -u flag and write the output to a file named schema.hcl:

atlas schema inspect -u "sqlite://demo.db" > schema.hcl

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

schema.hcl
table "users" {
schema = schema.main
column "id" {
null = false
type = integer
}
column "name" {
null = false
type = text
}
primary_key {
columns = [column.id]
}
}
schema "main" {
}

This first block represents a table resource with id and name columns. The schema field references the main schema that is defined in the block below. main is the name SQLite gives to the primary database file. The primary_key sub-block establishes the id column as the primary key for the table.

Atlas mimics the syntax of the database that the user is working against. In this case, the type for the id column is integer, and the name column's is text.

info

For in-depth details on the atlas schema inspect command, covering aspects like excluding tables, output formats, and more, see Inspecting Existing Database Schemas.

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

atlas schema inspect -u "sqlite://demo.db" -w

Entity relationship diagram of SQLite 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.main
column "id" {
null = false
type = integer
}
column "name" {
null = false
type = text
}
primary_key {
columns = [column.id]
}
}
table "repos" {
schema = schema.main
column "id" {
null = false
type = integer
}
column "name" {
null = false
type = text
}
column "owner_id" {
null = false
type = integer
}
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 "main" {
}

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 "sqlite://demo.db" \
--to file://schema.hcl \
--dev-url "sqlite://dev?mode=memory"

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 `repos` (
`id` integer NOT NULL,
`name` text NOT NULL,
`owner_id` integer NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_repo_owner` FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- ok (774.416µs)

-------------------------
-- 847.708µs
-- 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 check the result, run the inspect command with the -w flag once more and view the updated ERD.

Entity relationship diagram of SQLite 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 "sqlite://dev?mode=memory"

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 "sqlite://demo.db" \
--to "atlas://app" \
--dev-url "sqlite://dev?mode=memory"

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.
atlas migrate diff initial \
--to file://schema.hcl \
--dev-url "sqlite://dev?mode=memory"

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 `users` (
`id` integer NOT NULL,
`name` text NOT NULL,
PRIMARY KEY (`id`)
);
-- Create "repos" table
CREATE TABLE `repos` (
`id` integer NOT NULL,
`name` text NOT NULL,
`owner_id` integer NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_repo_owner` FOREIGN KEY (`owner_id`) REFERENCES `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 "sqlite://dev?mode=memory"

Migration Directory created with atlas migrate push

Once the migration directory is pushed, Atlas prints a URL to the created directory.

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 demo.db database already has the users and repos tables from the declarative section, so we will apply the migrations to a second database file, local.db. SQLite creates the file on first connection, so there is nothing to set up.

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

atlas.hcl
env "local" {
url = "sqlite://local.db"
migration {
dir = "atlas://app"
}
}

To apply the local directory without pushing it, set dir = "file://migrations" instead.

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
Migrating to version 20261129135110 (1 migrations in total):

-- migrating version 20261129135110
-> CREATE TABLE `users` (
`id` integer NOT NULL,
`name` text NOT NULL,
PRIMARY KEY (`id`)
);
-> CREATE TABLE `repos` (
`id` integer NOT NULL,
`name` text NOT NULL,
`owner_id` integer NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_repo_owner` FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- ok (638.875µs)

-------------------------
-- 8.264792ms
-- 1 migration
-- 2 sql statements

When the directory comes from Atlas Cloud, Atlas also prints a link to the deployment report.

Migration deployment report created with atlas migrate apply

Atlas records applied versions in an atlas_schema_revisions table. A SQLite database has a single schema, so this table lives in the same file as your tables.

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.main
column "id" {
null = false
type = integer
}
column "name" {
null = false
type = text
}
primary_key {
columns = [column.id]
}
}
table "repos" {
schema = schema.main
column "id" {
null = false
type = integer
}
column "name" {
null = false
type = text
}
column "owner_id" {
null = false
type = integer
}
column "description" {
null = true
type = text
}
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.main
column "id" {
null = false
type = integer
}
column "message" {
null = false
type = text
}
column "repo_id" {
null = false
type = integer
}
column "author_id" {
null = false
type = integer
}
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_commit_repo" {
columns = [column.repo_id]
ref_columns = [table.repos.column.id]
on_update = NO_ACTION
on_delete = NO_ACTION
}
}
schema "main" {
}

Next, run the atlas migrate diff command again:

atlas migrate diff add_commits \
--to file://schema.hcl \
--dev-url "sqlite://dev?mode=memory"

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

20261129135904_add_commits.sql
-- Add column "description" to table: "repos"
ALTER TABLE `repos` ADD COLUMN `description` text NULL;
-- Create "commits" table
CREATE TABLE `commits` (
`id` integer NOT NULL,
`message` text NOT NULL,
`repo_id` integer NOT NULL,
`author_id` integer NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_commit_author` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT `fk_commit_repo` FOREIGN KEY (`repo_id`) REFERENCES `repos` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);

Adding a nullable column is one of the changes SQLite's ALTER TABLE supports, so Atlas plans a single ADD COLUMN statement.

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

atlas migrate push app \
--dev-url "sqlite://dev?mode=memory"

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 20261129135904 from 20261129135110 (1 migrations in total):

-- migrating version 20261129135904
-> ALTER TABLE `repos` ADD COLUMN `description` text NULL;
-> CREATE TABLE `commits` (
`id` integer NOT NULL,
`message` text NOT NULL,
`repo_id` integer NOT NULL,
`author_id` integer NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_commit_author` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT `fk_commit_repo` FOREIGN KEY (`repo_id`) REFERENCES `repos` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- ok (608.042µs)

-------------------------
-- 4.971541ms
-- 1 migration
-- 2 sql statements

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

Changes That Rebuild a Table​

SQLite cannot change a column's nullability, type, or default in place. For these changes, Atlas plans a table rebuild: it creates a new table with the desired definition, copies the rows, drops the old table, and renames the new one.

Make the description column required, with an empty string as its default:

schema.hcl
  column "description" {
null = false
default = ""
type = text
}

Generate the migration:

atlas migrate diff require_description \
--to file://schema.hcl \
--dev-url "sqlite://dev?mode=memory"
20261129140357_require_description.sql
-- Disable the enforcement of foreign-keys constraints
PRAGMA foreign_keys = off;
-- Create "new_repos" table
CREATE TABLE `new_repos` (
`id` integer NOT NULL,
`name` text NOT NULL,
`owner_id` integer NOT NULL,
`description` text NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
CONSTRAINT `fk_repo_owner` FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Copy rows from old table "repos" to new temporary table "new_repos"
INSERT INTO `new_repos` (`id`, `name`, `owner_id`, `description`) SELECT `id`, `name`, `owner_id`, IFNULL(`description`, '') AS `description` FROM `repos`;
-- Drop "repos" table after copying rows
DROP TABLE `repos`;
-- Rename temporary table "new_repos" to "repos"
ALTER TABLE `new_repos` RENAME TO `repos`;
-- Enable back the enforcement of foreign-keys constraints
PRAGMA foreign_keys = on;

The plan has three parts to notice:

  • PRAGMA foreign_keys = off and on turn off foreign key enforcement for the rebuild and turn it back on after it.
  • The INSERT ... SELECT wraps description in IFNULL(..., ''), so existing NULL values get the new default instead of violating NOT NULL.
  • The fk_repo_owner foreign key is recreated on the new table.

Apply it with atlas migrate apply --env local, as before. Review rebuild migrations before applying them to large tables, since the copy rewrites every row. Without a DEFAULT, making a column NOT NULL fails if it contains NULL values: the LT101 lint check reports this case.

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 SQLite database schema. We covered both declarative and versioned migration workflows, showed how Atlas plans the table rebuilds SQLite needs, and pushed schemas and migrations to an Atlas workspace.

To keep going:

Have questions? Feedback? Message our team on Intercom.