Package: aerich
AerichEnglish |РусскийIntroductionAerich is a database migrations tool for TortoiseORM, which is like alembic for SQLAlchemy, or like Django ORM with
it's own migration solution.InstallJust install from pypi:pipinstallaerichQuick Start>aerich-h

Usage:aerich[OPTIONS]COMMAND[ARGS]...

Options:-V,--versionShowtheversionandexit.-c,--configTEXTConfigfile.[default:pyproject.toml]--appTEXTTortoise-ORMappname.-h,--helpShowthismessageandexit.

Commands:downgradeDowngradetospecifiedversion.headsShowcurrentavailableheadsinmigratelocation.historyListallmigrateitems.initInitconfigfileandgeneraterootmigratelocation.init-dbGenerateschemaandgenerateappmigratelocation.inspectdbIntrospectsthedatabasetablestostandardoutputas...migrateGeneratemigratechangesfile.upgradeUpgradetospecifiedversion.UsageYou need addaerich.modelsto yourTortoise-ORMconfig first. Example:TORTOISE_ORM={"connections":{"default":"mysql://root:123456@127.0.0.1:3306/test"},"apps":{"models":{"models":["tests.models","aerich.models"],"default_connection":"default",},},}Initialization>aerichinit-h

Usage:aerichinit[OPTIONS]Initconfigfileandgeneraterootmigratelocation.

Options:-t,--tortoise-ormTEXTTortoise-ORMconfigmoduledictvariable,likesettings.TORTOISE_ORM.[required]--locationTEXTMigratestorelocation.[default:./migrations]-s,--src_folderTEXTFolderofthesource,relativetotheprojectroot.-h,--helpShowthismessageandexit.Initialize the config file and migrations location:>aerichinit-ttests.backends.mysql.TORTOISE_ORM

Successcreatemigratelocation./migrations
Successwriteconfigtopyproject.tomlInit db>aerichinit-db

Successcreateappmigratelocation./migrations/models
Successgenerateschemaforapp"models"If your Tortoise-ORM app is not the defaultmodels, you must specify the correct app via--app,
e.g.aerich --app other_models init-db.Update models and make migrate>aerichmigrate--namedrop_column

Successmigrate1_202029051520102929_drop_column.pyFormat of migrate filename is{version_num}_{datetime}_{name|update}.py.Ifaerichguesses you are renaming a column, it will askRename {old_column} to {new_column} [True]. You can chooseTrueto rename column without column drop, or chooseFalseto drop the column then create. Note that the latter may
lose data.Upgrade to latest version>aerichupgrade

Successupgrade1_202029051520102929_drop_column.pyNow your db is migrated to latest.Downgrade to specified version>aerichdowngrade-h

Usage:aerichdowngrade[OPTIONS]Downgradetospecifiedversion.

Options:-v,--versionINTEGERSpecifiedversion,defaulttolast.[default:-1]-d,--deleteDeleteversionfilesatthesametime.[default:False]--yesConfirmtheactionwithoutprompting.-h,--helpShowthismessageandexit.>aerichdowngrade

Successdowngrade1_202029051520102929_drop_column.pyNow your db is rolled back to the specified version.Show history>aerichhistory1_202029051520102929_drop_column.pyShow heads to be migrated>aerichheads

1_202029051520102929_drop_column.pyInspect db tables to TortoiseORM modelCurrentlyinspectdbsupport MySQL & Postgres & SQLite.Usage:aerichinspectdb[OPTIONS]IntrospectsthedatabasetablestostandardoutputasTortoiseORMmodel.

Options:-t,--tableTEXTWhichtablestoinspect.-h,--helpShowthismessageandexit.Inspect all tables and print to console:aerich--appmodelsinspectdbInspect a specified table in the default app and redirect tomodels.py:aerichinspectdb-tuser>models.pyFor example, you table is:CREATETABLE`test`(`id`intNOTNULLAUTO_INCREMENT,`decimal`decimal(10,2)NOTNULL,`date`dateDEFAULTNULL,`datetime`datetimeNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,`time`timeDEFAULTNULL,`float`floatDEFAULTNULL,`string`varchar(200)COLLATEutf8mb4_general_ciDEFAULTNULL,`tinyint`tinyintDEFAULTNULL,PRIMARYKEY(`id`),KEY`asyncmy_string_index`(`string`))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4COLLATE=utf8mb4_general_ciNow runaerich inspectdb -t testto see the generated model:fromtortoiseimportModel,fieldsclassTest(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,)Note that this command is limited and can't infer some fields, such asIntEnumField,ForeignKeyField, and others.Multiple databasestortoise_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",},},}You only need to specifyaerich.modelsin one app, and must specify--appwhen runningaerich migrateand so on.RestoreaerichworkflowIn some cases, such as broken changes from upgrade ofaerich, you can't runaerich migrateoraerich upgrade, you
can make the following steps:dropaerichtable.deletemigrations/{app}directory.rerunaerich init-db.Note that these actions is safe, also you can do that to reset your migrations if your migration files is too many.Useaerichin applicationYou can useaerichout of cli by useCommandclass.fromaerichimportCommandcommand=Command(tortoise_config=config,app='models')awaitcommand.init()awaitcommand.migrate('test')LicenseThis project is licensed under theApache-2.0License.