sqldef is the easiest idempotent schema management tool for MySQL, PostgreSQL, SQLite3, and SQL Server that uses plain SQL DDLs. Define your desired schema in SQL, and sqldef generates and applies the migrations to update your database.
With sqldef, you maintain a single SQL file with your complete schema. To modify your schema - add columns, change constraints, or create indexes - simply edit this file. sqldef compares desired against current schema and generates the appropriate DDLs, ensuring your database reaches the desired state from any starting point.
Each database gets its own command (mysqldef
, psqldef
, sqlite3def
, mssqldef
) that mimics the connection options of the native database client, making it familiar and easy to integrate into existing workflows. The tool comes as a single binary with no dependencies, and provides idempotent operations that are safe to run multiple times.
This is inspired by Ridgepole but using SQL, so there's no need to remember Ruby DSL.
This is the basic workflow, which is identical across all databases - only the connection options differ between commands.
Note: Replace $sqldef
with the appropriate command for your database:
mysqldef
for MySQLpsqldef
for PostgreSQLsqlite3def
for SQLitemssqldef
for SQL Server
$sqldef [connection-options] --export > schema.sql
Export the existing database schema to review your starting point.
Edit schema.sql
to add, remove, or change columns/tables/indexes:
CREATE TABLE users (
id BIGINT PRIMARY KEY,
name VARCHAR(100),
age INTEGER, -- Added new column
created_at TIMESTAMP
);
$sqldef [connection-options] --dry-run < schema.sql
Show the migrations that will be applied without executing them (e.g., ALTER TABLE users ADD COLUMN age INTEGER
).
$sqldef [connection-options] < schema.sql
Apply the necessary DDLs to transform current schema to desired state.
Running again shows no changes needed - operations are idempotent.
Download the single-binary executable for your favorite database from:
https://github.com/sqldef/sqldef/releases
Docker images are available on Docker Hub:
https://hub.docker.com/u/sqldef
Debian packages might be supported in the future, but for now they have not been implemented yet.
# mysqldef
wget -O - https://github.com/sqldef/sqldef/releases/latest/download/mysqldef_linux_amd64.tar.gz \
| tar xvz
# psqldef
wget -O - https://github.com/sqldef/sqldef/releases/latest/download/psqldef_linux_amd64.tar.gz \
| tar xvz
# sqlite3def
wget -O - https://github.com/sqldef/sqldef/releases/latest/download/sqlite3def_linux_amd64.tar.gz \
| tar xvz
# mssqldef
wget -O - https://github.com/sqldef/sqldef/releases/latest/download/mssqldef_linux_amd64.tar.gz \
| tar xvz
Homebrew tap is available.
# mysqldef
brew install sqldef/sqldef/mysqldef
# psqldef
brew install sqldef/sqldef/psqldef
# sqlite3def
brew install sqldef/sqldef/sqlite3def
# mssqldef
brew install sqldef/sqldef/mssqldef
If you update parser/parser.y
, run:
$ make parser
Use the following commands to prepare command line tools and DB servers for running tests.
# Linux
$ sudo apt install mysql-client postgresql-client sqlite3
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
$ sudo apt-get update && sudo apt-get install mssql-tools # then add: export PATH="$PATH:/opt/mssql-tools/bin"
# macOS
$ brew install libpq && brew link --force libpq
$ brew install microsoft/mssql-release/mssql-tools
# Start database
$ docker compose up
# Run all tests
$ make test
# Run *def tests
$ go test ./cmd/*def
# Run a single test
$ go test ./cmd/mysqldef -run=TestApply/CreateTable
Please file a pull request if you have a feature request.
If you're unsure what to do, you may file a "Feature requests" ticket on Discussions and discuss how to implement that with the community.
The tagpr
and sqldef
workflows are used to release sqldef.
- (optional) A maintainer labels a pull request (PR) with
minor
ormajor
to manage the next version. - When a PR is merged to the default branch,
tagpr
creates a PR to bump the version and update the CHANGELOG.md ("release PR"). - A maintainer reviews the release PR and merges it.
tagpr
creates and pushes a release tag, which triggers the next workflow.sqldef
workflows creates a GitHub release, build artifacts, upload them to the GitHub release.
Unless it's a pretty big change that needs a discussion, we encourage sqldef maintainers to merge and release their own Pull Requests without asking/waiting for reviews.
We also expect them to release sqldef as frequently as possible. When there's a behavior change, sqldef should have at least one release on that day.
- @k0kubun
- @knaka (sqlite3def)
- @odz (mssqldef)
- @hokaccha (psqldef)
- @gfx (psqldef)
These are the component they were contributing to when they became a maintainer, but they're allowed to maintain every part of sqldef.
- @ytakaya (mssqldef)
Unless otherwise noted, the sqldef source files are distributed under the MIT License found in the LICENSE file.
parser is distributed under the Apache Version 2.0 license found in the parser/LICENSE.md file.