From 3c8883315443dd6bb0135d5d39e915d5adde7067 Mon Sep 17 00:00:00 2001 From: long2ice Date: Mon, 27 Jun 2022 11:29:47 +0800 Subject: [PATCH 1/2] fix: decimal field change (#246) --- aerich/__init__.py | 6 +++--- aerich/{inspect => inspectdb}/__init__.py | 0 aerich/{inspect => inspectdb}/mysql.py | 2 +- aerich/{inspect => inspectdb}/postgres.py | 2 +- aerich/{inspect => inspectdb}/sqlite.py | 2 +- aerich/migrate.py | 10 ++++++++-- tests/models.py | 1 + tests/old_models.py | 1 + tests/test_migrate.py | 19 +++++++++++++++++++ 9 files changed, 35 insertions(+), 8 deletions(-) rename aerich/{inspect => inspectdb}/__init__.py (100%) rename aerich/{inspect => inspectdb}/mysql.py (98%) rename aerich/{inspect => inspectdb}/postgres.py (98%) rename aerich/{inspect => inspectdb}/sqlite.py (98%) diff --git a/aerich/__init__.py b/aerich/__init__.py index 7303e58..2eec79c 100644 --- a/aerich/__init__.py +++ b/aerich/__init__.py @@ -8,9 +8,9 @@ from tortoise.transactions import in_transaction from tortoise.utils import get_schema_sql from aerich.exceptions import DowngradeError -from aerich.inspect.mysql import InspectMySQL -from aerich.inspect.postgres import InspectPostgres -from aerich.inspect.sqlite import InspectSQLite +from aerich.inspectdb.mysql import InspectMySQL +from aerich.inspectdb.postgres import InspectPostgres +from aerich.inspectdb.sqlite import InspectSQLite from aerich.migrate import Migrate from aerich.models import Aerich from aerich.utils import ( diff --git a/aerich/inspect/__init__.py b/aerich/inspectdb/__init__.py similarity index 100% rename from aerich/inspect/__init__.py rename to aerich/inspectdb/__init__.py diff --git a/aerich/inspect/mysql.py b/aerich/inspectdb/mysql.py similarity index 98% rename from aerich/inspect/mysql.py rename to aerich/inspectdb/mysql.py index 7d62bff..64dc2ba 100644 --- a/aerich/inspect/mysql.py +++ b/aerich/inspectdb/mysql.py @@ -1,6 +1,6 @@ from typing import List -from aerich.inspect import Column, Inspect +from aerich.inspectdb import Column, Inspect class InspectMySQL(Inspect): diff --git a/aerich/inspect/postgres.py b/aerich/inspectdb/postgres.py similarity index 98% rename from aerich/inspect/postgres.py rename to aerich/inspectdb/postgres.py index 8327618..0f22bb1 100644 --- a/aerich/inspect/postgres.py +++ b/aerich/inspectdb/postgres.py @@ -2,7 +2,7 @@ from typing import List, Optional from tortoise import BaseDBAsyncClient -from aerich.inspect import Column, Inspect +from aerich.inspectdb import Column, Inspect class InspectPostgres(Inspect): diff --git a/aerich/inspect/sqlite.py b/aerich/inspectdb/sqlite.py similarity index 98% rename from aerich/inspect/sqlite.py rename to aerich/inspectdb/sqlite.py index 885b9c0..7f35e1f 100644 --- a/aerich/inspect/sqlite.py +++ b/aerich/inspectdb/sqlite.py @@ -1,6 +1,6 @@ from typing import List -from aerich.inspect import Column, Inspect +from aerich.inspectdb import Column, Inspect class InspectSQLite(Inspect): diff --git a/aerich/migrate.py b/aerich/migrate.py index e114858..82d30ee 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -422,8 +422,14 @@ class Migrate: cls._drop_index(model, (field_name,), unique), upgrade, True ) elif option == "db_field_types.": - # continue since repeated with others - continue + if new_data_field.get("field_type") == "DecimalField": + # modify column + cls._add_operator( + cls._modify_field(model, new_data_field), + upgrade, + ) + else: + continue elif option == "default": if not ( is_default_function(old_new[0]) or is_default_function(old_new[1]) diff --git a/tests/models.py b/tests/models.py index 07c141f..597ee59 100644 --- a/tests/models.py +++ b/tests/models.py @@ -29,6 +29,7 @@ class User(Model): is_active = fields.BooleanField(default=True, description="Is Active") is_superuser = fields.BooleanField(default=False, description="Is SuperUser") intro = fields.TextField(default="") + longitude = fields.DecimalField(max_digits=10, decimal_places=8) class Email(Model): diff --git a/tests/old_models.py b/tests/old_models.py index 70c2f23..b8ffc5d 100644 --- a/tests/old_models.py +++ b/tests/old_models.py @@ -29,6 +29,7 @@ class User(Model): is_superuser = fields.BooleanField(default=False, description="Is SuperUser") avatar = fields.CharField(max_length=200, default="") intro = fields.TextField(default="") + longitude = fields.DecimalField(max_digits=12, decimal_places=9) class Email(Model): diff --git a/tests/test_migrate.py b/tests/test_migrate.py index b615441..b659cfa 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -644,6 +644,21 @@ old_models_describe = { "constraints": {}, "db_field_types": {"": "TEXT", "mysql": "LONGTEXT"}, }, + { + "name": "longitude", + "unique": False, + "default": None, + "indexed": False, + "nullable": False, + "db_column": "longitude", + "docstring": None, + "generated": False, + "field_type": "DecimalField", + "constraints": {}, + "description": None, + "python_type": "decimal.Decimal", + "db_field_types": {"": "DECIMAL(16,14)", "sqlite": "VARCHAR(40)"}, + }, ], "fk_fields": [], "backward_fk_fields": [ @@ -813,6 +828,7 @@ def test_migrate(mocker: MockerFixture): "ALTER TABLE `user` MODIFY COLUMN `last_login` DATETIME(6) NOT NULL COMMENT 'Last Login'", "ALTER TABLE `user` MODIFY COLUMN `is_active` BOOL NOT NULL COMMENT 'Is Active' DEFAULT 1", "ALTER TABLE `user` MODIFY COLUMN `is_superuser` BOOL NOT NULL COMMENT 'Is SuperUser' DEFAULT 0", + "ALTER TABLE `user` MODIFY COLUMN `longitude` DECIMAL(10,8) NOT NULL", "ALTER TABLE `user` ADD UNIQUE INDEX `uid_user_usernam_9987ab` (`username`)", "CREATE TABLE IF NOT EXISTS `newmodel` (\n `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,\n `name` VARCHAR(50) NOT NULL\n) CHARACTER SET utf8mb4;", "ALTER TABLE `category` MODIFY COLUMN `created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)", @@ -849,6 +865,7 @@ def test_migrate(mocker: MockerFixture): "ALTER TABLE `user` MODIFY COLUMN `last_login` DATETIME(6) NOT NULL COMMENT 'Last Login'", "ALTER TABLE `user` MODIFY COLUMN `is_active` BOOL NOT NULL COMMENT 'Is Active' DEFAULT 1", "ALTER TABLE `user` MODIFY COLUMN `is_superuser` BOOL NOT NULL COMMENT 'Is SuperUser' DEFAULT 0", + "ALTER TABLE `user` MODIFY COLUMN `longitude` DECIMAL(12,9) NOT NULL", "ALTER TABLE `product` MODIFY COLUMN `body` LONGTEXT NOT NULL", "ALTER TABLE `email` MODIFY COLUMN `is_primary` BOOL NOT NULL DEFAULT 0", ] @@ -885,6 +902,7 @@ def test_migrate(mocker: MockerFixture): 'ALTER TABLE "user" ALTER COLUMN "last_login" TYPE TIMESTAMPTZ USING "last_login"::TIMESTAMPTZ', 'ALTER TABLE "user" ALTER COLUMN "intro" TYPE TEXT USING "intro"::TEXT', 'ALTER TABLE "user" ALTER COLUMN "is_active" TYPE BOOL USING "is_active"::BOOL', + 'ALTER TABLE "user" ALTER COLUMN "longitude" TYPE DECIMAL(10,8) USING "longitude"::DECIMAL(10,8)', 'CREATE INDEX "idx_product_name_869427" ON "product" ("name", "type_db_alias")', 'CREATE INDEX "idx_email_email_4a1a33" ON "email" ("email")', 'CREATE TABLE "email_user" ("email_id" INT NOT NULL REFERENCES "email" ("email_id") ON DELETE CASCADE,"user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE)', @@ -915,6 +933,7 @@ def test_migrate(mocker: MockerFixture): 'ALTER TABLE "user" ALTER COLUMN "is_superuser" TYPE BOOL USING "is_superuser"::BOOL', 'ALTER TABLE "user" ALTER COLUMN "is_active" TYPE BOOL USING "is_active"::BOOL', 'ALTER TABLE "user" ALTER COLUMN "intro" TYPE TEXT USING "intro"::TEXT', + 'ALTER TABLE "user" ALTER COLUMN "longitude" TYPE DECIMAL(12,9) USING "longitude"::DECIMAL(12,9)', 'ALTER TABLE "product" ALTER COLUMN "created_at" TYPE TIMESTAMPTZ USING "created_at"::TIMESTAMPTZ', 'ALTER TABLE "product" ALTER COLUMN "is_reviewed" TYPE BOOL USING "is_reviewed"::BOOL', 'ALTER TABLE "product" ALTER COLUMN "body" TYPE TEXT USING "body"::TEXT', From d6627906c7b4e18877736286de91677b395fec09 Mon Sep 17 00:00:00 2001 From: long2ice Date: Mon, 27 Jun 2022 11:36:09 +0800 Subject: [PATCH 2/2] test: fix test_migrate --- tests/test_migrate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_migrate.py b/tests/test_migrate.py index b659cfa..41d9dde 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -657,7 +657,7 @@ old_models_describe = { "constraints": {}, "description": None, "python_type": "decimal.Decimal", - "db_field_types": {"": "DECIMAL(16,14)", "sqlite": "VARCHAR(40)"}, + "db_field_types": {"": "DECIMAL(12,9)", "sqlite": "VARCHAR(40)"}, }, ], "fk_fields": [],