-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange.rs
More file actions
49 lines (44 loc) · 1.33 KB
/
change.rs
File metadata and controls
49 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use serde_json::Value;
use crate::queue::{ChangeOperation, NewChange};
#[derive(Debug, Clone, PartialEq)]
pub struct RowChange {
pub table_name: String,
pub operation: ChangeOperation,
pub primary_key: String,
pub payload: Option<Value>,
pub wal_frame: Option<String>,
pub cursor: Option<String>,
}
impl RowChange {
pub fn into_new_change(self) -> NewChange {
let payload = self
.payload
.map(|value| serde_json::to_vec(&value).expect("row change payload serializes"));
NewChange {
table_name: self.table_name,
operation: self.operation,
primary_key: self.primary_key,
payload,
wal_frame: self.wal_frame,
cursor: self.cursor,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn converts_to_new_change() {
let row = RowChange {
table_name: "prices".into(),
operation: ChangeOperation::Update,
primary_key: "pk1".into(),
payload: Some(serde_json::json!({"foo": "bar"})),
wal_frame: Some("frame-1".into()),
cursor: Some("cursor".into()),
};
let change = row.into_new_change();
assert_eq!(change.table_name, "prices");
assert!(change.payload.unwrap().contains(&b'b'));
}
}