Skip to content

Commit

Permalink
Support indexes change. (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Aug 31, 2021
1 parent 4a83021 commit 699b032
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 141 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.5

### 0.5.8

- Support `indexes` change. (#193)

### 0.5.7

- Fix no module found error. (#188) (#189)
Expand Down
26 changes: 16 additions & 10 deletions aerich/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,21 @@ async def migrate(cls, name) -> str:
return await cls._generate_diff_sql(name)

@classmethod
def _add_operator(cls, operator: str, upgrade=True, fk_m2m=False):
def _add_operator(cls, operator: str, upgrade=True, fk_m2m_index=False):
"""
add operator,differentiate fk because fk is order limit
:param operator:
:param upgrade:
:param fk_m2m:
:param fk_m2m_index:
:return:
"""
if upgrade:
if fk_m2m:
if fk_m2m_index:
cls._upgrade_fk_m2m_index_operators.append(operator)
else:
cls.upgrade_operators.append(operator)
else:
if fk_m2m:
if fk_m2m_index:
cls._downgrade_fk_m2m_index_operators.append(operator)
else:
cls.downgrade_operators.append(operator)
Expand Down Expand Up @@ -192,7 +192,8 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
new_unique_together = set(
map(lambda x: tuple(x), new_model_describe.get("unique_together"))
)

old_indexes = set(map(lambda x: tuple(x), old_model_describe.get("indexes")))
new_indexes = set(map(lambda x: tuple(x), new_model_describe.get("indexes")))
old_pk_field = old_model_describe.get("pk_field")
new_pk_field = new_model_describe.get("pk_field")
# pk field
Expand Down Expand Up @@ -224,7 +225,7 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
new_models.get(change[0][1].get("model_name")),
),
upgrade,
fk_m2m=True,
fk_m2m_index=True,
)
elif action == "remove":
add = False
Expand All @@ -235,14 +236,19 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
cls._downgrade_m2m.append(table)
add = True
if add:
cls._add_operator(cls.drop_m2m(table), upgrade, fk_m2m=True)
cls._add_operator(cls.drop_m2m(table), upgrade, True)
# add unique_together
for index in new_unique_together.difference(old_unique_together):
cls._add_operator(cls._add_index(model, index, True), upgrade, True)
# remove unique_together
for index in old_unique_together.difference(new_unique_together):
cls._add_operator(cls._drop_index(model, index, True), upgrade, True)

# add indexes
for index in new_indexes.difference(old_indexes):
cls._add_operator(cls._add_index(model, index, False), upgrade, True)
# remove indexes
for index in old_indexes.difference(new_indexes):
cls._add_operator(cls._drop_index(model, index, False), upgrade, True)
old_data_fields = old_model_describe.get("data_fields")
new_data_fields = new_model_describe.get("data_fields")

Expand Down Expand Up @@ -356,7 +362,7 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
model, fk_field, new_models.get(fk_field.get("python_type"))
),
upgrade,
fk_m2m=True,
fk_m2m_index=True,
)
# drop fk
for old_fk_field_name in set(old_fk_fields_name).difference(
Expand All @@ -371,7 +377,7 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
model, old_fk_field, old_models.get(old_fk_field.get("python_type"))
),
upgrade,
fk_m2m=True,
fk_m2m_index=True,
)
# change fields
for field_name in set(new_data_fields_name).intersection(set(old_data_fields_name)):
Expand Down
2 changes: 1 addition & 1 deletion aerich/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.7"
__version__ = "0.5.8"
Loading

0 comments on commit 699b032

Please sign in to comment.