Yoyo Migrations is a robust, database-agnostic migration tool for Python projects, enabling users to manage SQL-based schema changes. It supports various database systems with both synchronous and asynchronous drivers. Currently at version 9.0.0, it maintains an active release cadence, introducing new features and refining API usability while periodically updating Python version support.
pip install yoyo-migrationsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically apply Yoyo migrations using a temporary SQLite in-memory database and dynamically created migration files. It connects to the database, reads migrations from a specified directory, applies them, and then prints the updated table schema before cleaning up. Note the use of `await` for async database operations, a common pattern in Yoyo 9.x.
Upgrade your Python environment to 3.7 or a newer version.
Refactor database connection calls to use a single connection string URL. Consult the Yoyo documentation for correct URL formats for your specific database.
Be mindful of your chosen database driver. If using an async driver (e.g., `asyncpg`), ensure all `yoyo.connections.connect()` and `backend` object methods are called with `await` within an `async` function. If using a sync driver (e.g., `psycopg2-binary`), `await` is not necessary but using `asyncio.run` to call the main function that wraps `connect` is still fine.
Design migration scripts to handle existing states gracefully (e.g., `CREATE TABLE IF NOT EXISTS`, `ALTER TABLE ... ADD COLUMN ... IF NOT EXISTS`). Test your migrations thoroughly in environments resembling production before deployment.
For CLI usage, always ensure `yoyo.ini` is correctly configured and located. For programmatic integration, explicitly provide the `migrations` directory path to `get_migrations()` and the database URI to `connect()`, managing configuration outside of `yoyo.ini`.