39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Migration: add account_id to investments table.
|
|
Run once: python scripts/add_investment_account.py
|
|
Safe to re-run — skips if the column already exists.
|
|
"""
|
|
import sys, os
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from app import create_app
|
|
from app.extensions import db
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
with db.engine.connect() as conn:
|
|
# Check if column already exists
|
|
result = conn.execute(db.text(
|
|
"SELECT COUNT(*) FROM information_schema.columns "
|
|
"WHERE table_schema = DATABASE() "
|
|
"AND table_name = 'investments' "
|
|
"AND column_name = 'account_id'"
|
|
))
|
|
exists = result.scalar()
|
|
|
|
if exists:
|
|
print("Column investments.account_id already exists — nothing to do.")
|
|
else:
|
|
conn.execute(db.text(
|
|
"ALTER TABLE investments "
|
|
"ADD COLUMN account_id INT NULL DEFAULT NULL, "
|
|
"ADD INDEX ix_investments_account_id (account_id), "
|
|
"ADD CONSTRAINT fk_investments_account_id "
|
|
" FOREIGN KEY (account_id) REFERENCES accounts(id) "
|
|
" ON DELETE SET NULL"
|
|
))
|
|
conn.commit()
|
|
print("Added investments.account_id column successfully.")
|