tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

Guide: Integrate to zalando postgres operator and FastAPI in Kubernetes cluster #254

Open caniko opened 1 year ago

caniko commented 1 year ago

I have a zalando postgres-operator based database cluster on my k8 cluster. I have a separate FastAPI + tortoise ORM app. Tortoise uses the postgres db generated by the operator.

How do I integrate aerich to this workflow?

I was thinking of just having a startup script that runs and applies migrations at the during pod/container startup:

aerich init -t <some path>
aerich init-db
aerich migrate
aerich upgrade

<run WSGI or ASGI server command>

Any improvements or better ideas alltogether?

long2ice commented 1 year ago

What about this https://github.com/tortoise/aerich#use-aerich-in-application?

caniko commented 1 year ago

What about this https://github.com/tortoise/aerich#use-aerich-in-application?

Should I keep this as test? Or just blank -> update?

await command.migrate("test")
isaquealves commented 1 year ago

What about this https://github.com/tortoise/aerich#use-aerich-in-application?

Should I keep this as test? Or just blank -> update?

await command.migrate("test")

@caniko

Migrate.migrate just reads models to generate a new migration file.

You could use await command.upgrade() instead, in order to apply existing migrations. This way your database will be in the expected state after a new migration is submitted.

caniko commented 1 year ago

Can you guys activate discussions? This issue should probably become a thread. Closing it makes no sense.

long2ice commented 1 year ago

You can migrate use aerich migrate command local, and use Command to do auto upgrade when application startup, just call the init and upgrade method