-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: migration with duplicate renaming of columns in some cases #395
Open
waketzheng
wants to merge
13
commits into
tortoise:dev
Choose a base branch
from
waketzheng:fix-380-rename-multi-fields
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
henadzit
reviewed
Dec 20, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add explanation to the PR description about why your changes fix the issue? Having a good description of changes really helps to review the code.
henadzit
reviewed
Dec 21, 2024
waketzheng
force-pushed
the
fix-380-rename-multi-fields
branch
from
December 23, 2024 09:24
6f8b524
to
4324f9e
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #380
TODO:
Description
1. Why change
_rename_new/_rename_old
to_rename_fields
aerich generate migration sql mainly by this two line:
When generating the upgrade SQL, if reply true for the click.prompt, the field_name will be added to class var
_rename_new
(list[str]).Then while generating downgrade SQL, Line398 will check a field_name whether in the class var to decide if it's a rename or drop/add.
This can cause a bug that: if the different models have the same new field name, and one by rename with another by drop/add, will generate two rename sql lines in downgrade, for example:
$ aerich migrate
$ cat migrations/models/1*
You can see there are two rename lines in the downgrade!
So I use a
_rename_fields: Dict[str, Dict[str, str]] = {} # {'model': {'old_field': 'new_field'}}
instead, to avoid this problem.2. How to avoid asking 4 times for 2 fields rename
Skip fields that is confirmed to rename in previous for loop.Verify
A complex case:
python files
Shell command
rm -rf db.sqlite* migrations/ aerich init -t settings.TORTOISE_ORM aerich init-db cp new_models.py models.py
aerich migrate
and type 6 times enter to rename all fields:Got migration:
cat migrations/models/1*
aerich migrate
and type some f to only rename A.a/B.a/B.bGot migration:
cat migrations/models/1*