remove redundant indices table and optimize index weight data handling

This commit is contained in:
Your Name 2025-10-08 15:28:39 +08:00
parent 981c7e84ba
commit 1d1c04f85f
2 changed files with 6 additions and 23 deletions

View File

@ -137,14 +137,6 @@ 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,
@ -618,19 +610,6 @@ def initialize_database() -> MigrationResult:
with db_session() as session:
cursor = session.cursor()
# 指数相关表已在 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:
try:

View File

@ -387,8 +387,7 @@ _TABLE_SCHEMAS: Dict[str, str] = {
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)
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""",
"fund_basic": """
@ -1274,6 +1273,11 @@ def fetch_index_weight(start: date, end: date, index_code: str) -> Iterable[Dict
{"index_code": index_code, "start_date": start_str, "end_date": end_str},
limit=5000,
)
# Filter out rows where con_code is null to avoid DB constraint violation
if df is not None and not df.empty:
df = df.dropna(subset=["con_code"])
# Rename con_code to ts_code to match database schema
df = df.rename(columns={"con_code": "ts_code"})
return _df_to_records(df, ["index_code", "trade_date", "ts_code", "weight"])