Skip to main content

A tiny, lightweight, package for managing database migrations. Designed to work without INI files or dynamic module loading, making it ideal for compiled or frozen applications as well as traditional servers.

Project description

Tiny Migrations

Introdution

PyPI version

A tiny, lightweight, package for managing sqlite database migrations. Designed to work without INI files or dynamic module loading, making it ideal for compiled or frozen applications as well as traditional servers.

Installation

Install via pip:

pip install tiny-migrations

Defining Migrations

Create migration classes by subclassing MigrationBase. Each migration must implement the up(db_connection) method.

from tiny_migrations import MigrationBase

class CreateUsersTable(MigrationBase):
    def up(self, db_connection):
        cursor = db_connection.cursor()
        cursor.execute("""
            CREATE TABLE users (
                id INTEGER PRIMARY KEY,
                username TEXT NOT NULL
            );
        """)
        db_connection.commit()
        cursor.close()

Each migration requires a unique ID and a description:

migration1 = CreateUsersTable("001", "Create users table")

You can specify dependencies using the depends_on argument:

migration2 = AddEmailColumn("002", "Add email column", depends_on=migration1)

Setting Up the Database Connection

Use a direct sqlite3.Connection object:

import sqlite3

db_conn = sqlite3.connect("my_database.db")

Running Migrations

Pass your migrations to TinyMigrations and run them in order:

from tiny_migrations import TinyMigrations

tm = TinyMigrations(db_conn)
tm.migrate([migration1, migration2])

Migration Dependencies

Migrations must depend on a previous migration. Ensure you pass them in dependency order:

migration2 = AddEmailColumn("002", "Add email column", depends_on=migration1)
tm.migrate([migration1, migration2])

Targeted Migration

You can migrate up to a specific migration by passing its unique ID:

tm.migrate([migration1, migration2, migration3], target_migration="002")

Example: Adding and Modifying Tables

class AddAgeColumn(MigrationBase):
    def up(self, db_connection):
        cursor = db_connection.cursor()
        cursor.execute("ALTER TABLE users ADD COLUMN age INTEGER;")
        db_connection.commit()
        cursor.close()

migration3 = AddAgeColumn("003", "Add age column", depends_on=migration2)

tm.migrate([migration1, migration2, migration3])

Stamping Migrations

You can mark a migration as applied without actually running its up method using the stamp feature. This is useful when you want to record that a migration has already been applied manually or outside of Tiny Migrations.

tm.stamp("001", "Stamp test migration")

Troubleshooting

  • If you get a dependency error, check the order and depends_on attributes.
  • Make sure your up method receives the database connection argument.

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

tiny_migrations-0.1.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiny_migrations-0.1.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tiny_migrations-0.1.2.tar.gz.

File metadata

  • Download URL: tiny_migrations-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for tiny_migrations-0.1.2.tar.gz
Algorithm Hash digest
SHA256 062770f344a3dd02755e456f24d930aeb2cf7b82110114b4e1bdad2786003332
MD5 f7a87a5f7a1e7174d796e4a11d717665
BLAKE2b-256 96e3831f54be2059e2599bdd6b157bf288723f2d93075637495332621f176dc3

See more details on using hashes here.

File details

Details for the file tiny_migrations-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for tiny_migrations-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3059daa4b361fc0a530cd988140a3884e55c32f5b640f52ab2ec78b460e88ab1
MD5 ff91fd8c9c9480143a75fc4d7eeed555
BLAKE2b-256 f76b624faa0a4bc374e5cedfa6e33f69977464baed56365f043cb1514ed428e5

See more details on using hashes here.

Supported by

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