sqlite-migrate is a simple database migration system for SQLite, built upon the sqlite-utils library. It allows users to define database schema changes and data manipulations as Python functions within numbered files, which can then be applied incrementally. The current version is 0.1b0, with releases occurring periodically to address bugs and introduce new features during its beta phase.
pip install sqlite-migrateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a simple migration, apply it to an SQLite database, and verify the changes. It involves creating a temporary migration file and directory, then using the `Migrations` class to execute the migration.
For existing databases from `0.1aX`, consider creating a backup, then manually altering the `_sqlite_migrations` table to include `migration_set` in its primary key, or use a fresh database instance with `0.1b0+` for new projects. If using migration sets, ensure older migrations (before 0.1b0) are associated with a single 'default' migration set or handled carefully during schema upgrade.
Ensure every migration Python file includes `def migrate(db):` at the module level. Example: `def migrate(db):
db['mytable'].create({'id': int})`Always use zero-padded, sequential numbering for migration files, e.g., `001_initial_schema.py`, `002_add_user_email.py`, `010_refactor_addresses.py`.
If safe, drop and recreate the `_sqlite_migrations` table. For production databases, carefully manage the upgrade of this internal table's schema, potentially by manually adding the `migration_set` column and altering the primary key to `(migration_set, name)` before applying new migrations.
Review the migration file(s) that are intended to be executed and ensure each one contains a function named `migrate` that takes a single argument, `db` (e.g., `def migrate(db): ...`).