tortoise / aerich

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

Aerich puts all migrations in one folder, I expect per-app folders structure #171

Open syabro opened 3 years ago

syabro commented 3 years ago

I have config

TORTOISE_ORM = {
    "connections": {"default": DATABASE_URI},
    "apps": {
        "thirdparty": {'models': ['aerich.models']},
        "zendr": {
            "models": [
                "subscriptions.models",
                "users.models",
                "userbots.models",
            ],
            "default_connection": "default",
        },
    },
}

When I run init-db I got

(zendr) ▲ zendr aerich init-db
Success create app migrate location .migrations/thirdparty
Success generate schema for app "thirdparty"

(zendr) ▲ zendr ls .migrations/
thirdparty

Zendr app models were put into thirdparty directory. I expect it to have thirdparty and zendr folders.

What do I do wrong?

long2ice commented 3 years ago

If you use more than one app, use --app option, or just use one

syabro commented 3 years ago

@long2ice emm, if I have 10 apps, I have to run command 10 times? It doesn't make any sense. Why not automatically split migrations by folders apps?

long2ice commented 3 years ago

Usually one app one database

syabro commented 3 years ago

Why call it apps then if you're emulating Django interface when app has very different meaning?

abe-winter commented 3 years ago

yeah this bit me as well -- init-db seems to create an initial migration for all apps, but then migrate is only migrating the first app, here:

https://github.com/tortoise/aerich/blob/af632218750c1b6ee8571f70bd5de3d1eec3d2d2/aerich/cli.py#L76

I guess you're saying this is because init-db keys on connection and migrate keys on app name? The init-db piece seems to be happening here:

https://github.com/tortoise/aerich/blob/af632218750c1b6ee8571f70bd5de3d1eec3d2d2/aerich/__init__.py#L125

I guess I can work around this in my codebase by having two named DB connections? but this is bad in prod because it doubles the pool size. also when I tried it, I got a 'cyclic fk errors' error. it's also confusing that one command keys by connection, the other by app name.

possible solution: is it worth creating an --all-apps flag for the migrate command? I'm happy to submit a PR if that's useful

abe-winter commented 3 years ago

I did a POC here with an --all-apps flag https://github.com/abe-winter/aerich

It lets the init-db and migrate commands operate on multiple tortoise apps at once by supporting app=None in a bunch of places

my use case for apps is to let a fastapi project import plugins which have sql tables that get included in migrations -- I may be misusing / misunderstanding what tortoise apps are for, sorry in advance if that's the case

another design approach: make --app param support multiple entries, turn the app state var into a list?

pmdevita commented 2 years ago

It would be nice to have per app functionality like Django, I have an application that needs to load apps modularly and having all of the migrations get merged into one file means I can't properly separate them.

the-akpan commented 1 year ago

Has there been any update on this, because I am having the same issue.

the-akpan commented 1 year ago

I made a slight hack to work around this, wrote a script to loop through the apps keys of the config dict then ran the aerich command on each individually.

errors = []
for app in apps:
    if os.system(f"aerich --app {app} {command}"):
        errors.append(app)
if errors:
    print("Errors encountered on %" % ",".join(errors))
long2ice commented 1 year ago

If not --app specific, maybe we should migrate all app default

the-akpan commented 1 year ago

If not --app specific, maybe we should migrate all app default

That would be the expected behavior.

long2ice commented 1 year ago

PR is welcome!