sqlite-diff-rs 0.1.0

Build SQLite changeset and patchset binary formats programmatically, without SQLite
Documentation
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Bit-for-bit parity tests between our library and rusqlite's session extension.
//!
//! These tests verify that our changeset/patchset binary output is **byte-identical**
//! to what `SQLite`'s session extension produces for the same sequence of operations.
//!
//! Previous tests only compared at the semantic level (via `ParsedDiffSet::PartialEq`),
//! which uses `HashMap` comparison and thus ignores:
//! - Table ordering differences
//! - Row ordering differences within a table
//!
//! This file catches both classes of bug by comparing raw `Vec<u8>` output.
#![cfg(feature = "testing")]

use sqlite_diff_rs::testing::{assert_bit_parity, assert_patchset_sql_parity};
use sqlite_diff_rs::{
    ChangeDelete, ChangeSet, ChangesetFormat, DiffOps, Insert, PatchDelete, PatchSet,
    PatchsetFormat, SimpleTable, Update, Value,
};

// =============================================================================
// Single-table, single-operation tests
// =============================================================================

#[test]
fn bit_parity_single_insert() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    assert_patchset_sql_parity(
        &[users],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
        ],
    );
}

#[test]
fn bit_parity_single_insert_integer_only() {
    let nums = SimpleTable::new("nums", &["id", "val"], &[0]);
    assert_patchset_sql_parity(
        &[nums],
        &[
            "CREATE TABLE nums (id INTEGER PRIMARY KEY, val INTEGER)",
            "INSERT INTO nums (id, val) VALUES (1, 42)",
        ],
    );
}

#[test]
fn bit_parity_single_insert_with_null() {
    let items = SimpleTable::new("items", &["id", "description", "price"], &[0]);
    assert_patchset_sql_parity(
        &[items],
        &[
            "CREATE TABLE items (id INTEGER PRIMARY KEY, description TEXT, price REAL)",
            "INSERT INTO items (id, description, price) VALUES (1, NULL, 9.99)",
        ],
    );
}

#[test]
fn bit_parity_single_insert_with_real() {
    let measurements = SimpleTable::new("measurements", &["id", "value"], &[0]);
    assert_patchset_sql_parity(
        &[measurements],
        &[
            "CREATE TABLE measurements (id INTEGER PRIMARY KEY, value REAL)",
            "INSERT INTO measurements (id, value) VALUES (1, 3.14)",
        ],
    );
}

#[test]
fn bit_parity_single_insert_empty_string() {
    let strings = SimpleTable::new("strings", &["id", "value"], &[0]);
    assert_patchset_sql_parity(
        &[strings],
        &[
            "CREATE TABLE strings (id INTEGER PRIMARY KEY, value TEXT)",
            "INSERT INTO strings (id, value) VALUES (1, '')",
        ],
    );
}

// =============================================================================
// Multiple rows in a single table (tests row ordering)
// =============================================================================

#[test]
fn bit_parity_two_inserts_same_table() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    assert_patchset_sql_parity(
        &[users],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "INSERT INTO users (id, name) VALUES (2, 'Bob')",
        ],
    );
}

#[test]
fn bit_parity_three_inserts_same_table() {
    let users = SimpleTable::new("users", &["id", "name", "age"], &[0]);
    assert_patchset_sql_parity(
        &[users],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",
            "INSERT INTO users (id, name, age) VALUES (1, 'Alice', 30)",
            "INSERT INTO users (id, name, age) VALUES (2, 'Bob', 25)",
            "INSERT INTO users (id, name, age) VALUES (3, 'Charlie', 35)",
        ],
    );
}

#[test]
fn bit_parity_insert_then_update() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    assert_patchset_sql_parity(
        &[users],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "UPDATE users SET name = 'Alicia' WHERE id = 1",
        ],
    );
}

#[test]
fn bit_parity_insert_then_delete_cancel() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    assert_patchset_sql_parity(
        &[users],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "DELETE FROM users WHERE id = 1",
        ],
    );
}

#[test]
fn bit_parity_two_inserts_one_deleted() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    assert_patchset_sql_parity(
        &[users],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "INSERT INTO users (id, name) VALUES (2, 'Bob')",
            "DELETE FROM users WHERE id = 1",
        ],
    );
}

// =============================================================================
// Multi-table tests (tests table ordering)
// =============================================================================

#[test]
fn bit_parity_two_tables() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    let posts = SimpleTable::new("posts", &["id", "title"], &[0]);
    assert_patchset_sql_parity(
        &[users, posts],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "INSERT INTO posts (id, title) VALUES (1, 'Hello')",
        ],
    );
}

#[test]
fn bit_parity_two_tables_reverse_order() {
    // Insert into posts first, then users
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    let posts = SimpleTable::new("posts", &["id", "title"], &[0]);
    assert_patchset_sql_parity(
        &[users, posts],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT)",
            "INSERT INTO posts (id, title) VALUES (1, 'Hello')",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
        ],
    );
}

#[test]
fn bit_parity_three_tables() {
    let alpha = SimpleTable::new("alpha", &["id", "val"], &[0]);
    let beta = SimpleTable::new("beta", &["id", "val"], &[0]);
    let gamma = SimpleTable::new("gamma", &["id", "val"], &[0]);
    assert_patchset_sql_parity(
        &[alpha, beta, gamma],
        &[
            "CREATE TABLE alpha (id INTEGER PRIMARY KEY, val TEXT)",
            "CREATE TABLE beta (id INTEGER PRIMARY KEY, val TEXT)",
            "CREATE TABLE gamma (id INTEGER PRIMARY KEY, val TEXT)",
            "INSERT INTO alpha (id, val) VALUES (1, 'a')",
            "INSERT INTO beta (id, val) VALUES (1, 'b')",
            "INSERT INTO gamma (id, val) VALUES (1, 'c')",
        ],
    );
}

#[test]
fn bit_parity_table_cancel_and_readd() {
    let table_a = SimpleTable::new("table_a", &["id", "val"], &[0]);
    let table_b = SimpleTable::new("table_b", &["id", "val"], &[0]);
    assert_patchset_sql_parity(
        &[table_a, table_b],
        &[
            "CREATE TABLE table_a (id INTEGER PRIMARY KEY, val TEXT)",
            "CREATE TABLE table_b (id INTEGER PRIMARY KEY, val TEXT)",
            "INSERT INTO table_a (id, val) VALUES (1, 'a1')",
            "INSERT INTO table_b (id, val) VALUES (1, 'b1')",
            "DELETE FROM table_a WHERE id = 1",
            "INSERT INTO table_a (id, val) VALUES (2, 'a2')",
        ],
    );
}

#[test]
fn bit_parity_cancelled_table_excluded() {
    let users = SimpleTable::new("users", &["id", "name"], &[0]);
    let posts = SimpleTable::new("posts", &["id", "title"], &[0]);
    assert_patchset_sql_parity(
        &[users, posts],
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "DELETE FROM users WHERE id = 1",
            "INSERT INTO posts (id, title) VALUES (1, 'Hello')",
        ],
    );
}

// =============================================================================
// Composite primary keys
// =============================================================================

#[test]
fn bit_parity_composite_pk() {
    let order_items =
        SimpleTable::new("order_items", &["order_id", "item_id", "quantity"], &[0, 1]);
    assert_patchset_sql_parity(
        &[order_items],
        &[
            "CREATE TABLE order_items (order_id INTEGER, item_id INTEGER, quantity INTEGER, PRIMARY KEY (order_id, item_id))",
            "INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 100, 5)",
            "INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 101, 3)",
        ],
    );
}

// =============================================================================
// Builder API parity (not going through FromStr)
// =============================================================================

#[test]
fn bit_parity_builder_single_insert() {
    let schema = SimpleTable::new("users", &["id", "name"], &[0]);

    let changeset: ChangeSet<SimpleTable, String, Vec<u8>> = ChangeSet::new().insert(
        Insert::<_, String, Vec<u8>>::from(schema.clone())
            .set(0, 1i64)
            .unwrap()
            .set(1, "Alice")
            .unwrap(),
    );
    let our_changeset: Vec<u8> = changeset.build();

    let patchset: PatchSet<SimpleTable, String, Vec<u8>> = PatchSet::new().insert(
        Insert::<_, String, Vec<u8>>::from(schema)
            .set(0, 1i64)
            .unwrap()
            .set(1, "Alice")
            .unwrap(),
    );
    let our_patchset: Vec<u8> = patchset.build();

    assert_bit_parity(
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
        ],
        &our_changeset,
        &our_patchset,
    );
}

#[test]
fn bit_parity_builder_two_inserts() {
    let schema = SimpleTable::new("users", &["id", "name"], &[0]);

    let changeset: ChangeSet<SimpleTable, String, Vec<u8>> = ChangeSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alice")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema.clone())
                .set(0, 2i64)
                .unwrap()
                .set(1, "Bob")
                .unwrap(),
        );
    let our_changeset: Vec<u8> = changeset.build();

    let patchset: PatchSet<SimpleTable, String, Vec<u8>> = PatchSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alice")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema)
                .set(0, 2i64)
                .unwrap()
                .set(1, "Bob")
                .unwrap(),
        );
    let our_patchset: Vec<u8> = patchset.build();

    assert_bit_parity(
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "INSERT INTO users (id, name) VALUES (2, 'Bob')",
        ],
        &our_changeset,
        &our_patchset,
    );
}

#[test]
fn bit_parity_builder_insert_then_update() {
    let schema = SimpleTable::new("users", &["id", "name"], &[0]);

    let changeset: ChangeSet<SimpleTable, String, Vec<u8>> = ChangeSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alice")
                .unwrap(),
        )
        .update(
            Update::<SimpleTable, ChangesetFormat, String, Vec<u8>>::from(schema.clone())
                .set(0, 1i64, 1i64)
                .unwrap()
                .set(1, "Alice", "Alicia")
                .unwrap(),
        );
    let our_changeset: Vec<u8> = changeset.build();

    let patchset: PatchSet<SimpleTable, String, Vec<u8>> = PatchSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alice")
                .unwrap(),
        )
        .update(
            Update::<SimpleTable, PatchsetFormat, String, Vec<u8>>::from(schema)
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alicia")
                .unwrap(),
        );
    let our_patchset: Vec<u8> = patchset.build();

    assert_bit_parity(
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "UPDATE users SET name = 'Alicia' WHERE id = 1",
        ],
        &our_changeset,
        &our_patchset,
    );
}

#[test]
fn bit_parity_builder_two_tables() {
    let schema_u = SimpleTable::new("users", &["id", "name"], &[0]);
    let schema_p = SimpleTable::new("posts", &["id", "title"], &[0]);

    let changeset: ChangeSet<SimpleTable, String, Vec<u8>> = ChangeSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_u.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alice")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_p.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "Hello")
                .unwrap(),
        );
    let our_changeset: Vec<u8> = changeset.build();

    let patchset: PatchSet<SimpleTable, String, Vec<u8>> = PatchSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_u)
                .set(0, 1i64)
                .unwrap()
                .set(1, "Alice")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_p)
                .set(0, 1i64)
                .unwrap()
                .set(1, "Hello")
                .unwrap(),
        );
    let our_patchset: Vec<u8> = patchset.build();

    assert_bit_parity(
        &[
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT)",
            "INSERT INTO users (id, name) VALUES (1, 'Alice')",
            "INSERT INTO posts (id, title) VALUES (1, 'Hello')",
        ],
        &our_changeset,
        &our_patchset,
    );
}

#[test]
fn bit_parity_builder_table_cancel_and_readd() {
    let schema_a = SimpleTable::new("table_a", &["id", "val"], &[0]);
    let schema_b = SimpleTable::new("table_b", &["id", "val"], &[0]);

    // Changeset
    let changeset: ChangeSet<SimpleTable, String, Vec<u8>> = ChangeSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_a.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "a1")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_b.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "b1")
                .unwrap(),
        )
        .delete(
            ChangeDelete::<_, String, Vec<u8>>::from(schema_a.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "a1")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_a.clone())
                .set(0, 2i64)
                .unwrap()
                .set(1, "a2")
                .unwrap(),
        );
    let our_changeset: Vec<u8> = changeset.build();

    // Patchset (delete uses PK only)
    let patchset: PatchSet<SimpleTable, String, Vec<u8>> = PatchSet::new()
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_a.clone())
                .set(0, 1i64)
                .unwrap()
                .set(1, "a1")
                .unwrap(),
        )
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_b)
                .set(0, 1i64)
                .unwrap()
                .set(1, "b1")
                .unwrap(),
        )
        .delete(PatchDelete::new(schema_a.clone(), vec![Value::Integer(1)]))
        .insert(
            Insert::<_, String, Vec<u8>>::from(schema_a)
                .set(0, 2i64)
                .unwrap()
                .set(1, "a2")
                .unwrap(),
        );
    let our_patchset: Vec<u8> = patchset.build();

    assert_bit_parity(
        &[
            "CREATE TABLE table_a (id INTEGER PRIMARY KEY, val TEXT)",
            "CREATE TABLE table_b (id INTEGER PRIMARY KEY, val TEXT)",
            "INSERT INTO table_a (id, val) VALUES (1, 'a1')",
            "INSERT INTO table_b (id, val) VALUES (1, 'b1')",
            "DELETE FROM table_a WHERE id = 1",
            "INSERT INTO table_a (id, val) VALUES (2, 'a2')",
        ],
        &our_changeset,
        &our_patchset,
    );
}

// =============================================================================
// Data type edge cases
// =============================================================================

#[test]
fn bit_parity_integer_boundaries() {
    let numbers = SimpleTable::new("numbers", &["id", "value"], &[0]);
    assert_patchset_sql_parity(
        &[numbers],
        &[
            "CREATE TABLE numbers (id INTEGER PRIMARY KEY, value INTEGER)",
            "INSERT INTO numbers (id, value) VALUES (1, 0)",
            "INSERT INTO numbers (id, value) VALUES (2, -1)",
            "INSERT INTO numbers (id, value) VALUES (3, 127)",
            "INSERT INTO numbers (id, value) VALUES (4, 128)",
            "INSERT INTO numbers (id, value) VALUES (5, 9223372036854775807)",
            "INSERT INTO numbers (id, value) VALUES (6, -9223372036854775808)",
        ],
    );
}

#[test]
fn bit_parity_float_values() {
    let floats = SimpleTable::new("floats", &["id", "value"], &[0]);
    assert_patchset_sql_parity(
        &[floats],
        &[
            "CREATE TABLE floats (id INTEGER PRIMARY KEY, value REAL)",
            "INSERT INTO floats (id, value) VALUES (1, 0.0)",
            "INSERT INTO floats (id, value) VALUES (2, 3.14159265358979)",
            "INSERT INTO floats (id, value) VALUES (3, -273.15)",
        ],
    );
}

#[test]
fn bit_parity_unicode_text() {
    let strings = SimpleTable::new("strings", &["id", "value"], &[0]);
    assert_patchset_sql_parity(
        &[strings],
        &[
            "CREATE TABLE strings (id INTEGER PRIMARY KEY, value TEXT)",
            "INSERT INTO strings (id, value) VALUES (1, '日本語')",
            "INSERT INTO strings (id, value) VALUES (2, 'Ñoño')",
        ],
    );
}

#[test]
fn bit_parity_blob_value() {
    let blobs = SimpleTable::new("blobs", &["id", "data"], &[0]);
    assert_patchset_sql_parity(
        &[blobs],
        &[
            "CREATE TABLE blobs (id INTEGER PRIMARY KEY, data BLOB)",
            "INSERT INTO blobs (id, data) VALUES (1, X'DEADBEEF')",
        ],
    );
}

#[test]
fn bit_parity_all_nulls() {
    let items = SimpleTable::new("items", &["id", "a", "b", "c"], &[0]);
    assert_patchset_sql_parity(
        &[items],
        &[
            "CREATE TABLE items (id INTEGER PRIMARY KEY, a TEXT, b REAL, c INTEGER)",
            "INSERT INTO items (id, a, b, c) VALUES (1, NULL, NULL, NULL)",
        ],
    );
}