from app import create_app from app.extensions import db app = create_app() with app.app_context(): try: db.session.execute(db.text('''DROP INDEX IF EXISTS ix_training_records_user_id;''')) db.session.execute(db.text('''ALTER TABLE training_records DROP CONSTRAINT IF EXISTS uq_training_records_user_id;''')) db.session.commit() print("Legacy unique constraint on user_id dropped.") except Exception as e: print("Error dropping legacy unique constraint/index:", e) db.session.rollback() try: db.session.execute(db.text('''ALTER TABLE training_records ADD COLUMN IF NOT EXISTS training_version VARCHAR(64) NOT NULL DEFAULT 'onboarding-v1';''')) db.session.execute(db.text('''ALTER TABLE training_records ADD COLUMN IF NOT EXISTS exam_mode VARCHAR(20) NOT NULL DEFAULT 'unified';''')) db.session.execute(db.text('''ALTER TABLE training_records ADD COLUMN IF NOT EXISTS safety_stage VARCHAR(20);''')) db.session.execute(db.text('''ALTER TABLE training_records ADD COLUMN IF NOT EXISTS safety_progress JSONB;''')) db.session.execute(db.text('''CREATE INDEX IF NOT EXISTS ix_training_records_training_version ON training_records (training_version);''')) db.session.execute(db.text('''CREATE INDEX IF NOT EXISTS ix_training_records_safety_stage ON training_records (safety_stage);''')) db.session.commit() print("Runtime columns and indexes added.") except Exception as e: print("Error adding runtime columns/indexes:", e) db.session.rollback() try: db.session.execute(db.text('''ALTER TABLE training_records ADD COLUMN IF NOT EXISTS training_type VARCHAR(50) NOT NULL DEFAULT 'onboarding';''')) db.session.execute(db.text('''CREATE INDEX IF NOT EXISTS ix_training_records_training_type ON training_records (training_type);''')) db.session.commit() print("Column and index added.") except Exception as e: print("Error adding column/index:", e) db.session.rollback() try: db.session.execute(db.text('''ALTER TABLE training_records ADD CONSTRAINT uq_user_training_type UNIQUE (user_id, training_type);''')) db.session.commit() print("Constraint added.") except Exception as e: print("Constraint likely exists:", e) db.session.rollback() try: db.session.execute(db.text('''ALTER TABLE schedule_tasks ADD COLUMN IF NOT EXISTS job_params JSONB;''')) db.session.commit() print("job_params column added to schedule_tasks.") except Exception as e: print("Error adding job_params to schedule_tasks:", e) db.session.rollback()