4 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
Sqldef allows you to manage database schemas using plain SQL across multiple database systems like MySQL and PostgreSQL. You define your schema in a single SQL file, and sqldef generates the necessary migrations to update your database. It supports idempotent operations, making it safe to run multiple times without unintended changes.
If you do, here's more
sqldef is a schema management tool designed for multiple SQL databases, including MySQL, PostgreSQL, SQLite3, and SQL Server. It allows users to write their database schema in plain SQL and automatically generates migration scripts to align the database with the desired schema. The process is straightforward: maintain a single SQL file that represents the complete schema. When changes are needed, such as adding columns or modifying constraints, you edit this file. sqldef then compares the current schema with the desired state and produces the necessary Data Definition Language (DDL) commands to effect those changes.
Each database type has its own command for ease of use: `mysqldef` for MySQL, `psqldef` for PostgreSQL, `sqlite3def` for SQLite3, and `mssqldef` for SQL Server. These commands mimic the connection options of the respective native clients, making integration into existing workflows seamless. The tool runs as a single binary with no external dependencies, ensuring that operations are safe to execute multiple times without unintended consequences. It also supports offline comparisons between SQL files, which is useful for CI/CD practices, schema validation, and generating migration scripts without needing to connect to a database.
Renaming tables, columns, and indexes is facilitated by using an annotation system with the `@renamed` keyword. This allows for clear migration paths when schema changes occur. For instance, if a table named `user_accounts` needs to be renamed to `users`, the desired schema can include the annotation, and sqldef will handle the renaming in the generated migration. The article outlines practical examples for using sqldef in both database mode and offline mode. The tool is available for download through GitHub, with Docker images for those who prefer containerized solutions.
Questions about this article
No questions yet.