consolidate index schema definitions into main schema file

This commit is contained in:
sam 2025-10-08 13:56:37 +08:00
parent 454b9579c7
commit 981c7e84ba
2 changed files with 65 additions and 46 deletions

View File

@ -5,7 +5,7 @@ import sqlite3
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Iterable, List 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 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 ( CREATE TABLE IF NOT EXISTS index_basic (
ts_code TEXT PRIMARY KEY, ts_code TEXT PRIMARY KEY,
name TEXT, 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 ( CREATE TABLE IF NOT EXISTS fund_basic (
ts_code TEXT PRIMARY KEY, ts_code TEXT PRIMARY KEY,
name TEXT, name TEXT,
@ -509,6 +559,8 @@ REQUIRED_TABLES = (
"stk_limit", "stk_limit",
"index_basic", "index_basic",
"index_daily", "index_daily",
"index_weight",
"index_dailybasic",
"fund_basic", "fund_basic",
"fund_nav", "fund_nav",
"fut_basic", "fut_basic",
@ -566,9 +618,18 @@ def initialize_database() -> MigrationResult:
with db_session() as session: with db_session() as session:
cursor = session.cursor() cursor = session.cursor()
# 初始化指数相关表 # 指数相关表已在 SCHEMA_STATEMENTS 中定义
initialize_index_membership_tables(session)
add_default_indices() # 添加默认指数数据
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: for statement in SCHEMA_STATEMENTS:

View File

@ -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)
)