refactor(channel): Auto scan models defined for alembic migrate

This commit is contained in:
SunnyQjm 2023-05-16 12:52:18 +08:00
parent dd65189c08
commit 6c6b0c0c3b
4 changed files with 19 additions and 13 deletions

View File

@ -59,7 +59,7 @@ sysom_server:
sysom_service:
path:
root_path: !concat [*global_root_path, "/server/microservice"]
root_path: !concat [*global_root_path, "/server"]
framework:
gclient:
protocol: cmg

View File

@ -52,7 +52,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = mariadb+pymysql://sysom:sysom_admin@127.0.0.1/sysom
sqlalchemy.url = ""
[post_write_hooks]

View File

@ -1,22 +1,29 @@
import inspect
import app.models as models
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from app.models import Base
from alembic import context
from pathlib import Path
from sysom_utils import ConfigParser
from conf.settings import YAML_CONFIG
##################################################################
# Load yaml config first
##################################################################
BASE_DIR = Path(__file__).resolve().parent
YAML_GLOBAL_CONFIG_PATH = f"{BASE_DIR.parent.parent}/conf/config.yml"
YAML_SERVICE_CONFIG_PATH = f"{BASE_DIR.parent}/config.yml"
YAML_CONFIG = ConfigParser(YAML_GLOBAL_CONFIG_PATH, YAML_SERVICE_CONFIG_PATH)
mysql_config = YAML_CONFIG.get_server_config().db.mysql
##################################################################
# Scan models
##################################################################
service_tables = []
for name, data in inspect.getmembers(models, inspect.isclass):
if data.__module__ != "app.models":
continue
if "__tablename__" in data.__dict__:
service_tables.append(data.__dict__["__tablename__"])
elif "__table__" in data.__dict__:
service_tables.append(data.__dict__["__table__"])
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
@ -46,9 +53,8 @@ target_metadata = Base.metadata
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def include_object(object, name, type_, reflected, compare_to):
if type_ == "table" and name not in ["sys_channel_setting", "sys_channel_params"]:
if type_ == "table" and name not in service_tables:
return False
return True

View File

@ -16,7 +16,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
##################################################################
# Load yaml config first
##################################################################
YAML_GLOBAL_CONFIG_PATH = f"/etc/sysom/config.yml"
YAML_GLOBAL_CONFIG_PATH = f"{BASE_DIR.parent.parent}/conf/config.yml"
YAML_SERVICE_CONFIG_PATH = f"{BASE_DIR}/config.yml"
YAML_CONFIG = ConfigParser(YAML_GLOBAL_CONFIG_PATH, YAML_SERVICE_CONFIG_PATH)