Skip to main content

Wandern is a small, no-nonsense database migration tool for python

Project description

Wandern

Wandern is a database migration tool written in Python. Wandern provides a simple, straight-forward interface for creating and managing migrations with plain SQL files, giving you the flexibility to add your own SQL statements.

Wandern demo

Installation

PIP

pip install wandern

UV

uv add wandern

Using postgresql In order to use postgresql, you need to install extra dependencies.

PIP

pip install "wandern[postgresql]"

UV

uv add wandern --extra postgresql

Quick Start

  1. Initialize wandern in your project:
    wandern init
    

This creates a .wd.json file in your current working directory. You can specify an optional migration directory (default: wd_migrations inside current directory). Here is an example .wd.json configuration

{
  "dsn": "",
  "migration_dir": "/Users/<username>/<project_dir>/wd_migrations",
  "file_format": "{version}-{datetime:%Y%m%d_%H%M%S}-{message}",
  "migration_table": "wd_migrations"
}
  • dsn - The connection string of the database you want to apply your migrations to. Currently only supports sqlite and postgresql
    • Sqlite - dsn should start with sqlite://
    • Postgresql - dsn should start with postgresql://
  • migration_dir - The directory where the generated migration files will be stored. You can configure it later.
  • file_format - a python f-string format specifying the format of the generated filename.

Available settings

  • version - specify the version (autogenerated 8-character ID)
  • slug - a base64 encoded message slug
  • message - migration message provided during generating a new file.
  • author - name of the author provided during generating a new file.
  • epoch - the current POSIX timestamp
  • datetime - the current datetime (can be formatted using standard python datetime strftime format)
  1. Generate your first migration:
    wandern generate --message "create users table"
    

This will create a new empty migration file in your migration directory. You have to add the SQL expressions manually for both the UP and the DOWN revisions.

  1. Apply migrations:
    wandern up
    

This applies the UP revisions to the configured database and inserts a new row to the migration table (it creates the migration table first if it does not exist already).

  1. Downgrade migrations:
    wandern down
    

This will run the DOWN revisions of the migrations applied in the database and remove the rows from the table in sequence.

  1. Agentic Code Generation
    wandern prompt
    

Wandern offers support for generating migrations using LLMs, currently only supporting OpenAI and Google as providers. You can install the dependencies with:

pip install wandern[openai]

or

pip install wandern[google-genai]

You must provide your own API key (e.g. OPENAI_API_KEY or GOOGLE_API_KEY or GEMINI_API_KEY) in your environment variable to use it.

To see all the commands, see Available commands

Supported Databases

  • SQLite
  • PostgreSQL
  • MySQL (coming soon)
  • MSSQL (coming soon)

Available Commands

wandern init [directory]

Initialize wandern for a new project.

If you do not specify a directory, Wandern will assume the default migration folder name (wd_migrations) in your current working directory.

Options:

  • --interactive, -i - Run initialization in interactive mode
  • directory - Path to the directory to contain migration scripts (optional)

wandern generate

Generate a new empty migration file.

Wandern will check the last local migration file and set the down revision ID of the new migration to point to that. If this is your first migration, the down revision ID will be blank.

A generated migration file looks like the following:

/*Autogenerated by Wandern, please add your migration SQL here.

Timestamp: {{ current_timestamp }}

Revision ID: {{ revision_id }}
Revises: {{ own_revision_id }}
Message: {{ message }}
Tags: {{ tags }}
Author: {{ author }}
*/

-- UP

-- Add your UP migration SQL here

-- DOWN

-- Add your DOWN migration SQL here

Generated migration files do not contain any SQL. You have to write your own UP and DOWN SQL statements in their respective areas, identified by the comments. Note: Wandern does not check the validity of the written SQL statements, it is your responsibility to write correct and dialect-specific SQL statements so that they can be run without any errors.

Options:

  • --message, -m - Brief description of the migration (required)
  • --author, -a - Author of the migration (defaults to system user)
  • --tags, -t - Comma-separated list of tags (optional)

wandern prompt

Generate a filled migration file from natural language prompt using LLM Agents. Currently only supports OpenAI (must provide OPENAI_API_KEY) and Google (must provide GOOGLE_API_KEY or GEMINI_API_KEY).

You need to install additional dependencies with the extras openai or google-genai respectively.

Options:

  • --author, -a - Author of the migration (defaults to system user)
  • --tags, -t - Comma-separated list of tags (optional)

wandern up

Apply pending migrations to the database.

This command compares the local migration file versions against the revisions inserted into the database, and applies anything that's missing. If run without specifying the number of steps, it will apply all the migrations from the beginning. You can also filter by author name or tags. However, in order to ensure that the revisions can be safely downgraded, it is required that the revisions for a particular author or a particular set of tags are sequential, i.e. there is no missing revision between two subsequent migrations. Otherwise Wandern will raise an error.

Divergence and circular reference checking

Wandern takes care of revision divergence, i.e. when you have two migration files created from the same down revision ID. This can happen due to two people pushing to the version control at the same time from a snapshot and both creating a new migration file from the last revision ID. In such cases, you have to fix the divergence yourself and either replace the two conflicting files with one file containing the merged stuff, or apply them sequentially. There is also an error raised if there is any circular dependency between two local revisions, although that is fairly uncommon as generating files using wandern generate will always use the last revision ID as the down ID.

Options:

  • --steps - Number of migration steps to apply (default: all)
  • --tags, -t - Apply only migrations with specified tags
  • --author, -a - Apply only migrations by specified author

wandern down

Roll back applied migrations.

Just like wandern up, you can specify the number of steps to the command. The command checks the migration entries in the database and finds their corresponding files in the local migration folder and runs their DOWN SQL expressions sequentially until the number of steps is satisfied, or everything is downgraded to the very beginning.

Options:

  • --steps - Number of migration steps to roll back (default: all)

wandern reset

Reset all migrations by rolling back all applied migrations. This is the same as running wandern down without any steps. Note: This does not remove the migration table from the database.

wandern browse

Browse database migrations interactively with filtering options. You can filter by author name, select one or more tags, or by created date.

Options:

  • --all, -A - Include all migrations (both local and database)

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

wandern-0.0.2.tar.gz (37.1 kB view details)

Uploaded Source

Built Distribution

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

wandern-0.0.2-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file wandern-0.0.2.tar.gz.

File metadata

  • Download URL: wandern-0.0.2.tar.gz
  • Upload date:
  • Size: 37.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.14

File hashes

Hashes for wandern-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2b4d633b546fd1b501972c10235f08e48f7e5c79848d74ec3345878760fab4d2
MD5 b7daf422360751f58d96e9fa8c7a2670
BLAKE2b-256 060a737b11364e90d269346f4b4b72d5af20d5e55b34c43efc80156b289a461f

See more details on using hashes here.

File details

Details for the file wandern-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: wandern-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.14

File hashes

Hashes for wandern-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6798dd842e2cf5b6f94bc7fd1a66884844bbafb7f646b93504ef061eb883a914
MD5 a2f6b37ca104be5f8c0cb4b8dc89cffe
BLAKE2b-256 181e2687fc532e9841df44e2fd0c544e10390ba0892d051aa3db296f85cac1db

See more details on using hashes here.

Supported by

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