Skip to main content

python-agnostic-migrations

Extremely simple migration tool for python projects that is agnostic to what it is migrating.

Usage

Create an instance of AgnosticMigrator and pass in 2 things:

  1. An absolute path to a folder containing migration files
  2. A storage mechanism to keep track of what migrations have been run. It will need to be an implementation of the `AgnosticMigratorStorage" class

We provide a sample implementation of AgnosticMigratorInMemoryStorage for running tests or trying out the library, but it is recommended to use a storage service of some kind, like s3, dynamodb, file storage, or a database.

Example

import os
from agnostic_migrations import AgnosticMigrator, AgnosticMigratorInMemoryStorage

migrations_path = os.path.dirname(os.path.abspath(__file__)) + "/migrations"

def execute(): 
  migrator = AgnosticMigrator(
      migrations_path,
      AgnosticMigratorInMemoryStorage() #  or your storage implementation here
  )
  migrator.run_migrations()

Migrations

The migration files have no specific naming convention. The process will simply consider every file ending in .py as a migration file, and process them in alphabetical order. Because of this, it is recommended to prefix with equal digit number, like so:

  • 001-initial-migration.py
  • 002-update-schema.py
  • 003-add-user.py

Example Migration File

Migration files must create a migration variable that is an instance of AgnosticMigration to be valid.

from agnostic_migrations import AgnosticMigration

def apply():
    print("Migrating application state.")
    # Execute custom code here to interface with tool you wish to run migration against


def revert():
    print("An error occurred during application. Safely roll back changes here.")
    # Execute custom code here to interface with tool you wish to run revert against in the case of a failed apply

migration = AgnosticMigration(apply, revert)

WHY?

Having an agnostic migration tool is very helpful for 2 cases:

  1. The tool you are wanting to manage is not a traditional relational database, and you want to be able to use a liquibase/flyway style migration to manage its state reliably and at scale.
  2. You want to be able to manage the migrations or state of multiple dependencies with the same tooling.
  3. You want to be able to leverage existing code in your codebase to interact with the tool and create the migrations. This can be something like an sdk you are familiar with, or custom code you have already written for your application.

Inspiration

This library was created as a lightweight python implementation of a pattern similar to Umzug, which is a similar tool written for typescript.

Metadata

Release files for agnostic-migrations 1.0.0

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

Source distribution (sdist)

Source distribution for agnostic-migrations 1.0.0
File Size Uploaded
agnostic_migrations-1.0.0.tar.gz 2.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agnostic-migrations 1.0.0
File Interpreter ABI Platform
agnostic_migrations-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.7 kB

Release files / agnostic_migrations-1.0.0.tar.gz

Download URL agnostic_migrations-1.0.0.tar.gz
Size 2.6 kB
Tags Source
SHA-256 checksum
How to use checksums
aff9b37aa32614e82c217e4391b93a8992d2a742b90714b69fa761e903a51571
BLAKE2b-256 checksum
How to use checksums
74d043908533f9844e4e4aa6623108e40994c2cca4dd581879cf7c35ec141959
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / agnostic_migrations-1.0.0-py3-none-any.whl

Download URL agnostic_migrations-1.0.0-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e0321ac4af916b81fa6f39afc49fc5a229c81b151a5e659a7a1dbb48d99099a
BLAKE2b-256 checksum
How to use checksums
3b65decbd527274a616a0c8cff47bfeb07725dc94e7f77e065b38839a48b1539
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release 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