feat(system):优化后端
1.新增后端测试 2.增加了后端的加密 3.增加了i18n(国际化)
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"""Alembic 迁移环境。
|
||||
|
||||
设计要点:
|
||||
1. 数据库 URL 从 ``POSTGRES_*`` 环境变量动态拼装,不污染 ``alembic.ini``;
|
||||
2. 复用项目本身的 ORM metadata:``BaseDataModel.metadata`` 让 autogenerate
|
||||
能识别全部表;
|
||||
3. 与运行期保持一致使用 ``asyncpg`` 异步驱动 —— 通过 ``async_engine_from_config``
|
||||
建立 AsyncEngine,再借 ``run_sync`` 把 alembic 的 sync migration 执行入口
|
||||
挂载进去。这样无需额外引入 psycopg 等同步驱动。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
from pathlib import Path
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
if str(PROJECT_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from kilostar.core.postgres_database.model.base import BaseDataModel # noqa: E402
|
||||
from kilostar.core.postgres_database import model as _model_pkg # noqa: F401,E402
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
|
||||
def _build_db_url() -> str:
|
||||
"""从 ``POSTGRES_*`` 环境变量拼装一个 asyncpg URL。"""
|
||||
user = os.environ.get("POSTGRES_USER", "postgres")
|
||||
password = os.environ.get("POSTGRES_PASSWORD", "postgrespassword")
|
||||
host = os.environ.get("POSTGRES_HOST", "127.0.0.1")
|
||||
port = os.environ.get("POSTGRES_PORT", "5432")
|
||||
db = os.environ.get("POSTGRES_DB", "kilostar")
|
||||
return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{db}"
|
||||
|
||||
|
||||
config.set_main_option("sqlalchemy.url", _build_db_url())
|
||||
|
||||
target_metadata = BaseDataModel.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""离线模式:不连库,直接把 SQL 渲染到 stdout。"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
compare_type=True,
|
||||
compare_server_default=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def _do_run_migrations(connection: Connection) -> None:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
compare_server_default=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def _run_async_migrations() -> None:
|
||||
connectable = async_engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(_do_run_migrations)
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
asyncio.run(_run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,29 @@
|
||||
"""initial baseline
|
||||
|
||||
Revision ID: 0001_initial
|
||||
Revises:
|
||||
Create Date: 2026-05-31 00:00:00
|
||||
|
||||
这是 Alembic 接入时的占位 baseline。
|
||||
|
||||
- 全新部署:``alembic upgrade head`` 会跑过这条 no-op,
|
||||
然后由后续 ``alembic revision --autogenerate`` 生成真正的建表脚本,
|
||||
或在首次部署时由应用 ``Base.metadata.create_all`` 直接建表,再 ``alembic stamp head``。
|
||||
- 已有数据库:直接 ``alembic stamp 0001_initial`` 标定基线,再做后续 autogenerate。
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
|
||||
revision: str = "0001_initial"
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
pass
|
||||
@@ -0,0 +1,20 @@
|
||||
# Alembic versions
|
||||
|
||||
迁移脚本会被自动生成到这个目录。
|
||||
|
||||
常用命令(在项目根目录运行):
|
||||
|
||||
- 生成 baseline(首次接入,已有数据库):
|
||||
`alembic stamp head`
|
||||
|
||||
- 自动检测 ORM 与 DB 差异并生成迁移:
|
||||
`alembic revision --autogenerate -m "your message"`
|
||||
|
||||
- 应用所有未执行的迁移:
|
||||
`alembic upgrade head`
|
||||
|
||||
- 回滚一个版本:
|
||||
`alembic downgrade -1`
|
||||
|
||||
- 查看历史:
|
||||
`alembic history --verbose`
|
||||
Reference in New Issue
Block a user