'use strict'; module.exports = { async up(queryInterface) { await queryInterface.sequelize.transaction(async (transaction) => { await queryInterface.sequelize.query(` ALTER TABLE community.vocab_course ADD COLUMN IF NOT EXISTS cefr_level TEXT, ADD COLUMN IF NOT EXISTS course_kind TEXT NOT NULL DEFAULT 'core', ADD COLUMN IF NOT EXISTS prerequisite_course_id INTEGER NULL REFERENCES community.vocab_course(id) ON DELETE SET NULL; CREATE TABLE IF NOT EXISTS community.vocab_conversation_session ( id SERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES community."user"(id) ON DELETE CASCADE, lesson_id INTEGER NOT NULL REFERENCES community.vocab_course_lesson(id) ON DELETE CASCADE, scenario JSONB NOT NULL DEFAULT '{}'::jsonb, turns JSONB NOT NULL DEFAULT '[]'::jsonb, summary JSONB NULL, status TEXT NOT NULL DEFAULT 'active', created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS vocab_conversation_session_user_lesson_idx ON community.vocab_conversation_session(user_id, lesson_id, updated_at DESC); CREATE TABLE IF NOT EXISTS community.vocab_listening_activity ( id SERIAL PRIMARY KEY, lesson_id INTEGER NOT NULL REFERENCES community.vocab_course_lesson(id) ON DELETE CASCADE, audio_url TEXT NOT NULL, transcript TEXT NOT NULL, translation TEXT NULL, speaker_profile JSONB NOT NULL DEFAULT '{}'::jsonb, speed TEXT NOT NULL DEFAULT 'natural', questions JSONB NOT NULL DEFAULT '[]'::jsonb, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS vocab_listening_activity_lesson_idx ON community.vocab_listening_activity(lesson_id); `, { transaction }); }); }, async down(queryInterface) { await queryInterface.sequelize.query(` DROP TABLE IF EXISTS community.vocab_listening_activity; DROP TABLE IF EXISTS community.vocab_conversation_session; ALTER TABLE community.vocab_course DROP COLUMN IF EXISTS prerequisite_course_id, DROP COLUMN IF EXISTS course_kind, DROP COLUMN IF EXISTS cefr_level; `); } };