Managing database schema changes can be a daunting task, especially when working with complex applications. Developers often find themselves tangled in migration scripts and schema discrepancies. Aerich steps in to simplify this process for TortoiseORM users, providing a seamless way to handle database migrations with ease. Whether you're new to TortoiseORM or a seasoned developer, Aerich helps ensure your application's database schema remains in sync with your models.
What Is Aerich?
Aerich is a dedicated database migrations tool for TortoiseORM, designed to streamline the process of managing database schema changes. It works similarly to Alembic for SQLAlchemy or the migration solutions found in Django ORM. Aerich offers a command-line interface (CLI) that allows developers to generate, apply, and manage migrations efficiently, making it a critical addition to your TortoiseORM toolkit.
Key Features
- Easy Migration Management: Generate and apply migrations with simple commands, reducing the overhead of manual schema updates.
- Database Introspection: Inspect your database tables to create initial migration files effortlessly.
- Rollback Support: Downgrade your schema to previous versions easily, providing flexibility during development.
- Version Control: Keep track of migration history, ensuring you can revert to any previous state if needed.
- Configuration Support: Use configuration files like
pyproject.tomlto manage your migration settings seamlessly. - Multi-Database Support: Works with MySQL, PostgreSQL, and SQLite, providing flexibility for various projects.
- Integration with TortoiseORM: Directly integrates with TortoiseORM models, ensuring migrations reflect your data structure accurately.
Installation & Setup
Getting started with Aerich is straightforward. You can install it via PyPI or directly from the GitHub repository. Here’s how:
pip install "aerich[toml]"
If you prefer the latest version from GitHub, use the following command:
pip install "aerich[toml] @git+https://github.com/tortoise/aerich"
After installation, you'll need to configure TortoiseORM to use Aerich. Add aerich.models to your TortoiseORM configuration as follows:
TORTOISE_ORM = {
"connections": {"default": "mysql://root:123456@127.0.0.1:3306/test"},
"apps": {
"models": {
"models": ["tests.models", "aerich.models"],
"default_connection": "default",
},
},
}
How to Use It
Once installed and configured, you can start using Aerich for managing your database migrations. Here’s a practical example:
# Initialize the Aerich configuration and migration folder
> aerich init
# Generate your first migration based on model changes
> aerich init-migrations
# Apply the migration to your database
> aerich upgrade
# To check your migration history
> aerich history
This simple workflow allows you to keep your database schema in sync with your application models effortlessly.
Who Should Use Aerich?
Aerich is an excellent choice for developers and teams using TortoiseORM for their Python applications. If you're looking for a reliable way to manage database migrations without the hassle of manual scripts, Aerich is for you. It’s particularly beneficial for applications with frequent schema changes, ensuring that your database remains consistent and up to date.
Final Thoughts
Aerich is a powerful and practical tool for managing database migrations within TortoiseORM. Its ease of use, combined with robust features, makes it an essential component of any TortoiseORM-based project. Whether you’re working on a small project or a large-scale application, Aerich can help streamline your development process, allowing you to focus more on building features rather than managing migrations. Give it a try, and see how it can simplify your workflow!