feat(market-data): 迁移 Tushare 同步到 PostgreSQL

This commit is contained in:
yuxuanhui
2026-08-05 17:57:32 +08:00
parent e69bcbe821
commit d7d5ed8979
34 changed files with 3230 additions and 3 deletions
+1
View File
@@ -0,0 +1 @@
"""Alembic migration package for Zhixing Server."""
+51
View File
@@ -0,0 +1,51 @@
"""Alembic environment using the server's single Settings entry point."""
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from zhixing_server.bootstrap.config import get_settings
from zhixing_server.modules.market_data.infrastructure.schema import metadata
config = context.config
settings = get_settings()
config.set_main_option("sqlalchemy.url", settings.database_url.replace("%", "%%"))
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = metadata
def run_migrations_offline() -> None:
"""Run migrations without opening a database connection."""
context.configure(
url=settings.database_url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
compare_type=True,
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations against the configured PostgreSQL connection."""
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)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
@@ -0,0 +1,140 @@
"""Create the market-data bounded-context schema."""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "0001_market_data"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
"""Create market master, facts, and auditable synchronization tables."""
op.create_table(
"market_stock",
sa.Column("ts_code", sa.String(12), primary_key=True),
sa.Column("name", sa.String(128), nullable=False),
sa.Column("market", sa.String(32), nullable=False),
sa.Column("exchange", sa.String(16), nullable=False),
sa.Column("list_status", sa.String(2), nullable=False),
sa.Column("list_date", sa.Date()),
sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.text("true")),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
)
op.create_table(
"market_daily_bar",
sa.Column("ts_code", sa.String(12), nullable=False),
sa.Column("trade_date", sa.Date(), nullable=False),
*[
sa.Column(field, sa.Numeric(20, 6))
for field in ("open", "high", "low", "close", "pre_close", "change", "pct_chg")
],
sa.Column("vol", sa.Numeric(24, 6)),
sa.Column("amount", sa.Numeric(24, 6)),
sa.Column("source_adj", sa.String(8), nullable=False, server_default="qfq"),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.PrimaryKeyConstraint("ts_code", "trade_date"),
)
op.create_index("ix_market_daily_bar_trade_date", "market_daily_bar", ["trade_date"])
op.create_table(
"market_daily_basic",
sa.Column("ts_code", sa.String(12), nullable=False),
sa.Column("trade_date", sa.Date(), nullable=False),
*[
sa.Column(field, sa.Numeric(24, 6))
for field in (
"close",
"turnover_rate",
"turnover_rate_f",
"volume_ratio",
"pe",
"pe_ttm",
"pb",
"ps",
"ps_ttm",
"dv_ratio",
"dv_ttm",
"total_share",
"float_share",
"free_share",
"total_mv",
"circ_mv",
)
],
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.PrimaryKeyConstraint("ts_code", "trade_date"),
)
op.create_index("ix_market_daily_basic_trade_date", "market_daily_basic", ["trade_date"])
op.create_table(
"market_sync_batch",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("target_trade_date", sa.Date(), nullable=False),
sa.Column("window_start", sa.Date(), nullable=False),
sa.Column("mode", sa.String(16), nullable=False),
sa.Column("status", sa.String(24), nullable=False),
sa.Column("target_count", sa.Integer(), nullable=False),
sa.Column("valid_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("coverage", sa.Numeric(8, 6), nullable=False, server_default="0"),
sa.Column("strategy_eligible", sa.Boolean(), nullable=False, server_default="false"),
sa.Column("parent_batch_id", sa.String(36)),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.Column("finished_at", sa.DateTime(timezone=True)),
)
op.create_table(
"market_sync_item",
sa.Column("batch_id", sa.String(36), nullable=False),
sa.Column("item_kind", sa.String(24), nullable=False),
sa.Column("item_key", sa.String(64), nullable=False),
sa.Column("status", sa.String(24), nullable=False),
sa.Column("inserted_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("updated_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("unchanged_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("fingerprint", sa.String(64)),
sa.Column("error_type", sa.String(64)),
sa.Column("error_message", sa.Text()),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()"),
),
sa.PrimaryKeyConstraint("batch_id", "item_kind", "item_key"),
)
op.create_index("ix_market_sync_item_status", "market_sync_item", ["batch_id", "status"])
def downgrade() -> None:
"""Drop market-data tables in dependency-safe order."""
op.drop_index("ix_market_sync_item_status", table_name="market_sync_item")
op.drop_table("market_sync_item")
op.drop_table("market_sync_batch")
op.drop_index("ix_market_daily_basic_trade_date", table_name="market_daily_basic")
op.drop_table("market_daily_basic")
op.drop_index("ix_market_daily_bar_trade_date", table_name="market_daily_bar")
op.drop_table("market_daily_bar")
op.drop_table("market_stock")