day-persistence 0.2.0

SQLite persistence for the Day observable model.
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
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The container against the Recorder: the change-log fold as SQL, asserted headlessly with a
//! hand-written `Model` impl (what `#[derive(Model)]` will emit).

use day_macros::Observable;
use day_model::{Keyed, Op};
use day_persistence::{
    ColumnDef, ColumnValue, DbError, DbErrorKind, MigrationPlan, Model, ModelContainer, Recorder,
    Row, SqlType, Value, schema,
};
use day_reactive::Binding;

#[derive(Observable, Clone, Default, PartialEq, Debug)]
struct Note {
    #[obs(key)]
    id: u32,
    title: String,
    body: String,
    pinned: bool,
}

impl Model for Note {
    const TABLE: &'static str = "notes";
    const KEY: &'static str = "id";
    const COLUMNS: &'static [ColumnDef] = &[
        ColumnDef {
            name: "id",
            field: "id",
            sql: SqlType::Integer,
            not_null: true,
            unique: false,
            indexed: false,
        },
        ColumnDef {
            name: "title",
            field: "title",
            sql: SqlType::Text,
            not_null: true,
            unique: false,
            indexed: false,
        },
        ColumnDef {
            name: "body",
            field: "body",
            sql: SqlType::Text,
            not_null: true,
            unique: false,
            indexed: false,
        },
        ColumnDef {
            name: "pinned",
            field: "pinned",
            sql: SqlType::Integer,
            not_null: true,
            unique: false,
            indexed: false,
        },
    ];
    fn to_row(&self) -> Vec<Value> {
        vec![
            self.id.to_sqlite_value(),
            self.title.to_sqlite_value(),
            self.body.to_sqlite_value(),
            self.pinned.to_sqlite_value(),
        ]
    }
    fn from_row(row: &dyn Row) -> Result<Self, DbError> {
        fn col<T: ColumnValue + Default>(row: &dyn Row, i: usize) -> Result<T, DbError> {
            match row.get(i) {
                Value::Null => Ok(T::default()),
                v => T::from_sqlite_value(v),
            }
        }
        Ok(Note {
            id: col(row, 0)?,
            title: col(row, 1)?,
            body: col(row, 2)?,
            pinned: col(row, 3)?,
        })
    }
    fn default_row() -> Vec<Value> {
        Note::default().to_row()
    }
}

fn note_row(id: i64, title: &str, body: &str, pinned: i64) -> Vec<Value> {
    vec![
        Value::Int(id),
        Value::Text(title.into()),
        Value::Text(body.into()),
        Value::Int(pinned),
    ]
}

fn open_with_one_note() -> (ModelContainer, day_persistence::RecorderLog) {
    let (driver, log) = Recorder::new();
    let driver = driver.with_table("notes", vec![note_row(1, "before", "…", 0)]);
    let container = ModelContainer::open(driver, schema![Note]).expect("recorder open");
    // Nothing is resident at open; editing a row starts by faulting it in.
    container.ensure_resident::<Note>(&[1]).expect("fault");
    log.clear();
    (container, log)
}

#[test]
fn twenty_keystrokes_fold_to_one_update() {
    let (container, _log) = open_with_one_note();
    let store = container.cache::<Note>();

    let mut title = String::new();
    let sql = container
        .record_sql(|| {
            for ch in "typing, one keystroke".chars().take(20) {
                title.push(ch);
                store.elem(1).title().write(title.clone());
            }
        })
        .expect("save");

    assert_eq!(sql, ["UPDATE \"notes\" SET \"title\" = ? WHERE \"id\" = ?"]);
}

#[test]
fn the_update_carries_the_final_value() {
    let (container, log) = open_with_one_note();
    let store = container.cache::<Note>();

    container
        .record_sql(|| {
            store.elem(1).title().write("draft".into());
            store.elem(1).title().write("final".into());
        })
        .expect("save");

    let entries = log.entries();
    let update = entries
        .iter()
        .find(|(sql, _)| sql.starts_with("UPDATE"))
        .expect("an UPDATE was issued");
    assert_eq!(
        update.1,
        vec![Value::Text("final".into()), Value::Int(1)],
        "the row is read at flush time, so only the last value reaches SQL"
    );
}

#[test]
fn two_fields_fold_into_one_update() {
    let (container, _log) = open_with_one_note();
    let store = container.cache::<Note>();

    let sql = container
        .record_sql(|| {
            store.elem(1).title().write("t".into());
            store.elem(1).pinned().write(true);
            store.elem(1).title().write("tt".into());
        })
        .expect("save");

    assert_eq!(
        sql,
        ["UPDATE \"notes\" SET \"title\" = ?, \"pinned\" = ? WHERE \"id\" = ?"]
    );
}

#[test]
fn an_insert_absorbs_the_edits_that_fill_it() {
    let (container, _log) = open_with_one_note();
    let store = container.cache::<Note>();

    let sql = container
        .record_sql(|| {
            store.restructure("add", Op::Insert, 2, |v| {
                v.push(Note {
                    id: 2,
                    ..Default::default()
                });
            });
            store.elem(2).title().write("new".into());
            store.elem(2).body().write("body".into());
        })
        .expect("save");

    assert_eq!(
        sql,
        [
            "INSERT INTO \"notes\" (\"id\", \"title\", \"body\", \"pinned\") VALUES (?, ?, ?, ?) ON CONFLICT(\"id\") DO UPDATE SET \"title\" = excluded.\"title\", \"body\" = excluded.\"body\", \"pinned\" = excluded.\"pinned\""
        ]
    );
}

#[test]
fn a_delete_absorbs_everything_before_it() {
    let (container, _log) = open_with_one_note();
    let store = container.cache::<Note>();

    let sql = container
        .record_sql(|| {
            store.elem(1).title().write("doomed".into());
            store.restructure("remove", Op::Delete, 1, |v| {
                v.remove(1);
            });
        })
        .expect("save");

    assert_eq!(sql, ["DELETE FROM \"notes\" WHERE \"id\" = ?"]);
}

#[test]
fn rows_fault_on_demand_and_open_loads_none() {
    let (driver, log) = Recorder::new();
    let driver = driver.with_table(
        "notes",
        vec![note_row(1, "a", "…", 0), note_row(7, "b", "…", 1)],
    );
    let container = ModelContainer::open(driver, schema![Note]).expect("recorder open");
    let store = container.cache::<Note>();

    // The lazy contract, asserted twice over: the cache is empty, and the open issued no
    // row SELECT at all.
    assert!(store.keys().is_empty(), "open loads nothing");
    assert!(
        !log.sql()
            .iter()
            .any(|s| s.starts_with("SELECT \"id\", \"title\"")),
        "no row scan at open: {:?}",
        log.sql()
    );

    container.ensure_resident::<Note>(&[1, 7]).expect("fault");
    let mut keys = store.keys();
    keys.sort_unstable();
    assert_eq!(keys, [1, 7]);
    assert!(store.elem(7).pinned().peek());
    assert_eq!(store.elem(1).title().peek(), "a");
}

#[test]
fn a_null_reads_as_the_field_default() {
    let (driver, _log) = Recorder::new();
    let driver = driver.with_table(
        "notes",
        vec![vec![Value::Int(1), Value::Null, Value::Null, Value::Null]],
    );
    let container = ModelContainer::open(driver, schema![Note]).expect("recorder open");
    container.ensure_resident::<Note>(&[1]).expect("fault");
    let store = container.cache::<Note>();

    assert_eq!(store.elem(1).title().peek(), "");
    assert!(!store.elem(1).pinned().peek());
}

#[test]
fn opening_creates_the_table_and_stores_a_fingerprint() {
    let (driver, log) = Recorder::new();
    let _container = ModelContainer::open(driver, schema![Note]).expect("recorder open");

    let sql = log.sql();
    assert!(
        sql.iter()
            .any(|s| s.starts_with("CREATE TABLE \"notes\" (\"id\" INTEGER PRIMARY KEY")),
        "table DDL was issued: {sql:?}"
    );
    assert!(
        sql.iter()
            .any(|s| s.contains("INSERT INTO _day_schema") && s.contains("fingerprint")),
        "fingerprint was stored: {sql:?}"
    );
}

#[test]
fn a_wholesale_rewrite_resyncs_the_table() {
    let (container, log) = open_with_one_note();
    let store = container.cache::<Note>();

    store.update("import", |k| {
        *k = Keyed::new(vec![
            Note {
                id: 3,
                title: "kept".into(),
                ..Default::default()
            },
            Note {
                id: 4,
                title: "also".into(),
                ..Default::default()
            },
        ]);
    });
    log.clear();
    container.save().expect("save");

    let sql: Vec<String> = log
        .sql()
        .into_iter()
        .filter(|s| !matches!(s.as_str(), "BEGIN" | "COMMIT"))
        .collect();
    // The cache is a working set: a wholesale rewrite upserts what it holds and touches
    // nothing else. Rows outside the cache were never part of the rewrite, and deleting
    // "the rest" would delete data the rewrite never saw. Deleting is an explicit act.
    assert_eq!(sql.len(), 2, "{sql:?}");
    assert!(sql.iter().all(|s| s.starts_with("INSERT INTO")));
}

#[test]
fn autosave_flushes_at_turn_end() {
    let (container, log) = open_with_one_note();
    let store = container.cache::<Note>();

    // A turn only drains when something observes, as the UI always does. One binding
    // stands in for it.
    day_reactive::Effect::new(move || {
        store.elem(1).title().read();
    });
    day_reactive::batch(|| {
        store.elem(1).title().write("typed".into());
    });

    assert!(
        log.sql()
            .iter()
            .any(|s| s.starts_with("UPDATE \"notes\" SET \"title\"")),
        "the turn's end flushed without an explicit save: {:?}",
        log.sql()
    );
}

#[test]
fn migration_stages_run_in_order_and_move_the_version() {
    let (driver, log) = Recorder::new();
    let plan = MigrationPlan::new()
        .custom(1, 2, |conn| {
            conn.execute("ALTER TABLE notes RENAME COLUMN heading TO title", &[])
                .map(|_| ())
        })
        .custom(0, 1, |conn| {
            conn.execute("UPDATE notes SET pinned = 0", &[]).map(|_| ())
        });
    let _container = ModelContainer::open_with(driver, schema![Note], plan).expect("recorder open");

    let sql = log.sql();
    let backfill = sql
        .iter()
        .position(|s| s.starts_with("UPDATE notes SET pinned"))
        .expect("stage 0→1 ran");
    let rename = sql
        .iter()
        .position(|s| s.starts_with("ALTER TABLE notes RENAME"))
        .expect("stage 1→2 ran");
    assert!(backfill < rename, "stages sorted by from-version: {sql:?}");
}

#[test]
fn a_newer_file_is_refused() {
    let (driver, _log) = Recorder::new();
    // The version probe reads column 0 of its SELECT; serve it a bare version row.
    let driver = driver.with_table("_day_schema", vec![vec![Value::Int(9)]]);
    let plan = MigrationPlan::new().custom(0, 1, |_| Ok(()));

    let err = ModelContainer::open_with(driver, schema![Note], plan)
        .err()
        .expect("open refused");
    assert_eq!(err.kind, DbErrorKind::Schema);
    assert!(err.message.contains("newer"), "{}", err.message);
}

#[test]
fn a_gap_in_the_stages_is_an_error() {
    let (driver, _log) = Recorder::new();
    let plan = MigrationPlan::new().custom(3, 4, |_| Ok(()));

    let err = ModelContainer::open_with(driver, schema![Note], plan)
        .err()
        .expect("open refused");
    assert_eq!(err.kind, DbErrorKind::Schema);
    assert!(err.message.contains("does not start"), "{}", err.message);
}

#[test]
fn fingerprints_answer_to_every_declared_detail() {
    // Same model, twice: stable.
    assert_eq!(
        day_persistence::model_fingerprint::<Note>(),
        day_persistence::model_fingerprint::<Note>()
    );

    #[derive(Observable, Clone, Default, PartialEq, Debug)]
    struct Other {
        #[obs(key)]
        id: u32,
        title: String,
    }
    impl Model for Other {
        const TABLE: &'static str = "notes";
        const KEY: &'static str = "id";
        const COLUMNS: &'static [ColumnDef] = &[
            ColumnDef {
                name: "id",
                field: "id",
                sql: SqlType::Integer,
                not_null: true,
                unique: false,
                indexed: false,
            },
            ColumnDef {
                name: "title",
                field: "title",
                sql: SqlType::Text,
                not_null: true,
                unique: false,
                indexed: false,
            },
        ];
        fn to_row(&self) -> Vec<Value> {
            vec![self.id.to_sqlite_value(), self.title.to_sqlite_value()]
        }
        fn from_row(row: &dyn Row) -> Result<Self, DbError> {
            Ok(Other {
                id: u32::from_sqlite_value(row.get(0))?,
                title: String::from_sqlite_value(row.get(1))?,
            })
        }
        fn default_row() -> Vec<Value> {
            Other::default().to_row()
        }
    }
    assert_ne!(
        day_persistence::model_fingerprint::<Note>(),
        day_persistence::model_fingerprint::<Other>(),
        "fewer columns, different fingerprint"
    );
}

#[test]
fn a_transient_labeled_change_writes_nothing() {
    let (container, _log) = open_with_one_note();
    let store = container.cache::<Note>();

    // A row-level restructure that names no column: the fold treats it as a full-row write.
    let sql = container
        .record_sql(|| {
            store.restructure("touch", Op::Set, 1, |v| {
                if let Some(n) = v.get_mut(1) {
                    n.title = "still".into();
                }
            });
        })
        .expect("save");
    assert_eq!(
        sql,
        [
            "INSERT INTO \"notes\" (\"id\", \"title\", \"body\", \"pinned\") VALUES (?, ?, ?, ?) ON CONFLICT(\"id\") DO UPDATE SET \"title\" = excluded.\"title\", \"body\" = excluded.\"body\", \"pinned\" = excluded.\"pinned\""
        ],
        "a change that names no column resolves to a whole-row upsert, never silence"
    );
}