72 lines
4.1 KiB
Python
72 lines
4.1 KiB
Python
"""显式迁移策略归属。默认只检查;--apply只用于已确认的隔离演练或155维护窗口。"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from sqlalchemy import text
|
|
from app.db import session as db
|
|
from app.core.strategy_book import TABLES
|
|
from config.settings import settings
|
|
|
|
|
|
def inspect(connection, table):
|
|
return list(connection.execute(text('SHOW COLUMNS FROM '+table)).mappings())
|
|
|
|
|
|
def migrate(apply=False, documents_only=False):
|
|
if settings.PMS_TABLE_PREFIX:
|
|
raise RuntimeError('此迁移脚本只允许155模拟实例或无前缀隔离演练')
|
|
results = []
|
|
if documents_only:
|
|
if apply:
|
|
sql = (Path(__file__).resolve().parents[1]/'ddl_rotation_v1.sql').read_text()
|
|
sql = '\n'.join(l for l in sql.splitlines() if not l.strip().startswith('--')).strip().rstrip(';')
|
|
with db.get_engine('proxy').begin() as c:
|
|
c.execute(text(sql))
|
|
return [{'documents_only': True, 'applied': apply}]
|
|
with db.get_engine('proxy').begin() as c:
|
|
for table in sorted(TABLES):
|
|
columns = inspect(c, table)
|
|
before = c.execute(text('SELECT COUNT(*) FROM '+table)).scalar_one()
|
|
original_cols = [r['Field'] for r in columns if r['Field'] != 'book_id']
|
|
projection = ','.join('`'+name+'`' for name in original_cols)
|
|
old_rows = c.execute(text('SELECT '+projection+' FROM '+table+' ORDER BY `id`')).mappings().all()
|
|
checksum = hashlib.sha256(json.dumps([dict(r) for r in old_rows], sort_keys=True, default=str).encode()).hexdigest()
|
|
exists = any(r['Field'] == 'book_id' for r in columns)
|
|
if apply and not exists:
|
|
c.execute(text('ALTER TABLE '+table+" ADD COLUMN book_id VARCHAR(16) NOT NULL DEFAULT 'original'"))
|
|
if apply:
|
|
other = c.execute(text('SELECT COUNT(*) FROM '+table+" WHERE book_id NOT IN ('original','micro30')")).scalar_one()
|
|
if other:
|
|
raise RuntimeError('存在未知策略归属')
|
|
if table == 'pms_position':
|
|
indexes = list(c.execute(text('SHOW INDEX FROM pms_position')).mappings())
|
|
for name in {r['Key_name'] for r in indexes if r['Key_name'] != 'PRIMARY' and not r['Non_unique']}:
|
|
cols = [r['Column_name'] for r in sorted(indexes, key=lambda r: r['Seq_in_index']) if r['Key_name'] == name]
|
|
if cols == ['ts_code']:
|
|
if not re.fullmatch(r'[A-Za-z0-9_]+', name):
|
|
raise RuntimeError('索引名非法')
|
|
c.execute(text('ALTER TABLE pms_position DROP INDEX `'+name+'`, ADD UNIQUE KEY uk_book_code (book_id,ts_code)'))
|
|
after = c.execute(text('SELECT COUNT(*) FROM '+table)).scalar_one()
|
|
after_rows = c.execute(text('SELECT '+projection+' FROM '+table+' ORDER BY `id`')).mappings().all()
|
|
after_checksum = hashlib.sha256(json.dumps([dict(r) for r in after_rows], sort_keys=True, default=str).encode()).hexdigest()
|
|
if before != after or checksum != after_checksum:
|
|
raise RuntimeError('迁移时数量、现金、成本或历史字段变化,必须在维护窗口重核')
|
|
results.append({'table': table, 'rows': before, 'book_id_existed': exists, 'applied': apply, 'original_columns_sha256': checksum})
|
|
if apply:
|
|
sql = (Path(__file__).resolve().parents[1]/'ddl_rotation_v1.sql').read_text()
|
|
sql = '\n'.join(l for l in sql.splitlines() if not l.strip().startswith('--')).strip().rstrip(';')
|
|
c.execute(text(sql))
|
|
return results
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--apply', action='store_true')
|
|
parser.add_argument('--documents-only', action='store_true', help='仅建观察和纸面记录表,不迁移交易账本')
|
|
args = parser.parse_args()
|
|
print(json.dumps(migrate(args.apply, args.documents_only), ensure_ascii=False, indent=2))
|