Skip to content

Commit 3f52ac3

Browse files
committed
Support rename table. (#139)
1 parent f8aa7a8 commit 3f52ac3

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fix rename field on the field add. (#134)
88
- Fix postgres field type change error. (#135)
99
- Fix inspectdb for `FloatField`. (#138)
10+
- Support `rename table`. (#139)
1011

1112
### 0.5.1
1213

aerich/ddl/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class BaseDDL:
2626
_CHANGE_COLUMN_TEMPLATE = (
2727
'ALTER TABLE "{table_name}" CHANGE {old_column_name} {new_column_name} {new_column_type}'
2828
)
29+
_RENAME_TABLE_TEMPLATE = 'ALTER TABLE "{old_table_name}" RENAME TO "{new_table_name}"'
2930

3031
def __init__(self, client: "BaseDBAsyncClient"):
3132
self.client = client
@@ -230,3 +231,9 @@ def alter_column_null(self, model: "Type[Model]", field_describe: dict):
230231

231232
def set_comment(self, model: "Type[Model]", field_describe: dict):
232233
raise NotImplementedError
234+
235+
def rename_table(self, model: "Type[Model]", old_table_name: str, new_table_name: str):
236+
db_table = model._meta.db_table
237+
return self._RENAME_TABLE_TEMPLATE.format(
238+
table_name=db_table, old_table_name=old_table_name, new_table_name=new_table_name
239+
)

aerich/ddl/mysql/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class MysqlDDL(BaseDDL):
2828
_DROP_FK_TEMPLATE = "ALTER TABLE `{table_name}` DROP FOREIGN KEY `{fk_name}`"
2929
_M2M_TABLE_TEMPLATE = "CREATE TABLE `{table_name}` (`{backward_key}` {backward_type} NOT NULL REFERENCES `{backward_table}` (`{backward_field}`) ON DELETE CASCADE,`{forward_key}` {forward_type} NOT NULL REFERENCES `{forward_table}` (`{forward_field}`) ON DELETE CASCADE){extra}{comment}"
3030
_MODIFY_COLUMN_TEMPLATE = "ALTER TABLE `{table_name}` MODIFY COLUMN {column}"
31+
_RENAME_TABLE_TEMPLATE = "ALTER TABLE `{old_table_name}` RENAME TO `{new_table_name}`"
3132

3233
def alter_column_null(self, model: "Type[Model]", field_describe: dict):
3334
raise NotSupportError("Alter column null is unsupported in MySQL.")

aerich/migrate.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
176176
pass
177177
else:
178178
old_model_describe = old_models.get(new_model_str)
179-
179+
# rename table
180+
new_table = new_model_describe.get("table")
181+
old_table = old_model_describe.get("table")
182+
if new_table != old_table:
183+
cls._add_operator(cls.rename_table(model, old_table, new_table), upgrade)
180184
old_unique_together = set(
181185
map(lambda x: tuple(x), old_model_describe.get("unique_together"))
182186
)
@@ -406,6 +410,10 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
406410
if old_model not in new_models.keys():
407411
cls._add_operator(cls.drop_model(old_models.get(old_model).get("table")), upgrade)
408412

413+
@classmethod
414+
def rename_table(cls, model: Type[Model], old_table_name: str, new_table_name: str):
415+
return cls.ddl.rename_table(model, old_table_name, new_table_name)
416+
409417
@classmethod
410418
def add_model(cls, model: Type[Model]):
411419
return cls.ddl.create_table(model)

tests/old_models.py

+3
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ class Config(Model):
6161
key = fields.CharField(max_length=20)
6262
value = fields.JSONField()
6363
status: Status = fields.IntEnumField(Status, default=Status.on)
64+
65+
class Meta:
66+
table = "configs"

tests/test_migrate.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"models.Config": {
147147
"name": "models.Config",
148148
"app": "models",
149-
"table": "config",
149+
"table": "configs",
150150
"abstract": False,
151151
"description": None,
152152
"docstring": None,
@@ -790,6 +790,7 @@ def test_migrate(mocker: MockerFixture):
790790
"ALTER TABLE `config` ALTER COLUMN `status` DROP DEFAULT",
791791
"ALTER TABLE `email` ADD `address` VARCHAR(200) NOT NULL",
792792
"ALTER TABLE `email` DROP COLUMN `user_id`",
793+
"ALTER TABLE `configs` RENAME TO `config`",
793794
"ALTER TABLE `product` RENAME COLUMN `image` TO `pic`",
794795
"ALTER TABLE `email` RENAME COLUMN `id` TO `email_id`",
795796
"ALTER TABLE `email` DROP FOREIGN KEY `fk_email_user_5b58673d`",
@@ -813,6 +814,7 @@ def test_migrate(mocker: MockerFixture):
813814
"ALTER TABLE `config` ALTER COLUMN `status` SET DEFAULT 1",
814815
"ALTER TABLE `email` ADD `user_id` INT NOT NULL",
815816
"ALTER TABLE `email` DROP COLUMN `address`",
817+
"ALTER TABLE `config` RENAME TO `configs`",
816818
"ALTER TABLE `product` RENAME COLUMN `pic` TO `image`",
817819
"ALTER TABLE `email` RENAME COLUMN `email_id` TO `id`",
818820
"ALTER TABLE `email` ADD CONSTRAINT `fk_email_user_5b58673d` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE",
@@ -839,6 +841,7 @@ def test_migrate(mocker: MockerFixture):
839841
'ALTER TABLE "email" DROP COLUMN "user_id"',
840842
'ALTER TABLE "product" RENAME COLUMN "image" TO "pic"',
841843
'ALTER TABLE "email" RENAME COLUMN "id" TO "email_id"',
844+
'ALTER TABLE "configs" RENAME TO "config"',
842845
'ALTER TABLE "email" DROP CONSTRAINT "fk_email_user_5b58673d"',
843846
'CREATE INDEX "idx_email_email_4a1a33" ON "email" ("email")',
844847
'CREATE UNIQUE INDEX "uid_product_name_f14935" ON "product" ("name", "type")',
@@ -860,6 +863,7 @@ def test_migrate(mocker: MockerFixture):
860863
'ALTER TABLE "config" ALTER COLUMN "status" SET DEFAULT 1',
861864
'ALTER TABLE "email" ADD "user_id" INT NOT NULL',
862865
'ALTER TABLE "email" DROP COLUMN "address"',
866+
'ALTER TABLE "config" RENAME TO "configs"',
863867
'ALTER TABLE "product" RENAME COLUMN "pic" TO "image"',
864868
'ALTER TABLE "email" RENAME COLUMN "email_id" TO "id"',
865869
'ALTER TABLE "email" ADD CONSTRAINT "fk_email_user_5b58673d" FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE',

0 commit comments

Comments
 (0)