Files
checklist/backend/migrate_conditional_questions.py

46 lines
1.3 KiB
Python

"""
Migration script to add conditional questions support
Run this script to add parent_question_id and show_if_answer columns
"""
from sqlalchemy import create_engine, text
import os
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://checklist_user:checklist_pass_2024@localhost:5432/checklist_db")
engine = create_engine(DATABASE_URL)
migrations = [
"""
ALTER TABLE questions
ADD COLUMN IF NOT EXISTS parent_question_id INTEGER REFERENCES questions(id) ON DELETE CASCADE;
""",
"""
ALTER TABLE questions
ADD COLUMN IF NOT EXISTS show_if_answer VARCHAR(50);
""",
"""
CREATE INDEX IF NOT EXISTS idx_questions_parent
ON questions(parent_question_id);
"""
]
def run_migration():
print("🔄 Starting migration for conditional questions...")
with engine.connect() as conn:
for i, migration in enumerate(migrations, 1):
try:
conn.execute(text(migration))
conn.commit()
print(f"✅ Migration {i}/{len(migrations)} completed")
except Exception as e:
print(f"❌ Error in migration {i}: {e}")
conn.rollback()
return False
print("✅ All migrations completed successfully!")
return True
if __name__ == "__main__":
run_migration()