fix: alembic version_num 长度修复 + 前端部署配置
- env.py: 自动创建 alembic_version 表时使用 VARCHAR(255) 避免截断 - docker-compose: 添加 frontend nginx 服务 + alembic 挂载 - nginx.conf: SPA 路由 + API 代理
This commit is contained in:
+19
-1
@@ -3,7 +3,7 @@ from logging.config import fileConfig
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
from sqlalchemy import engine_from_config, pool, text
|
||||
from alembic import context
|
||||
|
||||
# 把项目根加入 pythonpath,让 alembic 能找到 src 包
|
||||
@@ -29,6 +29,23 @@ if config.config_file_name is not None:
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def _ensure_version_table(connection):
|
||||
"""确保 alembic_version 表存在且 version_num 列足够长。
|
||||
|
||||
Alembic 默认 version_num 是 String(32),但我们的迁移名较长(如
|
||||
0005_prediction_status_and_stats_provenance 有 41 个字符),会导致
|
||||
StringDataRightTruncation 错误。
|
||||
"""
|
||||
result = connection.execute(text(
|
||||
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'alembic_version')"
|
||||
))
|
||||
if not result.scalar():
|
||||
connection.execute(text(
|
||||
"CREATE TABLE alembic_version (version_num VARCHAR(255) NOT NULL PRIMARY KEY)"
|
||||
))
|
||||
connection.commit()
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode."""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
@@ -52,6 +69,7 @@ def run_migrations_online() -> None:
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
_ensure_version_table(connection)
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
|
||||
+13
-1
@@ -6,7 +6,7 @@ services:
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD 未设置}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-football}
|
||||
ports:
|
||||
- "${POSTGRES_PORT:-5432}:5432"
|
||||
- "${POSTGRES_PORT:-5433}:5432"
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
@@ -26,6 +26,18 @@ services:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./src:/app/src
|
||||
- ./alembic:/app/alembic
|
||||
- ./alembic.ini:/app/alembic.ini
|
||||
|
||||
frontend:
|
||||
image: nginx:alpine
|
||||
ports:
|
||||
- "${FRONTEND_PORT:-3000}:80"
|
||||
volumes:
|
||||
- ./frontend/dist:/usr/share/nginx/html
|
||||
- ./frontend/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
||||
depends_on:
|
||||
- api
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
|
||||
Reference in New Issue
Block a user