9cf1c9ab11
Implements the 14 core ORM models for the yunke data-mirror system using SQLAlchemy 2.0 Mapped/mapped_column style, driven by the field lists in docs/superpowers/specs/2026-08-01-yunke-data-dictionary.md: Department, Staff, WechatAccount, WhatsappAccount, Customer, ChatGroup, GroupMember, Message, Media, PhoneCall, Sms, SyncState, YkAccount, SyncRun. Conventions: - Natural source PKs where they exist (friend_id, wx_id, msg_id, room_id, media_hash, call_id, sms_id, dept_id, user_id); surrogate Integer PKs otherwise (SyncState, YkAccount, SyncRun). GroupMember uses a composite (channel, room_id, member_id) PK. - Common audit columns version/raw_json/fetched_at/updated_at on every source-mirrored table. - channel only where meaningful: Customer/Message/Media/ChatGroup/ GroupMember/Sms use messaging channels; PhoneCall.channel is call-type; SyncState.channel scopes the cursor. Omitted on Staff/Department/ WechatAccount/WhatsappAccount/YkAccount/SyncRun. - Message composite index on (channel, owner_account_id, talker_id, timestamp) for the hot per-talker time-order query path. - message_table_name(channel, timestamp) helper documents the future message_<channel>_<YYYYMM> sharding convention; the Message model still maps to a single physical table for now (sharding is a later task). All types are portable generic SQLAlchemy types so tests run on in-memory SQLite while prod targets MySQL. Tests verify Customer round-trip, raw_json JSON round-trip, Message natural PK, GroupMember composite PK, the Message composite index, and full table creation on SQLite. Alembic wired via migrations/env.py to Base.metadata + get_settings().db_url with an async->sync driver swap (mysql+asyncmy -> mysql+pymysql, sqlite+aiosqlite -> sqlite). Initial migration autogenerated against an empty SQLite DB; server defaults normalized to sa.func.now() so the file is MySQL-portable. upgrade head is intentionally not run (no MySQL yet). Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
2.9 KiB
Python
92 lines
2.9 KiB
Python
"""Alembic migration environment.
|
|
|
|
Wired to:
|
|
* ``app.db.base.Base.metadata`` (the single source of truth for our schema), and
|
|
* ``app.config.get_settings().db_url`` (the same URL the application uses).
|
|
|
|
Production runs against MySQL via ``mysql+asyncmy://``. Alembic drives a
|
|
**synchronous** engine, so :func:`_sync_url` swaps the async driver for its
|
|
sync counterpart (``mysql+asyncmy`` -> ``mysql+pymysql`` and
|
|
``sqlite+aiosqlite`` -> ``sqlite``). For local autogenerate without a running
|
|
MySQL server, point ``DB_URL`` at an empty SQLite file, e.g.::
|
|
|
|
DB_URL="sqlite+aiosqlite:///./_autogen.db" alembic revision --autogenerate -m "..."
|
|
"""
|
|
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
from app.config import get_settings
|
|
from app.db import models # noqa: F401 (registers all tables on Base.metadata)
|
|
from app.db.base import Base
|
|
|
|
# this is the Alembic Config object, which provides
|
|
# access to the values within the .ini file in use.
|
|
config = context.config
|
|
|
|
# Interpret the config file for Python logging.
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# Single source of truth for schema; Alembic compares against this.
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def _sync_url(url: str) -> str:
|
|
"""Convert an async DB URL to its synchronous equivalent.
|
|
|
|
Alembic's default :func:`engine_from_config` builds a sync engine, so we
|
|
swap async drivers out. Pass-through if the URL is already sync.
|
|
"""
|
|
if url.startswith("mysql+asyncmy://"):
|
|
return url.replace("mysql+asyncmy://", "mysql+pymysql://", 1)
|
|
if url.startswith("sqlite+aiosqlite://"):
|
|
return url.replace("sqlite+aiosqlite://", "sqlite://", 1)
|
|
return url
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""Run migrations in 'offline' mode (emit SQL without a live connection)."""
|
|
url = _sync_url(get_settings().db_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 run_migrations_online() -> None:
|
|
"""Run migrations in 'online' mode (open a connection, run, close)."""
|
|
config.set_main_option("sqlalchemy.url", _sync_url(get_settings().db_url))
|
|
|
|
connectable = engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
compare_type=True,
|
|
compare_server_default=True,
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|