slumber_core 5.3.0

Core library for Slumber. Not intended for external use.
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
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
//! The database is responsible for persisting data, including requests and
//! responses.

mod convert;
mod migrations;
#[cfg(test)]
mod tests;

use crate::{
    collection::{Collection, CollectionFile, ProfileId, RecipeId},
    database::convert::{CollectionPath, SqlWrap},
    http::{Exchange, ExchangeSummary, RequestId},
};
use chrono::Utc;
use rusqlite::{Connection, OptionalExtension, named_params};
use slumber_util::{ResultTraced, paths};
use std::{
    borrow::Cow,
    fmt::Debug,
    io,
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
};
use strum::IntoDiscriminant;
use thiserror::Error;
use tracing::{debug, info};
use uuid::Uuid;

/// Maximum number of commands to store in history **per collection**. When we
/// hit the cap, the oldest commands get evicted.
const MAX_COMMAND_HISTORY_SIZE: u32 = 100;

/// A SQLite database for persisting data. Generally speaking, any error that
/// occurs *after* opening the DB connection should be an internal bug, but
/// should be shown to the user whenever possible. All operations are blocking,
/// to enable calling from the view code. Do not call on every frame though,
/// cache results in UI state for as long as they're needed.
///
/// There is only one database for an entire system. All collection share the
/// same DB, and can modify concurrently. Generally any data that is unique
/// to a collection should have an FK column to the `collections` table.
///
/// This uses an `Arc` internally, so it's safe and cheap to clone.
///
/// Schema is defined in `migrations`
#[derive(Clone, Debug)]
pub struct Database {
    /// Data is stored in a sqlite DB. Mutex is needed for multi-threaded
    /// access. This is a bottleneck but the access rate should be so low that
    /// it doesn't matter. If it does become a bottleneck, we could spawn
    /// one connection per thread, but the code would be a bit more
    /// complicated.
    connection: Arc<Mutex<Connection>>,
}

impl Database {
    const FILE: &'static str = "state.sqlite";

    /// Load the database. This will perform migrations, but can be called from
    /// anywhere in the app. The migrations will run on first connection, and
    /// not after that.
    pub fn load() -> Result<Self, DatabaseError> {
        Self::from_path(&Self::path())
    }

    /// [Self::load], but from a particular data directory. Useful only for
    /// tests, when the default DB path shouldn't be used.
    pub fn from_directory(directory: &Path) -> Result<Self, DatabaseError> {
        let path = directory.join(Self::FILE);
        Self::from_path(&path)
    }

    fn from_path(path: &Path) -> Result<Self, DatabaseError> {
        paths::create_parent(path).map_err(DatabaseError::Directory)?;

        info!(?path, "Loading database");
        let mut connection = Connection::open(path)
            .and_then(|conn| {
                conn.pragma_update(None, "foreign_keys", "ON")?;
                // Use WAL for concurrency
                conn.pragma_update(None, "journal_mode", "WAL")?;
                Ok(conn)
            })
            .map_err(DatabaseError::add_context("Opening database"))?;
        Self::migrate(&mut connection)?;
        Ok(Self {
            connection: Arc::new(Mutex::new(connection)),
        })
    }

    /// Path to the database file
    pub fn path() -> PathBuf {
        paths::data_directory().join(Self::FILE)
    }

    /// Apply database migrations
    fn migrate(connection: &mut Connection) -> Result<(), DatabaseError> {
        migrations::migrations()
            .to_latest(connection)
            .map_err(DatabaseError::Migrate)
    }

    /// Get a reference to the DB connection. Panics if the lock is poisoned
    fn connection(&self) -> impl '_ + DerefMut<Target = Connection> {
        self.connection.lock().expect("Connection lock poisoned")
    }

    /// Get a list of all collections
    pub fn get_collections(
        &self,
    ) -> Result<Vec<CollectionMetadata>, DatabaseError> {
        // Wish we had try blocks ¯\_(ツ)_/¯
        self.connection()
            .prepare("SELECT * FROM collections")
            .and_then(|mut stmt| {
                stmt.query_map([], |row| row.try_into())?
                    .collect::<rusqlite::Result<Vec<_>>>()
            })
            .map_err(DatabaseError::add_context("Querying collections"))
            .traced()
    }

    /// Get a collection's ID by its path. This will canonicalize the path to
    /// ensure it matches what we've stored in the DB. Return an error if the
    /// path isn't in the DB.
    pub fn get_collection_id(
        &self,
        path: &Path,
    ) -> Result<CollectionId, DatabaseError> {
        // Convert to canonicalize and make serializable
        let path = CollectionPath::try_from_path_maybe_missing(path)?;

        self.connection()
            .query_row(
                "SELECT id FROM collections WHERE path = :path",
                named_params! {":path": &path},
                |row| row.get::<_, CollectionId>("id"),
            )
            .map_err(|error| match error {
                rusqlite::Error::QueryReturnedNoRows => {
                    DatabaseError::ResourceUnknown {
                        kind: "collection",
                        id: path.to_string(),
                    }
                }
                _ => {
                    DatabaseError::with_context(error, "Querying collection ID")
                }
            })
            .traced()
    }

    /// Get metadata about a collection from its ID
    pub fn get_collection_metadata(
        &self,
        id: CollectionId,
    ) -> Result<CollectionMetadata, DatabaseError> {
        self.connection()
            .query_row(
                "SELECT * FROM collections WHERE id = :id",
                named_params! {":id": id},
                |row| CollectionMetadata::try_from(row),
            )
            .map_err(|error| match error {
                rusqlite::Error::QueryReturnedNoRows => {
                    DatabaseError::ResourceUnknown {
                        kind: "collection",
                        id: id.to_string(),
                    }
                }
                _ => DatabaseError::with_context(
                    error,
                    "Querying collection metadata",
                ),
            })
            .traced()
    }

    /// Delete a collection from the DB, including all requests and other rows
    /// associated with it
    pub fn delete_collection(
        &self,
        collection: CollectionId,
    ) -> Result<(), DatabaseError> {
        // Delete all rows referencing the collection before deleting the
        // collection. It would be nice to use `ON DELETE CASCADE`, but you can
        // only set that when the table is created. Sqlite doesn't allowing
        // creating or modifying foreign keys on a table once it's created.
        // Manually deleting everything is simpler than writing a huge migration
        // just to make this automatic

        let statements = [
            "DELETE FROM requests_v2 WHERE collection_id = :id",
            "DELETE FROM ui_state_v2 WHERE collection_id = :id",
            "DELETE FROM commands WHERE collection_id = :id",
            "DELETE FROM collections WHERE id = :id",
        ];

        let mut connection = self.connection();
        connection
            .transaction()
            .and_then(|tx| {
                for statement in statements {
                    tx.prepare(statement)?
                        .execute(named_params! {":id": collection})?;
                }
                tx.commit()?;
                Ok(())
            })
            .map_err({
                DatabaseError::add_context(format!(
                    "Deleting collection `{collection}`"
                ))
            })
            .traced()
    }

    /// Migrate all data for one collection into another, deleting the source
    /// collection
    pub fn merge_collections(
        &self,
        source: CollectionId,
        target: CollectionId,
    ) -> Result<(), DatabaseError> {
        info!(?source, ?target, "Merging database state");
        let mut connection = self.connection();
        let tx = connection.transaction()?;

        // Update each table in individually
        tx.execute(
            "UPDATE requests_v2 SET collection_id = :target
                WHERE collection_id = :source",
            named_params! {":source": source, ":target": target},
        )
        .map_err(DatabaseError::add_context("Merging table `requests_v2`"))
        .traced()?;
        tx.execute(
            // Overwrite UI state. Maybe this isn't the best UX, but sqlite
            // doesn't provide an "UPDATE OR DELETE" so this is easiest and
            // still reasonable
            "UPDATE OR REPLACE ui_state_v2 SET collection_id = :target
                WHERE collection_id = :source",
            named_params! {":source": source, ":target": target},
        )
        .map_err(DatabaseError::add_context("Merging table `ui_state_v2`"))
        .traced()?;
        tx.execute(
            // Overwrite command history. If there's an overlap, we'll take the
            // timestamp from the source collection because it's easiest.
            // Sqlite doesn't have an "UPDATE OR DELETE"
            "UPDATE OR REPLACE commands SET collection_id = :target
                WHERE collection_id = :source",
            named_params! {":source": source, ":target": target},
        )
        .map_err(DatabaseError::add_context("Merging table `commands`"))
        .traced()?;

        // Delete the collection now that nothing is referencing it
        tx.execute(
            "DELETE FROM collections WHERE id = :source",
            named_params! {":source": source},
        )
        .map_err(DatabaseError::add_context("Deleting source collection"))
        .traced()?;
        tx.commit()?;

        Ok(())
    }

    /// Get all requests for all collections
    pub fn get_all_requests(
        &self,
    ) -> Result<Vec<ExchangeSummary>, DatabaseError> {
        // Wish we had try blocks ¯\_(ツ)_/¯
        self.connection()
            .prepare(
                "SELECT id, recipe_id, profile_id, start_time, end_time,
                    status_code FROM requests_v2 ORDER BY start_time DESC",
            )
            .and_then(|mut stmt| {
                stmt.query_map((), |row| row.try_into())?
                    .collect::<rusqlite::Result<Vec<_>>>()
            })
            .map_err(DatabaseError::add_context("Querying requests"))
            .traced()
    }

    /// Delete a single exchange by ID. Return the number of deleted requests
    pub fn delete_request(
        &self,
        request_id: RequestId,
    ) -> Result<usize, DatabaseError> {
        self.connection()
            .execute(
                "DELETE FROM requests_v2 WHERE id = :request_id",
                named_params! {":request_id": request_id},
            )
            .map_err({
                DatabaseError::add_context(format!(
                    "Deleting request `{request_id}`"
                ))
            })
            .traced()
    }

    /// Convert this database connection into a handle for a single collection
    /// file. This will store the collection in the DB if it isn't already,
    /// then grab its generated ID to create a [CollectionDatabase].
    pub fn into_collection(
        self,
        file: &CollectionFile,
    ) -> Result<CollectionDatabase, DatabaseError> {
        // Convert to canonicalize and make serializable
        let path = CollectionPath::try_from_path(file.path())?;

        // We have to set/get in two separate queries, because RETURNING doesn't
        // return anything if the insert didn't modify
        self.connection()
            .execute(
                "INSERT INTO collections (id, path) VALUES (:id, :path)
                ON CONFLICT(path) DO NOTHING",
                named_params! {
                    ":id": CollectionId::new(),
                    ":path": &path,
                },
            )
            .map_err(DatabaseError::add_context("Setting collection ID"))
            .traced()?;
        let collection_id = self
            .connection()
            .query_row(
                "SELECT id FROM collections WHERE path = :path",
                named_params! {":path": &path},
                |row| row.get::<_, CollectionId>("id"),
            )
            .map_err(DatabaseError::add_context("Querying collection ID"))
            .traced()?;

        Ok(CollectionDatabase {
            collection_id,
            database: self,
        })
    }
}

/// A collection-specific database handle. This is a wrapper around a [Database]
/// that restricts all queries to a specific collection ID. Use
/// [Database::into_collection] to obtain one. You can freely clone this.
#[derive(Clone, Debug)]
pub struct CollectionDatabase {
    collection_id: CollectionId,
    database: Database,
}

impl CollectionDatabase {
    /// Get metadata for the collection associated with this DB handle
    pub fn metadata(&self) -> Result<CollectionMetadata, DatabaseError> {
        self.database
            .connection()
            .query_row(
                "SELECT * FROM collections WHERE id = :id",
                named_params! {":id": self.collection_id},
                |row| row.try_into(),
            )
            .map_err(DatabaseError::add_context("Querying collection path"))
            .traced()
    }

    /// Get the root database, which has access to all collections
    pub fn root(&self) -> &Database {
        &self.database
    }

    /// Store the collection's display name in the DB
    ///
    /// This should be set
    /// whenever the collection file is loaded to ensure it's up to date in
    /// the database. If this fails it will log the result, but not return it.
    /// There's nothing meaningful for the caller to do with the result
    /// beyond log it again.
    pub fn set_name(&self, collection: &Collection) {
        let name = collection.name.as_deref();
        let _ = self
            .database
            .connection()
            .execute(
                "UPDATE collections SET name = :name WHERE id = :id",
                named_params! {":id": self.collection_id, ":name": name},
            )
            .map_err(DatabaseError::add_context("Updating collection name"))
            .traced();
    }

    /// Get a request by ID, or `None` if it does not exist in history.
    pub fn get_request(
        &self,
        request_id: RequestId,
    ) -> Result<Option<Exchange>, DatabaseError> {
        self.database
            .connection()
            .query_row(
                "SELECT * FROM requests_v2
                WHERE collection_id = :collection_id
                    AND id = :request_id
                ORDER BY start_time DESC LIMIT 1",
                named_params! {
                    // Include collection ID just to be extra safe
                    ":collection_id": self.collection_id,
                    ":request_id": request_id,
                },
                |row| row.try_into(),
            )
            .optional()
            .map_err(DatabaseError::add_context(format!(
                "Querying request {request_id} from database"
            )))
            .traced()
    }

    /// Get the most recent request+response (by start time) for a profile +
    /// recipe, or `None` if there has never been one received. If the given
    /// profile is `None`, match all requests that have no associated profile.
    pub fn get_latest_request(
        &self,
        profile_id: ProfileFilter,
        recipe_id: &RecipeId,
    ) -> Result<Option<Exchange>, DatabaseError> {
        self.database
            .connection()
            .query_row(
                // `IS` needed for profile_id so `None` will match `NULL`.
                // We want to dynamically ignore the profile filter if the user
                // is asking for all profiles. Dynamically modifying the query
                // is really ugly so the easiest thing is to use an additional
                // parameter to bypass the filter
                "SELECT * FROM requests_v2
                WHERE collection_id = :collection_id
                    AND (:ignore_profile_id OR profile_id IS :profile_id)
                    AND recipe_id = :recipe_id
                ORDER BY start_time DESC LIMIT 1",
                named_params! {
                    ":collection_id": self.collection_id,
                    ":ignore_profile_id": profile_id == ProfileFilter::All,
                    ":profile_id": profile_id,
                    ":recipe_id": recipe_id,
                },
                |row| row.try_into(),
            )
            .optional()
            .map_err(DatabaseError::add_context(format!(
                "Querying request [profile={profile_id:?}; \
                    recipe={recipe_id}] from database"
            )))
            .traced()
    }

    /// Get a list of all requests for a profile+recipe combo
    pub fn get_recipe_requests(
        &self,
        profile_filter: ProfileFilter,
        recipe_id: &RecipeId,
    ) -> Result<Vec<ExchangeSummary>, DatabaseError> {
        self.database
            .connection()
            .prepare(
                // `IS` needed for profile_id so `None` will match `NULL`.
                // We want to dynamically ignore the profile filter if the user
                // is asking for all profiles. Dynamically modifying the query
                // is really ugly so the easiest thing is to use an additional
                // parameter to bypass the filter
                "SELECT id, recipe_id, profile_id, start_time, end_time,
                    status_code FROM requests_v2
                WHERE collection_id = :collection_id
                    AND (:ignore_profile_id OR profile_id IS :profile_id)
                    AND recipe_id = :recipe_id
                ORDER BY start_time DESC",
            )?
            .query_map(
                named_params! {
                    ":collection_id": self.collection_id,
                    ":ignore_profile_id": profile_filter == ProfileFilter::All,
                    ":profile_id": profile_filter,
                    ":recipe_id": recipe_id,
                },
                |row| row.try_into(),
            )
            .map_err(DatabaseError::add_context(
                "Querying request history from database",
            ))
            .traced()?
            .collect::<rusqlite::Result<Vec<_>>>()
            .map_err(DatabaseError::add_context("Extracting request history"))
    }

    /// Get all requests for this collection
    pub fn get_all_requests(
        &self,
    ) -> Result<Vec<ExchangeSummary>, DatabaseError> {
        self.database
            .connection()
            .prepare(
                "SELECT id, recipe_id, profile_id, start_time, end_time,
                    status_code FROM requests_v2
                WHERE collection_id = :collection_id ORDER BY start_time DESC",
            )
            .and_then(|mut stmt| {
                stmt.query_map(
                    named_params! {":collection_id": self.collection_id},
                    |row| row.try_into(),
                )?
                .collect::<rusqlite::Result<Vec<_>>>()
            })
            .map_err(DatabaseError::add_context(format!(
                "Querying all requests for collection `{}`",
                self.collection_id
            )))
            .traced()
    }

    /// Add a new exchange to history. The HTTP engine is responsible for
    /// inserting its own exchanges. Only requests that received a valid HTTP
    /// response should be stored. In-flight requests, invalid requests, and
    /// requests that failed to complete (e.g. because of a network error)
    /// should not (and cannot) be stored.
    pub fn insert_exchange(
        &self,
        exchange: &Exchange,
    ) -> Result<(), DatabaseError> {
        debug!(
            id = %exchange.id,
            status = exchange.response.status.as_u16(),
            recipe = %exchange.request.recipe_id,
            "Adding exchange to database",
        );
        self.database
            .connection()
            .execute(
                "INSERT INTO
                requests_v2 (
                    id,
                    collection_id,
                    profile_id,
                    recipe_id,
                    start_time,
                    end_time,
                    http_version,
                    method,
                    url,
                    request_headers,
                    request_body_kind,
                    request_body,
                    status_code,
                    response_headers,
                    response_body
                )
                VALUES (
                    :id,
                    :collection_id,
                    :profile_id,
                    :recipe_id,
                    :start_time,
                    :end_time,
                    :http_version,
                    :method,
                    :url,
                    :request_headers,
                    :request_body_kind,
                    :request_body,
                    :status_code,
                    :response_headers,
                    :response_body
                )",
                named_params! {
                    ":id": exchange.id,
                    ":collection_id": self.collection_id,
                    ":profile_id": &exchange.request.profile_id,
                    ":recipe_id": &exchange.request.recipe_id,
                    ":start_time": &exchange.start_time,
                    ":end_time": &exchange.end_time,

                    ":http_version": exchange.request.http_version,
                    ":method": exchange.request.method,
                    ":url": exchange.request.url.as_str(),
                    ":request_headers": SqlWrap(&exchange.request.headers),
                    ":request_body_kind": exchange.request.body.discriminant(),
                    ":request_body": exchange.request.body.bytes(),

                    ":status_code": exchange.response.status.as_u16(),
                    ":response_headers": SqlWrap(&exchange.response.headers),
                    ":response_body": exchange.response.body.bytes().deref(),
                },
            )
            .map_err({
                DatabaseError::add_context(format!(
                    "Inserting request `{}`",
                    exchange.id
                ))
            })
            .traced()?;
        Ok(())
    }

    /// Delete all requests for a recipe+profile combo. Return the IDs of the
    /// deleted requests
    pub fn delete_recipe_requests(
        &self,
        profile_id: ProfileFilter,
        recipe_id: &RecipeId,
    ) -> Result<Vec<RequestId>, DatabaseError> {
        info!(
            collection = ?self.metadata(),
            %recipe_id,
            ?profile_id,
            "Deleting all requests for recipe+profile",
        );
        self.database
            .connection()
            .prepare(
                "DELETE FROM requests_v2 WHERE collection_id = :collection_id
                    AND (:ignore_profile_id OR profile_id IS :profile_id)
                    AND recipe_id = :recipe_id
                RETURNING id",
            )
            .and_then(|mut stmt| {
                stmt.query_map(
                    named_params! {
                        ":collection_id": self.collection_id,
                        // `IS` needed for profile_id so `None` will match
                        // `NULL`. We want to dynamically ignore the profile
                        // filter if the user is asking for all profiles.
                        // Dynamically modifying the query is really ugly so the
                        // easiest thing is to use an additional parameter to
                        // bypass the filter
                        ":ignore_profile_id": profile_id == ProfileFilter::All,
                        ":profile_id": profile_id,
                        ":recipe_id": recipe_id,
                    },
                    |row| row.get::<_, RequestId>("id"),
                )?
                .collect::<rusqlite::Result<Vec<_>>>()
            })
            .map_err({
                DatabaseError::add_context(format!(
                    "Deleting requests for recipe `{recipe_id}`"
                ))
            })
            .traced()
    }

    /// Delete a single request by ID
    pub fn delete_request(
        &self,
        request_id: RequestId,
    ) -> Result<(), DatabaseError> {
        info!(
            collection = ?self.metadata(),
            %request_id,
            "Deleting request"
        );
        self.database
            .connection()
            .execute(
                "DELETE FROM requests_v2 WHERE id = :request_id",
                named_params! {":request_id": request_id},
            )
            .map_err({
                DatabaseError::add_context(format!(
                    "Deleting request `{request_id}`"
                ))
            })
            .traced()?;
        Ok(())
    }

    /// Get the value of a UI state field. Key type is included as part of the
    /// key, to disambiguate between keys of identical structure
    pub fn get_ui(
        &self,
        key_type: &str,
        key: &str,
    ) -> Result<Option<String>, DatabaseError> {
        let value = self
            .database
            .connection()
            .query_row(
                "SELECT value FROM ui_state_v2
                WHERE collection_id = :collection_id
                    AND key_type = :key_type
                    AND key = :key",
                named_params! {
                    ":collection_id": self.collection_id,
                    ":key_type": key_type,
                    ":key": &key,
                },
                |row| row.get("value"),
            )
            .optional()
            .map_err(DatabaseError::add_context(format!(
                "Querying UI state key `{key:?}`"
            )))
            .traced()?;
        Ok(value)
    }

    /// Set all UI state fields
    ///
    /// This is done as a bulk update because the DB insert is a significant
    /// portion of the overall TUI frame time. Batching all writes into a single
    /// query/transaction speeds it up a bit. The persistence store is
    /// responsible for batching all the rows together and calling this at
    /// the end.
    pub fn set_ui(&self, settings: &[UiSetting]) -> Result<(), DatabaseError> {
        let mut conn = self.database.connection();
        conn.transaction()
            .and_then(|tx| {
                let mut stmt = tx.prepare(
                    // Upsert!
                    "INSERT INTO ui_state_v2
                        (collection_id, key_type, key, value)
                    VALUES (:collection_id, :key_type, :key, :value)
                    ON CONFLICT DO UPDATE SET value = excluded.value",
                )?;

                for setting in settings {
                    stmt.execute(named_params! {
                        ":collection_id": self.collection_id,
                        ":key_type": setting.key_type,
                        ":key": setting.key,
                        ":value": setting.value,
                    })?;
                }
                drop(stmt);
                tx.commit()
            })
            .map_err(DatabaseError::add_context("Inserting UI state key"))
            .traced()?;
        Ok(())
    }

    /// Insert a query/export command into the command history table. Commands
    /// are deduped in history, so if it's already in the table, just update
    /// the timestamp on it
    pub fn insert_command(&self, command: &str) -> Result<(), DatabaseError> {
        // Shitty try block
        let deleted = (|| {
            let connection = self.database.connection();
            connection.execute(
                // Holy fuck it's an upsert!!
                // Also, delete any commands beyond the max size
                "INSERT INTO commands (collection_id, command, time)
                VALUES (:collection_id, :command, :time)
                ON CONFLICT DO UPDATE SET time = :time",
                named_params! {
                    ":collection_id": self.collection_id,
                    ":command": command,
                    ":time": Utc::now(),
                },
            )?;
            // Delete oldest commands beyond max size. Cap is PER COLLECTION!!
            //
            // SQLite supports LIMIT/OFFSET directly in the DELETE which would
            // work here, but it requires a compile-time option to be enabled.
            // The CTE is just easier that fucking around with that.
            // https://sqlite.org/compile.html#enable_update_delete_limit
            connection.execute(
                "WITH to_delete AS
                    (SELECT command FROM commands WHERE
                        collection_id = :collection_id
                        ORDER BY time DESC
                        LIMIT -1 OFFSET :max_history_size)
                DELETE FROM commands WHERE command IN to_delete",
                named_params! {
                    ":collection_id": self.collection_id,
                    ":max_history_size": MAX_COMMAND_HISTORY_SIZE
                },
            )
        })()
        .map_err({
            DatabaseError::add_context(format!(
                "Inserting command `{command:?}`"
            ))
        })
        .traced()?;

        if deleted > 0 {
            debug!("Evicted {deleted} rows from `commands` table");
        }

        Ok(())
    }

    /// Get historical query/export commands that start with the given prefix.
    /// Results will be ordered descending by their most recent execution time
    /// (most recent commands first).
    pub fn get_commands(
        &self,
        prefix: &str,
    ) -> Result<Vec<String>, DatabaseError> {
        self.database
            .connection()
            .prepare(
                "SELECT command FROM commands
                WHERE collection_id = :collection_id
                    AND command LIKE :prefix || '%'
                ORDER BY time DESC",
            )?
            .query_map(
                named_params! {
                    ":collection_id": self.collection_id,
                    ":prefix": prefix,
                },
                |row| row.get("command"),
            )
            .map_err(DatabaseError::add_context(format!(
                "Querying commands with prefix `{prefix}`",
            )))
            .and_then(|cursor| {
                cursor.collect::<rusqlite::Result<Vec<_>>>().map_err(
                    DatabaseError::add_context("Extracting command history"),
                )
            })
            .traced()
    }

    /// Get a command from the command history table
    ///
    /// ## Params
    ///
    /// - `offset`: Index of the command to grab, with 0 being the most recent
    ///   and moving back in time from there
    /// - `exclude`: Command string to exclude from the results. The command
    ///   prompt excludes the current text from results to prevent duplicates.
    pub fn get_command(
        &self,
        offset: u32,
        exclude: &str,
    ) -> Result<Option<String>, DatabaseError> {
        self.database
            .connection()
            .query_row(
                "SELECT command FROM commands
                WHERE collection_id = :collection_id AND command != :exclude
                ORDER BY time DESC
                LIMIT 1 OFFSET :offset",
                named_params! {
                    ":collection_id": self.collection_id,
                    ":offset": offset,
                    ":exclude": exclude,
                },
                |row| row.get("command"),
            )
            .optional()
            .map_err(DatabaseError::add_context("Querying commands"))
            .traced()
    }

    /// Get the unique ID of this collection
    pub fn collection_id(&self) -> CollectionId {
        self.collection_id
    }
}

/// A unique ID for a collection. This is generated when the collection is
/// inserted into the DB.
#[derive(
    Copy, Clone, Debug, derive_more::Display, derive_more::FromStr, PartialEq,
)]
#[cfg_attr(any(test, feature = "test"), derive(Eq, Hash))]
pub struct CollectionId(Uuid);

impl CollectionId {
    fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

/// Info about a collection from the database
#[derive(Clone, Debug)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
pub struct CollectionMetadata {
    pub id: CollectionId,
    pub path: PathBuf,
    pub name: Option<String>,
}

impl CollectionMetadata {
    /// Get the name if available, otherwise fall back to the path
    pub fn display_name(&self) -> String {
        self.name
            .clone()
            .unwrap_or_else(|| self.path.display().to_string())
    }
}

#[cfg(any(test, feature = "test"))]
impl slumber_util::Factory for Database {
    fn factory((): ()) -> Self {
        let mut connection = Connection::open_in_memory().unwrap();
        Self::migrate(&mut connection).unwrap();
        Self {
            connection: Arc::new(Mutex::new(connection)),
        }
    }
}

#[cfg(any(test, feature = "test"))]
impl slumber_util::Factory for CollectionDatabase {
    fn factory((): ()) -> Self {
        use slumber_util::paths::get_repo_root;
        Database::factory(())
            .into_collection(
                &CollectionFile::new(Some(get_repo_root().join("slumber.yml")))
                    .unwrap(),
            )
            .expect("Error initializing DB collection")
    }
}

/// Define how to filter requests by profile
#[derive(Clone, Debug, Default, PartialEq)]
pub enum ProfileFilter<'a> {
    /// Show requests with _no_ associated profile
    None,
    /// Show requests for a particular profile
    Some(Cow<'a, ProfileId>),
    /// Show requests for all profiles
    #[default]
    All,
}

impl ProfileFilter<'_> {
    /// Does the profile ID match this filter?
    pub fn matches(&self, profile_id: Option<&ProfileId>) -> bool {
        match self {
            Self::None => profile_id.is_none(),
            Self::Some(expected) => profile_id == Some(expected),
            Self::All => true,
        }
    }

    /// Get a `'static` copy of this filter. If the filter is a borrowed profile
    /// ID, it will be cloned
    pub fn into_owned(self) -> ProfileFilter<'static> {
        match self {
            Self::None => ProfileFilter::None,
            Self::Some(profile_id) => {
                ProfileFilter::Some(Cow::Owned(profile_id.into_owned()))
            }
            Self::All => ProfileFilter::All,
        }
    }
}

/// Convert from a specific profile
impl<'a> From<&'a ProfileId> for ProfileFilter<'a> {
    fn from(profile_id: &'a ProfileId) -> Self {
        Self::Some(Cow::Borrowed(profile_id))
    }
}

/// Convert from an option that defines either *no* profile or a specific one
impl<'a> From<Option<&'a ProfileId>> for ProfileFilter<'a> {
    fn from(value: Option<&'a ProfileId>) -> Self {
        match value {
            Some(profile_id) => Self::Some(Cow::Borrowed(profile_id)),
            None => Self::None,
        }
    }
}

/// Convert from a specific profile
impl From<ProfileId> for ProfileFilter<'static> {
    fn from(profile_id: ProfileId) -> Self {
        Self::Some(Cow::Owned(profile_id))
    }
}

/// Convert from an option that defines either *no* profile or a specific one
impl From<Option<ProfileId>> for ProfileFilter<'static> {
    fn from(value: Option<ProfileId>) -> Self {
        match value {
            Some(profile_id) => Self::Some(Cow::Owned(profile_id)),
            None => Self::None,
        }
    }
}

/// Useful for CLI arguments
impl From<Option<Option<ProfileId>>> for ProfileFilter<'static> {
    fn from(value: Option<Option<ProfileId>>) -> Self {
        match value {
            Some(Some(profile_id)) => Self::Some(Cow::Owned(profile_id)),
            Some(None) => Self::None,
            None => Self::All,
        }
    }
}

/// A row in the ui_state table
///
/// [CollectionDatabase::set_ui] takes a list of settings. This struct is a
/// single element in that list.
pub struct UiSetting {
    pub key_type: &'static str,
    pub key: String,
    pub value: String,
}

/// Any error that can occur while accessing the local database
#[derive(Debug, Error)]
pub enum DatabaseError {
    /// An error with additional context attached
    #[error("{context}")]
    Context { context: String, error: Box<Self> },

    /// Error creating the parent directory of the DB file
    #[error("Error creating data directory")]
    Directory(#[source] io::Error),

    /// Error applying migrations to the DBs
    #[error(transparent)]
    Migrate(rusqlite_migration::Error),

    /// Error getting the path for a collection file
    #[error("Getting collection path `{}`", path.display())]
    Path {
        path: PathBuf,
        #[source]
        error: io::Error,
    },

    /// Queried for some resource by a unique identifier, but it wasn't found
    /// in the DB
    #[error("Unknown {kind} `{id}`")]
    ResourceUnknown { kind: &'static str, id: String },

    /// Any SQL error. Generally this should be wrapped in a [Self::Context]
    #[error(transparent)]
    Sqlite(#[from] rusqlite::Error),
}

impl DatabaseError {
    /// Create a function that will attach the given context to an error.
    /// Convenient to pass to [Result::map_err].
    pub fn add_context<Ctx, E>(context: Ctx) -> impl FnOnce(E) -> Self
    where
        Ctx: Into<String>,
        E: Into<Self>,
    {
        move |error| Self::Context {
            context: context.into(),
            error: Box::new(error.into()),
        }
    }

    /// Attach context to an error error
    pub fn with_context<Ctx, E>(error: E, context: Ctx) -> Self
    where
        Ctx: Into<String>,
        E: Into<Self>,
    {
        Self::Context {
            context: context.into(),
            error: Box::new(error.into()),
        }
    }
}