Skip to main content

larzmigrate

Schema migrations for SQL and larzdb. Pure Python, zero dependencies.

Evolve a schema safely: write ordered migrations with an up and an optional down, and larzmigrate applies the pending ones in order, records what it did, and can roll back — against any DB-API connection (sqlite3, psycopg2, …) or a larzdb database.

import sqlite3
from larzmigrate import Migrator, SQLBackend

m = Migrator(SQLBackend(sqlite3.connect("app.db")))

m.add_sql("001_users",
          up="CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
          down="DROP TABLE users")

@m.migration("002_seed")
def seed(conn):
    conn.execute("INSERT INTO users (name) VALUES ('admin')")

m.migrate()        # apply everything pending, in order (idempotent)
m.rollback()       # undo the most recent migration
m.status()         # [("001_users", "applied"), ("002_seed", "applied")]

What makes it different

  • Storage-agnostic. The same migrator drives a SQL database (via any DB-API connection) or a larzdb / key-value store — pick the backend, write your migrations once.
  • Detects edited migrations. Every migration is checksummed. If one that's already applied gets changed, migrate() errors (checksum drift) instead of silently diverging from what actually ran — the bug that bites teams.
  • Real rollback. down migrations run in reverse; rollback(steps=N) undoes the last N.
  • Idempotent. Re-running only applies what's pending; safe to run on every deploy.
  • Zero dependencies. No Alembic, no ORM.

Install

pip install larzmigrate

Backends

from larzmigrate import Migrator, SQLBackend, KVBackend, DictStore

# SQL: any DB-API connection (sqlite3, psycopg2, MySQLdb, ...)
Migrator(SQLBackend(connection))

# larzdb or any get/put store
Migrator(KVBackend(larzdb_database))
Migrator(KVBackend(DictStore()))     # in-memory

Function migrations receive the backend's context — the DB connection (SQL) or the store (KV) — so they can do whatever that backend allows.

Commands

m.migrate()                  # apply all pending
m.migrate(target="003_x")    # apply up to (and including) an id
m.rollback()                 # undo the most recent
m.rollback(steps=3)          # undo the last 3
m.status()                   # [(id, "applied"|"pending"), ...]
m.pending(); m.applied()

Tests

python -m unittest discover -s tests -v   # 13 tests incl. real sqlite3 + drift

The Larz stack

Pure-Python, zero-dependency building blocks: larz · larzchain · larzmoney · larzcrypt · larzdb · larzagent · larzchart · larzmark · larztask · larzvault · larzvm · larzcache · larzvalidate · larzid · larzrpc · larzstate · larzhttp · larzconf · larzcron · larzlimit · larzlog · larzcli · larzretry · larztime · larzpdf · larzpack · larztemplate · larzcolor · larztable · larzjson · larzbus · larzmigrate

License

MIT © larz-scripter

Download files

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

Source Distribution

larzmigrate-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

larzmigrate-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file larzmigrate-0.1.0.tar.gz.

File metadata

  • Download URL: larzmigrate-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzmigrate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2e13762c3d822e5e1e2959e755e6c4150f4a52e003896e23cd29106ffb446679
MD5 ed667052ab64180417622a2d30663b08
BLAKE2b-256 1948b2855c87a4458cc1cf9acc460a982881e014e0965ad6c7121e9807f19281

See more details on using hashes here.

File details

Details for the file larzmigrate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzmigrate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzmigrate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0772eac2183b977e9eee69c8762d4e7dd7d5a51f38dcfcbee076dd82d76785ba
MD5 cd1eb23b579e865314e6a59f618e675b
BLAKE2b-256 ab72a35910c7f329da42b14faa0d2f1325af8a50adba22c499e1a285a25c158a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page