Skip to main content

Helps manage migrations for databases and anything else

Project description GitHub issues PyPI - Downloads PyPI PyPI - Python Version

Migrate anything - database (etc.) migration utility, especially for Python projects.

What is this?

It’s kinda annoying how often you run into the question of how to handle migrations in your project, and there hasn’t seem to emerged any good, DB -agnostic, framework-agnostic, and storage-agnostic tool to manage them.

This project is an attempt to change that.

Basically what it does when you run migrate-anything migrations is:

  1. Find all the files migrations/*.py and sort them
  2. Any that are not yet registered in the DB will be loaded, their up() is executed, and the file’s contents stored in the DB
  3. Any files that are missing from the fs but are in the DB will have their code loaded from the DB and their down() is executed - in reverse order


Licensing is important. This project uses BSD 3-clause license, and adds no other dependencies to your project (it does use a few things during build & testing) - that’s about as simple, safe, and free to use as it gets.

For more information check the LICENSE -file.

Usage examples

Firstly you’ll need this package in your project. Pick one of these:

pip install -U migrate-anything
poetry add migrate-anything
pipenv install migrate-anything

Simply put, create a Python package, don’t be too clever and call it e.g. migrations. Then put files in that package:

# migrations/
from migrate_anything import configure, CSVStorage

# migrations/
# Please note that this is built for a completely hypothetical DB layer
from my_db import get_db

DB = get_db()

def up():

def down():

This would configure your migrations’ status to be stored in a local file called migration_status.csv and set up your first migration script. If you have a my_db module that works like this you could just run this with a single command:

migrate-anything migrations
poetry run migrate-anything migrations
pipenv run migrate-anything migrations

Now in the real world you might want something more durable and a realistic example, so here’s e.g. what you’d do when using MongoDB:

from migrate_anything import configure, MongoDBStorage
import pymongo

db = pymongo.MongoClient().my_db

from pymongo import MongoClient

client = MongoClient()
db = client.my_db

def up():
        "id": "post-1",
        "title": "We're live!",
        "content": "This is our first post, yay."

def down():

This would configure storage to a my_db.migrations MongoDB collection.

Future ideas include support for other DB engines (feel free to contribute), and Kubernetes ConfigMap. Annoyingly storage to Kubernetes from inside a pod and in code is not quite as simple as just running kubectl.

Oh and your Kubernetes pods will likely require the necessary RBAC rules to manage their ConfigMap. It’s unfortunately kinda complex, but I’m sure you can figure it out e.g. with this guide.

Alternatively you can just write your own - it’s easy.

from migrate_anything import configure

class CustomStorage(object):
    def __init__(self, file):
        self.file = file

    def save_migration(self, name, code):
        with open(self.file, "a", encoding="utf-8") as file:
            file.write("{},{}\n".format(name, code))

    def list_migrations(self):
            with open(self.file, encoding="utf-8") as file:
                return [
                    for line in file.readlines()
                    if line.strip()  # Skip empty lines
        except FileNotFoundError:
            return []

    def remove_migration(self, name):
        migrations = [
            migration for migration in self.list_migrations() if migration[0] != name

        with open(self.file, "w", encoding="utf-8") as file:
            for row in migrations:


You can also check out the examples.


This project is run on GitHub using the issue tracking and pull requests here. If you want to contribute, feel free to submit issues (incl. feature requests) or PRs here.

Project details

Download files

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

Files for migrate-anything, version 0.1.6
Filename, size File type Python version Upload date Hashes
Filename, size migrate_anything-0.1.6-py2.py3-none-any.whl (9.3 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size migrate-anything-0.1.6.tar.gz (7.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page