WIP: Add conditional questions - backend models and schemas updated
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
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