From 27ff914f77bd718825848d3a2b97c7914637413f Mon Sep 17 00:00:00 2001 From: M Fari Madyan Date: Wed, 25 Nov 2026 15:40:27 +0700 Subject: [PATCH] fix(seed): store demo question options as JSON arrays and repair existing rows seed_demo_data.sql inserted question options as a JSON object ({"A": "10", ...}), but Question.options is mapped as List, so opening or starting the demo exams failed with 500. - Seed the four affected questions with JSON arrays, the format the editor writes. - Add V4 migration that converts existing object-shaped options into arrays (ordered by key, so the original option order is kept). Rows already stored as arrays are not touched. --- .../migration/V4__fix_question_options_format.sql | 15 +++++++++++++++ .../resources/db/migration/seed_demo_data.sql | 8 ++++---- 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/resources/db/migration/V4__fix_question_options_format.sql diff --git a/backend/src/main/resources/db/migration/V4__fix_question_options_format.sql b/backend/src/main/resources/db/migration/V4__fix_question_options_format.sql new file mode 100644 index 0000000..e4efc2b --- /dev/null +++ b/backend/src/main/resources/db/migration/V4__fix_question_options_format.sql @@ -0,0 +1,15 @@ +-- V4: repair question options stored as a JSON object instead of a JSON array. +-- +-- Question.options is mapped as List and the grader compares the chosen option's text with +-- correct_answer. Older versions of seed_demo_data.sql inserted options as {"A": "10", "B": "20", ...}, +-- which Jackson cannot read as a list: opening those exams in the editor or starting them failed with 500. +-- +-- The application itself always writes arrays, so only rows from the old seed are touched. Keys are +-- option letters (A..E) or "Benar"/"Salah", so ordering by key keeps the original option order. +-- Running it on a database without such rows changes nothing. + +UPDATE questions +SET options = COALESCE( + (SELECT jsonb_agg(entry.value ORDER BY entry.key) FROM jsonb_each(questions.options) AS entry), + '[]'::jsonb) +WHERE jsonb_typeof(options) = 'object'; diff --git a/backend/src/main/resources/db/migration/seed_demo_data.sql b/backend/src/main/resources/db/migration/seed_demo_data.sql index 0445817..021d5c4 100644 --- a/backend/src/main/resources/db/migration/seed_demo_data.sql +++ b/backend/src/main/resources/db/migration/seed_demo_data.sql @@ -155,12 +155,12 @@ BEGIN -- Questions INSERT INTO questions (examination_id, question_text, question_type, options, correct_answer, points) VALUES - (v_exam1, 'Jika matriks A berordo 2x2 memiliki determinan 5, maka nilai dari determinan 2A adalah...', 'multiple_choice', '{"A": "10", "B": "20", "C": "25", "D": "40", "E": "50"}'::jsonb, '20', 20), - (v_exam1, 'Dua garis sejajar memiliki gradien yang sama besar.', 'true_false', '{"Benar": "Benar", "Salah": "Salah"}'::jsonb, 'Benar', 20), + (v_exam1, 'Jika matriks A berordo 2x2 memiliki determinan 5, maka nilai dari determinan 2A adalah...', 'multiple_choice', '["10", "20", "25", "40", "50"]'::jsonb, '20', 20), + (v_exam1, 'Dua garis sejajar memiliki gradien yang sama besar.', 'true_false', '["Benar", "Salah"]'::jsonb, 'Benar', 20), (v_exam1, 'Jelaskan konsep invers matriks dan sebutkan syarat sebuah matriks memiliki invers!', 'essay', NULL, 'Matriks memiliki invers jika determinan != 0 (non-singular).', 60), - (v_exam2, 'Anotasi Spring Boot yang digunakan untuk menandai kelas sebagai REST Controller adalah...', 'multiple_choice', '{"A": "@Controller", "B": "@RestController", "C": "@Service", "D": "@Component", "E": "@Endpoint"}'::jsonb, '@RestController', 30), - (v_exam2, 'React hook apa yang digunakan untuk mengelola efek samping seperti data fetching?', 'multiple_choice', '{"A": "useState", "B": "useEffect", "C": "useMemo", "D": "useContext", "E": "useRef"}'::jsonb, 'useEffect', 30), + (v_exam2, 'Anotasi Spring Boot yang digunakan untuk menandai kelas sebagai REST Controller adalah...', 'multiple_choice', '["@Controller", "@RestController", "@Service", "@Component", "@Endpoint"]'::jsonb, '@RestController', 30), + (v_exam2, 'React hook apa yang digunakan untuk mengelola efek samping seperti data fetching?', 'multiple_choice', '["useState", "useEffect", "useMemo", "useContext", "useRef"]'::jsonb, 'useEffect', 30), (v_exam2, 'Jelaskan perbedaan synchronous dan asynchronous dalam konteks pemrosesan Web API!', 'essay', NULL, 'Synchronous blocking, asynchronous non-blocking.', 40); -- Exam Attempt