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
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.
- PyPI package: https://pypi.org/project/tiny-migrations/
- Free software: MIT License
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_onattributes. - Make sure your
upmethod receives the database connection argument.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
062770f344a3dd02755e456f24d930aeb2cf7b82110114b4e1bdad2786003332
|
|
| MD5 |
f7a87a5f7a1e7174d796e4a11d717665
|
|
| BLAKE2b-256 |
96e3831f54be2059e2599bdd6b157bf288723f2d93075637495332621f176dc3
|
File details
Details for the file tiny_migrations-0.1.2-py3-none-any.whl.
File metadata
- Download URL: tiny_migrations-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3059daa4b361fc0a530cd988140a3884e55c32f5b640f52ab2ec78b460e88ab1
|
|
| MD5 |
ff91fd8c9c9480143a75fc4d7eeed555
|
|
| BLAKE2b-256 |
f76b624faa0a4bc374e5cedfa6e33f69977464baed56365f043cb1514ed428e5
|