Skip to main content

No project description provided

Project description

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.

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

agnostic_migrations-1.0.0.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

agnostic_migrations-1.0.0-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file agnostic_migrations-1.0.0.tar.gz.

File metadata

  • Download URL: agnostic_migrations-1.0.0.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for agnostic_migrations-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aff9b37aa32614e82c217e4391b93a8992d2a742b90714b69fa761e903a51571
MD5 f4ac156ecff1aa6e95902e4e03d58944
BLAKE2b-256 74d043908533f9844e4e4aa6623108e40994c2cca4dd581879cf7c35ec141959

See more details on using hashes here.

File details

Details for the file agnostic_migrations-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agnostic_migrations-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e0321ac4af916b81fa6f39afc49fc5a229c81b151a5e659a7a1dbb48d99099a
MD5 b83bf93d6c0787b3772fd4a9fa95a40b
BLAKE2b-256 3b65decbd527274a616a0c8cff47bfeb07725dc94e7f77e065b38839a48b1539

See more details on using hashes here.

Supported by

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