Spaces:
Running
Running
Upload database_sql.py
Browse files- database_sql.py +25 -4
database_sql.py
CHANGED
|
@@ -109,18 +109,17 @@ def init_sql_db():
|
|
| 109 |
def _auto_migrate_p7_fields():
|
| 110 |
"""
|
| 111 |
🔄 P7后悔模式:自动迁移新增字段
|
| 112 |
-
检查并添加 ownerships 表的新字段
|
| 113 |
"""
|
| 114 |
from sqlalchemy import inspect
|
| 115 |
|
| 116 |
try:
|
| 117 |
inspector = inspect(engine)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
if 'ownerships' in inspector.get_table_names():
|
| 121 |
columns = [col['name'] for col in inspector.get_columns('ownerships')]
|
| 122 |
|
| 123 |
-
# 添加 P7 新增字段
|
| 124 |
with engine.connect() as conn:
|
| 125 |
if 'price_paid' not in columns:
|
| 126 |
if 'sqlite' in SQLALCHEMY_DATABASE_URL:
|
|
@@ -141,8 +140,30 @@ def _auto_migrate_p7_fields():
|
|
| 141 |
logger.info("迁移完成: 添加 ownerships.refunded_at 字段")
|
| 142 |
|
| 143 |
conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
-
logger.warning(f"
|
| 146 |
|
| 147 |
|
| 148 |
def get_db():
|
|
|
|
| 109 |
def _auto_migrate_p7_fields():
|
| 110 |
"""
|
| 111 |
🔄 P7后悔模式:自动迁移新增字段
|
| 112 |
+
检查并添加 ownerships 表和 transactions 表的新字段
|
| 113 |
"""
|
| 114 |
from sqlalchemy import inspect
|
| 115 |
|
| 116 |
try:
|
| 117 |
inspector = inspect(engine)
|
| 118 |
|
| 119 |
+
# ========== 1. 迁移 ownerships 表 ==========
|
| 120 |
if 'ownerships' in inspector.get_table_names():
|
| 121 |
columns = [col['name'] for col in inspector.get_columns('ownerships')]
|
| 122 |
|
|
|
|
| 123 |
with engine.connect() as conn:
|
| 124 |
if 'price_paid' not in columns:
|
| 125 |
if 'sqlite' in SQLALCHEMY_DATABASE_URL:
|
|
|
|
| 140 |
logger.info("迁移完成: 添加 ownerships.refunded_at 字段")
|
| 141 |
|
| 142 |
conn.commit()
|
| 143 |
+
|
| 144 |
+
# ========== 2. 迁移 transactions 表(提现相关新字段) ==========
|
| 145 |
+
if 'transactions' in inspector.get_table_names():
|
| 146 |
+
columns = [col['name'] for col in inspector.get_columns('transactions')]
|
| 147 |
+
|
| 148 |
+
with engine.connect() as conn:
|
| 149 |
+
# 定义 transactions 表的新列
|
| 150 |
+
new_columns = {
|
| 151 |
+
'alipay_account': 'VARCHAR',
|
| 152 |
+
'real_name': 'VARCHAR',
|
| 153 |
+
'withdraw_status': 'VARCHAR',
|
| 154 |
+
'payment_order_id': 'VARCHAR',
|
| 155 |
+
'net_amount': 'INTEGER',
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
for col_name, col_type in new_columns.items():
|
| 159 |
+
if col_name not in columns:
|
| 160 |
+
conn.execute(text(f"ALTER TABLE transactions ADD COLUMN {col_name} {col_type}"))
|
| 161 |
+
logger.info(f"[DB Migration] 添加列 transactions.{col_name}")
|
| 162 |
+
|
| 163 |
+
conn.commit()
|
| 164 |
+
|
| 165 |
except Exception as e:
|
| 166 |
+
logger.warning(f"字段迁移跳过 (可能已存在): {e}")
|
| 167 |
|
| 168 |
|
| 169 |
def get_db():
|