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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
// SPDX-FileCopyrightText: 2024 Mathieu Fenniak <mathieu@fenniak.net>
//
// SPDX-License-Identifier: GPL-3.0-or-later

use log::debug;
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
use sqlx::{
    Executor, Pool, Postgres, Transaction,
    postgres::{PgPoolOptions, types::PgInterval},
};
use std::{collections::HashMap, path::PathBuf, time::Duration};
use tokio::sync::OnceCell;
use uuid::Uuid;

use crate::platform::{TestIdentifier, TestPlatform};

use super::{
    CoverageDatabase, CoverageDatabaseDetailedError, CoverageDatabaseError, ResultWithContext, Tag,
    commit_coverage_data::{
        CommitCoverageData, CoverageIdentifier, FileCoverage, FileReference, FunctionCoverage,
    },
    full_coverage_data::FullCoverageData,
    tag::TagArray,
};

pub struct PostgresCoverageDatabase {
    database_url: String,
    connection: OnceCell<Pool<Postgres>>,
}

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

impl From<sqlx::migrate::MigrateError> for CoverageDatabaseError {
    fn from(value: sqlx::migrate::MigrateError) -> Self {
        CoverageDatabaseError::DatabaseError(value.to_string())
    }
}

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

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

type TestCaseToIdMap<'a, TI> = HashMap<&'a TI, Uuid>;
type IdToTestCaseMap<'a, TI> = HashMap<Uuid, &'a TI>;

impl PostgresCoverageDatabase {
    pub fn new(database_url: String) -> PostgresCoverageDatabase {
        PostgresCoverageDatabase {
            database_url,
            connection: OnceCell::new(),
        }
    }

    async fn get_pool(&self) -> Result<&Pool<Postgres>, CoverageDatabaseError> {
        self.connection
            .get_or_try_init(|| async {
                let pool = PgPoolOptions::new()
                    .max_connections(5)
                    .connect(&self.database_url)
                    .await?;
                sqlx::migrate!("../db/postgres/migrations")
                    .run(&pool)
                    .await?;
                Ok(pool)
                // *connection = Some(pool);
            })
            .await
    }

    async fn upsert_project<'e, E>(
        executor: E,
        project_name: &str,
    ) -> Result<Uuid, CoverageDatabaseDetailedError>
    where
        E: Executor<'e, Database = Postgres> + Send,
    {
        let record = sqlx::query!(
            r"
                INSERT INTO project (id, name)
                VALUES (uuid_generate_v4(), $1)
                ON CONFLICT (name)
                    -- 'do nothing' but returning the record's id rather than omitting row
                    DO UPDATE SET name = EXCLUDED.name
                RETURNING id
            ",
            project_name
        )
        .fetch_one(executor)
        .await
        .context("upsert into project")?;
        Ok(record.id)
    }

    async fn delete_old_commit_data(
        tx: &mut Transaction<'static, Postgres>,
        project_id: &Uuid,
        commit_identifier: &str,
        tags: &[Tag],
    ) -> Result<(), CoverageDatabaseDetailedError> {
        let tag_value = serde_json::to_value(TagArray(tags))?;
        sqlx::query!(
            r"DELETE FROM scm_commit WHERE scm_identifier = $1 AND project_id = $2 AND tags = $3",
            Value::String(String::from(commit_identifier)),
            project_id,
            tag_value,
        )
        .execute(&mut **tx)
        .await
        .context("delete from scm_commit")?;
        Ok(())
    }

    async fn load_ancestor_scm_commit_id(
        tx: &mut Transaction<'static, Postgres>,
        project_id: &Uuid,
        ancestor_commit_identifier: Option<&str>,
        tags: &[Tag],
    ) -> Result<Option<Uuid>, CoverageDatabaseDetailedError> {
        let tag_value = serde_json::to_value(TagArray(tags))?;
        match ancestor_commit_identifier {
            Some(ancestor_commit_identifier) => {
                let scm_commit_id = sqlx::query!(
                    r"SELECT id FROM scm_commit WHERE scm_identifier = $1 AND project_id = $2 AND tags = $3",
                    Value::String(String::from(ancestor_commit_identifier)),
                    project_id,
                    tag_value,
                )
                .fetch_one(&mut **tx)
                .await
                .context("loading scm_commit from ancestor_commit_identifier")?;

                Ok(Some(scm_commit_id.id))
            }
            None => Ok(None),
        }
    }

    async fn create_scm_commit(
        tx: &mut Transaction<'static, Postgres>,
        project_id: &Uuid,
        ancestor_scm_commit_id: Option<Uuid>,
        commit_identifier: &str,
        tags: &[Tag],
    ) -> Result<Uuid, CoverageDatabaseDetailedError> {
        let tag_value = serde_json::to_value(TagArray(tags))?;
        Ok(sqlx::query!(
            r"
            INSERT INTO scm_commit
                (id, project_id, ancestor_scm_commit_id, scm_identifier, tags)
            VALUES
                (uuid_generate_v4(), $1, $2, $3, $4)
            RETURNING id
            ",
            project_id,
            ancestor_scm_commit_id,
            Value::String(String::from(commit_identifier)),
            tag_value,
        )
        .fetch_one(&mut **tx)
        .await
        .context("insert into scm_commit")?
        .id)
    }

    async fn load_relevant_test_case_ids<'a, TI, CI>(
        tx: &mut Transaction<'static, Postgres>,
        project_id: &Uuid,
        coverage_data: &'a CommitCoverageData<TI, CI>,
    ) -> Result<(TestCaseToIdMap<'a, TI>, IdToTestCaseMap<'a, TI>), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier + DeserializeOwned,
        CI: CoverageIdentifier + DeserializeOwned,
    {
        let mut test_case_to_test_case_id_map = HashMap::new();
        let mut test_case_id_to_test_case_map = HashMap::new();

        let project_test_cases = sqlx::query!(
            r"
            SELECT test_case.id, test_case.test_identifier
            FROM test_case
            WHERE project_id = $1
            ",
            project_id,
        )
        .fetch_all(&mut **tx)
        .await
        .context("loading project_test_cases")?;
        for project_test_case in project_test_cases {
            let test_identifier: TI = serde_json::from_value(project_test_case.test_identifier)?;
            if let Some(stored_ti) = coverage_data.existing_test_set().get(&test_identifier) {
                let test_case_id = 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);
            }
        }

        Ok((test_case_to_test_case_id_map, test_case_id_to_test_case_map))
    }

    async fn insert_missing_test_cases<'a, TI, CI>(
        tx: &mut Transaction<'static, Postgres>,
        project_id: &Uuid,
        test_case_to_test_case_id_map: &mut HashMap<&'a TI, Uuid>,
        test_case_id_to_test_case_map: &mut HashMap<Uuid, &'a TI>,
        coverage_data: &'a CommitCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let mut test_case_id_vec = vec![];
        let mut test_case_identifiers_vec = 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(); // would be neat to move to server-side gen, but, no reason
                test_case_id_vec.push(test_case_id);
                test_case_identifiers_vec.push(serde_json::to_value(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);
            }
        }
        sqlx::query!(
            r"
            INSERT INTO test_case
                (id, test_identifier, project_id)
            SELECT
                *, $1
            FROM
                UNNEST($2::uuid[], $3::jsonb[])
            ",
            project_id,
            &test_case_id_vec,
            &test_case_identifiers_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into test_case")?;

        Ok(())
    }

    async fn insert_commit_test_cases<'a, TI, CI>(
        tx: &mut Transaction<'static, Postgres>,
        scm_commit_id: &Uuid,
        test_case_to_test_case_id_map: &HashMap<&'a TI, Uuid>,
        coverage_data: &'a CommitCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        // Batch insert into commit_test_case...
        let mut test_case_id_vec = 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");
            test_case_id_vec.push(*test_case_id);
        }
        sqlx::query!(
            r"
            INSERT INTO commit_test_case
                (scm_commit_id, test_case_id)
            SELECT
                $1, *
            FROM
                UNNEST($2::uuid[])
            ",
            scm_commit_id,
            &test_case_id_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into commit_test_case")?;

        Ok(())
    }

    async fn save_normalized_coverage_data<'a, TI, CI>(
        tx: &mut Transaction<'static, Postgres>,
        scm_commit_id: &Uuid,
        test_case_to_test_case_id_map: &HashMap<&'a TI, Uuid>,
        coverage_data: &'a CommitCoverageData<TI, CI>,
    ) -> Result<HashMap<&'a TI, Uuid>, CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let mut test_case_to_test_case_execution_id_map = HashMap::new();

        let mut insert_test_case_execution_id_vec = vec![];
        let mut insert_test_case_execution_test_case_id_vec = vec![];

        let mut insert_commit_test_case_execution_id_vec = vec![];

        let mut insert_test_case_file_covered_id_vec = vec![];
        let mut insert_test_case_file_covered_file_identifier_vec = vec![];

        let mut insert_test_case_func_covered_id_vec = vec![];
        let mut insert_test_case_func_covered_func_identifier_vec = vec![];

        let mut insert_test_case_ci_covered_id_vec = vec![];
        let mut insert_test_case_ci_covered_cis_vec = vec![];

        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 = Uuid::new_v4();
            test_case_to_test_case_execution_id_map.insert(tc, test_case_execution_id);

            insert_test_case_execution_id_vec.push(test_case_execution_id);
            insert_test_case_execution_test_case_id_vec.push(*test_case_id);
            insert_commit_test_case_execution_id_vec.push(test_case_execution_id);

            if let Some(files_covered) = coverage_data.executed_test_to_files_map().get(tc) {
                for path in files_covered {
                    insert_test_case_file_covered_id_vec.push(test_case_execution_id);
                    insert_test_case_file_covered_file_identifier_vec
                        .push(serde_json::to_value(path.to_string_lossy())?);
                }
            }

            if let Some(funcs_covered) = coverage_data.executed_test_to_functions_map().get(tc) {
                for path in funcs_covered {
                    insert_test_case_func_covered_id_vec.push(test_case_execution_id);
                    insert_test_case_func_covered_func_identifier_vec
                        .push(serde_json::to_value(path)?);
                }
            }

            if let Some(cis_covered) = coverage_data
                .executed_test_to_coverage_identifier_map()
                .get(tc)
            {
                for ci in cis_covered {
                    insert_test_case_ci_covered_id_vec.push(test_case_execution_id);
                    insert_test_case_ci_covered_cis_vec.push(serde_json::to_value(ci)?);
                }
            }
        }

        sqlx::query!(
            r"
            INSERT INTO test_case_execution
                (id, test_case_id)
            SELECT
                *
            FROM
                UNNEST($1::uuid[], $2::uuid[])
            ",
            &insert_test_case_execution_id_vec,
            &insert_test_case_execution_test_case_id_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into test_case_execution")?;
        sqlx::query!(
            r"
            INSERT INTO commit_test_case_executed
                (scm_commit_id, test_case_execution_id)
            SELECT
                $1, *
            FROM
                UNNEST($2::uuid[])
            ",
            scm_commit_id,
            &insert_commit_test_case_execution_id_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into commit_test_case_executed")?;
        sqlx::query!(
            r"
            INSERT INTO test_case_file_covered
                (test_case_execution_id, file_identifier)
            SELECT
                *
            FROM
                UNNEST($1::uuid[], $2::jsonb[])
            ",
            &insert_test_case_file_covered_id_vec,
            &insert_test_case_file_covered_file_identifier_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into test_case_file_covered")?;
        sqlx::query!(
            r"
            INSERT INTO test_case_function_covered
                (test_case_execution_id, function_identifier)
            SELECT
                *
            FROM
                UNNEST($1::uuid[], $2::jsonb[])
            ",
            &insert_test_case_func_covered_id_vec,
            &insert_test_case_func_covered_func_identifier_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into test_case_function_covered")?;
        sqlx::query!(
            r"
            INSERT INTO test_case_coverage_identifier_covered
                (test_case_execution_id, coverage_identifier)
            SELECT
                *
            FROM
                UNNEST($1::uuid[], $2::jsonb[])
            ",
            &insert_test_case_ci_covered_id_vec,
            &insert_test_case_ci_covered_cis_vec,
        )
        .execute(&mut **tx)
        .await
        .context("insert into test_case_coverage_identifier_covered")?;

        Ok(test_case_to_test_case_execution_id_map)
    }

    async fn save_denormalized_coverage_data<TI>(
        tx: &mut Transaction<'static, Postgres>,
        scm_commit_id: &Uuid,
        test_case_to_test_case_execution_id_map: &HashMap<&TI, Uuid>,
        ancestor_scm_commit_id: Option<Uuid>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
    {
        let coverage_map_id = sqlx::query!(
            r"
            INSERT INTO coverage_map (id, scm_commit_id)
            VALUES (uuid_generate_v4(), $1)
            RETURNING id
            ",
            scm_commit_id,
        )
        .fetch_one(&mut **tx)
        .await
        .context("insert into coverage_map")?
        .id;

        let mut test_case_execution_id_vec = vec![];
        for tc_id in test_case_to_test_case_execution_id_map.values() {
            test_case_execution_id_vec.push(*tc_id);
        }

        sqlx::query!(
            r"
            INSERT INTO coverage_map_test_case_executed
                (test_case_execution_id, coverage_map_id)
            SELECT
                *, $1
            FROM
                UNNEST($2::uuid[])
            ",
            coverage_map_id,
            &test_case_execution_id_vec,
        )
        .execute(&mut **tx)
        .await?;

        if ancestor_scm_commit_id.is_some() {
            sqlx::query!(
                r"
                INSERT INTO coverage_map_test_case_executed
                    (test_case_execution_id, coverage_map_id)
                SELECT
                    test_case_execution_id, $2
                FROM
                    coverage_map_test_case_executed
                    INNER JOIN coverage_map ON (coverage_map_test_case_executed.coverage_map_id = coverage_map.id)
                    INNER JOIN test_case_execution ON (coverage_map_test_case_executed.test_case_execution_id = test_case_execution.id)
                WHERE
                    -- Copy-forward everything from the ancestor commit...
                    coverage_map.scm_commit_id = $1 AND

                    -- Unless the same test_case_id is present on the new coverage map already.
                    NOT EXISTS (
                        SELECT 1 FROM
                            coverage_map_test_case_executed inner_executed
                            INNER JOIN test_case_execution inner_execution ON (inner_executed.test_case_execution_id = inner_execution.id)
                        WHERE
                            inner_executed.coverage_map_id = $2 AND
                            inner_execution.test_case_id = test_case_execution.test_case_id
                    ) AND

                    -- And the test case must be a part of the new commit
                    EXISTS (
                        SELECT 1 FROM
                            commit_test_case ctc
                        WHERE
                            ctc.scm_commit_id = $3
                            AND ctc.test_case_id = test_case_execution.test_case_id
                    )
                ",
                ancestor_scm_commit_id,
                coverage_map_id,
                scm_commit_id,
            )
            .execute(&mut **tx)
            .await
            .context("populate denormalized coverage_map_test_case_executed from ancestor commit")?;
        }

        Ok(())
    }

    async fn save_denormalized_file_references<TI, CI>(
        tx: &mut Transaction<'static, Postgres>,
        scm_commit_id: &Uuid,
        coverage_data: &CommitCoverageData<TI, CI>,
        ancestor_scm_commit_id: Option<Uuid>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let mut referencing_filepath_vec = vec![];
        let mut target_filepath_vec = vec![];
        let mut exclude_ancestor_referencing_files = vec![];
        for (referencing_file, target_files) in coverage_data.file_references_files_map() {
            if target_files.is_empty() && ancestor_scm_commit_id.is_some() {
                // This coverage data parsed `referencing_file` and explicitly found that it has no references -- a very
                // common scenario.  If it previously had references in the ancestor commit, we need to make sure we
                // don't copy those forward because of an absence of overwriting data in this commit.
                exclude_ancestor_referencing_files
                    .push(referencing_file.to_string_lossy().to_string());
            } else {
                for target_file in target_files {
                    referencing_filepath_vec.push(referencing_file.to_string_lossy().to_string());
                    target_filepath_vec.push(target_file.to_string_lossy().to_string());
                }
            }
        }

        sqlx::query!(
            r"
            INSERT INTO commit_file_reference
                (referencing_filepath, target_filepath, id, scm_commit_id)
            SELECT
                *, uuid_generate_v4(), $1
            FROM
                UNNEST($2::text[], $3::text[])
            ",
            &scm_commit_id,
            &referencing_filepath_vec,
            &target_filepath_vec
        )
        .execute(&mut **tx)
        .await
        .context("insert into commit_file_reference")?;

        debug!(
            "save_denormalized_file_references may copy-forward from ancestor commit: {ancestor_scm_commit_id:?}"
        );
        if ancestor_scm_commit_id.is_some() {
            sqlx::query!(
                r"
                INSERT INTO commit_file_reference
                    (id, scm_commit_id, referencing_filepath, target_filepath)
                SELECT
                    uuid_generate_v4(), $1, referencing_filepath, target_filepath
                FROM
                    commit_file_reference
                WHERE
                    -- Copy-forward everything from the ancestor commit...
                    commit_file_reference.scm_commit_id = $2 AND

                    -- Unless the same referencing_filepath is present on the new commit already.
                    NOT EXISTS (
                        SELECT 1 FROM
                            commit_file_reference inner_reference
                        WHERE
                            inner_reference.scm_commit_id = $1 AND
                            inner_reference.referencing_filepath = commit_file_reference.referencing_filepath
                    ) AND

                        -- Exclude files that are known to have no references in the new commit
                        NOT (referencing_filepath = ANY ($3::text[]))
                ",
                scm_commit_id,
                ancestor_scm_commit_id,
                &exclude_ancestor_referencing_files,
            )
            .execute(&mut **tx)
            .await
            .context("populate denormalized commit_file_reference from ancestor commit")?;
        }

        Ok(())
    }

    async fn touch_coverage_map(
        pool: &Pool<Postgres>,
        coverage_map_id: &Uuid,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        sqlx::query!(
            r"
            UPDATE coverage_map
            SET last_read_timestamp = NOW()
            WHERE id = $1
            ",
            coverage_map_id
        )
        .execute(pool)
        .await
        .context("UPDATE coverage_map")?;
        Ok(())
    }

    async fn read_coverage_test_cases<TI, CI>(
        pool: &Pool<Postgres>,
        coverage_map_id: &Uuid,
        coverage_data: &mut FullCoverageData<TI, CI>,
        test_case_id_to_test_identifier_map: &mut HashMap<Uuid, TI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier + DeserializeOwned,
        CI: CoverageIdentifier,
    {
        let all_test_cases = sqlx::query!(
            r"
            SELECT
                test_case.id, test_case.test_identifier
            FROM
                coverage_map
                INNER JOIN coverage_map_test_case_executed ON (coverage_map_test_case_executed.coverage_map_id = coverage_map.id)
                INNER JOIN test_case_execution ON (test_case_execution.id = coverage_map_test_case_executed.test_case_execution_id)
                INNER JOIN test_case ON (test_case.id = test_case_execution.test_case_id)
            WHERE
                coverage_map.id = $1
            ",
            coverage_map_id
        )
        .fetch_all(pool)
        .await
        .context("loading test cases from coverage_map")?;

        for record in all_test_cases {
            let test_identifier: TI = serde_json::from_value(record.test_identifier)?;
            test_case_id_to_test_identifier_map.insert(record.id, test_identifier.clone());
            coverage_data.add_existing_test(test_identifier);
        }

        Ok(())
    }

    async fn read_file_coverage_data<TI, CI>(
        pool: &Pool<Postgres>,
        coverage_map_id: &Uuid,
        coverage_data: &mut FullCoverageData<TI, CI>,
        test_case_id_to_test_identifier_map: &HashMap<Uuid, TI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let all_files_by_test_case = sqlx::query!(
            r"
            SELECT
                test_case_execution.test_case_id, test_case_file_covered.file_identifier
            FROM
                coverage_map
                INNER JOIN coverage_map_test_case_executed ON (coverage_map_test_case_executed.coverage_map_id = coverage_map.id)
                INNER JOIN test_case_execution ON (test_case_execution.id = coverage_map_test_case_executed.test_case_execution_id)
                INNER JOIN test_case_file_covered ON (test_case_file_covered.test_case_execution_id = test_case_execution.id)
            WHERE
                coverage_map.id = $1
            ",
            coverage_map_id
        )
        .fetch_all(pool)
        .await
        .context("loading from test_case_file_covered for coverage_map")?;
        for record in all_files_by_test_case {
            coverage_data.add_file_to_test(FileCoverage {
                test_identifier: test_case_id_to_test_identifier_map
                    .get(&record.test_case_id)
                    .expect("test_case_id_to_test_identifier_map lookup")
                    .clone(),
                file_name: PathBuf::from(
                    record
                        .file_identifier
                        .as_str()
                        .expect("file_identifier must be json str"),
                ),
            });
        }
        Ok(())
    }

    async fn read_function_coverage_data<TI, CI>(
        pool: &Pool<Postgres>,
        coverage_map_id: &Uuid,
        coverage_data: &mut FullCoverageData<TI, CI>,
        test_case_id_to_test_identifier_map: &HashMap<Uuid, TI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let all_functions_by_test_case = sqlx::query!(
            r"
            SELECT
                test_case_execution.test_case_id, test_case_function_covered.function_identifier
            FROM
                coverage_map
                INNER JOIN coverage_map_test_case_executed ON (coverage_map_test_case_executed.coverage_map_id = coverage_map.id)
                INNER JOIN test_case_execution ON (test_case_execution.id = coverage_map_test_case_executed.test_case_execution_id)
                INNER JOIN test_case_function_covered ON (test_case_function_covered.test_case_execution_id = test_case_execution.id)
            WHERE
                coverage_map.id = $1
            ",
            coverage_map_id
        )
        .fetch_all(pool)
        .await
        .context("loading from test_case_function_covered for coverage_map")?;
        for record in all_functions_by_test_case {
            coverage_data.add_function_to_test(FunctionCoverage {
                test_identifier: test_case_id_to_test_identifier_map
                    .get(&record.test_case_id)
                    .expect("test_case_id_to_test_identifier_map lookup")
                    .clone(),
                function_name: String::from(
                    record
                        .function_identifier
                        .as_str()
                        .expect("file_identifier must be json str"),
                ),
            });
        }
        Ok(())
    }

    async fn read_coverage_identifier_data<TI, CI>(
        pool: &Pool<Postgres>,
        coverage_map_id: &Uuid,
        coverage_data: &mut FullCoverageData<TI, CI>,
        test_case_id_to_test_identifier_map: &HashMap<Uuid, TI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let all_cis_by_test_case = sqlx::query!(
            r"
            SELECT
                test_case_execution.test_case_id, test_case_coverage_identifier_covered.coverage_identifier
            FROM
                coverage_map
                INNER JOIN coverage_map_test_case_executed ON (coverage_map_test_case_executed.coverage_map_id = coverage_map.id)
                INNER JOIN test_case_execution ON (test_case_execution.id = coverage_map_test_case_executed.test_case_execution_id)
                INNER JOIN test_case_coverage_identifier_covered ON (test_case_coverage_identifier_covered.test_case_execution_id = test_case_execution.id)
            WHERE
                coverage_map.id = $1
            ",
            coverage_map_id
        )
        .fetch_all(pool)
        .await
        .context("loading from test_case_coverage_identifier_covered for coverage_map")?;
        for record in all_cis_by_test_case {
            coverage_data.add_heuristic_coverage_to_test(
                test_case_id_to_test_identifier_map
                    .get(&record.test_case_id)
                    .expect("test_case_id_to_test_identifier_map lookup")
                    .clone(),
                serde_json::from_value(record.coverage_identifier)?,
            );
        }
        Ok(())
    }

    async fn read_referenced_file_data<TI, CI>(
        pool: &Pool<Postgres>,
        project_id: &Uuid,
        commit_identifier: &str,
        coverage_data: &mut FullCoverageData<TI, CI>,
    ) -> Result<(), CoverageDatabaseDetailedError>
    where
        TI: TestIdentifier,
        CI: CoverageIdentifier,
    {
        let all_referenced_files = sqlx::query!(
            r"
            SELECT
                referencing_filepath, target_filepath
            FROM
                commit_file_reference
                INNER JOIN scm_commit ON (commit_file_reference.scm_commit_id = scm_commit.id)
            WHERE
                scm_commit.project_id = $1 AND
                scm_commit.scm_identifier = $2
            ",
            project_id,
            Value::String(String::from(commit_identifier)),
        )
        .fetch_all(pool)
        .await
        .context("loading from test_case_coverage_identifier_covered for coverage_map")?;
        for record in all_referenced_files {
            coverage_data.add_file_reference(FileReference {
                referencing_file: record.referencing_filepath.into(),
                target_file: record.target_filepath.into(),
            });
        }
        Ok(())
    }
}

impl CoverageDatabase for PostgresCoverageDatabase {
    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,
    {
        let tx = self.get_pool().await?;
        let mut tx = tx.begin().await?;

        let project_id = Self::upsert_project(&mut *tx, project_name).await?;
        Self::delete_old_commit_data(&mut tx, &project_id, commit_identifier, tags).await?;
        let ancestor_scm_commit_id = Self::load_ancestor_scm_commit_id(
            &mut tx,
            &project_id,
            ancestor_commit_identifier,
            tags,
        )
        .await?;
        let scm_commit_id = Self::create_scm_commit(
            &mut tx,
            &project_id,
            ancestor_scm_commit_id,
            commit_identifier,
            tags,
        )
        .await?;

        let (mut test_case_to_test_case_id_map, mut test_case_id_to_test_case_map) =
            Self::load_relevant_test_case_ids(&mut tx, &project_id, coverage_data).await?;

        Self::insert_missing_test_cases(
            &mut tx,
            &project_id,
            &mut test_case_to_test_case_id_map,
            &mut test_case_id_to_test_case_map,
            coverage_data,
        )
        .await?;

        Self::insert_commit_test_cases(
            &mut tx,
            &scm_commit_id,
            &test_case_to_test_case_id_map,
            coverage_data,
        )
        .await?;

        let test_case_to_test_case_execution_id_map = Self::save_normalized_coverage_data(
            &mut tx,
            &scm_commit_id,
            &test_case_to_test_case_id_map,
            coverage_data,
        )
        .await?;

        Self::save_denormalized_coverage_data(
            &mut tx,
            &scm_commit_id,
            &test_case_to_test_case_execution_id_map,
            ancestor_scm_commit_id,
        )
        .await?;

        Self::save_denormalized_file_references(
            &mut tx,
            &scm_commit_id,
            coverage_data,
            ancestor_scm_commit_id,
        )
        .await?;

        tx.commit().await?;

        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,
    {
        let pool = self.get_pool().await?;

        let project_id = Self::upsert_project(pool, project_name).await?;

        let tag_value = serde_json::to_value(TagArray(tags))?;
        let coverage_map_id = sqlx::query!(
            r"
            SELECT coverage_map.id
            FROM
                coverage_map
                INNER JOIN scm_commit ON (scm_commit.id = coverage_map.scm_commit_id)
            WHERE
                scm_commit.project_id = $1 AND
                scm_commit.scm_identifier = $2 AND
                scm_commit.tags = $3
            ",
            project_id,
            Value::String(String::from(commit_identifier)),
            tag_value,
        )
        .fetch_optional(pool)
        .await
        .context("loading coverage_map_id")?;
        let Some(coverage_map_id) = coverage_map_id else {
            // This commit doesn't have any data.
            return Ok(None);
        };

        Self::touch_coverage_map(pool, &coverage_map_id.id).await?;

        let mut coverage_data = FullCoverageData::new();
        let mut test_case_id_to_test_identifier_map = HashMap::new();

        Self::read_coverage_test_cases(
            pool,
            &coverage_map_id.id,
            &mut coverage_data,
            &mut test_case_id_to_test_identifier_map,
        )
        .await?;

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

        Self::read_file_coverage_data(
            pool,
            &coverage_map_id.id,
            &mut coverage_data,
            &test_case_id_to_test_identifier_map,
        )
        .await?;

        Self::read_function_coverage_data(
            pool,
            &coverage_map_id.id,
            &mut coverage_data,
            &test_case_id_to_test_identifier_map,
        )
        .await?;

        Self::read_coverage_identifier_data(
            pool,
            &coverage_map_id.id,
            &mut coverage_data,
            &test_case_id_to_test_identifier_map,
        )
        .await?;

        Self::read_referenced_file_data(pool, &project_id, commit_identifier, &mut coverage_data)
            .await?;

        Ok(Some(coverage_data))
    }

    async fn has_any_coverage_data<TP: TestPlatform>(
        &self,
        project_name: &str,
    ) -> Result<bool, CoverageDatabaseDetailedError> {
        let pool = self.get_pool().await?;

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

        let project_id = Self::upsert_project(pool, project_name).await?;
        let coverage_map_id = sqlx::query!(
            r"
            SELECT
                coverage_map.id
            FROM
                coverage_map
                INNER JOIN scm_commit ON (scm_commit.id = coverage_map.scm_commit_id)
            WHERE
                scm_commit.project_id = $1
            LIMIT 1
            ",
            project_id
        )
        .fetch_optional(pool)
        .await?;

        Ok(coverage_map_id.is_some())
    }

    async fn clear_project_data<TP: TestPlatform>(
        &self,
        project_name: &str,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        let pool = self.get_pool().await?;
        // FIXME: theoretically should filter to just the data from TestPlatform
        sqlx::query!("DELETE FROM project WHERE name = $1", project_name)
            .execute(pool)
            .await
            .context("delete in clear_project_data")?;
        Ok(())
    }

    async fn intermittent_clean(
        &self,
        older_than: &Duration,
    ) -> Result<(), CoverageDatabaseDetailedError> {
        let pool = self.get_pool().await?;

        let duration_us = older_than
            .as_micros()
            .try_into()
            .map_err(|e| CoverageDatabaseError::DatabaseError(format!("u182 -> i64 error: {e:?}")))
            .context("converting Duration into PgInterval")?;

        sqlx::query!(
            "DELETE FROM coverage_map WHERE last_read_timestamp < (NOW() - $1::interval)",
            PgInterval {
                months: 0,
                days: 0,
                microseconds: duration_us,
            }
        )
        .execute(pool)
        .await
        .context("delete in intermittent_clean")?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use anyhow::Result;
    use named_lock::NamedLock;
    use serde_json::Value;
    use std::{env, sync::LazyLock, time::Duration};

    use crate::{
        coverage::{
            CoverageDatabase, commit_coverage_data::CommitCoverageData, db_tests,
            postgres_sqlx::PostgresCoverageDatabase,
        },
        platform::rust::{RustCoverageIdentifier, RustTestIdentifier, RustTestPlatform},
    };

    // Avoid running multiple concurrent tests that use the external DB.  An external intra-process lock is used to
    // allow these tests to exclude each other even when run in multiple processes, eg. with testtrim or
    // cargo-nexttest.
    static DB_MUTEX: LazyLock<NamedLock> =
        LazyLock::new(|| NamedLock::create("testtrim-postgres_sqlx-tests").unwrap());

    fn create_test_db() -> PostgresCoverageDatabase {
        let test_db_url = env::var("TESTTRIM_UNITTEST_PGSQL_URL")
            .or(env::var("TESTTRIM_DATABASE_URL"))
            .expect("TESTTRIM_UNITTEST_PGSQL_URL or TESTTRIM_DATABASE_URL must be set for postgres_sqlx tests");

        PostgresCoverageDatabase::new(test_db_url)
    }

    async fn cleanup() {
        sqlx::query!("DELETE FROM project WHERE name = 'testtrim-tests'")
            .execute(create_test_db().get_pool().await.unwrap())
            .await
            .expect("delete stmt failed");
    }

    #[tokio::test]
    async fn has_any_coverage_data_false() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        db_tests::has_any_coverage_data_false(db).await;
    }

    #[tokio::test]
    async fn save_empty() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        db_tests::save_empty(db).await;
    }

    #[tokio::test]
    async fn has_any_coverage_data_true() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        db_tests::has_any_coverage_data_true(db).await;
    }

    #[tokio::test]
    async fn load_empty() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        db_tests::load_empty(db).await;
    }

    #[tokio::test]
    async fn load_updates_last_read_timestamp() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;

        let ts_fetcher = async |db: &mut PostgresCoverageDatabase| {
            let pool = db.get_pool().await.unwrap();

            let coverage_map = sqlx::query!(
                r"
                SELECT
                    coverage_map.id, coverage_map.last_read_timestamp
                FROM
                    coverage_map
                    INNER JOIN scm_commit ON (scm_commit.id = coverage_map.scm_commit_id)
                    INNER JOIN project ON (project.id = scm_commit.project_id)
                WHERE
                    project.name = $1 AND
                    scm_commit.scm_identifier = $2
                ",
                "testtrim-tests",
                Value::String(String::from("c1"))
            )
            .fetch_optional(pool)
            .await;

            assert!(coverage_map.is_ok());
            let coverage_map = coverage_map.unwrap();
            assert!(coverage_map.is_some());
            let coverage_map = coverage_map.unwrap();
            coverage_map.last_read_timestamp
        };

        let mut db = create_test_db();

        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 = ts_fetcher(&mut db).await;

        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 = ts_fetcher(&mut db).await;
        println!("{first_timestamp:?}");
        println!("{second_timestamp:?}");
        assert!(
            second_timestamp > first_timestamp,
            "second timestamp > first timestamp"
        );
    }

    #[tokio::test]
    async fn intermittent_clean() -> Result<()> {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;

        let mut db = create_test_db();

        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 |db: &mut PostgresCoverageDatabase| {
            let pool = db.get_pool().await.unwrap();
            sqlx::query!(r#"SELECT coverage_map.* FROM coverage_map INNER JOIN scm_commit ON (scm_commit.id = coverage_map.scm_commit_id) WHERE scm_commit.scm_identifier IN ('"d1"', '"d2"')"#)
            .fetch_all(pool)
            .await
        };
        let data = check_data(&mut db).await?;
        assert_eq!(data.len(), 2);

        let do_tweak = async |db: &mut PostgresCoverageDatabase| {
            let pool = db.get_pool().await.unwrap();
            sqlx::query!(
                r"
                UPDATE coverage_map SET last_read_timestamp = '2024-01-01' WHERE id = $1
                ",
                data.get(0).unwrap().id
            )
            .execute(pool)
            .await
        };
        do_tweak(&mut db).await?;

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

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

        Ok(())
    }

    #[tokio::test]
    async fn save_and_load_no_ancestor() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        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_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        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_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        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_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        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_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        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_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        db_tests::independent_tags(db).await;
    }

    #[tokio::test]
    async fn load_first_case() {
        let _db_mutex = DB_MUTEX.lock();
        cleanup().await;
        let db = create_test_db();
        db_tests::load_first_case(db).await;
    }
}