### SQL Table Definition Example Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md An example SQL `CREATE TABLE` statement for a table named 'test'. This demonstrates the structure of a database table that can be introspected by Aerich. ```sql CREATE TABLE `test` ( `id` int NOT NULL AUTO_INCREMENT, `decimal` decimal(10, 2) NOT NULL, `date` date DEFAULT NULL, `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `time` time DEFAULT NULL, `float` float DEFAULT NULL, `string` varchar(200) COLLATE utf8mb4_general_ci DEFAULT NULL, `tinyint` tinyint DEFAULT NULL, PRIMARY KEY (`id`), KEY `asyncmy_string_index` (`string`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci ``` -------------------------------- ### Install Aerich Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Install the Aerich package using pip. This is the first step to start using Aerich for your Tortoise ORM projects. ```shell pip install aerich ``` -------------------------------- ### Tortoise ORM Configuration Example Source: https://github.com/tortoise/aerich/blob/dev/README.md An example Python dictionary showing how to configure Tortoise ORM, including the necessary inclusion of 'aerich.models' for migration integration. ```python TORTOISE_ORM = { "connections": {"default": "mysql://root:123456@127.0.0.1:3306/test"}, "apps": { "models": { "models": ["tests.models", "aerich.models"], "default_connection": "default", }, }, } ``` -------------------------------- ### Initialize Aerich Configuration Source: https://github.com/tortoise/aerich/blob/dev/README.md Example command to initialize Aerich, specifying the Tortoise ORM configuration module. This creates the migration directory and config file. ```shell > aerich init -t tests.backends.mysql.TORTOISE_ORM Success create migrate location ./migrations Success write config to pyproject.toml ``` -------------------------------- ### Install Aerich Source: https://github.com/tortoise/aerich/blob/dev/README.md Installs Aerich with TOML support using pip. This is the primary method to get the tool. ```shell pip install "aerich[toml]" ``` -------------------------------- ### SQL Table Schema Example Source: https://github.com/tortoise/aerich/blob/dev/README.md An example SQL `CREATE TABLE` statement for a table named 'test'. This schema includes various data types like decimal, date, datetime, time, float, string, and tinyint, along with primary key and index definitions. ```sql CREATE TABLE `test` ( `id` int NOT NULL AUTO_INCREMENT, `decimal` decimal(10, 2) NOT NULL, `date` date DEFAULT NULL, `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `time` time DEFAULT NULL, `float` float DEFAULT NULL, `string` varchar(200) COLLATE utf8mb4_general_ci DEFAULT NULL, `tinyint` tinyint DEFAULT NULL, PRIMARY KEY (`id`), KEY `asyncmy_string_index` (`string`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci ``` -------------------------------- ### Tortoise ORM Multi-Database Configuration Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Configuration example for Tortoise ORM to manage multiple database connections. It shows how to define different applications with their respective models and default connections. ```python tortoise_orm = { "connections": { "default": expand_db_url(db_url, True), "second": expand_db_url(db_url_second, True), }, "apps": { "models": {"models": ["tests.models", "aerich.models"], "default_connection": "default"}, "models_second": {"models": ["tests.models_second"], "default_connection": "second", }, }, } ``` -------------------------------- ### Tortoise ORM Configuration Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Example configuration for Tortoise ORM, demonstrating how to include 'aerich.models' in your application's model list. This is crucial for Aerich to manage migrations. ```python TORTOISE_ORM = { "connections": {"default": "mysql://root:123456@127.0.0.1:3306/test"}, "apps": { "models": { "models": ["tests.models", "aerich.models"], "default_connection": "default", }, }, } ``` -------------------------------- ### Tortoise ORM Configuration for Multiple Databases Source: https://github.com/tortoise/aerich/blob/dev/README.md An example Python dictionary defining the Tortoise ORM configuration for managing multiple databases. It specifies connection strings for 'default' and 'second' databases and maps applications to these connections. ```python tortoise_orm = { "connections": { "default": "postgres://postgres_user:postgres_pass@127.0.0.1:5432/db1", "second": "postgres://postgres_user:postgres_pass@127.0.0.1:5432/db2", }, "apps": { "models": {"models": ["tests.models", "aerich.models"], "default_connection": "default"}, "models_second": {"models": ["tests.models_second"], "default_connection": "second", }, }, } ``` -------------------------------- ### Aerich CLI Commands Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Overview of the Aerich command-line interface (CLI) and its available commands for managing database migrations. This includes initialization, inspection, migration generation, and application. ```APIDOC Usage: aerich [OPTIONS] COMMAND [ARGS]... Options: -V, --version Show the version and exit. -c, --config TEXT Config file. [default: pyproject.toml] --app TEXT Tortoise-ORM app name. -h, --help Show this message and exit. Commands: downgrade Downgrade to specified version. heads Show current available heads in migrate location. history List all migrate items. init Init config file and generate root migrate location. init-db Generate schema and generate app migrate location. inspectdb Introspects the database tables to standard output as... migrate Generate migrate changes file. upgrade Upgrade to specified version. ``` -------------------------------- ### Aerich Init Command Usage Source: https://github.com/tortoise/aerich/blob/dev/README.md Shows the help message for the 'aerich init' command, detailing options for initializing the configuration file and migration location. ```APIDOC Usage: aerich init [OPTIONS] Init config file and generate root migrate location. Options: -t, --tortoise-orm TEXT Tortoise-ORM config module dict variable, like settings.TORTOISE_ORM. [required] --location TEXT Migrate store location. [default: ./migrations] -s, --src_folder TEXT Folder of the source, relative to the project root. -h, --help Show this message and exit. ``` -------------------------------- ### Initialize Aerich Configuration Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to initialize the Aerich configuration file and generate the root migration location. Requires specifying the Tortoise ORM configuration module. ```APIDOC Usage: aerich init [OPTIONS] Init config file and generate root migrate location. Options: -t, --tortoise-orm TEXT Tortoise-ORM config module dict variable, like settings.TORTOISE_ORM. [required] --location TEXT Migrate store location. [default: ./migrations] -s, --src_folder TEXT Folder of the source, relative to the project root. -h, --help Show this message and exit. ``` -------------------------------- ### Initialize Database Schema Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to generate the database schema and create the app migration location. Use the --app option if your Tortoise ORM app is not named 'models'. ```APIDOC Usage: aerich init-db Generate schema and generate app migrate location. Example: aerich --app other_models init-db ``` -------------------------------- ### Using Aerich Command Programmatically Source: https://github.com/tortoise/aerich/blob/dev/README.md Demonstrates how to use the `aerich.Command` class within a Python application to manage migrations. It shows initializing the command with configuration and app names, and then executing migration operations like `migrate` and `upgrade` asynchronously. ```python from aerich import Command async with Command(tortoise_config=config, app='models') as command: await command.migrate('test') await command.upgrade() ``` -------------------------------- ### Aerich CLI Usage Source: https://github.com/tortoise/aerich/blob/dev/README.md Displays the main help message for the Aerich command-line interface, listing available commands and global options. ```APIDOC Usage: aerich [OPTIONS] COMMAND [ARGS]... Options: -V, --version Show the version and exit. -c, --config TEXT Config file. [default: pyproject.toml] --app TEXT Tortoise-ORM app name. -h, --help Show this message and exit. Commands: downgrade Downgrade to specified version. heads Show current available heads in migrate location. history List all migrate items. init Init config file and generate root migrate location. init-db Generate schema and generate app migrate location. inspectdb Introspects the database tables to standard output as... migrate Generate migrate changes file. upgrade Upgrade to specified version. ``` -------------------------------- ### Programmatic Aerich Command Execution Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Demonstrates how to use the Aerich `Command` class to execute Aerich operations programmatically within a Python application. This includes initializing Aerich and running migrations. ```python from aerich import Command command = Command(tortoise_config=config, app='models') await command.init() await command.migrate('test') ``` -------------------------------- ### Aerich Init DB Command Usage Source: https://github.com/tortoise/aerich/blob/dev/README.md Displays the help message for the 'aerich init-db' command, used to generate the database schema and app migration location. ```APIDOC Usage: aerich init-db [OPTIONS] Generate schema and generate app migrate location. Options: --app TEXT Tortoise-ORM app name. [default: models] -h, --help Show this message and exit. ``` -------------------------------- ### Inspect All Tables with Aerich Source: https://github.com/tortoise/aerich/blob/dev/README.md Demonstrates how to inspect all database tables and print the generated Tortoise ORM models to the console using the `aerich inspectdb` command with a specified application name. ```shell aerich --app models inspectdb ``` -------------------------------- ### Show Current Heads Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to display the current available migration heads, indicating which versions are ready to be applied. ```APIDOC Usage: aerich heads [OPTIONS] Show current available heads in migrate location. Options: -h, --help Show this message and exit. Example: aerich heads ``` -------------------------------- ### Show Migration History Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to list all applied and pending migration files, showing the history of schema changes. ```APIDOC Usage: aerich history [OPTIONS] List all migrate items. Options: -h, --help Show this message and exit. Example: aerich history ``` -------------------------------- ### Inspect Specific Table and Redirect Output Source: https://github.com/tortoise/aerich/blob/dev/README.md Shows how to inspect a particular table (e.g., 'user') using `aerich inspectdb -t user` and redirect the output to a Python file (e.g., `models.py`) for model definition. ```shell aerich inspectdb -t user > models.py ``` -------------------------------- ### Initialize Database Schema Source: https://github.com/tortoise/aerich/blob/dev/README.md Command to generate the database schema and the migration location for a specific Tortoise ORM app. The default app name is 'models'. ```shell > aerich init-db Success create app migrate location ./migrations/models Success generate schema for app "models" ``` ```shell > aerich --app other_models init-db Success create app migrate location ./migrations/other_models Success generate schema for app "other_models" ``` -------------------------------- ### Show Migration History Source: https://github.com/tortoise/aerich/blob/dev/README.md Lists all applied migration files in chronological order. ```shell > aerich history 1_202029051520102929_drop_column.py ``` -------------------------------- ### Upgrade Database Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to apply pending migrations and upgrade the database schema to the latest version. ```APIDOC Usage: aerich upgrade [OPTIONS] Upgrade to specified version. Options: -h, --help Show this message and exit. Example: aerich upgrade ``` -------------------------------- ### Aerich inspectdb CLI Usage Source: https://github.com/tortoise/aerich/blob/dev/README.md Provides the command-line interface usage for the `aerich inspectdb` command. This command introspects database tables and generates Tortoise ORM model definitions. It supports options for specifying tables and directing output. ```shell Usage: aerich inspectdb [OPTIONS] Introspects the database tables to standard output as TortoiseORM model. Options: -t, --table TEXT Which tables to inspect. -h, --help Show this message and exit. ``` -------------------------------- ### Aerich Inspect DB Command Usage Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Provides the command-line interface for Aerich's `inspectdb` command. It allows introspection of database tables to generate Tortoise ORM models. Supports specifying tables and viewing help. ```shell Usage: aerich inspectdb [OPTIONS] Introspects the database tables to standard output as TortoiseORM model. Options: -t, --table TEXT Which tables to inspect. -h, --help Show this message and exit. ``` ```shell aerich --app models inspectdb ``` ```shell aerich inspectdb -t user > models.py ``` -------------------------------- ### Show Pending Migrations (Heads) Source: https://github.com/tortoise/aerich/blob/dev/README.md Displays the migration files that are available to be applied to the database. ```shell > aerich heads 1_202029051520102929_drop_column.py ``` -------------------------------- ### Downgrade Database Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to revert the database schema to a previous version. You can specify a version number or downgrade to the last applied version. ```APIDOC Usage: aerich downgrade [OPTIONS] Downgrade to specified version. Options: -v, --version INTEGER Specified version, default to last. [default: -1] -d, --delete Delete version files at the same time. [default: False] --yes Confirm the action without prompting. -h, --help Show this message and exit. Example: aerich downgrade aerich downgrade -v 1 ``` -------------------------------- ### Aerich Upgrade/Downgrade with --fake Option Source: https://github.com/tortoise/aerich/blob/dev/README.md Illustrates the use of the `--fake` flag with `aerich upgrade` and `aerich downgrade` commands. This option applies migrations to the database schema without executing the SQL, useful for marking migrations as applied or correcting migration states. ```shell aerich upgrade --fake aerich --app models upgrade --fake aerich downgrade --fake -v 2 aerich --app models downgrade --fake -v 2 ``` -------------------------------- ### Generate Migration File Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md Command to generate a migration file based on detected schema changes. You can provide a name for the migration. Aerich may prompt for column renaming decisions. ```APIDOC Usage: aerich migrate [OPTIONS] Generate migrate changes file. Options: -n, --name TEXT Migration name. -h, --help Show this message and exit. Example: aerich migrate --name drop_column Note: If Aerich detects a column rename, it will prompt: 'Rename {old_column} to {new_column} [True]'. Choosing 'True' renames the column without deletion, 'False' deletes and recreates, potentially losing data. ``` -------------------------------- ### Aerich Downgrade Command Usage Source: https://github.com/tortoise/aerich/blob/dev/README.md Shows the help message for the 'aerich downgrade' command, which allows rolling back the database to a specific migration version. ```APIDOC Usage: aerich downgrade [OPTIONS] Downgrade to specified version. Options: -v, --version INTEGER Specified version, default to last. [default: -1] -d, --delete Delete version files at the same time. [default: False] --yes Confirm the action without prompting. -h, --help Show this message and exit. ``` -------------------------------- ### Generate Migration File Source: https://github.com/tortoise/aerich/blob/dev/README.md Creates a new migration file based on database schema changes. The migration can be named, or an empty file can be generated. ```shell > aerich migrate --name drop_column Success migrate 1_202029051520102929_drop_column.py ``` ```shell > aerich migrate --name add_index --empty Success migrate 1_202326122220101229_add_index.py ``` -------------------------------- ### Upgrade Database to Latest Version Source: https://github.com/tortoise/aerich/blob/dev/README.md Applies all pending migrations to bring the database schema up to the latest version. ```shell > aerich upgrade Success upgrade 1_202029051520102929_drop_column.py ``` -------------------------------- ### Generated Tortoise ORM Model from SQL Source: https://github.com/tortoise/aerich/blob/dev/README_RU.md The Tortoise ORM Python model generated by Aerich's `inspectdb` command for the 'test' table. It maps SQL data types to Tortoise ORM fields, including primary keys, nullability, and default values. ```python from tortoise import Model, fields class Test(Model): date = fields.DateField(null=True, ) datetime = fields.DatetimeField(auto_now=True, ) decimal = fields.DecimalField(max_digits=10, decimal_places=2, ) float = fields.FloatField(null=True, ) id = fields.IntField(pk=True, ) string = fields.CharField(max_length=200, null=True, ) time = fields.TimeField(null=True, ) tinyint = fields.BooleanField(null=True, ) ``` -------------------------------- ### Downgrade Database to Specified Version Source: https://github.com/tortoise/aerich/blob/dev/README.md Reverts the database schema to a specified migration version. By default, it downgrades to the last applied version. ```shell > aerich downgrade Success downgrade 1_202029051520102929_drop_column.py ``` -------------------------------- ### Generated Tortoise ORM Model from Schema Source: https://github.com/tortoise/aerich/blob/dev/README.md The Python code representing a Tortoise ORM model generated by `aerich inspectdb` from a SQL schema. It maps SQL data types to corresponding Tortoise ORM fields like `DateField`, `DatetimeField`, `DecimalField`, `FloatField`, `IntField`, `CharField`, `TimeField`, and `BooleanField`. ```python from tortoise import Model, fields class Test(Model): date = fields.DateField(null=True) datetime = fields.DatetimeField(auto_now=True) decimal = fields.DecimalField(max_digits=10, decimal_places=2) float = fields.FloatField(null=True) id = fields.IntField(primary_key=True) string = fields.CharField(max_length=200, null=True) time = fields.TimeField(null=True) tinyint = fields.BooleanField(null=True) ``` -------------------------------- ### Ignoring Tables in Aerich Migrations Source: https://github.com/tortoise/aerich/blob/dev/README.md Shows how to prevent Aerich from managing specific database tables by setting `managed = False` within the `Meta` class of a Tortoise ORM model. This setting is specifically for `aerich migrate` and does not affect `aerich init-db`. ```python class MyModel(Model): class Meta: managed = False ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.