From 40c7ef7fd604be165b3221102fca6c8b166621d2 Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Thu, 21 Oct 2021 15:43:18 +0100 Subject: [PATCH 1/2] Merge repeated `if` statements into single `if` --- aerich/__init__.py | 5 +---- aerich/ddl/__init__.py | 2 +- aerich/utils.py | 3 +-- tests/test_ddl.py | 10 ++++------ 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/aerich/__init__.py b/aerich/__init__.py index 7641062..f9f4e26 100644 --- a/aerich/__init__.py +++ b/aerich/__init__.py @@ -100,11 +100,8 @@ class Command: return ret async def history(self): - ret = [] versions = Migrate.get_all_version_files() - for version in versions: - ret.append(version) - return ret + return [version for version in versions] async def inspectdb(self, tables: List[str]): connection = get_app_connection(self.tortoise_config, self.app) diff --git a/aerich/ddl/__init__.py b/aerich/ddl/__init__.py index 2818955..ed94ffc 100644 --- a/aerich/ddl/__init__.py +++ b/aerich/ddl/__init__.py @@ -184,7 +184,7 @@ class BaseDDL: "idx" if not unique else "uid", model, field_names ), table_name=model._meta.db_table, - column_names=", ".join([self.schema_generator.quote(f) for f in field_names]), + column_names=", ".join(self.schema_generator.quote(f) for f in field_names), ) def drop_index(self, model: "Type[Model]", field_names: List[str], unique=False): diff --git a/aerich/utils.py b/aerich/utils.py index c8bffdc..7a930dd 100644 --- a/aerich/utils.py +++ b/aerich/utils.py @@ -96,11 +96,10 @@ def get_version_content_from_file(version_file: Union[str, Path]) -> Dict: second = len(content) - 1 upgrade_content = content[first + len(_UPGRADE) : second].strip() # noqa:E203 downgrade_content = content[second + len(_DOWNGRADE) :].strip() # noqa:E203 - ret = { + return { "upgrade": list(filter(lambda x: x or False, upgrade_content.split(";\n"))), "downgrade": list(filter(lambda x: x or False, downgrade_content.split(";\n"))), } - return ret def write_version_file(version_file: Path, content: Dict): diff --git a/tests/test_ddl.py b/tests/test_ddl.py index 9227161..c710b33 100644 --- a/tests/test_ddl.py +++ b/tests/test_ddl.py @@ -72,18 +72,16 @@ def test_modify_column(): ret1 = Migrate.ddl.modify_column(User, User._meta.fields_map.get("is_active").describe(False)) if isinstance(Migrate.ddl, MysqlDDL): assert ret0 == "ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200)" + assert ( + ret1 + == "ALTER TABLE `user` MODIFY COLUMN `is_active` BOOL NOT NULL COMMENT 'Is Active' DEFAULT 1" + ) elif isinstance(Migrate.ddl, PostgresDDL): assert ( ret0 == 'ALTER TABLE "category" ALTER COLUMN "name" TYPE VARCHAR(200) USING "name"::VARCHAR(200)' ) - if isinstance(Migrate.ddl, MysqlDDL): - assert ( - ret1 - == "ALTER TABLE `user` MODIFY COLUMN `is_active` BOOL NOT NULL COMMENT 'Is Active' DEFAULT 1" - ) - elif isinstance(Migrate.ddl, PostgresDDL): assert ( ret1 == 'ALTER TABLE "user" ALTER COLUMN "is_active" TYPE BOOL USING "is_active"::BOOL' ) From 1a0371e9772d30cc272aa4be059362c9f3d3837d Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Thu, 15 Sep 2022 23:19:18 +0100 Subject: [PATCH 2/2] Update aerich/utils.py Co-authored-by: KDH --- aerich/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aerich/utils.py b/aerich/utils.py index 7a930dd..eb86108 100644 --- a/aerich/utils.py +++ b/aerich/utils.py @@ -97,8 +97,8 @@ def get_version_content_from_file(version_file: Union[str, Path]) -> Dict: upgrade_content = content[first + len(_UPGRADE) : second].strip() # noqa:E203 downgrade_content = content[second + len(_DOWNGRADE) :].strip() # noqa:E203 return { - "upgrade": list(filter(lambda x: x or False, upgrade_content.split(";\n"))), - "downgrade": list(filter(lambda x: x or False, downgrade_content.split(";\n"))), + "upgrade": [line for line in upgrade_content.split(";\n") if line], + "downgrade": [line for line in downgrade_content.split(";\n") if line], }