From c3ae68da4fc1a743cef9210a4e7e093f136399f1 Mon Sep 17 00:00:00 2001 From: ronalds Date: Fri, 21 Nov 2025 00:46:50 -0300 Subject: [PATCH] WIP: Add conditional questions - backend models and schemas updated --- backend/app/models.py | 7 ++++ backend/app/schemas.py | 3 ++ backend/migrate_conditional_questions.py | 45 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 backend/migrate_conditional_questions.py diff --git a/backend/app/models.py b/backend/app/models.py index 93e3188..653e7a5 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -71,11 +71,18 @@ class Question(Base): allow_photos = Column(Boolean, default=True) max_photos = Column(Integer, default=3) requires_comment_on_fail = Column(Boolean, default=False) + + # Conditional logic + parent_question_id = Column(Integer, ForeignKey("questions.id"), nullable=True) + show_if_answer = Column(String(50), nullable=True) # Valor que dispara esta pregunta + created_at = Column(DateTime(timezone=True), server_default=func.now()) # Relationships checklist = relationship("Checklist", back_populates="questions") answers = relationship("Answer", back_populates="question") + subquestions = relationship("Question", backref="parent", remote_side=[id]) + class Inspection(Base): diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 4b8cd86..c806b18 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -97,6 +97,8 @@ class QuestionBase(BaseModel): allow_photos: bool = True max_photos: int = 3 requires_comment_on_fail: bool = False + parent_question_id: Optional[int] = None + show_if_answer: Optional[str] = None class QuestionCreate(QuestionBase): checklist_id: int @@ -113,6 +115,7 @@ class Question(QuestionBase): from_attributes = True + # Inspection Schemas class InspectionBase(BaseModel): or_number: Optional[str] = None diff --git a/backend/migrate_conditional_questions.py b/backend/migrate_conditional_questions.py new file mode 100644 index 0000000..38b7ffc --- /dev/null +++ b/backend/migrate_conditional_questions.py @@ -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()