vstorage/sync/
status.rs

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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//! Store and query status between synchronisations.
use std::{fs::create_dir_all, path::Path};

use log::{debug, error};
use sqlite::{Connection, ConnectionThreadSafe, OpenFlags, State};

use crate::{
    base::{Item, ItemRef},
    CollectionId, Etag, Href,
};

const SCHEMA_VERSION: i64 = 2;

/// Error interacting with status database.
#[derive(thiserror::Error, Debug)]
pub enum StatusError {
    #[error("Error interacting with sqlite backend: {0}")]
    Sqlite(#[from] sqlite::Error),
    #[error("UPDATE did no affect any rows")]
    NoUpdate,
    #[error("Could not create parent directories")]
    ParentDirs(#[source] std::io::Error),
}

#[derive(thiserror::Error, Debug)]
#[error("Finding stale mapping: {0}")]
pub struct FindStaleMappingsError(#[from] sqlite::Error);

/// Storages are synchronised between two "sides", 'a' or 'b'.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Side {
    A,
    B,
}

impl Side {
    #[must_use]
    pub fn opposite(self) -> Side {
        match self {
            Side::A => Side::B,
            Side::B => Side::A,
        }
    }

    #[must_use]
    pub fn as_char(self) -> char {
        match self {
            Side::A => 'a',
            Side::B => 'b',
        }
    }
}

impl std::fmt::Display for Side {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_char().fmt(f)
    }
}

/// State for an item at some point in time.
#[derive(PartialEq, Clone, Debug)]
pub struct ItemState<I: Item> {
    pub href: Href,
    pub uid: String,
    pub etag: Etag,
    pub hash: String,
    pub data: Option<I>,
}

impl<I: Item> PartialEq<ItemRef> for ItemState<I> {
    fn eq(&self, other: &ItemRef) -> bool {
        self.href.eq(&other.href) && self.etag.eq(&other.etag)
    }
}

impl<I: Item> ItemState<I> {
    /// Create an `ItemRef` by copying the `href` and `etag`.
    #[must_use]
    pub fn to_item_ref(&self) -> ItemRef {
        ItemRef {
            href: self.href.clone(),
            etag: self.etag.clone(),
        }
    }
}

/// The status for an item as retrieved from a [`StatusDatabase`].
pub(super) struct StatusForItem {
    pub(super) hash: String,
    pub(super) a: ItemRef,
    pub(super) b: ItemRef,
}

impl StatusForItem {
    #[must_use]
    pub fn into_item_refs(self) -> (ItemRef, ItemRef) {
        (self.a, self.b)
    }
}

pub(super) struct PropertyStatus {
    // The name of the property
    pub(super) property: String,
    pub(super) value: String,
}

/// A unique ID used for a mapping between two collections.
///
/// This is an opaque identifier, and can only be obtained from a status database.
#[derive(Clone, Debug, PartialEq, Copy)]
pub struct MappingUid(i64);

/// Connection to an on-disk status database.
pub struct StatusDatabase {
    conn: ConnectionThreadSafe,
}

impl StatusDatabase {
    // NOTE: side is stored as boolean, 0=a, 1=b.

    /// Open the database in readonly mode.
    ///
    /// Returns `None` if the database does not exist.
    ///
    /// # Errors
    ///
    /// Returns [`StatusError::Sqlite`] if sqlite fails to open the database.
    pub fn open_readonly(path: impl AsRef<Path>) -> Result<Option<StatusDatabase>, StatusError> {
        let flags = OpenFlags::new().with_read_only().with_full_mutex();
        match Connection::open_thread_safe_with_flags(path, flags) {
            Ok(conn) => Ok(Some(StatusDatabase { conn })),
            Err(e) if e.code == Some(14) => Ok(None),
            Err(e) => Err(StatusError::Sqlite(e)),
        }
    }

    /// Open or creates the database in read-write mode.
    ///
    /// # Errors
    ///
    /// Returns [`StatusError::Sqlite`] if sqlite fails to open or create the database.
    pub fn open_or_create(path: impl AsRef<Path>) -> Result<StatusDatabase, StatusError> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            create_dir_all(parent).map_err(StatusError::ParentDirs)?;
        }

        let db = StatusDatabase {
            conn: Connection::open_thread_safe(path)?,
        };

        db.init_schema()?;
        db.conn.execute("PRAGMA foreign_keys = ON")?;

        Ok(db)
    }

    /// This function is idempotent and safe to call more than once.
    ///
    /// In case of interruption during the first initialisation, later attempts will finalise
    /// creating tables and indexes.
    fn init_schema(&self) -> Result<(), StatusError> {
        debug!("Ensuring that status database is initialised.");
        self.conn
            .execute("CREATE TABLE IF NOT EXISTS meta (version INTEGER PRIMARY KEY)")?;

        let mut q = self
            .conn
            .prepare("INSERT OR IGNORE INTO meta (version) VALUES (?)")?;
        q.bind((1, SCHEMA_VERSION))?;
        q.next()?;

        self.conn.execute(concat!(
            "CREATE TABLE IF NOT EXISTS collections (",
            " uid INTEGER PRIMARY KEY AUTOINCREMENT,",
            " id_a TEXT,",
            " href_a TEXT NOT NULL,",
            " id_b TEXT,",
            " href_b TEXT NOT NULL",
            ")",
        ))?;
        self.conn
            .execute("CREATE UNIQUE INDEX IF NOT EXISTS href_a ON collections(href_a)")?;
        self.conn
            .execute("CREATE UNIQUE INDEX IF NOT EXISTS href_b ON collections(href_b)")?;
        // TODO: duplicate ids are not allowed.
        // FIXME: this also needs to be addressed in the discovery layer.

        // TODO: Etag should also be NOT NULL
        self.conn.execute(concat!(
            "CREATE TABLE IF NOT EXISTS items (",
            " ident TEXT NOT NULL,",
            " mapping_uid TEXT NOT NULL,",
            " hash TEXT NOT NULL,",
            " href_a TEXT NOT NULL,",
            " etag_a TEXT,",
            " href_b TEXT NOT NULL,",
            " etag_b TEXT,",
            " FOREIGN KEY(mapping_uid) REFERENCES collections(uid)",
            ")"
        ))?;
        self.conn
            .execute("CREATE UNIQUE INDEX IF NOT EXISTS by_ident ON items(ident, mapping_uid)")?;
        self.conn
            .execute("CREATE UNIQUE INDEX IF NOT EXISTS by_href ON items(href_a)")?;
        self.conn
            .execute("CREATE UNIQUE INDEX IF NOT EXISTS by_href ON items(href_b)")?;

        self.conn.execute(concat!(
            "CREATE TABLE IF NOT EXISTS properties (",
            " mapping_uid INTEGER NOT NULL,",
            " href_a TEXT NOT NULL,",
            " href_b TEXT NOT NULL,",
            " property TEXT NOT NULL,",
            " value TEXT NOT NULL,",
            " FOREIGN KEY(mapping_uid) REFERENCES collections(uid)",
            ")"
        ))?;
        self.conn.execute(
            "CREATE UNIQUE INDEX IF NOT EXISTS by_uid ON properties(mapping_uid, property)",
        )?;
        self.conn.execute(
            "CREATE UNIQUE INDEX IF NOT EXISTS by_href_a ON properties(href_a, property)",
        )?;
        self.conn.execute(
            "CREATE UNIQUE INDEX IF NOT EXISTS by_href_b ON properties(href_b, property)",
        )?;
        Ok(())
    }

    /// Returns an `ItemState`, if it exists AND has an Etag.
    pub(super) fn get_item_by_href<I: Item>(
        &self,
        side: Side,
        href: &str,
        mapping_uid: MappingUid,
    ) -> Result<Option<ItemState<I>>, StatusError> {
        let query = vec![
            &format!("SELECT ident, href_{side} AS href, hash, etag_{side} AS etag"),
            " FROM items",
            &format!(" WHERE href = ? AND etag_{side} IS NOT NULL AND mapping_uid = ?"),
        ]
        .into_iter()
        .collect::<String>();
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, href))?;
        statement.bind((2, mapping_uid.0))?;

        if let Ok(State::Row) = statement.next() {
            let Some(etag) = statement.read::<Option<String>, _>("etag")? else {
                return Ok(None);
            };
            Ok(Some(ItemState {
                href: statement.read::<String, _>("href")?,
                uid: statement.read::<String, _>("ident")?,
                etag: Etag::from(etag),
                hash: statement.read::<String, _>("hash")?,
                data: None,
            }))
        } else {
            Ok(None)
        }
    }

    pub(super) fn get_item_hash_by_uid(
        &self,
        mapping_uid: MappingUid,
        uid: &str,
    ) -> Result<Option<StatusForItem>, StatusError> {
        let query = concat!(
            "SELECT hash, etag_a, etag_b, href_a, href_b",
            " FROM items WHERE ident = ? AND mapping_uid = ?"
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, uid))?;
        statement.bind((2, mapping_uid.0))?;

        if let Ok(State::Row) = statement.next() {
            Ok(Some(StatusForItem {
                hash: statement.read::<String, _>("hash")?,
                a: ItemRef {
                    href: statement.read::<String, _>("href_a")?,
                    etag: statement.read::<String, _>("etag_a")?.into(),
                },
                b: ItemRef {
                    etag: statement.read::<String, _>("etag_b")?.into(),
                    href: statement.read::<String, _>("href_b")?,
                },
            }))
        } else {
            Ok(None)
        }
    }

    pub(super) fn all_uids(&self, mapping: MappingUid) -> Result<Vec<String>, StatusError> {
        let query = "SELECT DISTINCT ident FROM items WHERE mapping_uid = ?";

        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, mapping.0))?;

        let mut results = Vec::new();
        while let Ok(State::Row) = statement.next() {
            results.push(statement.read::<String, _>(0)?);
        }

        Ok(results)
    }

    /// Get the (internal) UID for a collection mapping.
    pub(super) fn get_mapping_uid(
        &self,
        href_a: &Href,
        href_b: &Href,
    ) -> Result<Option<MappingUid>, StatusError> {
        let query = "SELECT uid FROM collections WHERE href_a = ? AND href_b = ?";
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, href_a.as_str()))?;
        statement.bind((2, href_b.as_str()))?;

        if let State::Row = statement.next()? {
            Ok(Some(MappingUid(statement.read::<i64, _>("uid")?)))
        } else {
            Ok(None)
        }
    }

    pub(super) fn remove_collection(&self, mapping_uid: MappingUid) -> Result<(), StatusError> {
        let query = "DELETE FROM collections WHERE uid = ?";
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, mapping_uid.0))?;
        statement.next()?;
        Ok(())
    }

    pub(super) fn get_or_add_collection(
        &self,
        href_a: &str,
        href_b: &str,
        id_a: Option<&CollectionId>,
        id_b: Option<&CollectionId>,
    ) -> Result<MappingUid, StatusError> {
        let query = concat!(
            "INSERT OR IGNORE INTO collections(id_a, href_a, id_b, href_b)",
            " VALUES (?, ?, ?, ?)",
            " RETURNING uid",
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, id_a.map(CollectionId::as_ref)))?;
        statement.bind((2, href_a))?;
        statement.bind((3, id_b.map(CollectionId::as_ref)))?;
        statement.bind((4, href_b))?;
        // If a uid was returned (just inserted) return that.
        if statement.next()? == State::Row {
            return Ok(MappingUid(statement.read::<i64, _>("uid")?));
        };

        let query = "SELECT uid FROM collections WHERE href_a = ? AND href_b = ?";
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, href_a))?;
        statement.bind((2, href_b))?;

        if let State::Row = statement.next()? {
            Ok(MappingUid(statement.read::<i64, _>("uid")?))
        } else {
            // FIXME: reachable if existing an existing HREF conflicted only one row.
            unreachable!("uid missing for mapping immediately after INSERT");
        }
    }

    pub(super) fn insert_item(
        &self,
        mapping_uid: MappingUid,
        uid: &str,
        hash: &str,
        ref_a: &ItemRef,
        ref_b: &ItemRef,
    ) -> Result<(), StatusError> {
        let query = concat!(
            "INSERT INTO items(ident, mapping_uid, hash, href_a, etag_a, href_b, etag_b)",
            " VALUES (?, ?, ?, ?, ?, ?, ?)"
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, uid))?;
        statement.bind((2, mapping_uid.0))?;
        statement.bind((3, hash))?;

        statement.bind((4, ref_a.href.as_str()))?;
        statement.bind((5, ref_a.etag.as_ref()))?;
        statement.bind((6, ref_b.href.as_str()))?;
        statement.bind((7, ref_b.etag.as_ref()))?;
        statement.next()?;
        Ok(())
    }

    pub(super) fn update_item(
        &self,
        new_hash: &str,
        old_a: &ItemRef,
        old_b: &ItemRef,
        new_a: &ItemRef,
        new_b: &ItemRef,
    ) -> Result<(), StatusError> {
        // Items in other collections may have the same UID, so update by href.
        let query = concat!(
            "UPDATE items",
            " SET hash = :hash, etag_a = :new_etag_a, etag_b = :new_etag_b, href_a = :new_href_a, href_b = :new_href_b",
            " WHERE href_a = :old_href_a AND href_b = :old_href_b AND etag_a = :old_etag_a AND etag_b = :old_etag_b"
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((":hash", new_hash))?;

        statement.bind((":new_href_a", new_a.href.as_str()))?;
        statement.bind((":new_href_b", new_b.href.as_str()))?;
        statement.bind((":new_etag_a", Some(new_a.etag.as_str())))?;
        statement.bind((":new_etag_b", Some(new_b.etag.as_str())))?;

        statement.bind((":old_href_a", old_a.href.as_str()))?;
        statement.bind((":old_href_b", old_b.href.as_str()))?;
        statement.bind((":old_etag_a", Some(old_a.etag.as_str())))?;
        statement.bind((":old_etag_b", Some(old_b.etag.as_str())))?;
        statement.next()?;

        if self.conn.change_count() == 0 {
            error!("update_item did not affect any rows! old_a: {old_a:?}, old_b: {old_b:?}");
            Err(StatusError::NoUpdate)
        } else {
            Ok(())
        }
    }

    pub(super) fn delete_item(
        &self,
        mapping_uid: MappingUid,
        uid: &str,
        // FIXME: should take etag, in case another instance raced us and updated the item?
    ) -> Result<(), StatusError> {
        let query = "DELETE FROM items WHERE mapping_uid = ? AND ident = ?";
        let mut statement = self.conn.prepare(query)?;
        statement.bind((1, mapping_uid.0))?;
        statement.bind((2, uid))?;
        statement.next()?;
        Ok(())
    }

    pub(super) fn list_properties_for_collection(
        &self,
        mapping_uid: MappingUid,
    ) -> Result<Vec<PropertyStatus>, StatusError> {
        let query = concat!(
            "SELECT property, value",
            " FROM properties",
            " WHERE mapping_uid = :mapping_uid"
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((":mapping_uid", mapping_uid.0))?;

        let mut results = Vec::new();
        while let Ok(State::Row) = statement.next() {
            results.push(PropertyStatus {
                property: statement.read::<String, _>("property")?,
                value: statement.read::<String, _>("value")?,
            });
        }
        Ok(results)
    }

    pub(super) fn set_property(
        &self,
        mapping_uid: MappingUid,
        href_a: &str,
        href_b: &str,
        property: &str,
        value: &str,
    ) -> Result<(), StatusError> {
        let query = concat!(
            "INSERT OR REPLACE INTO properties (mapping_uid, href_a, href_b, property, value) ",
            "VALUES (:mapping_uid, :href_a, :href_b, :property, :value)",
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((":mapping_uid", mapping_uid.0))?;
        statement.bind((":href_a", href_a))?;
        statement.bind((":href_b", href_b))?;
        statement.bind((":property", property))?;
        statement.bind((":value", value))?;
        statement.next()?;
        Ok(())
    }

    pub(super) fn delete_property(
        &self,
        mapping_uid: MappingUid,
        href_a: &str,
        href_b: &str,
        property: &str,
    ) -> Result<(), StatusError> {
        let query = concat!(
            "DELETE FROM properties",
            " WHERE mapping_uid = :mapping_uid AND href_a = :href_a AND href_b = :href_b",
            " AND property = :property",
        );
        let mut statement = self.conn.prepare(query)?;
        statement.bind((":mapping_uid", mapping_uid.0))?;
        statement.bind((":href_a", href_a))?;
        statement.bind((":href_b", href_b))?;
        statement.bind((":property", property))?;
        statement.next()?;
        Ok(())
    }

    pub(super) fn find_stale_mappings(
        &self,
        active: impl Iterator<Item = MappingUid>,
    ) -> Result<Vec<MappingUid>, FindStaleMappingsError> {
        let params = active
            .map(|uid| uid.0.to_string())
            .collect::<Vec<_>>()
            .join(",");

        // No support for arrays. See: https://github.com/stainless-steel/sqlite/issues/38
        // The replacement String is constructed inside this function and its input is an i64.
        let query = "SELECT uid FROM collections WHERE uid NOT IN (?)".replace('?', &params);
        let mut statement = self.conn.prepare(query)?;

        let mut results = Vec::new();
        while let Ok(State::Row) = statement.next() {
            results.push(MappingUid(statement.read::<i64, _>("uid")?));
        }
        Ok(results)
    }

    pub(super) fn flush_stale_mappings(
        &self,
        active: impl IntoIterator<Item = MappingUid>,
    ) -> Result<(), StatusError> {
        let params = active
            .into_iter()
            .map(|uid| uid.0.to_string())
            .collect::<Vec<_>>()
            .join(",");

        // No support for arrays. See: https://github.com/stainless-steel/sqlite/issues/38
        // The replacement String is constructed inside this function and its input is an i64.
        self.conn
            .execute("DELETE FROM items WHERE mapping_uid IN (?)".replace('?', &params))?;
        self.conn
            .execute("DELETE FROM properties WHERE mapping_uid IN (?)".replace('?', &params))?;
        self.conn
            .execute("DELETE FROM collections WHERE uid IN (?)".replace('?', &params))?;

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use crate::{
        base::{IcsItem, ItemRef},
        sync::status::StatusError,
        CollectionId, Etag,
    };

    use super::{MappingUid, Side, StatusDatabase};

    #[test]
    fn test_writing_in_readonly_mode() {
        let db = StatusDatabase::open_readonly(":memory:").unwrap();
        let err = db.unwrap().init_schema().unwrap_err();
        let err_msg = err.to_string();
        assert!(err_msg.contains("attempt to write a readonly database"));
    }

    #[test]
    fn test_insert_and_get_item() {
        let db = StatusDatabase::open_or_create(":memory:").unwrap();
        let mapping_uid = MappingUid(1);
        let uid = "07da74e5-0a32-482a-bbdd-13fd1e45cce3";
        let hash = "HASH";
        let item_a = ItemRef {
            href: "/collections/work/item.ics".into(),
            etag: "123".into(),
        };
        let item_b = ItemRef {
            href: "work/item.ics".into(),
            etag: "abc000".into(),
        };
        db.get_or_add_collection(
            "/collections/work/",
            "work",
            Some(&"work".parse().unwrap()),
            Some(&"work".parse().unwrap()),
        )
        .unwrap();
        db.insert_item(mapping_uid, uid, hash, &item_a, &item_b)
            .unwrap();

        let item_a_fetched = db
            .get_item_by_href::<IcsItem>(Side::A, &item_a.href, mapping_uid)
            .unwrap()
            .unwrap();
        assert_eq!(item_a_fetched.uid, uid);
        assert_eq!(item_a_fetched.hash, hash);
        assert_eq!(item_a_fetched.href, item_a.href);
        assert_eq!(item_a_fetched.etag, item_a.etag);

        let item_b_fetched = db
            .get_item_by_href::<IcsItem>(Side::B, &item_b.href, mapping_uid)
            .unwrap()
            .unwrap();
        assert_eq!(item_b_fetched.uid, uid);
        assert_eq!(item_b_fetched.hash, hash);
        assert_eq!(item_b_fetched.href, item_b.href);
        assert_eq!(item_b_fetched.etag, item_b.etag);

        let item_status = db
            .get_item_hash_by_uid(mapping_uid, uid)
            .unwrap()
            .expect("status should return items that were just inserted");
        assert_eq!(item_status.hash, hash);

        let all = db.all_uids(mapping_uid).unwrap();
        let all_expected = vec![uid];
        assert_eq!(all, all_expected);

        db.delete_item(mapping_uid, uid).unwrap();
        assert!(db
            .get_item_by_href::<IcsItem>(Side::A, &item_a.href, mapping_uid)
            .unwrap()
            .is_none());
        assert!(db
            .get_item_by_href::<IcsItem>(Side::B, &item_b.href, mapping_uid)
            .unwrap()
            .is_none());
        assert!(db.get_item_hash_by_uid(mapping_uid, uid).unwrap().is_none());
        assert!(db.all_uids(mapping_uid).unwrap().is_empty());
    }
    #[test]
    fn test_insert_update_and_get_item() {
        let db = StatusDatabase::open_or_create(":memory:").unwrap();
        let mapping_uid = MappingUid(1);
        let uid = "07da74e5-0a32-482a-bbdd-13fd1e45cce3";
        let hash = "HASH";
        let item_a = ItemRef {
            href: "/collections/work/item.ics".into(),
            etag: "123".into(),
        };
        let item_b = ItemRef {
            href: "work/item.ics".into(),
            etag: "abc000".into(),
        };
        db.get_or_add_collection(
            "/collections/work/",
            "work",
            Some(&"work".parse().unwrap()),
            Some(&"work".parse().unwrap()),
        )
        .unwrap();
        db.insert_item(mapping_uid, uid, hash, &item_a, &item_b)
            .unwrap();

        let updated_hash = "ANOTHERHASH";
        let updated_etag_a = Etag::from("456");
        let updated_etag_b = Etag::from("def111");
        db.update_item(
            updated_hash,
            &item_a,
            &item_b,
            &ItemRef {
                etag: updated_etag_a.clone(),
                href: item_a.href.clone(),
            },
            &ItemRef {
                etag: updated_etag_b.clone(),
                href: item_b.href.clone(),
            },
        )
        .unwrap();

        let item_a_fetched = db
            .get_item_by_href::<IcsItem>(Side::A, &item_a.href, mapping_uid)
            .unwrap()
            .unwrap();
        assert_eq!(item_a_fetched.uid, uid);
        assert_eq!(item_a_fetched.hash, updated_hash);
        assert_eq!(item_a_fetched.href, item_a.href);
        assert_eq!(item_a_fetched.etag, updated_etag_a);

        let item_b_fetched = db
            .get_item_by_href::<IcsItem>(Side::B, &item_b.href, mapping_uid)
            .unwrap()
            .unwrap();
        assert_eq!(item_b_fetched.uid, uid);
        assert_eq!(item_b_fetched.hash, updated_hash);
        assert_eq!(item_b_fetched.href, item_b.href);
        assert_eq!(item_b_fetched.etag, updated_etag_b);

        let item_status = db
            .get_item_hash_by_uid(mapping_uid, uid)
            .unwrap()
            .expect("status should return items that were just inserted");
        assert_eq!(item_status.hash, updated_hash);

        let all = db.all_uids(mapping_uid).unwrap();
        let all_expected = vec![uid];
        assert_eq!(all, all_expected);
    }
    #[test]
    fn test_wrong_update() {
        let db = StatusDatabase::open_or_create(":memory:").unwrap();
        let mapping_uid = MappingUid(1);
        let uid = "07da74e5-0a32-482a-bbdd-13fd1e45cce3";
        let hash = "HASH";
        let item_a = ItemRef {
            href: "/collections/work/item.ics".into(),
            etag: "123".into(),
        };
        let item_b = ItemRef {
            href: "work/item.ics".into(),
            etag: "abc000".into(),
        };
        db.get_or_add_collection(
            "/collections/work/",
            "work",
            Some(&"work".parse().unwrap()),
            Some(&"work".parse().unwrap()),
        )
        .unwrap();
        db.insert_item(mapping_uid, uid, hash, &item_a, &item_b)
            .unwrap();

        let updated_hash = "ANOTHERHASH";
        let updated_etag_a = "456".into();
        let updated_etag_b = "def111".into();
        let err = db
            .update_item(
                updated_hash,
                &ItemRef {
                    href: "not/correct.ics".into(),
                    etag: item_a.etag,
                },
                &item_b,
                &ItemRef {
                    href: "not/correct.ics".into(),
                    etag: updated_etag_a,
                },
                &ItemRef {
                    href: item_b.href.clone(),
                    etag: updated_etag_b,
                },
            )
            .unwrap_err();
        assert!(matches!(err, StatusError::NoUpdate));
    }

    #[test]
    fn test_add_and_get_collection() {
        let db = StatusDatabase::open_or_create(":memory:").unwrap();
        let collection_id = "guests".parse::<CollectionId>().unwrap();
        let href_a = "/collections/guests";
        let href_b = "guests";
        let mapping_uid = db
            .get_or_add_collection(href_a, href_b, Some(&collection_id), Some(&collection_id))
            .unwrap();

        let gotten_uid = db
            .get_mapping_uid(&href_a.to_string(), &href_b.to_string())
            .unwrap()
            .expect("should obtain mapping that was just inserted");
        assert_eq!(mapping_uid, gotten_uid);

        db.remove_collection(mapping_uid).unwrap();

        let gotten_uid = db
            .get_mapping_uid(&href_a.to_string(), &href_b.to_string())
            .unwrap();
        assert!(gotten_uid.is_none());
    }
}