Settings CAS compared the new row to itself
Browse files at this commit4de958f3c2570d59c9fcec36b959bf1517637c06Parent: 9ce59bf- fix
- settings CAS compared the new row to itself
2 changed files +37 -11
- .sqlx/query-935e0ed64846d8c1ad7e317725d31da05a618a6ab0e628482399affad99261c8.json → .sqlx/query-83d5da7f52ee983e49f3c4dcd99472f5a8ba4a9017363f04c35173284da42037.json +11 -3
- src/db/settings.rs +26 -8
.sqlx/query-935e0ed64846d8c1ad7e317725d31da05a618a6ab0e628482399affad99261c8.json → .sqlx/query-83d5da7f52ee983e49f3c4dcd99472f5a8ba4a9017363f04c35173284da42037.jsonrenamed+11 -3
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "db_name": "PostgreSQL", |
| 3 | - "query": "WITH new_row AS (\n INSERT INTO settings (id, exam_kinds, grade_bands, max_file_bytes,\n chatbot_history_turns, max_chatbot_message_len,\n max_chatbot_threads, meal_slots,\n meal_cancel_cutoff_minutes)\n VALUES ('school', $1, $3, $4, $5, $6, $7, $8, $10)\n ON CONFLICT (id) DO UPDATE SET\n exam_kinds = $1,\n grade_bands = $3,\n max_file_bytes = $4,\n chatbot_history_turns = $5,\n max_chatbot_message_len = $6,\n max_chatbot_threads = $7,\n meal_slots = $8,\n meal_cancel_cutoff_minutes = $10\n WHERE settings.exam_kinds IS NOT DISTINCT FROM $1\n AND settings.grade_bands IS NOT DISTINCT FROM $3\n AND settings.max_file_bytes IS NOT DISTINCT FROM $4\n AND settings.chatbot_history_turns IS NOT DISTINCT FROM $5\n AND settings.max_chatbot_message_len IS NOT DISTINCT FROM $6\n AND settings.max_chatbot_threads IS NOT DISTINCT FROM $7\n AND settings.meal_slots IS NOT DISTINCT FROM $8\n AND settings.meal_cancel_cutoff_minutes IS NOT DISTINCT FROM $10\n AND COALESCE((SELECT array_agg(s.status ORDER BY s.status)\n FROM settings_attendance_status s\n WHERE s.settings = settings.id), '{}')\n IS NOT DISTINCT FROM $11::text[]\n AND (SELECT array_agg(t.tag ORDER BY t.tag)\n FROM settings_dietary_tag t\n WHERE t.settings = settings.id)\n IS NOT DISTINCT FROM $12::text[]\n RETURNING exam_kinds, grade_bands, max_file_bytes,\n chatbot_history_turns, max_chatbot_message_len,\n max_chatbot_threads, meal_slots, meal_cancel_cutoff_minutes),\n status_del AS (\n DELETE FROM settings_attendance_status\n WHERE settings = 'school' AND EXISTS (SELECT 1 FROM new_row)\n RETURNING 1),\n status_ins AS (\n INSERT INTO settings_attendance_status (settings, status)\n SELECT 'school', t.status FROM unnest($2::text[]) AS t(status)\n CROSS JOIN (SELECT count(*) AS deleted FROM status_del) d\n WHERE EXISTS (SELECT 1 FROM new_row)\n RETURNING status),\n tag_del AS (\n DELETE FROM settings_dietary_tag\n WHERE settings = 'school' AND EXISTS (SELECT 1 FROM new_row)\n RETURNING 1),\n tag_ins AS (\n INSERT INTO settings_dietary_tag (settings, tag)\n SELECT 'school', t.tag FROM unnest($9::text[]) AS t(tag)\n CROSS JOIN (SELECT count(*) AS deleted FROM tag_del) d\n WHERE EXISTS (SELECT 1 FROM new_row)\n RETURNING tag)\n SELECT r.exam_kinds AS \"exam_kinds: Json<Vec<ExamKindDef>>\",\n COALESCE((SELECT array_agg(status ORDER BY status)\n FROM status_ins), '{}') AS \"attendance_statuses!: Vec<String>\",\n r.grade_bands AS \"grade_bands: Json<Vec<GradeBand>>\",\n r.max_file_bytes,\n r.chatbot_history_turns,\n r.max_chatbot_message_len,\n r.max_chatbot_threads,\n r.meal_slots AS \"meal_slots: Json<Vec<MealSlotDef>>\",\n (SELECT array_agg(tag ORDER BY tag) FROM tag_ins) AS dietary_tags,\n r.meal_cancel_cutoff_minutes\n FROM new_row r", | |
| 3 | + "query": "WITH new_row AS (\n INSERT INTO settings (id, exam_kinds, grade_bands, max_file_bytes,\n chatbot_history_turns, max_chatbot_message_len,\n max_chatbot_threads, meal_slots,\n meal_cancel_cutoff_minutes)\n VALUES ('school', $1, $3, $4, $5, $6, $7, $8, $10)\n ON CONFLICT (id) DO UPDATE SET\n exam_kinds = $1,\n grade_bands = $3,\n max_file_bytes = $4,\n chatbot_history_turns = $5,\n max_chatbot_message_len = $6,\n max_chatbot_threads = $7,\n meal_slots = $8,\n meal_cancel_cutoff_minutes = $10\n WHERE settings.exam_kinds IS NOT DISTINCT FROM $13\n AND settings.grade_bands IS NOT DISTINCT FROM $14\n AND settings.max_file_bytes IS NOT DISTINCT FROM $15\n AND settings.chatbot_history_turns IS NOT DISTINCT FROM $16\n AND settings.max_chatbot_message_len IS NOT DISTINCT FROM $17\n AND settings.max_chatbot_threads IS NOT DISTINCT FROM $18\n AND settings.meal_slots IS NOT DISTINCT FROM $19\n AND settings.meal_cancel_cutoff_minutes IS NOT DISTINCT FROM $20\n AND COALESCE((SELECT array_agg(s.status ORDER BY s.status)\n FROM settings_attendance_status s\n WHERE s.settings = settings.id), '{}')\n IS NOT DISTINCT FROM $11::text[]\n AND (SELECT array_agg(t.tag ORDER BY t.tag)\n FROM settings_dietary_tag t\n WHERE t.settings = settings.id)\n IS NOT DISTINCT FROM $12::text[]\n RETURNING exam_kinds, grade_bands, max_file_bytes,\n chatbot_history_turns, max_chatbot_message_len,\n max_chatbot_threads, meal_slots, meal_cancel_cutoff_minutes),\n status_del AS (\n DELETE FROM settings_attendance_status\n WHERE settings = 'school' AND EXISTS (SELECT 1 FROM new_row)\n RETURNING 1),\n status_ins AS (\n INSERT INTO settings_attendance_status (settings, status)\n SELECT 'school', t.status FROM unnest($2::text[]) AS t(status)\n CROSS JOIN (SELECT count(*) AS deleted FROM status_del) d\n WHERE EXISTS (SELECT 1 FROM new_row)\n RETURNING status),\n tag_del AS (\n DELETE FROM settings_dietary_tag\n WHERE settings = 'school' AND EXISTS (SELECT 1 FROM new_row)\n RETURNING 1),\n tag_ins AS (\n INSERT INTO settings_dietary_tag (settings, tag)\n SELECT 'school', t.tag FROM unnest($9::text[]) AS t(tag)\n CROSS JOIN (SELECT count(*) AS deleted FROM tag_del) d\n WHERE EXISTS (SELECT 1 FROM new_row)\n RETURNING tag)\n SELECT r.exam_kinds AS \"exam_kinds: Json<Vec<ExamKindDef>>\",\n COALESCE((SELECT array_agg(status ORDER BY status)\n FROM status_ins), '{}') AS \"attendance_statuses!: Vec<String>\",\n r.grade_bands AS \"grade_bands: Json<Vec<GradeBand>>\",\n r.max_file_bytes,\n r.chatbot_history_turns,\n r.max_chatbot_message_len,\n r.max_chatbot_threads,\n r.meal_slots AS \"meal_slots: Json<Vec<MealSlotDef>>\",\n (SELECT array_agg(tag ORDER BY tag) FROM tag_ins) AS dietary_tags,\n r.meal_cancel_cutoff_minutes\n FROM new_row r", | |
| 4 | 4 | "describe": { |
| 5 | 5 | "columns": [ |
| 6 | 6 | { |
| @@ -117,7 +117,15 @@ | ||
| 117 | 117 | "TextArray", |
| 118 | 118 | "Int8", |
| 119 | 119 | "TextArray", |
| 120 | - "TextArray" | |
| 120 | + "TextArray", | |
| 121 | + "Jsonb", | |
| 122 | + "Jsonb", | |
| 123 | + "Int8", | |
| 124 | + "Int8", | |
| 125 | + "Int8", | |
| 126 | + "Int8", | |
| 127 | + "Jsonb", | |
| 128 | + "Int8" | |
| 121 | 129 | ] |
| 122 | 130 | }, |
| 123 | 131 | "nullable": [ |
| @@ -133,5 +141,5 @@ | ||
| 133 | 141 | true |
| 134 | 142 | ] |
| 135 | 143 | }, |
| 136 | - "hash": "935e0ed64846d8c1ad7e317725d31da05a618a6ab0e628482399affad99261c8" | |
| 144 | + "hash": "83d5da7f52ee983e49f3c4dcd99472f5a8ba4a9017363f04c35173284da42037" | |
| 137 | 145 | } |
src/db/settings.rsmodified+26 -8
| @@ -195,6 +195,16 @@ where | ||
| 195 | 195 | .map(|slots| serde_json::to_value(&slots.0)) |
| 196 | 196 | .transpose() |
| 197 | 197 | .map_err(|e| AppError::Internal(format!("settings encode: {e}")))?; |
| 198 | + let expected_exam_kinds = serde_json::to_value(&expected.exam_kinds.0) | |
| 199 | + .map_err(|e| AppError::Internal(format!("settings encode: {e}")))?; | |
| 200 | + let expected_grade_bands = serde_json::to_value(&expected.grade_bands.0) | |
| 201 | + .map_err(|e| AppError::Internal(format!("settings encode: {e}")))?; | |
| 202 | + let expected_meal_slots = expected | |
| 203 | + .meal_slots | |
| 204 | + .as_ref() | |
| 205 | + .map(|slots| serde_json::to_value(&slots.0)) | |
| 206 | + .transpose() | |
| 207 | + .map_err(|e| AppError::Internal(format!("settings encode: {e}")))?; | |
| 198 | 208 | let attendance = sorted(&settings.attendance_statuses); |
| 199 | 209 | let expected_attendance = sorted(&expected.attendance_statuses); |
| 200 | 210 | // An empty `Some` must compare equal to the stored NULL an empty set |
| @@ -221,14 +231,14 @@ where | ||
| 221 | 231 | max_chatbot_threads = $7, |
| 222 | 232 | meal_slots = $8, |
| 223 | 233 | meal_cancel_cutoff_minutes = $10 |
| 224 | - WHERE settings.exam_kinds IS NOT DISTINCT FROM $1 | |
| 225 | - AND settings.grade_bands IS NOT DISTINCT FROM $3 | |
| 226 | - AND settings.max_file_bytes IS NOT DISTINCT FROM $4 | |
| 227 | - AND settings.chatbot_history_turns IS NOT DISTINCT FROM $5 | |
| 228 | - AND settings.max_chatbot_message_len IS NOT DISTINCT FROM $6 | |
| 229 | - AND settings.max_chatbot_threads IS NOT DISTINCT FROM $7 | |
| 230 | - AND settings.meal_slots IS NOT DISTINCT FROM $8 | |
| 231 | - AND settings.meal_cancel_cutoff_minutes IS NOT DISTINCT FROM $10 | |
| 234 | + WHERE settings.exam_kinds IS NOT DISTINCT FROM $13 | |
| 235 | + AND settings.grade_bands IS NOT DISTINCT FROM $14 | |
| 236 | + AND settings.max_file_bytes IS NOT DISTINCT FROM $15 | |
| 237 | + AND settings.chatbot_history_turns IS NOT DISTINCT FROM $16 | |
| 238 | + AND settings.max_chatbot_message_len IS NOT DISTINCT FROM $17 | |
| 239 | + AND settings.max_chatbot_threads IS NOT DISTINCT FROM $18 | |
| 240 | + AND settings.meal_slots IS NOT DISTINCT FROM $19 | |
| 241 | + AND settings.meal_cancel_cutoff_minutes IS NOT DISTINCT FROM $20 | |
| 232 | 242 | AND COALESCE((SELECT array_agg(s.status ORDER BY s.status) |
| 233 | 243 | FROM settings_attendance_status s |
| 234 | 244 | WHERE s.settings = settings.id), '{}') |
| @@ -284,6 +294,14 @@ where | ||
| 284 | 294 | settings.meal_cancel_cutoff_minutes, |
| 285 | 295 | &expected_attendance, |
| 286 | 296 | expected_dietary, |
| 297 | + expected_exam_kinds, | |
| 298 | + expected_grade_bands, | |
| 299 | + expected.max_file_bytes, | |
| 300 | + expected.chatbot_history_turns, | |
| 301 | + expected.max_chatbot_message_len, | |
| 302 | + expected.max_chatbot_threads, | |
| 303 | + expected_meal_slots, | |
| 304 | + expected.meal_cancel_cutoff_minutes, | |
| 287 | 305 | ) |
| 288 | 306 | .fetch_optional(db) |
| 289 | 307 | .await?; |