1use std::collections::BTreeMap;
10
11use super::candidate::PhysicalDocumentIdentity;
12use uqa_core::{DocId, Value};
13use uqa_sql::SQLError;
14use uqa_storage::document_store::Document;
15
16mod rows;
17pub use rows::{
18 decode_prepared_mutation_action_row, prepared_mutation_action_schema,
19 push_prepared_mutation_action,
20};
21
22const PREPARED_MUTATION_CODEC_VERSION: i64 = 4;
23
24#[derive(Debug, Clone, PartialEq)]
25pub struct PreparedDocumentRewrite {
26 pub table: String,
27 pub doc_id: DocId,
28 pub destination: Option<(String, DocId)>,
29 pub moved_through: Option<String>,
31 pub relocation: Option<DocId>,
33 pub partition_move_delete: Option<Box<PreparedDocumentDelete>>,
34 pub old_document: Document,
35 pub new_document: Document,
36 pub capture_partition_move_update_transition: bool,
37 pub referential_action: Option<Box<ReferentialRewrite>>,
39}
40
41impl PreparedDocumentRewrite {
42 pub fn is_partition_move_delete(&self) -> bool {
43 self.partition_move_delete.is_some()
44 }
45
46 pub fn referential_columns(&self) -> Option<&[String]> {
48 self.referential_action
49 .as_deref()
50 .map(|action| action.columns.as_slice())
51 }
52}
53
54#[derive(Debug, Clone, PartialEq)]
56pub struct ReferentialRewrite {
57 pub relation: String,
59 pub columns: Vec<String>,
61}
62
63#[derive(Debug, Clone, PartialEq)]
64pub struct PreparedDocumentDelete {
65 pub table: String,
66 pub doc_id: DocId,
67 pub document: Document,
68}
69
70#[derive(Debug, Clone, PartialEq)]
71pub struct PreparedDocumentInsert {
72 pub table: String,
73 pub doc_id: DocId,
74 pub document: Document,
75}
76
77#[derive(Debug, Clone, PartialEq)]
78pub enum PreparedMutationAction {
79 Insert(PreparedDocumentInsert),
80 Rewrite(PreparedDocumentRewrite),
81 Delete(PreparedDocumentDelete),
82}
83
84impl PreparedMutationAction {
85 pub fn written_rows(&self) -> Vec<PhysicalDocumentIdentity> {
87 let identity = |table: &str, doc_id: DocId| PhysicalDocumentIdentity {
88 table: table.to_string(),
89 doc_id,
90 };
91 match self {
92 Self::Insert(insert) => vec![identity(&insert.table, insert.doc_id)],
93 Self::Delete(delete) => vec![identity(&delete.table, delete.doc_id)],
94 Self::Rewrite(rewrite) => std::iter::once(identity(&rewrite.table, rewrite.doc_id))
95 .chain(
96 rewrite
97 .destination
98 .as_ref()
99 .map(|(table, doc_id)| identity(table, *doc_id)),
100 )
101 .chain(
102 rewrite
103 .relocation
104 .map(|doc_id| identity(&rewrite.table, doc_id)),
105 )
106 .collect(),
107 }
108 }
109}
110
111#[derive(Debug, Clone, PartialEq)]
112pub enum PreparedInsertConflict {
113 Unresolved,
114 Insert { doc_id: DocId, supplied: bool },
115 Skip,
116 Updated(PreparedDocumentRewrite),
117}
118
119pub fn encode_prepared_doc_id(doc_id: DocId) -> Value {
120 Value::Bytes(doc_id.to_be_bytes().to_vec())
121}
122
123pub fn decode_prepared_doc_id(value: Value, context: &str) -> Result<DocId, SQLError> {
124 let Value::Bytes(bytes) = value else {
125 return Err(SQLError::Internal(format!(
126 "{context} has a non-binary document id"
127 )));
128 };
129 let bytes: [u8; std::mem::size_of::<DocId>()] = bytes
130 .try_into()
131 .map_err(|_| SQLError::Internal(format!("{context} has an invalid document id width")))?;
132 Ok(DocId::from_be_bytes(bytes))
133}
134
135fn take_codec_version(fields: &mut BTreeMap<String, Value>, context: &str) -> Result<(), SQLError> {
136 match fields.remove("version") {
137 Some(Value::Int(PREPARED_MUTATION_CODEC_VERSION)) => Ok(()),
138 Some(Value::Int(version)) => Err(SQLError::Internal(format!(
139 "{context} has unsupported codec version {version}"
140 ))),
141 _ => Err(SQLError::Internal(format!(
142 "{context} has no valid codec version"
143 ))),
144 }
145}
146
147fn reject_unknown_fields(fields: &BTreeMap<String, Value>, context: &str) -> Result<(), SQLError> {
148 if fields.is_empty() {
149 return Ok(());
150 }
151 Err(SQLError::Internal(format!(
152 "{context} has unknown field `{}`",
153 fields.keys().next().expect("non-empty map")
154 )))
155}
156
157pub fn encode_prepared_document_rewrite(prepared: PreparedDocumentRewrite) -> Value {
158 Value::Map(BTreeMap::from([
159 (
160 "version".into(),
161 Value::Int(PREPARED_MUTATION_CODEC_VERSION),
162 ),
163 ("table".into(), Value::Str(prepared.table)),
164 ("doc_id".into(), encode_prepared_doc_id(prepared.doc_id)),
165 (
166 "destination".into(),
167 prepared.destination.map_or(Value::Null, |(table, doc_id)| {
168 Value::Map(BTreeMap::from([
169 ("table".into(), Value::Str(table)),
170 ("doc_id".into(), encode_prepared_doc_id(doc_id)),
171 ]))
172 }),
173 ),
174 (
175 "moved_through".into(),
176 prepared.moved_through.map_or(Value::Null, Value::Str),
177 ),
178 (
179 "relocation".into(),
180 prepared
181 .relocation
182 .map_or(Value::Null, encode_prepared_doc_id),
183 ),
184 (
185 "partition_move_delete".into(),
186 prepared
187 .partition_move_delete
188 .map_or(Value::Null, |delete| {
189 encode_prepared_document_delete(*delete)
190 }),
191 ),
192 ("old".into(), Value::Map(prepared.old_document)),
193 ("new".into(), Value::Map(prepared.new_document)),
194 (
195 "capture_partition_move_update_transition".into(),
196 Value::Bool(prepared.capture_partition_move_update_transition),
197 ),
198 (
199 "referential_action".into(),
200 prepared.referential_action.map_or(Value::Null, |action| {
201 Value::Map(BTreeMap::from([
202 ("relation".into(), Value::Str(action.relation)),
203 (
204 "columns".into(),
205 Value::List(action.columns.into_iter().map(Value::Str).collect()),
206 ),
207 ]))
208 }),
209 ),
210 ]))
211}
212
213#[expect(clippy::too_many_lines, reason = "preserves DML lock and event order")]
214pub fn decode_prepared_document_rewrite(value: Value) -> Result<PreparedDocumentRewrite, SQLError> {
215 let Value::Map(mut fields) = value else {
216 return Err(SQLError::Internal(
217 "prepared rewrite spill payload is not a map".into(),
218 ));
219 };
220 take_codec_version(&mut fields, "prepared rewrite spill payload")?;
221 let table = match fields.remove("table") {
222 Some(Value::Str(table)) => table,
223 _ => {
224 return Err(SQLError::Internal(
225 "prepared rewrite spill payload has no table".into(),
226 ))
227 }
228 };
229 let doc_id = decode_prepared_doc_id(
230 fields.remove("doc_id").ok_or_else(|| {
231 SQLError::Internal("prepared rewrite spill payload has no document id".into())
232 })?,
233 "prepared rewrite spill payload",
234 )?;
235 let destination = match fields.remove("destination") {
236 Some(Value::Null) | None => None,
237 Some(Value::Map(mut destination)) => {
238 let table = match destination.remove("table") {
239 Some(Value::Str(table)) => table,
240 _ => {
241 return Err(SQLError::Internal(
242 "prepared rewrite destination has no table".into(),
243 ))
244 }
245 };
246 let doc_id = decode_prepared_doc_id(
247 destination.remove("doc_id").ok_or_else(|| {
248 SQLError::Internal("prepared rewrite destination has no document id".into())
249 })?,
250 "prepared rewrite destination",
251 )?;
252 reject_unknown_fields(&destination, "prepared rewrite destination")?;
253 Some((table, doc_id))
254 }
255 Some(_) => {
256 return Err(SQLError::Internal(
257 "prepared rewrite destination is not a map".into(),
258 ))
259 }
260 };
261 let moved_through = match fields.remove("moved_through") {
262 Some(Value::Null) => None,
263 Some(Value::Str(table)) => Some(table),
264 _ => {
265 return Err(SQLError::Internal(
266 "prepared rewrite spill payload has no moved-through relation".into(),
267 ))
268 }
269 };
270 let relocation = match fields.remove("relocation") {
271 Some(Value::Null) => None,
272 Some(doc_id) => Some(decode_prepared_doc_id(
273 doc_id,
274 "prepared rewrite relocation",
275 )?),
276 None => {
277 return Err(SQLError::Internal(
278 "prepared rewrite spill payload has no relocation".into(),
279 ))
280 }
281 };
282 let partition_move_delete = match fields.remove("partition_move_delete") {
283 Some(Value::Null) | None => None,
284 Some(delete) => Some(Box::new(decode_prepared_document_delete(delete)?)),
285 };
286 let old_document = match fields.remove("old") {
287 Some(Value::Map(document)) => document,
288 _ => {
289 return Err(SQLError::Internal(
290 "prepared rewrite spill payload has no old document".into(),
291 ))
292 }
293 };
294 let new_document = match fields.remove("new") {
295 Some(Value::Map(document)) => document,
296 _ => {
297 return Err(SQLError::Internal(
298 "prepared rewrite spill payload has no new document".into(),
299 ))
300 }
301 };
302 let capture_partition_move_update_transition = match fields
303 .remove("capture_partition_move_update_transition")
304 {
305 Some(Value::Bool(capture)) => capture,
306 _ => {
307 return Err(SQLError::Internal(
308 "prepared rewrite spill payload has no partition movement transition mode".into(),
309 ))
310 }
311 };
312 let referential_action = match fields.remove("referential_action") {
313 Some(Value::Null) => None,
314 Some(action) => Some(Box::new(decode_referential_rewrite(action)?)),
315 None => {
316 return Err(SQLError::Internal(
317 "prepared rewrite spill payload has no referential action".into(),
318 ))
319 }
320 };
321 reject_unknown_fields(&fields, "prepared rewrite spill payload")?;
322 Ok(PreparedDocumentRewrite {
323 table,
324 doc_id,
325 destination,
326 moved_through,
327 relocation,
328 partition_move_delete,
329 old_document,
330 new_document,
331 capture_partition_move_update_transition,
332 referential_action,
333 })
334}
335
336fn decode_referential_rewrite(value: Value) -> Result<ReferentialRewrite, SQLError> {
337 let Value::Map(mut fields) = value else {
338 return Err(SQLError::Internal(
339 "prepared rewrite referential action is not a map".into(),
340 ));
341 };
342 let relation = match fields.remove("relation") {
343 Some(Value::Str(relation)) => relation,
344 _ => {
345 return Err(SQLError::Internal(
346 "prepared rewrite referential action has no relation".into(),
347 ))
348 }
349 };
350 let columns = match fields.remove("columns") {
351 Some(Value::List(columns)) => columns
352 .into_iter()
353 .map(|column| match column {
354 Value::Str(column) => Ok(column),
355 _ => Err(SQLError::Internal(
356 "prepared rewrite referential action has a non-text column".into(),
357 )),
358 })
359 .collect::<Result<Vec<_>, _>>()?,
360 _ => {
361 return Err(SQLError::Internal(
362 "prepared rewrite referential action has no column list".into(),
363 ))
364 }
365 };
366 reject_unknown_fields(&fields, "prepared rewrite referential action")?;
367 Ok(ReferentialRewrite { relation, columns })
368}
369
370pub fn encode_prepared_document_delete(prepared: PreparedDocumentDelete) -> Value {
371 Value::Map(BTreeMap::from([
372 (
373 "version".into(),
374 Value::Int(PREPARED_MUTATION_CODEC_VERSION),
375 ),
376 ("table".into(), Value::Str(prepared.table)),
377 ("doc_id".into(), encode_prepared_doc_id(prepared.doc_id)),
378 ("document".into(), Value::Map(prepared.document)),
379 ]))
380}
381
382pub fn decode_prepared_document_delete(value: Value) -> Result<PreparedDocumentDelete, SQLError> {
383 let Value::Map(mut fields) = value else {
384 return Err(SQLError::Internal(
385 "prepared delete spill payload is not a map".into(),
386 ));
387 };
388 take_codec_version(&mut fields, "prepared delete spill payload")?;
389 let table = match fields.remove("table") {
390 Some(Value::Str(table)) => table,
391 _ => {
392 return Err(SQLError::Internal(
393 "prepared delete spill payload has no table".into(),
394 ))
395 }
396 };
397 let doc_id = decode_prepared_doc_id(
398 fields.remove("doc_id").ok_or_else(|| {
399 SQLError::Internal("prepared delete spill payload has no document id".into())
400 })?,
401 "prepared delete spill payload",
402 )?;
403 let document = match fields.remove("document") {
404 Some(Value::Map(document)) => document,
405 _ => {
406 return Err(SQLError::Internal(
407 "prepared delete spill payload has no document".into(),
408 ))
409 }
410 };
411 reject_unknown_fields(&fields, "prepared delete spill payload")?;
412 Ok(PreparedDocumentDelete {
413 table,
414 doc_id,
415 document,
416 })
417}
418
419pub fn encode_prepared_insert_conflict(prepared: PreparedInsertConflict) -> Value {
420 let mut fields = BTreeMap::from([(
421 "version".into(),
422 Value::Int(PREPARED_MUTATION_CODEC_VERSION),
423 )]);
424 match prepared {
425 PreparedInsertConflict::Unresolved => {
426 fields.insert("kind".into(), Value::Str("unresolved".into()));
427 }
428 PreparedInsertConflict::Insert { doc_id, supplied } => {
429 fields.insert("kind".into(), Value::Str("insert".into()));
430 fields.insert("doc_id".into(), encode_prepared_doc_id(doc_id));
431 fields.insert("supplied".into(), Value::Bool(supplied));
432 }
433 PreparedInsertConflict::Skip => {
434 fields.insert("kind".into(), Value::Str("skip".into()));
435 }
436 PreparedInsertConflict::Updated(rewrite) => {
437 fields.insert("kind".into(), Value::Str("updated".into()));
438 fields.insert("rewrite".into(), encode_prepared_document_rewrite(rewrite));
439 }
440 }
441 Value::Map(fields)
442}
443
444pub fn decode_prepared_insert_conflict(value: Value) -> Result<PreparedInsertConflict, SQLError> {
445 let Value::Map(mut fields) = value else {
446 return Err(SQLError::Internal(
447 "prepared INSERT conflict payload is not a map".into(),
448 ));
449 };
450 take_codec_version(&mut fields, "prepared INSERT conflict payload")?;
451 let kind = match fields.remove("kind") {
452 Some(Value::Str(kind)) => kind,
453 _ => {
454 return Err(SQLError::Internal(
455 "prepared INSERT conflict payload has no kind".into(),
456 ))
457 }
458 };
459 let prepared = match kind.as_str() {
460 "unresolved" => PreparedInsertConflict::Unresolved,
461 "insert" => PreparedInsertConflict::Insert {
462 doc_id: decode_prepared_doc_id(
463 fields.remove("doc_id").ok_or_else(|| {
464 SQLError::Internal("prepared INSERT payload has no document identity".into())
465 })?,
466 "prepared INSERT action",
467 )?,
468 supplied: match fields.remove("supplied") {
469 Some(Value::Bool(supplied)) => supplied,
470 _ => {
471 return Err(SQLError::Internal(
472 "prepared INSERT payload has no supplied-id flag".into(),
473 ))
474 }
475 },
476 },
477 "skip" => PreparedInsertConflict::Skip,
478 "updated" => PreparedInsertConflict::Updated(decode_prepared_document_rewrite(
479 fields.remove("rewrite").ok_or_else(|| {
480 SQLError::Internal("prepared INSERT conflict payload has no rewrite plan".into())
481 })?,
482 )?),
483 _ => {
484 return Err(SQLError::Internal(format!(
485 "prepared INSERT conflict payload has unknown kind `{kind}`"
486 )))
487 }
488 };
489 reject_unknown_fields(&fields, "prepared INSERT conflict payload")?;
490 Ok(prepared)
491}
492
493pub fn encode_prepared_mutation_action(action: PreparedMutationAction) -> Value {
494 let (kind, plan) = match action {
495 PreparedMutationAction::Insert(insert) => (
496 "insert",
497 Value::Map(BTreeMap::from([
498 ("table".into(), Value::Str(insert.table)),
499 ("doc_id".into(), encode_prepared_doc_id(insert.doc_id)),
500 ("document".into(), Value::Map(insert.document)),
501 ])),
502 ),
503 PreparedMutationAction::Rewrite(rewrite) => {
504 ("rewrite", encode_prepared_document_rewrite(rewrite))
505 }
506 PreparedMutationAction::Delete(delete) => {
507 ("delete", encode_prepared_document_delete(delete))
508 }
509 };
510 Value::Map(BTreeMap::from([
511 (
512 "version".into(),
513 Value::Int(PREPARED_MUTATION_CODEC_VERSION),
514 ),
515 ("kind".into(), Value::Str(kind.into())),
516 ("plan".into(), plan),
517 ]))
518}
519
520pub fn decode_prepared_mutation_action(value: Value) -> Result<PreparedMutationAction, SQLError> {
521 let Value::Map(mut fields) = value else {
522 return Err(SQLError::Internal(
523 "prepared mutation action payload is not a map".into(),
524 ));
525 };
526 take_codec_version(&mut fields, "prepared mutation action payload")?;
527 let kind = match fields.remove("kind") {
528 Some(Value::Str(kind)) => kind,
529 _ => {
530 return Err(SQLError::Internal(
531 "prepared mutation action payload has no kind".into(),
532 ))
533 }
534 };
535 let plan = fields
536 .remove("plan")
537 .ok_or_else(|| SQLError::Internal("prepared mutation action payload has no plan".into()))?;
538 reject_unknown_fields(&fields, "prepared mutation action payload")?;
539 match kind.as_str() {
540 "insert" => {
541 let Value::Map(mut fields) = plan else {
542 return Err(SQLError::Internal(
543 "prepared insert action plan is not a map".into(),
544 ));
545 };
546 let table = match fields.remove("table") {
547 Some(Value::Str(table)) => table,
548 _ => {
549 return Err(SQLError::Internal(
550 "prepared insert action plan has no table".into(),
551 ))
552 }
553 };
554 let doc_id = decode_prepared_doc_id(
555 fields.remove("doc_id").ok_or_else(|| {
556 SQLError::Internal("prepared insert action plan has no document id".into())
557 })?,
558 "prepared insert action plan",
559 )?;
560 let document = match fields.remove("document") {
561 Some(Value::Map(document)) => document,
562 _ => {
563 return Err(SQLError::Internal(
564 "prepared insert action plan has no document".into(),
565 ))
566 }
567 };
568 reject_unknown_fields(&fields, "prepared insert action plan")?;
569 Ok(PreparedMutationAction::Insert(PreparedDocumentInsert {
570 table,
571 doc_id,
572 document,
573 }))
574 }
575 "rewrite" => Ok(PreparedMutationAction::Rewrite(
576 decode_prepared_document_rewrite(plan)?,
577 )),
578 "delete" => Ok(PreparedMutationAction::Delete(
579 decode_prepared_document_delete(plan)?,
580 )),
581 _ => Err(SQLError::Internal(format!(
582 "prepared mutation action payload has unknown kind `{kind}`"
583 ))),
584 }
585}
586
587#[cfg(test)]
588mod tests {
589 use super::*;
590
591 fn document(key: &str, value: i64) -> Document {
592 BTreeMap::from([(key.to_string(), Value::Int(value))])
593 }
594
595 fn rewrite() -> PreparedDocumentRewrite {
596 PreparedDocumentRewrite {
597 table: "public.source".into(),
598 doc_id: 7,
599 destination: Some(("public.destination".into(), 9)),
600 moved_through: Some("public.root".into()),
601 relocation: None,
602 partition_move_delete: Some(Box::new(PreparedDocumentDelete {
603 table: "public.source".into(),
604 doc_id: 7,
605 document: document("id", 7),
606 })),
607 old_document: document("value", 1),
608 new_document: document("value", 2),
609 capture_partition_move_update_transition: true,
610 referential_action: None,
611 }
612 }
613
614 fn referential_rewrite() -> PreparedDocumentRewrite {
615 PreparedDocumentRewrite {
616 table: "public.child".into(),
617 doc_id: 11,
618 destination: None,
619 moved_through: None,
620 relocation: Some(1 << 62),
621 partition_move_delete: None,
622 old_document: document("parent", 7),
623 new_document: document("parent", 9),
624 capture_partition_move_update_transition: false,
625 referential_action: Some(Box::new(ReferentialRewrite {
626 relation: "public.child".into(),
627 columns: vec!["parent".into(), "status".into()],
628 })),
629 }
630 }
631
632 #[test]
633 fn prepared_rewrite_and_delete_codec_round_trip() {
634 for expected in [rewrite(), referential_rewrite()] {
635 let actual = decode_prepared_document_rewrite(encode_prepared_document_rewrite(
636 expected.clone(),
637 ))
638 .unwrap();
639 assert_eq!(actual, expected);
640 }
641 let expected_delete = PreparedDocumentDelete {
642 table: "public.parent".into(),
643 doc_id: 3,
644 document: document("id", 3),
645 };
646 let actual_delete = decode_prepared_document_delete(encode_prepared_document_delete(
647 expected_delete.clone(),
648 ))
649 .unwrap();
650 assert_eq!(actual_delete, expected_delete);
651 }
652
653 #[test]
654 fn prepared_codec_rejects_version_width_and_unknown_fields() {
655 for version in [
656 PREPARED_MUTATION_CODEC_VERSION - 1,
657 PREPARED_MUTATION_CODEC_VERSION + 1,
658 ] {
659 let mut wrong_version = match encode_prepared_document_rewrite(rewrite()) {
660 Value::Map(fields) => fields,
661 _ => unreachable!(),
662 };
663 wrong_version.insert("version".into(), Value::Int(version));
664 assert!(decode_prepared_document_rewrite(Value::Map(wrong_version)).is_err());
665 }
666
667 assert!(decode_prepared_doc_id(Value::Bytes(vec![0; 3]), "test identity").is_err());
668
669 let mut unknown_field = match encode_prepared_document_delete(PreparedDocumentDelete {
670 table: "public.items".into(),
671 doc_id: 1,
672 document: document("id", 1),
673 }) {
674 Value::Map(fields) => fields,
675 _ => unreachable!(),
676 };
677 unknown_field.insert("unexpected".into(), Value::Null);
678 assert!(decode_prepared_document_delete(Value::Map(unknown_field)).is_err());
679
680 for invalid in [
681 Value::Int(1),
682 Value::Map(BTreeMap::from([(
683 "columns".into(),
684 Value::List(vec![Value::Str("parent".into())]),
685 )])),
686 Value::Map(BTreeMap::from([
687 ("relation".into(), Value::Str("public.child".into())),
688 ("columns".into(), Value::List(vec![Value::Int(1)])),
689 ])),
690 ] {
691 let mut invalid_action = match encode_prepared_document_rewrite(referential_rewrite()) {
692 Value::Map(fields) => fields,
693 _ => unreachable!(),
694 };
695 invalid_action.insert("referential_action".into(), invalid);
696 assert!(decode_prepared_document_rewrite(Value::Map(invalid_action)).is_err());
697 }
698 }
699
700 #[test]
701 fn prepared_insert_conflict_codec_round_trips_every_action() {
702 for expected in [
703 PreparedInsertConflict::Unresolved,
704 PreparedInsertConflict::Insert {
705 doc_id: 23,
706 supplied: true,
707 },
708 PreparedInsertConflict::Skip,
709 PreparedInsertConflict::Updated(rewrite()),
710 ] {
711 let actual =
712 decode_prepared_insert_conflict(encode_prepared_insert_conflict(expected.clone()))
713 .unwrap();
714 assert_eq!(actual, expected);
715 }
716 }
717
718 #[test]
719 fn prepared_insert_conflict_codec_rejects_malformed_payloads() {
720 assert!(decode_prepared_insert_conflict(Value::Str("skip".into())).is_err());
721
722 let mut missing_width =
723 match encode_prepared_insert_conflict(PreparedInsertConflict::Insert {
724 doc_id: 23,
725 supplied: false,
726 }) {
727 Value::Map(fields) => fields,
728 _ => unreachable!(),
729 };
730 missing_width.insert("doc_id".into(), Value::Bytes(vec![0; 3]));
731 assert!(decode_prepared_insert_conflict(Value::Map(missing_width)).is_err());
732
733 let mut unknown_field = match encode_prepared_insert_conflict(PreparedInsertConflict::Skip)
734 {
735 Value::Map(fields) => fields,
736 _ => unreachable!(),
737 };
738 unknown_field.insert("unexpected".into(), Value::Null);
739 assert!(decode_prepared_insert_conflict(Value::Map(unknown_field)).is_err());
740 }
741
742 #[test]
743 fn prepared_mutation_action_codec_round_trips_every_action() {
744 for expected in [
745 PreparedMutationAction::Insert(PreparedDocumentInsert {
746 table: "public.items".into(),
747 doc_id: 29,
748 document: document("id", 29),
749 }),
750 PreparedMutationAction::Rewrite(rewrite()),
751 PreparedMutationAction::Delete(PreparedDocumentDelete {
752 table: "public.items".into(),
753 doc_id: 31,
754 document: document("id", 31),
755 }),
756 ] {
757 let actual =
758 decode_prepared_mutation_action(encode_prepared_mutation_action(expected.clone()))
759 .unwrap();
760 assert_eq!(actual, expected);
761 }
762 }
763}