Files
customer-portal/customer_portal/migrations/003_add_email_preferences.py

76 lines
2.1 KiB
Python
Executable File

"""Add email preference columns to customers.
Run with:
docker exec customer_portal python -m customer_portal.migrations.003_add_email_preferences
"""
import os
from sqlalchemy import create_engine, text
def run_migration():
"""Add email_invoices and email_marketing columns."""
database_url = os.environ.get(
"DATABASE_URL", "postgresql://portal:portal@localhost:5432/customer_portal"
)
engine = create_engine(database_url)
with engine.connect() as conn:
# Check and add email_invoices column
result = conn.execute(
text(
"""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'customers' AND column_name = 'email_invoices'
"""
)
)
if not result.fetchone():
print("Adding email_invoices column...")
conn.execute(
text(
"""
ALTER TABLE customers
ADD COLUMN email_invoices BOOLEAN DEFAULT TRUE
"""
)
)
conn.commit()
print("Column email_invoices added.")
else:
print("Column email_invoices already exists.")
# Check and add email_marketing column
result = conn.execute(
text(
"""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'customers' AND column_name = 'email_marketing'
"""
)
)
if not result.fetchone():
print("Adding email_marketing column...")
conn.execute(
text(
"""
ALTER TABLE customers
ADD COLUMN email_marketing BOOLEAN DEFAULT FALSE
"""
)
)
conn.commit()
print("Column email_marketing added.")
else:
print("Column email_marketing already exists.")
print("Migration completed!")
if __name__ == "__main__":
run_migration()