tortoise / aerich

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

Wrong models state in db on new db upgrade (No changes detected) #133

Open Olegt0rr opened 3 years ago

Olegt0rr commented 3 years ago

To the table aerich in content field you're saving not current migration model state, but the latest app state.

How to reproduce:

  1. Create app and some migrations
  2. Create a new clear database
  3. Change something in your app's models
  4. Don't save migrations and run aerich upgrade

After these steps you can find already changed models from step 4. Try to save migration - aerich migrate --name some_changes No changes detected :)

To make easy reproducing I've made an example branch: https://github.com/Olegt0rr/aerich-example/tree/4-complex-models

Just set POSTGRES_URL env variable and run

  1. aerich upgrade (check aerich table!)
  2. aerich migration --name new_models (read about no changes detected)

If you'll try to save migration first, you'll get:

Traceback (most recent call last):
  File "/aerich-example/venv/bin/aerich", line 8, in <module>
    sys.exit(main())
  File "/aerich-example/venv/lib/python3.8/site-packages/aerich/cli.py", line 309, in main
    cli()
  File "/aerich-example/venv/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/aerich-example/venv/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/aerich-example/venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/aerich-example/venv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/aerich-example/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/aerich-example/venv/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/aerich-example/venv/lib/python3.8/site-packages/aerich/cli.py", line 38, in wrapper
    loop.run_until_complete(f(*args, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/aerich-example/venv/lib/python3.8/site-packages/aerich/cli.py", line 93, in migrate
    ret = await Migrate.migrate(name)
  File "/aerich-example/venv/lib/python3.8/site-packages/aerich/migrate.py", line 125, in migrate
    cls.diff_models(cls._last_version_content, new_version_content)
  File "/aerich-example/venv/lib/python3.8/site-packages/aerich/migrate.py", line 165, in diff_models
    old_models.pop(_aerich, None)
AttributeError: 'NoneType' object has no attribute 'pop'

Nothing can be done from this state: neither upgrade nor migrate. Checkmate.

Master-Y0da commented 3 years ago

@Olegt0rr Same problem here...you could find a solution?

Olegt0rr commented 3 years ago

@Olegt0rr Same problem here...you could find a solution?

I've found one way:

Still waiting for @long2ice to fix this behaviour

Master-Y0da commented 3 years ago

@Olegt0rr Your suggest work for me, thank you!! ...anyway this is very annoying. When I try aerich init-db command, it says that db it's already intied, so I had to delete the init.sql file and migration folder in order to migrate my new changes...

radiophysicist commented 3 years ago

Also encountered the same problem with aerich 0.5.7

agarjoshua commented 3 years ago

also encountering a similar problem, is there a solution apart from the one @Olegt0rr has suggested?

long2ice commented 3 years ago

https://github.com/tortoise/aerich#restore-aerich-workflow help?

agarjoshua commented 3 years ago

ah, @long2ice your solution does the trick,thanks

mathewvaughan commented 2 years ago

@long2ice I'd like to raise a PR for this - Going to try and get set up to contribute. Are you still active on this repo?

diegolatorre116 commented 1 year ago

At the moment the solution is to delete the migrations folder and redo migrations. Will this bug be fixed in the future?