Skip to content

Commit cf94c6f

Browse files
committed
Adopt new vtab for deletes
1 parent b28bb39 commit cf94c6f

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

crates/core/src/crud_vtab.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const SIMPLE_NAME: &CStr = c"powersync_crud";
2323

2424
// Structure:
2525
// CREATE TABLE powersync_crud_(data TEXT, options INT HIDDEN);
26-
// CREATE TABLE powersync_crud(op TEXT, id TEXT, data TEXT old_values TEXT, metadata TEXT);
26+
// CREATE TABLE powersync_crud(op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT);
2727
//
2828
// This is a insert-only virtual table. It generates transaction ids in ps_tx, and inserts data in
2929
// ps_crud(tx_id, data).
@@ -110,8 +110,11 @@ impl VirtualTable {
110110
id: &'a str,
111111
#[serde(rename = "type")]
112112
row_type: &'a str,
113+
#[serde(skip_serializing_if = "Option::is_none")]
113114
data: Option<&'a RawValue>,
115+
#[serde(skip_serializing_if = "Option::is_none")]
114116
old: Option<&'a RawValue>,
117+
#[serde(skip_serializing_if = "Option::is_none")]
115118
metadata: Option<&'a str>,
116119
}
117120

crates/core/src/views.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,23 @@ fn powersync_trigger_delete_sql_impl(
8484
let trigger_name = quote_identifier_prefixed("ps_view_delete_", view_name);
8585
let type_string = quote_string(name);
8686

87-
let old_fragment: Cow<'static, str> = match &table_info.diff_include_old {
88-
Some(include_old) => {
89-
let json = match include_old {
90-
DiffIncludeOld::OnlyForColumns { columns } => {
91-
json_object_fragment("OLD", &mut columns.iter().map(|c| c.as_str()))
92-
}
93-
DiffIncludeOld::ForAllColumns => {
94-
json_object_fragment("OLD", &mut table_info.column_names())
95-
}
96-
}?;
97-
98-
format!(", 'old', {json}").into()
99-
}
100-
None => "".into(),
101-
};
87+
let (old_data_name, old_data_value): (&'static str, Cow<'static, str>) =
88+
match &table_info.diff_include_old {
89+
Some(include_old) => {
90+
let mut json = match include_old {
91+
DiffIncludeOld::OnlyForColumns { columns } => {
92+
json_object_fragment("OLD", &mut columns.iter().map(|c| c.as_str()))
93+
}
94+
DiffIncludeOld::ForAllColumns => {
95+
json_object_fragment("OLD", &mut table_info.column_names())
96+
}
97+
}?;
98+
99+
json.insert(0, ',');
100+
(",old_values", json.into())
101+
}
102+
None => ("", "".into()),
103+
};
102104

103105
return if !local_only && !insert_only {
104106
let mut trigger = format!(
@@ -108,9 +110,7 @@ INSTEAD OF DELETE ON {quoted_name}
108110
FOR EACH ROW
109111
BEGIN
110112
DELETE FROM {internal_name} WHERE id = OLD.id;
111-
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'DELETE', 'type', {type_string}, 'id', OLD.id{old_fragment}));
112-
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES({type_string}, OLD.id);
113-
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, {MAX_OP_ID});
113+
INSERT INTO powersync_crud(op,id,type{old_data_name}) VALUES ('DELETE',OLD.id,{type_string}{old_data_value});
114114
END"
115115
);
116116

@@ -126,9 +126,7 @@ FOR EACH ROW
126126
WHEN NEW._deleted IS TRUE
127127
BEGIN
128128
DELETE FROM {internal_name} WHERE id = NEW.id;
129-
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'DELETE', 'type', {type_string}, 'id', NEW.id{old_fragment}, 'metadata', NEW._metadata));
130-
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES({type_string}, NEW.id);
131-
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, {MAX_OP_ID});
129+
INSERT INTO powersync_crud(op,id,type,metadata{old_data_name}) VALUES ('DELETE',OLD.id,{type_string},NEW._metadata{old_data_value});
132130
END"
133131
).expect("writing to string should be infallible");
134132
}

dart/test/crud_test.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void main() {
248248
'id': 1,
249249
'tx_id': 1,
250250
'data':
251-
'{"op":"PUT","id":"foo","type":"users","data":{"my":"value"},"old":null,"metadata":null}',
251+
'{"op":"PUT","id":"foo","type":"users","data":{"my":"value"}}',
252252
});
253253
});
254254

@@ -285,8 +285,7 @@ void main() {
285285
expect(row, {
286286
'id': 1,
287287
'tx_id': 1,
288-
'data':
289-
'{"op":"DELETE","id":"foo","type":"users","data":null,"old":null,"metadata":null}',
288+
'data': '{"op":"DELETE","id":"foo","type":"users"}',
290289
});
291290
});
292291

@@ -300,7 +299,7 @@ void main() {
300299
'id': 1,
301300
'tx_id': 1,
302301
'data':
303-
'{"op":"DELETE","id":"foo","type":"users","data":null,"old":null,"metadata":"my metadata"}',
302+
'{"op":"DELETE","id":"foo","type":"users","metadata":"my metadata"}',
304303
});
305304
});
306305

@@ -320,7 +319,7 @@ void main() {
320319
'id': 1,
321320
'tx_id': 1,
322321
'data':
323-
'{"op":"PUT","id":"foo","type":"users","data":{"my":"value"},"old":{"previous":"value"},"metadata":null}',
322+
'{"op":"PUT","id":"foo","type":"users","data":{"my":"value"},"old":{"previous":"value"}}',
324323
});
325324
});
326325
});

0 commit comments

Comments
 (0)