WIP: Add conditional questions - backend models and schemas updated
This commit is contained in:
45
backend/migrate_conditional_questions.py
Normal file
45
backend/migrate_conditional_questions.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
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()
|
||||
Reference in New Issue
Block a user