From ceb1e0ffefdf4b4c010f191d9ba059dac5f1a2e3 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 17:07:09 +0800 Subject: [PATCH 1/7] Activate type hint check in ci --- Makefile | 2 ++ aerich/ddl/__init__.py | 2 +- aerich/inspectdb/postgres.py | 2 +- tests/test_migrate.py | 12 +++++++----- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index e9b807c..9e97a17 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,8 @@ style: deps _style _check: @black --check $(black_opts) $(checkfiles) || (echo "Please run 'make style' to auto-fix style issues" && false) @ruff check $(checkfiles) + @mypy $(checkfiles) + @bandit -r aerich check: deps _check test: deps diff --git a/aerich/ddl/__init__.py b/aerich/ddl/__init__.py index 4e062c7..cb57274 100644 --- a/aerich/ddl/__init__.py +++ b/aerich/ddl/__init__.py @@ -135,7 +135,7 @@ class BaseDDL: self.schema_generator._column_comment_generator( table=db_table, column=db_column, - comment=field_describe.get("description"), + comment=description, ) if description else "" diff --git a/aerich/inspectdb/postgres.py b/aerich/inspectdb/postgres.py index d4a7761..f77bc29 100644 --- a/aerich/inspectdb/postgres.py +++ b/aerich/inspectdb/postgres.py @@ -56,7 +56,7 @@ from information_schema.constraint_column_usage const right join information_schema.columns c using (column_name, table_catalog, table_schema, table_name) where c.table_catalog = $1 and c.table_name = $2 - and c.table_schema = $3""" + and c.table_schema = $3""" # nosec:B608 ret = await self.conn.execute_query_dict(sql, [self.database, table, self.schema]) for row in ret: columns.append( diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 4614606..e7115d9 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -1,4 +1,5 @@ from pathlib import Path +from typing import List, cast import pytest import tortoise @@ -792,13 +793,13 @@ old_models_describe = { def should_add_user_id_column_type_alter_sql() -> bool: + if tortoise.__version__ < "0.21": + return False # tortoise-orm>=0.21 changes IntField constraints # from {"ge": 1, "le": 2147483647} to {"ge": -2147483648,"le": 2147483647} - user_id_constraints = old_models_describe["models.Category"]["data_fields"][-1]["constraints"] - return ( - tortoise.__version__ >= "0.21" - and tortoise.fields.data.IntField.constraints != user_id_constraints - ) + data_fields = cast(List[dict], old_models_describe["models.Category"]["data_fields"]) + user_id_constraints = data_fields[-1]["constraints"] + return tortoise.fields.data.IntField.constraints != user_id_constraints def test_migrate(mocker: MockerFixture): @@ -825,6 +826,7 @@ def test_migrate(mocker: MockerFixture): if isinstance(Migrate.ddl, SqliteDDL): with pytest.raises(NotSupportError): Migrate.diff_models(old_models_describe, models_describe) + with pytest.raises(NotSupportError): Migrate.diff_models(models_describe, old_models_describe, False) else: Migrate.diff_models(old_models_describe, models_describe) From c42fdab74ddd88d44be29267cfebc0cf66ba7422 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 17:11:27 +0800 Subject: [PATCH 2/7] Add cache action to ci --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e8d441d..7346003 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,12 @@ jobs: steps: - name: Start MySQL run: sudo systemctl start mysql.service + - uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }} + restore-keys: | + ${{ runner.os }}-pip- - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: From bceeb236c2375d2aec9dbb527cd4503da716e624 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 17:21:46 +0800 Subject: [PATCH 3/7] Checking cache action --- aerich/utils.py | 4 ++-- tests/test_utils.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aerich/utils.py b/aerich/utils.py index 14664a0..6e4fb88 100644 --- a/aerich/utils.py +++ b/aerich/utils.py @@ -4,7 +4,7 @@ import re import sys from pathlib import Path from types import ModuleType -from typing import Dict, Optional +from typing import Dict, Optional, Union from click import BadOptionUsage, ClickException, Context from tortoise import BaseDBAsyncClient, Tortoise @@ -95,7 +95,7 @@ def is_default_function(string: str) -> Optional[re.Match]: return re.match(r"^$", str(string or "")) -def import_py_file(file: Path) -> ModuleType: +def import_py_file(file: Union[str, Path]) -> ModuleType: module_name, file_ext = os.path.splitext(os.path.split(file)[-1]) spec = importlib.util.spec_from_file_location(module_name, file) module = importlib.util.module_from_spec(spec) # type:ignore[arg-type] diff --git a/tests/test_utils.py b/tests/test_utils.py index 654bc0d..9d640d2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,6 @@ from aerich.utils import import_py_file -def test_import_py_file(): +def test_import_py_file() -> None: m = import_py_file("aerich/utils.py") assert getattr(m, "import_py_file") From 24a2087b78af5106e7de69ab47ff09f572316ddf Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 17:35:00 +0800 Subject: [PATCH 4/7] Skip make deps if cache hint in ci --- .github/workflows/ci.yml | 5 ++++- Makefile | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7346003..8060f22 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,6 +35,9 @@ jobs: run: | pip install -U pip poetry poetry config virtualenvs.create false + - name: Install Dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: make deps - name: CI env: MYSQL_PASS: root @@ -43,4 +46,4 @@ jobs: POSTGRES_PASS: 123456 POSTGRES_HOST: 127.0.0.1 POSTGRES_PORT: 5432 - run: make ci + run: make _ci diff --git a/Makefile b/Makefile index 9e97a17..0f05d70 100644 --- a/Makefile +++ b/Makefile @@ -44,4 +44,5 @@ testall: deps _test_all build: deps @poetry build -ci: check _testall +_ci: _check _testall +ci: deps _ci From 480087df07dc6b5da9017eb905d77a081a804656 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 17:38:51 +0800 Subject: [PATCH 5/7] Add id for cache step --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8060f22..8853573 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,7 @@ jobs: - name: Start MySQL run: sudo systemctl start mysql.service - uses: actions/cache@v4 + id: cache with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }} From fc68f99c8669bd5ca1ef3bfbf031ca9ba8c4d590 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 18:02:58 +0800 Subject: [PATCH 6/7] fixing cache action --- .github/workflows/ci.yml | 6 +----- Makefile | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8853573..7346003 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,6 @@ jobs: - name: Start MySQL run: sudo systemctl start mysql.service - uses: actions/cache@v4 - id: cache with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }} @@ -36,9 +35,6 @@ jobs: run: | pip install -U pip poetry poetry config virtualenvs.create false - - name: Install Dependencies - if: steps.cache.outputs.cache-hit != 'true' - run: make deps - name: CI env: MYSQL_PASS: root @@ -47,4 +43,4 @@ jobs: POSTGRES_PASS: 123456 POSTGRES_HOST: 127.0.0.1 POSTGRES_PORT: 5432 - run: make _ci + run: make ci diff --git a/Makefile b/Makefile index 0f05d70..ff10279 100644 --- a/Makefile +++ b/Makefile @@ -44,5 +44,4 @@ testall: deps _test_all build: deps @poetry build -_ci: _check _testall -ci: deps _ci +ci: deps _check _testall From 9e46fbf55d16c97fafc0f6371ff14f14989bf34d Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 6 Jun 2024 18:20:57 +0800 Subject: [PATCH 7/7] rollback ci command --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ff10279..9e97a17 100644 --- a/Makefile +++ b/Makefile @@ -44,4 +44,4 @@ testall: deps _test_all build: deps @poetry build -ci: deps _check _testall +ci: check _testall