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

Running upgrade command does not apply migrations #264

Closed gonczor closed 1 year ago

gonczor commented 1 year ago

Applicable for version 0.6.4

I've tried to run aerich upgrade as shown in the docs, but the migrations, although marked as executed in the database, were not actually applied. After a bit of digging I found out that this is due to the change of these lines of code:

image

In commit https://github.com/tortoise/aerich/commit/e9b76bdd35cb34e25a2271709fc08c6c06a28032

Code which I used for testing: https://github.com/gonczor/fastapi-aerich

So this should either:

  1. be changed so that the generated migration script contains the query execution
  2. be reverted. In either case it would be useful to have some sort of a test checking whether the migration is applied (IDK, maybe mocking the execute_query() call and looking for its calls).

I would provide a PR, but I don't know which approach do you prefer.

EDIT As a workaround you can add this to your code to run migrations:

async def upgrade(db: BaseDBAsyncClient) -> List[str]:
    queries = [
        """ALTER TABLE "items" ADD "read_counter" INT NOT NULL  DEFAULT 0"""
    ]
    for query in queries:
        await db.execute_query(query)
    return queries

This could also be added to the template as another solution.

louison commented 1 year ago

Also applicable to version 0.7.0 apparently

long2ice commented 1 year ago

Fixed