Skip to main content

SQLAltery

A migration library for SQLAlchemy. A cleaner frontend to Alembic.

Guide

Set up management script

SQLAltery provides the "functional core". To use it in your project, you'll need to provide your own "imperative shell", but this is easy. Here's a starter managedb.py:

from pathlib import Path

import sqlalchemy as sa
from sqlaltery import CLI as SQLAlteryCLI

from my_app import my_model

class MyCLI(SQLAlteryCLI):
	# where to store migration files
	migration_dir = Path(__file__).parent / 'migration'
	# your model metadata
	metadata = my_model.Base.metadata
	# URL of the DB you want to migrate
	db_url = 'sqlite:///somedb.sqlite3'

if __name__ == '__main__':
	import funcli
	cli = MyCLI()
	funcli.run({
		# show pending changes to schema
		'diff': cli.diff,
		# create a new migration file from pending changes (if any)
		# `commit [--data]`
		'commit': cli.commit,
		# migrate DB to a revision
		# `sync [--revision <revision>] [--fake]`
		'sync': cli.sync,
	})

Terminology used here

You can name the operations anything, but this guide will use these terms:

  • commit: create a new migration (Django: py manage.py makemigrations)
  • sync: apply migrations (Django: py manage.py migrate)
  • sync --fake: fake migrations (Django: py manage.py migrate --fake); this updates only the migration metadata (table sqlaltery_migration)

Reset migrations

Assuming your database(s) are synced to latest, delete your migration dir, create a new migration, and do a fake sync:

rm -rf migration
py managedb.py commit
py managedb.py sync --fake

Data migrations

Data migrations usually involve a three-step process: expand, project, contract.

  • Expand the schema: T_old to T_exp \superset T_new, where T_exp is such that the underlying database can automatically project the data onto it (e.g., adding only nullable columns)
  • Project the data: apply a function T_exp -> T_new to the data; schema stays the same
  • Contract the schema: T_exp to T_new

SQLAltery can automatically split the migration into these steps. Run:

py managedb.py commit --data

Now, modify the newly created migration to write data migration code in the forwards and backwards methods in the generated file. You can access the current sa.MetaData in self.md. Once you're done, sync:

py managedb.py sync

Why not use Alembic directly?

  • Alembic diffs the head MD against current DB, not against latest migration
  • Alembic has split upgrade/downgrade
  • Alembic requires you to manually manage metadata in migrations (for doing queries)
  • Alembic creates a bunch of files (alembic.ini, env.py). The default case should be simple: just create an SQLAltery and use it; customize by passing arguments.

Internal use

Set up:

uv sync --no-install-workspace

Run checks:

bash check.sh

How to publish:

bash build.sh
py -m twine upload artifact/dist/<file>

Metadata

Release files for sqlaltery 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for sqlaltery 0.7.1
File Interpreter ABI Platform
sqlaltery-0.7.1-py3-none-any.whl Python 3 none any Details

Release files / sqlaltery-0.7.1-py3-none-any.whl

Download URL sqlaltery-0.7.1-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f31c8ebf2f36bcfa39683c470999787fd14175eb6d2a5e760e4675205d78e92
BLAKE2b-256 checksum
How to use checksums
f5f875cf3292c1db04c1612e8f096224eec885cbc54b439c8a3b0600cdd64845
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.3

Release history Release notifications | RSS feed

0.7.2

1 release file

This release

0.7.1 This release

1 release file

0.7.0

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.7

1 release file

0.5.6

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

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