From 981c7e84ba497040ca69217a14deb0bd38dc5cce Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 8 Oct 2025 13:56:37 +0800 Subject: [PATCH] consolidate index schema definitions into main schema file --- app/data/schema.py | 69 +++++++++++++++++++++++++++++++++++++--- app/data/schema_index.py | 42 ------------------------ 2 files changed, 65 insertions(+), 46 deletions(-) delete mode 100644 app/data/schema_index.py diff --git a/app/data/schema.py b/app/data/schema.py index 9da1928..e2675c2 100644 --- a/app/data/schema.py +++ b/app/data/schema.py @@ -5,7 +5,7 @@ import sqlite3 from dataclasses import dataclass, field from typing import Iterable, List -from app.data.schema_index import initialize_index_membership_tables, add_default_indices +# 指数相关表已直接在 SCHEMA_STATEMENTS 中定义 from app.utils.db import db_session @@ -137,6 +137,14 @@ SCHEMA_STATEMENTS: Iterable[str] = ( ); """, """ + CREATE TABLE IF NOT EXISTS indices ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + index_code VARCHAR(10) NOT NULL UNIQUE, + name VARCHAR(50) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + """, + """ CREATE TABLE IF NOT EXISTS index_basic ( ts_code TEXT PRIMARY KEY, name TEXT, @@ -181,6 +189,48 @@ SCHEMA_STATEMENTS: Iterable[str] = ( ); """, """ + CREATE TABLE IF NOT EXISTS index_weight ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + index_code VARCHAR(10) NOT NULL, + trade_date VARCHAR(8) NOT NULL, + ts_code VARCHAR(10) NOT NULL, + weight FLOAT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + """, + """ + CREATE INDEX IF NOT EXISTS idx_index_weight_lookup ON index_weight (index_code, trade_date); + """, + """ + CREATE TABLE IF NOT EXISTS index_member ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + index_code VARCHAR(10) NOT NULL, + trade_date VARCHAR(8) NOT NULL, + ts_code VARCHAR(10) NOT NULL, + is_new INTEGER DEFAULT 0, + is_delete INTEGER DEFAULT 0, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + """, + """ + CREATE INDEX IF NOT EXISTS idx_index_member_lookup ON index_member (index_code, trade_date); + """, + """ + CREATE TABLE IF NOT EXISTS index_dailybasic ( + ts_code VARCHAR(10) NOT NULL, + trade_date VARCHAR(8) NOT NULL, + turnover REAL, + turnover_ratio REAL, + pe_ttm REAL, + pb REAL, + ps_ttm REAL, + dv_ttm REAL, + total_mv REAL, + circ_mv REAL, + PRIMARY KEY (ts_code, trade_date) + ); + """, + """ CREATE TABLE IF NOT EXISTS fund_basic ( ts_code TEXT PRIMARY KEY, name TEXT, @@ -509,6 +559,8 @@ REQUIRED_TABLES = ( "stk_limit", "index_basic", "index_daily", + "index_weight", + "index_dailybasic", "fund_basic", "fund_nav", "fut_basic", @@ -566,9 +618,18 @@ def initialize_database() -> MigrationResult: with db_session() as session: cursor = session.cursor() - # 初始化指数相关表 - initialize_index_membership_tables(session) - add_default_indices() + # 指数相关表已在 SCHEMA_STATEMENTS 中定义 + + # 添加默认指数数据 + indices = [ + ("000300.SH", "沪深300"), + ("000905.SH", "中证500"), + ("000852.SH", "中证1000") + ] + cursor.executemany( + "INSERT OR IGNORE INTO indices (index_code, name) VALUES (?, ?)", + indices + ) # 创建表 for statement in SCHEMA_STATEMENTS: diff --git a/app/data/schema_index.py b/app/data/schema_index.py deleted file mode 100644 index 3d09519..0000000 --- a/app/data/schema_index.py +++ /dev/null @@ -1,42 +0,0 @@ -"""SQL schema for index membership.""" -from app.utils.db import db_session - -def initialize_index_membership_tables(conn): - """Create tables for tracking index membership.""" - conn.execute(""" - CREATE TABLE IF NOT EXISTS index_weight ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - index_code VARCHAR(10) NOT NULL, - trade_date VARCHAR(8) NOT NULL, - ts_code VARCHAR(10) NOT NULL, - weight FLOAT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - INDEX idx_index_weight_lookup (index_code, trade_date) - ) - """) - -def add_default_indices(): - """Add default index list.""" - indices = [ - ("000300.SH", "沪深300"), - ("000905.SH", "中证500"), - ("000852.SH", "中证1000") - ] - with db_session() as conn: - conn.execute(""" - CREATE TABLE IF NOT EXISTS indices ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - index_code VARCHAR(10) NOT NULL UNIQUE, - name VARCHAR(50) NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - ) - """) - - for code, name in indices: - conn.execute( - """ - INSERT OR IGNORE INTO indices (index_code, name) - VALUES (?, ?) - """, - (code, name) - ) \ No newline at end of file