testtrim 0.14.8

Intelligently select automated tests to run via code coverage analysis
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
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
// SPDX-FileCopyrightText: 2024 Mathieu Fenniak <mathieu@fenniak.net>
//
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::{
    coverage::{
        commit_coverage_data::{
            CommitCoverageData, CoverageIdentifier, FileCoverage, FileReference, FunctionCoverage,
        },
        full_coverage_data::FullCoverageData,
        tag::SortedTagArray,
    },
    platform::{TestIdentifier, TestPlatform},
};

use diesel::{
    connection::{Instrumentation, SimpleConnection as _},
    prelude::*,
};
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
use log::trace;
use serde::{Serialize, de::DeserializeOwned};
use std::{
    collections::HashMap,
    env::{self, VarError},
    fs, io,
    path::{Path, PathBuf},
    str::FromStr,
    time::Duration,
};
use thiserror::Error;
use time::{OffsetDateTime, PrimitiveDateTime};
use tokio::sync::{Mutex, MutexGuard, OnceCell};
use uuid::Uuid;

use super::{
    CoverageDatabase, CoverageDatabaseDetailedError, CoverageDatabaseError, ResultWithContext, Tag,
};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../db/sqlite/migrations");

struct DbLogger;

impl Instrumentation for DbLogger {
    fn on_connection_event(&mut self, event: diesel::connection::InstrumentationEvent<'_>) {
        trace!("DB event: {event:?}");
    }
}

#[derive(Error, Debug)]
pub enum DefaultDatabaseError {
    #[error("unset environment variable: `{0}`")]
    EnvironmentVariableError(#[from] VarError),
    #[error("i/o error: `{0}`")]
    IoError(#[from] io::Error),
}

impl From<diesel::result::Error> for CoverageDatabaseError {
    fn from(value: diesel::result::Error) -> Self {
        CoverageDatabaseError::DatabaseError(value.to_string())
    }
}

impl From<diesel::result::Error> for CoverageDatabaseDetailedError {
    fn from(value: diesel::result::Error) -> Self {
        CoverageDatabaseDetailedError {
            error: CoverageDatabaseError::DatabaseError(value.to_string()),
            context: None,
        }
    }
}

impl From<diesel::ConnectionError> for CoverageDatabaseError {
    fn from(value: diesel::ConnectionError) -> Self {
        CoverageDatabaseError::DatabaseError(value.to_string())
    }
}

impl From<diesel_migrations::MigrationError> for CoverageDatabaseError {
    fn from(value: diesel_migrations::MigrationError) -> Self {
        CoverageDatabaseError::DatabaseError(value.to_string())
    }
}

impl From<uuid::Error> for CoverageDatabaseError {
    fn from(value: uuid::Error) -> Self {
        CoverageDatabaseError::ParsingError(value.to_string())
    }
}

impl From<uuid::Error> for CoverageDatabaseDetailedError {
    fn from(value: uuid::Error) -> Self {
        CoverageDatabaseDetailedError {
            error: CoverageDatabaseError::ParsingError(value.to_string()),
            context: None,
        }
    }
}

pub struct DieselCoverageDatabase {
    database_url: String,
    connection: OnceCell<Mutex<SqliteConnection>>,
}

impl DieselCoverageDatabase {
    pub fn new_sqlite_from_default_url() -> Result<DieselCoverageDatabase, DefaultDatabaseError> {
        let target = match (env::var("XDG_CACHE_HOME"), env::var("LOCALAPPDATA")) {
            (Ok(xdg), _) => Path::new(&xdg).join("testtrim").join("testtrim.db"),
            (Err(_), Ok(localappdata)) => Path::new(&localappdata)
                .join("testtrim")
                .join("testtrim.db"),
            (Err(_), Err(_)) => Path::new(&env::var("HOME")?)
                .join(".cache")
                .join("testtrim")
                .join("testtrim.db"),
        };

        fs::create_dir_all(target.parent().unwrap()).or_else(|e| {
            if e.kind() == io::ErrorKind::AlreadyExists {
                Ok(())
            } else {
                Err(e)
            }
        })?;

        Ok(DieselCoverageDatabase::new_sqlite(String::from(
            target.to_string_lossy(),
        )))
    }

    pub fn new_sqlite(database_url: String) -> DieselCoverageDatabase {
        DieselCoverageDatabase {
            database_url,
            connection: OnceCell::new(),
        }
    }

    async fn get_connection(
        &self,
    ) -> Result<MutexGuard<'_, SqliteConnection>, CoverageDatabaseDetailedError> {
        Ok(self
            .connection
            .get_or_try_init(async || {
                // Create a new connection if it doesn't exist
                let mut connection = SqliteConnection::establish(&self.database_url)
                    .context("connecting to the database")?;
                connection.set_instrumentation(DbLogger {});

                connection.batch_execute(
                    "
                PRAGMA journal_mode = WAL;
                PRAGMA synchronous = OFF; -- don't fsync; let OS handle it
                PRAGMA wal_autocheckpoint = 1000;
                PRAGMA wal_checkpoint(TRUNCATE);
                ",
                )?;
                // FIXME: maybe enable foreign_keys -- but would have a performance hit so it might not be important for now
                Ok::<_, CoverageDatabaseDetailedError>(Mutex::new(connection))
            })
            .await?
            .lock()
            .await)
    }

    fn save_test_case_file_coverage<TI: TestIdentifier, CI: CoverageIdentifier>(
        conn: &mut SqliteConnection,
        test_case_execution_id: &Uuid,
        test_case: &TI,
        coverage_data: &CommitCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        use crate::schema::test_case_file_covered;

        if let Some(files_covered) = coverage_data.executed_test_to_files_map().get(test_case) {
            let mut tuples = vec![];
            for tc in files_covered {
                tuples.push((
                    test_case_file_covered::dsl::test_case_execution_id
                        .eq(test_case_execution_id.to_string()),
                    test_case_file_covered::dsl::file_identifier.eq(tc.to_string_lossy()), // FIXME: lossy?
                ));
            }
            for chunk in tuples.chunks(10000) {
                // HACK: avoid "too many SQL variables"
                diesel::insert_into(test_case_file_covered::dsl::test_case_file_covered)
                    .values(chunk)
                    .execute(conn)
                    .context("bulk insert into test_case_file_covered")?;
            }
        }

        Ok(())
    }

    fn save_test_case_function_coverage<TI: TestIdentifier, CI: CoverageIdentifier>(
        conn: &mut SqliteConnection,
        test_case_execution_id: &Uuid,
        test_case: &TI,
        coverage_data: &CommitCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        use crate::schema::test_case_function_covered;

        if let Some(functions_covered) = coverage_data
            .executed_test_to_functions_map()
            .get(test_case)
        {
            let mut tuples = vec![];
            for tc in functions_covered {
                tuples.push((
                    test_case_function_covered::dsl::test_case_execution_id
                        .eq(test_case_execution_id.to_string()),
                    test_case_function_covered::dsl::function_identifier.eq(tc),
                ));
            }
            for chunk in tuples.chunks(10000) {
                // HACK: avoid "too many SQL variables"
                diesel::insert_into(test_case_function_covered::dsl::test_case_function_covered)
                    .values(chunk)
                    .execute(conn)
                    .context("bulk insert into test_case_function_covered")?;
            }
        }

        Ok(())
    }

    fn save_test_case_coverage_identifiers<TI: TestIdentifier, CI: CoverageIdentifier>(
        conn: &mut SqliteConnection,
        test_case_execution_id: &Uuid,
        test_case: &TI,
        coverage_data: &CommitCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        use crate::schema::test_case_coverage_identifier_covered;

        if let Some(coverage_identifiers) = coverage_data
            .executed_test_to_coverage_identifier_map()
            .get(test_case)
        {
            let mut tuples = vec![];
            for tc in coverage_identifiers {
                tuples.push((
                    test_case_coverage_identifier_covered::dsl::test_case_execution_id
                        .eq(test_case_execution_id.to_string()),
                    test_case_coverage_identifier_covered::dsl::coverage_identifier
                        .eq(serde_json::to_string(&tc)?),
                ));
            }
            for chunk in tuples.chunks(10000) {
                // HACK: avoid "too many SQL variables"
                diesel::insert_into(test_case_coverage_identifier_covered::dsl::test_case_coverage_identifier_covered)
                    .values(chunk)
                    .execute(conn)
                    .context("bulk insert into test_case_coverage_identifier_covered")?;
            }
        }

        Ok(())
    }

    /// Now we build a comprehensive full test map by reading the ancestor commit's `coverage_map_test_case_executed`,
    /// and replacing any test cases in it from the commit that we just ran.  If there was no ancestor commit test map,
    /// then our current test suite should-be/must-be complete and we use that for `coverage_map_test_case_executed`.
    ///
    /// Hypothetically this entire operation could be done DB-side with a few commands, but:
    /// - `SQLite` doesn't have a server-side UUID generate function, preventing this from working with creating new IDs
    ///   in the denormalized tables
    /// - It's moderately complex to do in SQL, and when operating through the wet-noodle of a query builder it's even
    ///   more complex
    fn save_coverage_map<TI: TestIdentifier, CI: CoverageIdentifier>(
        conn: &mut SqliteConnection,
        scm_commit_id: Uuid,
        ancestor_scm_commit_id: Option<&String>,
        test_case_id_to_test_case_map: &HashMap<Uuid, &TI>,
        test_case_to_test_case_id_map: &HashMap<&TI, Uuid>,
        test_case_to_test_case_execution_id_map: &HashMap<&TI, Uuid>,
        coverage_data: &CommitCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        use crate::schema::{
            commit_file_reference, coverage_map, coverage_map_test_case_executed,
            test_case_execution,
        };

        let coverage_map_id = Uuid::new_v4();
        diesel::insert_into(coverage_map::dsl::coverage_map)
            .values((
                coverage_map::dsl::id.eq(coverage_map_id.to_string()),
                coverage_map::dsl::scm_commit_id.eq(scm_commit_id.to_string()),
            ))
            .execute(conn)
            .context("insert into coverage_map")?;

        #[derive(Queryable, Selectable)]
        #[diesel(table_name = crate::schema::test_case_execution)]
        struct TestCaseExecution {
            id: String,
            test_case_id: String,
        }
        let ancestor_test_cases: Option<Vec<TestCaseExecution>> = match ancestor_scm_commit_id {
            Some(ref ancestor_scm_commit_id) => {
                let scm_commit_id = coverage_map::dsl::coverage_map
                    .inner_join(
                        coverage_map_test_case_executed::dsl::coverage_map_test_case_executed
                            .inner_join(test_case_execution::dsl::test_case_execution),
                    )
                    .filter(coverage_map::dsl::scm_commit_id.eq(ancestor_scm_commit_id))
                    .select(TestCaseExecution::as_select())
                    .get_results(conn)
                    .context(
                        "loading coverage_map_test_case_executed from ancestor_commit_identifier",
                    )?;
                Some(scm_commit_id)
            }
            None => None,
        };
        let ancestor_test_cases = ancestor_test_cases.unwrap_or_default();

        let mut coverage_map_test_case_executed = HashMap::new();
        // Insert all the ancestor test executions.
        for test_case_execution in ancestor_test_cases {
            // Even if it's part of the ancestor commit, if it doesn't exist anymore in this commit let's not copy it
            // forward; this indicates a test case removed since the ancestor.
            if test_case_id_to_test_case_map
                .contains_key(&Uuid::parse_str(&test_case_execution.test_case_id)?)
            {
                coverage_map_test_case_executed
                    .insert(test_case_execution.test_case_id, test_case_execution.id);
            }
        }
        // Overwrite with all the test executions that we stored in this commit.
        for tc in coverage_data.executed_test_set() {
            let test_case_id = test_case_to_test_case_id_map
                .get(tc)
                .expect("populated earlier");
            let test_case_execution_id = test_case_to_test_case_execution_id_map
                .get(&tc)
                .expect("populated earlier");
            coverage_map_test_case_executed
                .insert(test_case_id.to_string(), test_case_execution_id.to_string());
        }

        let mut insertables = vec![];
        for test_case_execution_id in coverage_map_test_case_executed.values() {
            insertables.push((
                coverage_map_test_case_executed::dsl::coverage_map_id
                    .eq(coverage_map_id.to_string()),
                coverage_map_test_case_executed::dsl::test_case_execution_id
                    .eq(test_case_execution_id),
            ));
        }
        for chunk in insertables.chunks(10000) {
            // HACK: avoid "too many SQL variables"
            diesel::insert_into(
                coverage_map_test_case_executed::dsl::coverage_map_test_case_executed,
            )
            .values(chunk)
            .execute(conn)
            .context("insert into coverage_map_test_case_executed")?;
        }

        #[derive(Queryable, Selectable, Debug)]
        #[diesel(table_name = crate::schema::commit_file_reference)]
        struct CommitFileReference {
            referencing_filepath: String,
            target_filepath: String,
        }
        let ancestor_file_references: Option<Vec<CommitFileReference>> =
            match ancestor_scm_commit_id {
                Some(ref ancestor_scm_commit_id) => {
                    let scm_commit_id = commit_file_reference::dsl::commit_file_reference
                        .filter(
                            commit_file_reference::dsl::scm_commit_id.eq(ancestor_scm_commit_id),
                        )
                        .select(CommitFileReference::as_select())
                        .order(commit_file_reference::dsl::referencing_filepath)
                        .get_results(conn)
                        .context("loading commit_file_reference from ancestor_commit_identifier")?;
                    Some(scm_commit_id)
                }
                None => None,
            };
        let ancestor_file_references = ancestor_file_references.unwrap_or_default();
        let mut file_references_map: HashMap<String, Vec<String>> = HashMap::new();
        for anc in ancestor_file_references {
            file_references_map
                .entry(anc.referencing_filepath)
                .or_default()
                .push(anc.target_filepath);
        }
        // Overwrite file_references_map with any data that is in coverage_data; if a referencing file is in
        // coverage_data then it is understood to be a complete set of files referenced by that file.
        for (referencing_file, target_files) in coverage_data.file_references_files_map() {
            // FIXME: lossy PathBuf -> String
            let vec = target_files
                .iter()
                .map(|p| p.to_string_lossy().to_string())
                .collect();
            file_references_map.insert(referencing_file.to_string_lossy().to_string(), vec);
        }
        let mut insertables = vec![];
        for (referencing_file, target_files) in file_references_map {
            for target_file in target_files {
                insertables.push((
                    commit_file_reference::dsl::id.eq(Uuid::new_v4().to_string()),
                    commit_file_reference::dsl::scm_commit_id.eq(scm_commit_id.to_string()),
                    commit_file_reference::dsl::referencing_filepath.eq(referencing_file.clone()),
                    commit_file_reference::dsl::target_filepath.eq(target_file),
                ));
            }
        }
        for chunk in insertables.chunks(10000) {
            // HACK: avoid "too many SQL variables"
            diesel::insert_into(commit_file_reference::dsl::commit_file_reference)
                .values(chunk)
                .execute(conn)
                .context("insert into commit_file_reference")?;
        }

        Ok(())
    }

    fn ensure_project_id(
        conn: &mut SqliteConnection,
        project_name: &str,
    ) -> Result<Uuid, CoverageDatabaseDetailedError> {
        use crate::schema::project;

        let project_id = project::dsl::project
            .filter(project::dsl::name.eq(project_name))
            .select(project::dsl::id)
            .get_result::<String>(conn)
            .optional()
            .context("loading project_id")?;

        if let Some(project_id) = project_id {
            Ok(Uuid::from_str(&project_id)?)
        } else {
            let project_id = Uuid::new_v4();
            diesel::insert_into(project::dsl::project)
                .values((
                    project::dsl::id.eq(project_id.to_string()),
                    project::dsl::name.eq(project_name),
                ))
                .execute(conn)
                .context("insert into project")?;
            Ok(project_id)
        }
    }
}

impl CoverageDatabase for DieselCoverageDatabase {
    // impl CoverageDatabase<TI, CI> for DieselCoverageDatabase {
    async fn save_coverage_data<TP>(
        &self,
        project_name: &str,
        coverage_data: &CommitCoverageData<TP::TI, TP::CI>,
        commit_identifier: &str,
        ancestor_commit_identifier: Option<&str>,
        tags: &[Tag],
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TP: TestPlatform,
        TP::TI: TestIdentifier + Serialize + DeserializeOwned,
        TP::CI: CoverageIdentifier + Serialize + DeserializeOwned,
    {
        use crate::schema::{
            commit_test_case, commit_test_case_executed, scm_commit, test_case, test_case_execution,
        };

        // SQLite isn't going to do a JSON-aware comparison when searching, so we use a sorted serialization to try to
        // get consistent outputs that will work for a bare string comparison.
        let tags_string = serde_json::to_string(&SortedTagArray(tags))?;
        let mut conn_guard = self.get_connection().await?;
        let conn = &mut *conn_guard;

        conn.run_pending_migrations(MIGRATIONS).map_err(|e| {
            CoverageDatabaseError::DatabaseError(format!("failed to run pending migrations: {e}"))
        })?;

        // FIXME: ideally all of this should happen in a transaction, but I'm not sure it matters for SQLite

        let project_id = Self::ensure_project_id(conn, project_name)?;

        // In case this was a "re-run" on a commit, delete the old associated data.  Ideally this delete should
        // cascade...
        //
        // FIXME: does cascade delete require PRAGMA foreign_keys = ON?
        diesel::delete(
            scm_commit::dsl::scm_commit
                .filter(scm_commit::dsl::scm_identifier.eq(commit_identifier))
                .filter(scm_commit::dsl::tags.eq(&tags_string)),
        )
        .execute(conn)
        .context("delete from scm_commit")?;

        let ancestor_scm_commit_id: Option<String> = match ancestor_commit_identifier {
            Some(ancestor_commit_identifier) => {
                let scm_commit_id = scm_commit::dsl::scm_commit
                    .filter(scm_commit::dsl::scm_identifier.eq(ancestor_commit_identifier))
                    .filter(scm_commit::dsl::tags.eq(&tags_string))
                    .select(scm_commit::dsl::id)
                    .get_result::<String>(conn)
                    .context("loading scm_commit from ancestor_commit_identifier")?;
                Some(scm_commit_id)
            }
            None => None,
        };

        let scm_commit_id = Uuid::new_v4();
        diesel::insert_into(scm_commit::dsl::scm_commit)
            .values((
                scm_commit::dsl::id.eq(scm_commit_id.to_string()),
                scm_commit::dsl::project_id.eq(project_id.to_string()),
                scm_commit::dsl::ancestor_scm_commit_id.eq(&ancestor_scm_commit_id),
                scm_commit::dsl::scm_identifier.eq(commit_identifier),
                scm_commit::dsl::tags.eq(&tags_string),
            ))
            .execute(conn)
            .context("insert into scm_commit")?;

        let mut test_case_to_test_case_id_map = HashMap::new();
        let mut test_case_id_to_test_case_map = HashMap::new();
        let mut test_case_to_test_case_execution_id_map = HashMap::new();

        #[derive(Queryable, Selectable)]
        #[diesel(table_name = crate::schema::test_case)]
        struct TestCase {
            id: String,
            test_identifier: String,
        }
        let project_test_cases = test_case::dsl::test_case
            .filter(test_case::dsl::project_id.eq(project_id.to_string()))
            .select(TestCase::as_select())
            .get_results(conn)
            .context("loading project_test_cases")?;
        for project_test_case in project_test_cases {
            let test_identifier: TP::TI = serde_json::from_str(&project_test_case.test_identifier)?;
            if let Some(stored_ti) = coverage_data.existing_test_set().get(&test_identifier) {
                let test_case_id = Uuid::parse_str(&project_test_case.id)?;
                test_case_to_test_case_id_map.insert(stored_ti, test_case_id);
                test_case_id_to_test_case_map.insert(test_case_id, stored_ti);
            }
        }

        let mut insertables = vec![];
        for tc in coverage_data.existing_test_set() {
            if !test_case_to_test_case_id_map.contains_key(tc) {
                // New test identifier...
                let test_case_id = Uuid::new_v4();
                insertables.push((
                    test_case::dsl::id.eq(test_case_id.to_string()),
                    test_case::dsl::project_id.eq(project_id.to_string()),
                    test_case::dsl::test_identifier.eq(serde_json::to_string(&tc)?),
                ));
                test_case_to_test_case_id_map.insert(tc, test_case_id);
                test_case_id_to_test_case_map.insert(test_case_id, tc);
            }
        }
        for chunk in insertables.chunks(10000) {
            diesel::insert_into(test_case::dsl::test_case)
                .values(chunk)
                .execute(conn)
                .context("insert into test_case")?;
        }

        // Batch insert into commit_test_case...
        let mut insertables = vec![];
        for tc in coverage_data.existing_test_set() {
            let test_case_id = test_case_to_test_case_id_map
                .get(tc)
                .expect("populated earlier");
            insertables.push((
                commit_test_case::dsl::scm_commit_id.eq(scm_commit_id.to_string()),
                commit_test_case::dsl::test_case_id.eq(test_case_id.to_string()),
            ));
        }
        for chunk in insertables.chunks(10000) {
            // HACK: avoid "too many SQL variables"
            diesel::insert_into(commit_test_case::dsl::commit_test_case)
                .values(chunk)
                .execute(conn)
                .context("insert into commit_test_case")?;
        }

        let mut test_case_execution_insertables = vec![];
        let mut commit_test_case_insertables = vec![];
        for tc in coverage_data.existing_test_set() {
            if coverage_data.executed_test_set().contains(tc) {
                let test_case_id = test_case_to_test_case_id_map
                    .get(tc)
                    .expect("populated earlier");

                let test_case_execution_id = Uuid::new_v4();
                test_case_to_test_case_execution_id_map.insert(tc, test_case_execution_id);
                test_case_execution_insertables.push((
                    test_case_execution::dsl::id.eq(test_case_execution_id.to_string()),
                    test_case_execution::dsl::test_case_id.eq(test_case_id.to_string()),
                ));

                commit_test_case_insertables.push((
                    commit_test_case_executed::dsl::test_case_execution_id
                        .eq(test_case_execution_id.to_string()),
                    commit_test_case_executed::dsl::scm_commit_id.eq(scm_commit_id.to_string()),
                ));

                DieselCoverageDatabase::save_test_case_file_coverage(
                    conn,
                    &test_case_execution_id,
                    tc,
                    coverage_data,
                )?;

                DieselCoverageDatabase::save_test_case_function_coverage(
                    conn,
                    &test_case_execution_id,
                    tc,
                    coverage_data,
                )?;

                DieselCoverageDatabase::save_test_case_coverage_identifiers(
                    conn,
                    &test_case_execution_id,
                    tc,
                    coverage_data,
                )?;
            }
        }

        for chunk in test_case_execution_insertables.chunks(10000) {
            diesel::insert_into(test_case_execution::dsl::test_case_execution)
                .values(chunk)
                .execute(conn)
                .context("bulk insert into test_case_execution")?;
        }
        for chunk in commit_test_case_insertables.chunks(10000) {
            diesel::insert_into(commit_test_case_executed::dsl::commit_test_case_executed)
                .values(chunk)
                .execute(conn)
                .context("bulk insert into commit_test_case_executed")?;
        }

        DieselCoverageDatabase::save_coverage_map(
            conn,
            scm_commit_id,
            ancestor_scm_commit_id.as_ref(),
            &test_case_id_to_test_case_map,
            &test_case_to_test_case_id_map,
            &test_case_to_test_case_execution_id_map,
            coverage_data,
        )?;

        Ok(())
    }

    async fn read_coverage_data<TP>(
        &self,
        project_name: &str,
        commit_identifier: &str,
        tags: &[Tag],
    ) -> Result<Option<FullCoverageData<TP::TI, TP::CI>>, CoverageDatabaseDetailedError>
    where
        TP: TestPlatform,
        TP::TI: TestIdentifier + Serialize + DeserializeOwned,
        TP::CI: CoverageIdentifier + Serialize + DeserializeOwned,
    {
        use crate::schema::{
            commit_file_reference, coverage_map, coverage_map_test_case_executed, scm_commit,
            test_case, test_case_coverage_identifier_covered, test_case_execution,
            test_case_file_covered, test_case_function_covered,
        };

        // SQLite isn't going to do a JSON-aware comparison when searching, so we use a sorted serialization to try to
        // get consistent outputs that will work for a bare string comparison.
        let tags_string = serde_json::to_string(&SortedTagArray(tags))?;
        let mut conn_guard = self.get_connection().await?;
        let conn = &mut *conn_guard;

        conn.run_pending_migrations(MIGRATIONS).map_err(|e| {
            CoverageDatabaseError::DatabaseError(format!("failed to run pending migrations: {e}"))
        })?;

        let project_id = Self::ensure_project_id(conn, project_name)?;

        let coverage_map_id = coverage_map::dsl::coverage_map
            .inner_join(scm_commit::dsl::scm_commit)
            .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
            .filter(scm_commit::dsl::scm_identifier.eq(&commit_identifier))
            .filter(scm_commit::dsl::tags.eq(&tags_string))
            .select(coverage_map::dsl::id)
            .get_result::<String>(conn)
            .optional()
            .context("loading coverage_map_id")?;
        if coverage_map_id.is_none() {
            // This commit doesn't have any data.
            return Ok(None);
        }

        let now = OffsetDateTime::now_utc();
        let now = PrimitiveDateTime::new(now.date(), now.time());
        diesel::update(coverage_map::dsl::coverage_map)
            .set(coverage_map::dsl::last_read_timestamp.eq(now))
            .filter(coverage_map::dsl::id.eq(coverage_map_id.unwrap()))
            .execute(conn)
            .context("update coverage_map.last_read_timestamp")?;

        // FIXME: typing isn't helping us; this type is (test_case_id, test_identifier)
        let all_test_cases = coverage_map::dsl::coverage_map
            .inner_join(scm_commit::dsl::scm_commit)
            .inner_join(
                coverage_map_test_case_executed::dsl::coverage_map_test_case_executed.inner_join(
                    test_case_execution::dsl::test_case_execution
                        .inner_join(test_case::dsl::test_case),
                ),
            )
            .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
            .filter(scm_commit::dsl::scm_identifier.eq(&commit_identifier))
            .select((test_case::dsl::id, test_case::dsl::test_identifier))
            .get_results::<(String, String)>(conn)
            .context("loading test cases from coverage_map for commit_identifier")?;

        let mut coverage_data = FullCoverageData::new();

        if all_test_cases.is_empty() {
            // The denormalized coverage data was saved but was empty -- there were no test cases at all.
            return Ok(Some(coverage_data));
        }

        let mut test_case_id_to_test_identifier_map = HashMap::new();
        for (test_case_id, test_identifier) in all_test_cases {
            let test_identifier: TP::TI = serde_json::from_str(&test_identifier)?;
            test_case_id_to_test_identifier_map.insert(test_case_id, test_identifier.clone());
            coverage_data.add_existing_test(test_identifier);
        }

        // FIXME: typing isn't helping us; this type is (test_case_id, file_identifier)
        let all_files_by_test_case = test_case_file_covered::dsl::test_case_file_covered
            .inner_join(test_case_execution::dsl::test_case_execution.inner_join(
                coverage_map_test_case_executed::dsl::coverage_map_test_case_executed.inner_join(
                    coverage_map::dsl::coverage_map.inner_join(scm_commit::dsl::scm_commit),
                ),
            ))
            .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
            .filter(scm_commit::dsl::scm_identifier.eq(&commit_identifier))
            .select((
                test_case_execution::dsl::test_case_id,
                test_case_file_covered::dsl::file_identifier,
            ))
            .get_results::<(String, String)>(conn)
            .context(
                "loading from test_case_file_covered for commit_identifier via coverage_map",
            )?;
        for (test_case_id, file_identifier) in all_files_by_test_case {
            coverage_data.add_file_to_test(FileCoverage {
                test_identifier: test_case_id_to_test_identifier_map
                    .get(&test_case_id)
                    .expect("test_case_id_to_test_identifier_map lookup")
                    .clone(),
                file_name: PathBuf::from(file_identifier),
            });
        }

        // FIXME: typing isn't helping us; this type is (test_case_id, function_identifier)
        let all_functions_by_test_case =
            test_case_function_covered::dsl::test_case_function_covered
                .inner_join(
                    test_case_execution::dsl::test_case_execution.inner_join(
                        coverage_map_test_case_executed::dsl::coverage_map_test_case_executed
                            .inner_join(
                                coverage_map::dsl::coverage_map
                                    .inner_join(scm_commit::dsl::scm_commit),
                            ),
                    ),
                )
                .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
                .filter(scm_commit::dsl::scm_identifier.eq(&commit_identifier))
                .select((
                    test_case_execution::dsl::test_case_id,
                    test_case_function_covered::dsl::function_identifier,
                ))
                .get_results::<(String, String)>(conn)
                .context(
                    "loading from test_case_function_covered for commit_identifier via coverage_map",
                )?;
        for (test_case_id, function_identifier) in all_functions_by_test_case {
            coverage_data.add_function_to_test(FunctionCoverage {
                test_identifier: test_case_id_to_test_identifier_map
                    .get(&test_case_id)
                    .expect("test_case_id_to_test_identifier_map lookup")
                    .clone(),
                function_name: function_identifier,
            });
        }

        // FIXME: typing isn't helping us; this type is (test_case_id, function_identifier)
        let all_coverage_identifiers_by_test_case =
            test_case_coverage_identifier_covered::dsl::test_case_coverage_identifier_covered
                .inner_join(
                    test_case_execution::dsl::test_case_execution.inner_join(
                        coverage_map_test_case_executed::dsl::coverage_map_test_case_executed
                            .inner_join(
                                coverage_map::dsl::coverage_map
                                    .inner_join(scm_commit::dsl::scm_commit),
                            ),
                    ),
                )
                .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
                .filter(scm_commit::dsl::scm_identifier.eq(&commit_identifier))
                .select((
                    test_case_execution::dsl::test_case_id,
                    test_case_coverage_identifier_covered::dsl::coverage_identifier,
                ))
                .get_results::<(String, String)>(conn)
                .context("loading from test_case_coverage_identifier_covered for commit_identifier via coverage_map")?;
        for (test_case_id, coverage_identifier) in all_coverage_identifiers_by_test_case {
            coverage_data.add_heuristic_coverage_to_test(
                test_case_id_to_test_identifier_map
                    .get(&test_case_id)
                    .expect("test_case_id_to_test_identifier_map lookup")
                    .clone(),
                serde_json::from_str(&coverage_identifier)?,
            );
        }

        #[derive(Queryable, Selectable)]
        #[diesel(table_name = crate::schema::commit_file_reference)]
        struct CommitFileReference {
            referencing_filepath: String,
            target_filepath: String,
        }
        let all_referenced_files = commit_file_reference::dsl::commit_file_reference
            .inner_join(scm_commit::dsl::scm_commit)
            .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
            .filter(scm_commit::dsl::scm_identifier.eq(&commit_identifier))
            .select(CommitFileReference::as_select())
            .get_results::<CommitFileReference>(conn)
            .context("loading from commit_file_reference for commit_identifier")?;
        for fr in all_referenced_files {
            coverage_data.add_file_reference(FileReference {
                referencing_file: fr.referencing_filepath.into(),
                target_file: fr.target_filepath.into(),
            });
        }

        Ok(Some(coverage_data))
    }

    async fn has_any_coverage_data<TP: TestPlatform>(
        &self,
        project_name: &str,
    ) -> Result<bool, CoverageDatabaseDetailedError> {
        use crate::schema::{coverage_map, scm_commit};

        let mut conn_guard = self.get_connection().await?;
        let conn = &mut *conn_guard;

        conn.run_pending_migrations(MIGRATIONS).map_err(|e| {
            CoverageDatabaseError::DatabaseError(format!("failed to run pending migrations: {e}"))
        })?;

        // FIXME: theoretically should filter to just the data from TestPlatform

        let project_id = Self::ensure_project_id(conn, project_name)?;
        let coverage_map_id = coverage_map::dsl::coverage_map
            .inner_join(scm_commit::dsl::scm_commit)
            .filter(scm_commit::dsl::project_id.eq(project_id.to_string()))
            .select(coverage_map::dsl::id)
            .limit(1)
            .first::<String>(conn)
            .optional()
            .context("loading denormalized_coverage_map_id")?;

        Ok(coverage_map_id.is_some())
    }

    async fn clear_project_data<TP: TestPlatform>(
        &self,
        // FIXME: this cleanup isn't specific to the project being run, but should be; the problem is that without FK's
        // being enforced we can't use cascade deletes and instead have to do ugly stuff ourselves.  It's probably
        // OK-ish given the limited contexts in which the SQLite driver will be used -- testing and small-scale use --
        // but should eventually be fixed since a run-server backing on SQLite is plausible.
        _project_name: &str,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        use crate::schema::{
            commit_test_case, commit_test_case_executed, coverage_map,
            coverage_map_test_case_executed, project, scm_commit, test_case,
            test_case_coverage_identifier_covered, test_case_execution, test_case_file_covered,
            test_case_function_covered,
        };

        let mut conn_guard = self.get_connection().await?;
        let conn = &mut *conn_guard;

        conn.run_pending_migrations(MIGRATIONS).map_err(|e| {
            CoverageDatabaseError::DatabaseError(format!("failed to run pending migrations: {e}"))
        })?;

        // FIXME: theoretically should filter to just the data from TestPlatform
        diesel::delete(coverage_map_test_case_executed::dsl::coverage_map_test_case_executed)
            .execute(conn)?;
        diesel::delete(coverage_map::dsl::coverage_map).execute(conn)?;
        diesel::delete(
            test_case_coverage_identifier_covered::dsl::test_case_coverage_identifier_covered,
        )
        .execute(conn)?;
        diesel::delete(test_case_file_covered::dsl::test_case_file_covered).execute(conn)?;
        diesel::delete(test_case_function_covered::dsl::test_case_function_covered)
            .execute(conn)?;
        diesel::delete(commit_test_case_executed::dsl::commit_test_case_executed).execute(conn)?;
        diesel::delete(test_case_execution::dsl::test_case_execution).execute(conn)?;
        diesel::delete(commit_test_case::dsl::commit_test_case).execute(conn)?;
        diesel::delete(test_case::dsl::test_case).execute(conn)?;
        diesel::delete(scm_commit::dsl::scm_commit).execute(conn)?;
        diesel::delete(project::dsl::project).execute(conn)?;

        Ok(())
    }

    async fn intermittent_clean(
        &self,
        older_than: &Duration,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        use crate::schema::coverage_map;

        let mut conn_guard = self.get_connection().await?;
        let conn = &mut *conn_guard;

        let now = OffsetDateTime::now_utc();
        let now = PrimitiveDateTime::new(now.date(), now.time());
        let cutoff_time = now
            .checked_sub(time::Duration::seconds_f64(older_than.as_secs_f64()))
            .ok_or_else(|| {
                CoverageDatabaseError::DatabaseError(String::from("time overflow occurred"))
            })?;

        diesel::delete(coverage_map::dsl::coverage_map)
            .filter(coverage_map::dsl::last_read_timestamp.lt(cutoff_time))
            .execute(conn)
            .context("delete in intermittent_clean")?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use anyhow::Result;
    use time::macros::{date, time};

    use super::*;
    use crate::{
        coverage::{commit_coverage_data::CommitCoverageData, db_tests},
        platform::rust::{RustCoverageIdentifier, RustTestIdentifier, RustTestPlatform},
    };

    #[tokio::test]
    async fn has_any_coverage_data_false() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::has_any_coverage_data_false(db).await;
    }

    #[tokio::test]
    async fn save_empty() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::save_empty(db).await;
    }

    #[tokio::test]
    async fn has_any_coverage_data_true() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::has_any_coverage_data_true(db).await;
    }

    #[tokio::test]
    async fn load_empty() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::load_empty(db).await;
    }

    #[tokio::test]
    async fn load_updates_last_read_timestamp() {
        use crate::schema::*;

        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));

        let saved_data = CommitCoverageData::<RustTestIdentifier, RustCoverageIdentifier>::new();
        let result = db
            .save_coverage_data::<RustTestPlatform>("testtrim-tests", &saved_data, "c1", None, &[])
            .await;
        assert!(result.is_ok());

        let first_timestamp = {
            let mut conn_guard = db.get_connection().await.unwrap();
            let conn = &mut *conn_guard;

            let data = coverage_map::dsl::coverage_map
                .inner_join(scm_commit::dsl::scm_commit)
                .select((
                    coverage_map::dsl::id,
                    coverage_map::dsl::last_read_timestamp,
                ))
                .get_result::<(String, PrimitiveDateTime)>(conn)
                .optional();
            assert!(data.is_ok());
            let data = data.unwrap();
            assert!(data.is_some());
            let data = data.unwrap();
            data.1
        };

        let result = db
            .read_coverage_data::<RustTestPlatform>("testtrim-tests", "c1", &[])
            .await;
        assert!(result.is_ok());
        let result = result.unwrap();
        assert!(result.is_some());

        let second_timestamp = {
            let mut conn_guard = db.get_connection().await.unwrap();
            let conn = &mut *conn_guard;

            let data = coverage_map::dsl::coverage_map
                .inner_join(scm_commit::dsl::scm_commit)
                .select((
                    coverage_map::dsl::id,
                    coverage_map::dsl::last_read_timestamp,
                ))
                .get_result::<(String, PrimitiveDateTime)>(conn)
                .optional();
            assert!(data.is_ok());
            let data = data.unwrap();
            assert!(data.is_some());
            let data = data.unwrap();
            data.1
        };
        println!("{first_timestamp:?}");
        println!("{second_timestamp:?}");
        assert!(
            second_timestamp > first_timestamp,
            "second timestamp > first timestamp"
        );
    }

    #[tokio::test]
    async fn intermittent_clean() -> Result<()> {
        use crate::schema::*;

        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));

        let saved_data = CommitCoverageData::<RustTestIdentifier, RustCoverageIdentifier>::new();
        let result = db
            .save_coverage_data::<RustTestPlatform>("testtrim-tests", &saved_data, "d1", None, &[])
            .await;
        assert!(result.is_ok());

        let saved_data = CommitCoverageData::<RustTestIdentifier, RustCoverageIdentifier>::new();
        let result = db
            .save_coverage_data::<RustTestPlatform>("testtrim-tests", &saved_data, "d2", None, &[])
            .await;
        assert!(result.is_ok());

        let check_data = async || {
            let mut conn_guard = db.get_connection().await.unwrap();
            let conn = &mut *conn_guard;

            coverage_map::dsl::coverage_map
                .inner_join(scm_commit::dsl::scm_commit)
                .select((
                    coverage_map::dsl::id,
                    coverage_map::dsl::last_read_timestamp,
                ))
                .filter(scm_commit::dsl::scm_identifier.eq_any(vec!["d1", "d2"]))
                .get_results::<(String, PrimitiveDateTime)>(conn)
        };

        let data = check_data().await?;
        assert_eq!(data.len(), 2);

        let do_tweak = async || {
            let mut conn_guard = db.get_connection().await.unwrap();
            let conn = &mut *conn_guard;

            let time = PrimitiveDateTime::new(date!(2019 - 01 - 01), time!(0:00));
            diesel::update(coverage_map::dsl::coverage_map)
                .set(coverage_map::dsl::last_read_timestamp.eq(time))
                .filter(coverage_map::dsl::id.eq(&data.first().unwrap().0))
                .execute(conn)
                .context("update coverage_map.last_read_timestamp")
        };
        do_tweak().await?;

        db.intermittent_clean(&Duration::from_secs(1800)).await?;

        let data = check_data().await?;
        assert_eq!(data.len(), 1);

        Ok(())
    }

    #[tokio::test]
    async fn save_and_load_no_ancestor() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::save_and_load_no_ancestor(db).await;
    }

    /// Test an additive-only child coverage data set -- no overwrite/replacement of the ancestor
    #[tokio::test]
    async fn save_and_load_new_case_in_child() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::save_and_load_new_case_in_child(db).await;
    }

    /// Test a replacement-only child coverage data set -- the same test was run with new coverage data in the child
    #[tokio::test]
    async fn save_and_load_replacement_case_in_child() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::save_and_load_replacement_case_in_child(db).await;
    }

    /// Test a child coverage set which indicates a test was removed and no longer present
    #[tokio::test]
    async fn save_and_load_removed_case_in_child() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::save_and_load_removed_case_in_child(db).await;
    }

    /// Test that we can remove file references from an ancestor
    #[tokio::test]
    async fn remove_file_references_in_child() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::remove_file_references_in_child(db).await;
    }

    /// Test that save and load use independent data based upon tags
    #[tokio::test]
    async fn independent_tags() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::independent_tags(db).await;
    }

    #[tokio::test]
    async fn load_first_case() {
        let db = DieselCoverageDatabase::new_sqlite(String::from(":memory:"));
        db_tests::load_first_case(db).await;
    }
}