import asyncio from sqlalchemy import text from app.core.database import engine async def migrate(): async with engine.begin() as conn: res = await conn.execute(text(""" SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'vas_payment'; """)) cols = [r[0] for r in res.fetchall()] print("Existing columns in vas_payment:", cols) if "fee_rate" not in cols: print("Adding fee_rate column...") await conn.execute(text("ALTER TABLE vas_payment ADD COLUMN fee_rate DECIMAL(6,4) NOT NULL DEFAULT 0.0000 COMMENT '手续费率' AFTER random_offset;")) print("Added fee_rate.") else: print("fee_rate column already exists.") if "fee_amount" not in cols: print("Adding fee_amount column...") await conn.execute(text("ALTER TABLE vas_payment ADD COLUMN fee_amount INT NOT NULL DEFAULT 0 COMMENT '手续费金额' AFTER fee_rate;")) print("Added fee_amount.") else: print("fee_amount column already exists.") # Also initialize default provider config for fee_rate if null or empty import json print("Checking vas_payment_provider configs...") for name, cfg in [ ('stripe', {'fee_rate': 0.035, 'fee_fixed': 30, 'fee_label': '3.5% + €0.30'}), ('creem', {'fee_rate': 0.035, 'fee_fixed': 0, 'fee_label': '3.5%'}), ('wechat', {'fee_rate': 0.0, 'fee_fixed': 0, 'fee_label': '免手续费'}), ('alipay', {'fee_rate': 0.0, 'fee_fixed': 0, 'fee_label': '免手续费'}), ]: await conn.execute(text(""" UPDATE vas_payment_provider SET config = :cfg WHERE name = :name AND (config IS NULL OR config = 'null' OR config = ''); """), {'cfg': json.dumps(cfg), 'name': name}) print("Provider configs checked and updated.") print("Migration finished successfully.") if __name__ == "__main__": asyncio.run(migrate())