Files
life-echo/api/alembic/versions/0001_initial_schema.py
Kevin 8af37e5e8e 修复:CI 部署环境与 ref 错配、迁移碎片化、图片意图 source_span、章节物化脏版式、会话历史与本地语音不一致
新增:TTS 上传 COS 与分片、章节 reading_segments 物化与快照、markdown 清洗、会话消息 repository、语音 store 重构与相关测试
2026-03-20 16:43:02 +08:00

81 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""initial schema (squashed)
单一迁移pgvector + 当前全部 ORM 表(含 conversations.deleted_at 软删除);并补充 models 未声明的
story_image_intents.asset_id → assets 外键,以及每个 story 仅一条 primary intent 的唯一索引。
chapters 含 story 物化字段markdown_compose_dirty、markdown_composed_at、reading_segments_json
(阅读片段快照,随 ORM 一并 create_all
已并入原 0002stories-first无 chapter_sections / memoir_images.section_id与原 0003segments.tts_audio_urls
的语义:新库仅由当前 ORM 建表即可,无需后续 ALTER。
segments.audio_duration_seconds语音条时长秒数历史 API / Redis 回填)由 ORM 一并 create_all无独立迁移。
story_image_intents 无 source_span主图回填在正文末尾意图仅存 caption / prompt_brief 等)。
新库 / 删库重来:`alembic upgrade head`。
Revision ID: 0001_initial
Revises:
"""
from typing import Sequence, Union
from alembic import op
from sqlalchemy import text
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 _import_all_models() -> None:
from app.features.asset import models as _asset_models # noqa: F401
from app.features.auth import models as _auth_models # noqa: F401
from app.features.conversation import models as _conv_models # noqa: F401
from app.features.memory import models as _memory_models # noqa: F401
from app.features.memoir import models as _memoir_models # noqa: F401
from app.features.payment import models as _payment_models # noqa: F401
from app.features.story import models as _story_models # noqa: F401
from app.features.user import models as _user_models # noqa: F401
def upgrade() -> None:
conn = op.get_bind()
conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
from app.core.db import Base
_import_all_models()
Base.metadata.create_all(bind=conn)
op.create_foreign_key(
"fk_story_image_intents_asset_id_assets",
"story_image_intents",
"assets",
["asset_id"],
["id"],
ondelete="SET NULL",
)
op.execute(
"""
CREATE UNIQUE INDEX IF NOT EXISTS uq_story_primary_image_intent
ON story_image_intents (story_id)
WHERE intent_role = 'primary'
"""
)
def downgrade() -> None:
conn = op.get_bind()
from app.core.db import Base
op.execute("DROP INDEX IF EXISTS uq_story_primary_image_intent")
op.drop_constraint(
"fk_story_image_intents_asset_id_assets",
"story_image_intents",
type_="foreignkey",
)
_import_all_models()
Base.metadata.drop_all(bind=conn)
conn.execute(text("DROP EXTENSION IF EXISTS vector"))