perf: minor fixes

This commit is contained in:
Junyan Qin
2025-09-13 17:59:10 +08:00
parent e2071d9486
commit 6a7e88ffd6
7 changed files with 38 additions and 26 deletions

View File

@@ -1,8 +1,6 @@
from __future__ import annotations
import pip
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
import sys
from .. import database
@@ -12,19 +10,12 @@ class PostgreSQLDatabaseManager(database.BaseDatabaseManager):
"""PostgreSQL database manager"""
async def initialize(self) -> None:
postgresql_config = self.ap.instance_config.data.get('database', {}).get('postgresql', {})
# default to PostgreSQL with asyncpg driver
try:
__import__("asyncpg")
except ImportError:
print('以下依赖包未安装,将自动安装,请完成后重启程序:')
print(
'The dependence package asyncpg is missing, it will be installed automatically, please restart the program after completion:'
)
pip.main(['install', "asyncpg"])
print('已自动安装缺失的依赖包 asyncpg ,请重启程序。')
print('The missing dependence asyncpg have been installed automatically, please restart the program.')
sys.exit(0)
engine_url = self.ap.instance_config.data['system'].get('database', {}).get('engine_url', 'postgresql+asyncpg://root:***@127.0.0.1:5432/postgres')
host = postgresql_config.get('host', '127.0.0.1')
port = postgresql_config.get('port', 5432)
user = postgresql_config.get('user', 'postgres')
password = postgresql_config.get('password', 'postgres')
database = postgresql_config.get('database', 'postgres')
engine_url = f'postgresql+asyncpg://{user}:{password}@{host}:{port}/{database}'
self.engine = sqlalchemy_asyncio.create_async_engine(engine_url)