Skip to main content

A simple database migration system for SQLite, based on sqlite-utils

Project description

sqlite-migrate

PyPI Changelog Tests License

A simple database migration system for SQLite, based on sqlite-utils

This project is an early alpha. Expect breaking changes.

Installation

This tool works as a plugin for sqlite-utils. First install that:

pip install sqlite-utils

Then install this plugin like so:

sqlite-utils install sqlite-migrate

Migration files

This tool works against migration files. A migration file looks like this:

from sqlite_migrate import Migrations

# Pick a unique name here - it must not clash with other migration sets that
# the user might run against the same database.

migration = Migrations("creatures")

# Use this decorator against functions that implement migrations
@migration()
def m001_create_table(db):
    # db is a sqlite-utils Database instance
    db["creatures"].create(
        {"id": int, "name": str, "species": str},
        pk="id"
    )

@migration()
def m002_add_weight(db):
    # db is a sqlite-utils Database instance
    db["creatures"].add_column("weight", float)

Here is documentation on the Database instance passed to each migration function.

Running migrations

Running this command will execute those migrations in sequence against the specified database file.

Call migrate with a path to your database and a path to the migrations file you want to apply:

sqlite-utils migrate creatures.db path/to/migrations.py

Running this multiple times will have no additional affect, unless you add more migration functions to the file.

If you call it without arguments it will search for and apply any migrations.py files in the current directory or any of its subdirectories.

You can also pass the path to a directory, in which case all migrations.py files in that directory and its subdirectories will be applied:

sqlite-utils migrate creatures.db path/to/parent/

Listing migrations

Add --list to list migrations without running them, for example:

sqlite-utils migrate creatures.db --list

The output will look something like this:

Migrations for: creatures

  Applied:
    m001_create_table - 2023-07-23 04:09:40.324002
    m002_add_weight - 2023-07-23 04:09:40.324649
    m003_add_age - 2023-07-23 04:09:44.441616
    m003_cleanup - 2023-07-23 04:09:44.443394
    m004_cleanup - 2023-07-23 04:09:44.444184
    m005_cleanup - 2023-07-23 04:09:44.445389
    m006_cleanup - 2023-07-23 04:09:44.446742
    m007_cleanup - 2023-07-23 04:16:02.529983

  Pending:
    m008_cleanup

Verbose mode

Add -v or --verbose for verbose output, which will show the schema before and after the migrations were applied along with a diff:

sqlite-utils migrate creatures.db --verbose

Example output:

Migrating creatures.db

Schema before:

  CREATE TABLE [_sqlite_migrations] (
     [migration_set] TEXT,
     [name] TEXT PRIMARY KEY,
     [applied_at] TEXT
  );
  CREATE TABLE [creatures] (
     [id] INTEGER PRIMARY KEY,
     [name] TEXT,
     [species] TEXT
  , [weight] FLOAT);

Schema after:

  CREATE TABLE [_sqlite_migrations] (
     [migration_set] TEXT,
     [name] TEXT PRIMARY KEY,
     [applied_at] TEXT
  );
  CREATE TABLE "creatures" (
     [id] INTEGER PRIMARY KEY,
     [name] TEXT,
     [species] TEXT,
     [weight] FLOAT,
     [age] INTEGER,
     [shoe_size] INTEGER
  );

Schema diff:

    [name] TEXT PRIMARY KEY,
    [applied_at] TEXT
 );
-CREATE TABLE [creatures] (
+CREATE TABLE "creatures" (
    [id] INTEGER PRIMARY KEY,
    [name] TEXT,
-   [species] TEXT
-, [weight] FLOAT);
+   [species] TEXT,
+   [weight] FLOAT,
+   [age] INTEGER,
+   [shoe_size] INTEGER
+);

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlite-migrate-0.1a1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

sqlite_migrate-0.1a1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlite-migrate-0.1a1.tar.gz.

File metadata

  • Download URL: sqlite-migrate-0.1a1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for sqlite-migrate-0.1a1.tar.gz
Algorithm Hash digest
SHA256 5c37a63fd83c561f9f7a5d2cd71072fd00efd6b87839060b7437c9dc57b79a25
MD5 bf7cd777ba8bffd71296aaafba35645f
BLAKE2b-256 62c900f0c21f10d2799336a793779c46c1e39a1e6b28e34cbdf85ac3d70b3f30

See more details on using hashes here.

File details

Details for the file sqlite_migrate-0.1a1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlite_migrate-0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 f89e3c7053ac40e2ce7e4d029144ca570c96a364bb0afbb4a66275513ae7e6e3
MD5 c537cae776e031fb7b4c5d674c24f527
BLAKE2b-256 904cf5f33896fe3c590760a4bb4a57e9887a0a0bf56b89e9d56c03c777243d0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page