From 9752ce8e9295d7ea6c804d5da8187ce36f9c9341 Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Thu, 18 Jul 2024 20:22:34 +0800 Subject: [PATCH] Fix alembic migration failure caused by model (#359) --- backend/alembic/README | 1 - backend/alembic/README.md | 3 +++ backend/alembic/env.py | 22 +++++++++++--------- backend/app/admin/model/__init__.py | 1 - backend/app/generator/model/__init__.py | 1 - backend/app/generator/service/gen_service.py | 7 ++----- backend/utils/gen_template.py | 1 - 7 files changed, 17 insertions(+), 19 deletions(-) delete mode 100644 backend/alembic/README create mode 100644 backend/alembic/README.md diff --git a/backend/alembic/README b/backend/alembic/README deleted file mode 100644 index a23d4fb5..00000000 --- a/backend/alembic/README +++ /dev/null @@ -1 +0,0 @@ -Generic single-database configuration with an async dbapi. diff --git a/backend/alembic/README.md b/backend/alembic/README.md new file mode 100644 index 00000000..6530d55d --- /dev/null +++ b/backend/alembic/README.md @@ -0,0 +1,3 @@ +Generic single-database configuration with an async dbapi. + +If you add a new app to the framework, be sure to read the contents of the `env.py` file. diff --git a/backend/alembic/env.py b/backend/alembic/env.py index cb05ffd4..09f63145 100644 --- a/backend/alembic/env.py +++ b/backend/alembic/env.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# ruff: noqa: E402 +# ruff: noqa: E402, F403, I001, RUF100 import asyncio import os import sys @@ -28,14 +28,13 @@ fileConfig(config.config_file_name) # add your model's MetaData object here # for 'autogenerate' support -# https://alembic.sqlalchemy.org/en/latest/autogenerate.html#autogenerating-multiple-metadata-collections -from backend.app.admin.model import MappedBase as AdminBase -from backend.app.generator.model import MappedBase as GeneratorBase +from backend.common.model import MappedBase -target_metadata = [ - AdminBase.metadata, - GeneratorBase.metadata, -] +# if add new app, do like this +from backend.app.admin.model import * # noqa: F401 +from backend.app.generator.model import * # noqa: F401 + +target_metadata = MappedBase.metadata # other values from the config, defined by the needs of env.py, from backend.database.db_mysql import SQLALCHEMY_DATABASE_URL @@ -58,7 +57,7 @@ def run_migrations_offline(): url = config.get_main_option('sqlalchemy.url') context.configure( url=url, - target_metadata=target_metadata, # type: ignore + target_metadata=target_metadata, literal_binds=True, dialect_opts={'paramstyle': 'named'}, ) @@ -68,7 +67,10 @@ def run_migrations_offline(): def do_run_migrations(connection): - context.configure(connection=connection, target_metadata=target_metadata) # type: ignore + context.configure( + connection=connection, + target_metadata=target_metadata, + ) with context.begin_transaction(): context.run_migrations() diff --git a/backend/app/admin/model/__init__.py b/backend/app/admin/model/__init__.py index 36028d7e..fa2b0ccb 100644 --- a/backend/app/admin/model/__init__.py +++ b/backend/app/admin/model/__init__.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from backend.common.model import MappedBase # noqa: I001 from backend.app.admin.model.sys_api import Api from backend.app.admin.model.sys_casbin_rule import CasbinRule from backend.app.admin.model.sys_dept import Dept diff --git a/backend/app/generator/model/__init__.py b/backend/app/generator/model/__init__.py index 6ce035a3..5a74fa1c 100644 --- a/backend/app/generator/model/__init__.py +++ b/backend/app/generator/model/__init__.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from backend.common.model import MappedBase # noqa: I001 from backend.app.generator.model.gen_business import GenBusiness from backend.app.generator.model.gen_model import GenModel diff --git a/backend/app/generator/service/gen_service.py b/backend/app/generator/service/gen_service.py index 2193c271..94c08b55 100644 --- a/backend/app/generator/service/gen_service.py +++ b/backend/app/generator/service/gen_service.py @@ -117,7 +117,6 @@ class GenService: *gen_template.get_code_gen_path(tpl_path, business).split('/')[1:], ) code_folder = Path(str(code_filepath)).parent - code_folder_name = code_folder.name if not code_folder.exists(): code_folder.mkdir(parents=True, exist_ok=True) # 写入 init 文件 @@ -125,13 +124,11 @@ class GenService: if not init_filepath.exists(): async with aiofiles.open(init_filepath, 'w', encoding='utf-8') as f: await f.write(gen_template.init_content) - if code_folder_name == 'model': - await f.write(gen_template.model_content) # 写入代码文件呢 async with aiofiles.open(code_filepath, 'w', encoding='utf-8') as f: await f.write(code) # model init 文件补充 - if code_folder_name == 'model': + if code_folder.name == 'model': async with aiofiles.open(init_filepath, 'a', encoding='utf-8') as f: await f.write( f'from backend.app.{business.app_name}.model.{business.table_name_en} ' @@ -157,7 +154,7 @@ class GenService: else: zf.writestr( init_filepath, - f'{gen_template.init_content}{gen_template.model_content}' + f'{gen_template.init_content}' f'from backend.app.{business.app_name}.model.{business.table_name_en} ' f'import {to_pascal(business.table_name_en)}\n', ) diff --git a/backend/utils/gen_template.py b/backend/utils/gen_template.py index 1966f169..9efd5c46 100644 --- a/backend/utils/gen_template.py +++ b/backend/utils/gen_template.py @@ -18,7 +18,6 @@ class GenTemplate: enable_async=True, ) self.init_content = '#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n' - self.model_content = 'from backend.common.model import MappedBase # noqa: I001\n' def get_template(self, jinja_file: str) -> Template: """