qop - A simple database migration tool
qop is a command-line tool for managing database migrations. It's designed to be simple, straightforward, and easy to use.
Features
- Backend-agnostic design (supports PostgreSQL and SQLite)
- Simple migration file format (
up.sql,down.sql) - Timestamp-based migration IDs
- Command-line interface for managing migrations
Installation
Getting Started
-
Initialize a new project:
This will create a
qop.tomlfile in your current directory. -
Initialize the database:
-
Create your first migration:
This will create a new directory with
up.sqlanddown.sqlfiles. -
Apply the migration:
Configuration
qop is configured using a qop.toml file. Here are examples for both supported backends:
PostgreSQL Configuration
= ">=0.1.0"
[]
= { = "postgresql://postgres:password@localhost:5432/postgres" }
= "public"
= "migrations"
= 30
You can also use environment variables for the connection string:
= ">=0.1.0"
[]
= { = "DATABASE_URL" }
= "public"
= "migrations"
= 30
SQLite Configuration
= ">=0.1.0"
[]
= { = "sqlite:///path/to/database.db" }
= "migrations"
= 30
Or with environment variables:
= ">=0.1.0"
[]
= { = "DATABASE_URL" }
= "migrations"
= 30
The migration files are expected to be in a directory relative to the qop.toml file.
Usage
qop provides several commands to manage your database migrations through subsystems.
init
Initializes a new qop project by creating a qop.toml file in the current directory.
Arguments:
-p, --path <PATH>: Path to theqop.tomlconfiguration file. (default:./qop.toml)
subsystem
The core command for managing database-specific operations. Available aliases: sub, s
PostgreSQL Commands
All PostgreSQL operations are accessed through the postgres (alias: pg) subsystem:
qop subsystem postgres init
Initializes the migration table in your PostgreSQL database.
qop subsystem postgres new
Creates a new migration directory with up.sql and down.sql files.
This will create a directory structure like:
migrations/
└── id=1678886400000/
├── up.sql
└── down.sql
qop subsystem postgres up
Applies pending migrations. By default, it applies all pending migrations.
Arguments:
-p, --path <PATH>: Path to theqop.tomlconfiguration file. (default:qop.toml)-c, --count <COUNT>: The number of migrations to apply. If not specified, all pending migrations are applied.-t, --timeout <TIMEOUT>: Statement timeout in seconds.-d, --diff: Show migration diff before applying (experimental feature)
qop subsystem postgres down
Reverts applied migrations. By default, it reverts the last applied migration.
Arguments:
-p, --path <PATH>: Path to theqop.tomlconfiguration file. (default:qop.toml)-c, --count <COUNT>: The number of migrations to revert. (default: 1)-t, --timeout <TIMEOUT>: Statement timeout in seconds.-r, --remote: Use thedown.sqlfrom the database instead of the local file.-d, --diff: Show migration diff before applying (experimental feature)
qop subsystem postgres list
Lists all migrations, showing their status (applied or not) and when they were applied.
qop subsystem postgres history
Manages migration history with commands for syncing and fixing migration order.
qop subsystem postgres history sync
Upserts all remote migrations locally. This is useful for syncing migrations across multiple developers.
qop subsystem postgres history fix
Shuffles all non-run local migrations to the end of the chain. This is useful when you have created migrations out of order.
qop subsystem postgres diff
Shows pending migration operations without applying them (experimental feature).
qop subsystem postgres apply
Applies or reverts a specific migration by ID.
qop subsystem postgres apply up
Applies a specific migration.
Arguments:
<ID>: Migration ID to apply (required)-t, --timeout <TIMEOUT>: Statement timeout in seconds.
qop subsystem postgres apply down
Reverts a specific migration.
Arguments:
<ID>: Migration ID to revert (required)-t, --timeout <TIMEOUT>: Statement timeout in seconds.-r, --remote: Use thedown.sqlfrom the database instead of the local file.
SQLite Commands
All SQLite operations are accessed through the sqlite (alias: sql) subsystem and support the same commands as PostgreSQL:
qop subsystem sqlite init
Initializes the migration table in your SQLite database.
qop subsystem sqlite new
Creates a new migration directory with up.sql and down.sql files.
qop subsystem sqlite up
Applies pending migrations.
Arguments:
-p, --path <PATH>: Path to theqop.tomlconfiguration file. (default:qop.toml)-c, --count <COUNT>: The number of migrations to apply.-t, --timeout <TIMEOUT>: Statement timeout in seconds.-d, --diff: Show migration diff before applying (experimental feature)
qop subsystem sqlite down
Reverts applied migrations.
Arguments:
-p, --path <PATH>: Path to theqop.tomlconfiguration file. (default:qop.toml)-c, --count <COUNT>: The number of migrations to revert.-t, --timeout <TIMEOUT>: Statement timeout in seconds.-r, --remote: Use thedown.sqlfrom the database instead of the local file.-d, --diff: Show migration diff before applying (experimental feature)
qop subsystem sqlite list
Lists all migrations, showing their status and when they were applied.
qop subsystem sqlite history sync
Upserts all remote migrations locally.
qop subsystem sqlite history fix
Shuffles all non-run local migrations to the end of the chain.
qop subsystem sqlite diff
Shows pending migration operations without applying them (experimental feature).
qop subsystem sqlite apply up
Applies a specific migration by ID.
qop subsystem sqlite apply down
Reverts a specific migration by ID.
man
Renders the manual.
qop man
Arguments:
-o, --out <PATH>: Path to write documentation to (required)-f, --format <FORMAT>: Format for the documentation. Can bemanpagesormarkdown(required)
autocomplete
Renders shell completion scripts.
qop autocomplete
Arguments:
-o, --out <PATH>: Path to write completion script to (required)-s, --shell <SHELL>: The shell to generate completions for (bash,zsh,fish,elvish,powershell) (required)
Experimental Features
Some features require the --experimental (or -e) flag to enable:
diffcommand and--diffflag for showing migration differences before applying- These features provide preview functionality but are still under development