tsk-bin 0.11.0

tsk — work with a clear context
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
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
//! The Dolt backend: the ledger is a Dolt database in a directory beside the managed
//! repo's other state, published to the same git ref the file backend uses through
//! Dolt's `git+` remotes. Every operation runs the external `dolt` binary. Threads,
//! continuation entries, bindings and events are rows in the tables defined in
//! [`crate::ledger::dolt`]. Documents are not rows: they are Markdown files on a second
//! branch, `tsk/docs`, checked out as a worktree beside the database (see
//! [`crate::ledger::docs`]). A version 1 ledger still keeps them in a `documents` table
//! until `tsk ledger migrate-docs` moves them.

use std::path::{Path, PathBuf};

use crate::ledger::docs;
use crate::ledger::dolt::{
    self, flag, integer, lit, text, Dolt, BRANCH, LEGACY_LAYOUT_VERSION, REMOTE, REMOTE_BRANCH_NAME,
};
use crate::ledger::fetch::{fetch_tip, ref_kind, FetchOutcome, RefKind};
use crate::ledger::git::Git;
use crate::ledger::location::{
    docs_worktree_path, dolt_db_path, state_root_from_env, LedgerLocation, Repo,
};
use crate::ledger::manifest::{self, ManifestInfo, DOLT_SUPPORTED_VERSIONS};
use crate::ledger::push::{PushOutcome, PushResult, PushStatus, MAX_ATTEMPTS};
use crate::mission::model::{
    self, Category, Decision, Document, Mission, Outcome, Question, QuestionStatus, Role, Status,
    Task,
};
use crate::thread::lookup::{CloudBinding, Lookup};

use super::files::FileTree;
use super::{LedgerStore, PushReport, RefreshOutcome, ThreadInfo};

const MANIFEST_SOURCE: &str = "the manifest table";

/// What every command except `tsk ledger fetch` and `tsk ledger migrate-docs` says about
/// a version 1 ledger.
pub const VERSION_1_REFUSAL: &str = "error: this ledger stores its documents in Dolt (version 1). Run tsk ledger migrate-docs to move them to the tsk/docs branch.";

pub struct DoltStore {
    repo: Repo,
    state_root: PathBuf,
    /// Reads from this database instead of the one at the clone's usual path. The
    /// migration uses it to check a database it has built but not yet moved into place.
    db_override: Option<PathBuf>,
}

impl DoltStore {
    pub fn open(dir: &Path) -> Result<DoltStore, String> {
        let repo = Repo::discover(dir)?;
        let state_root = state_root_from_env()?;
        Ok(DoltStore {
            repo,
            state_root,
            db_override: None,
        })
    }

    /// The same store, reading and writing the database at `path`.
    pub fn with_db(mut self, path: PathBuf) -> DoltStore {
        self.db_override = Some(path);
        self
    }

    fn hint(&self) -> Option<PathBuf> {
        self.repo
            .read_clone_id()
            .ok()
            .flatten()
            .map(|id| dolt_db_path(&self.state_root, &id))
    }

    /// The database of an already materialised ledger.
    fn db(&self) -> Result<Dolt, String> {
        if let Some(path) = &self.db_override {
            return Ok(Dolt::new(path));
        }
        let missing = |what: String| {
            format!(
                "error: {}. Run tsk ledger fetch first, which creates the ledger database",
                what
            )
        };
        let clone_id = self.repo.read_clone_id()?.ok_or_else(|| {
            missing(format!(
                "this clone has no clone id yet ({} is absent or empty)",
                self.repo.clone_id_file().display()
            ))
        })?;
        let path = dolt_db_path(&self.state_root, &clone_id);
        if !is_database(&path) {
            return Err(missing(format!("{} does not exist", path.display())));
        }
        Ok(Dolt::new(path))
    }

    fn describe(&self, what: &str) -> String {
        match self.hint() {
            Some(root) => format!("{} in {}", what, root.display()),
            None => what.to_string(),
        }
    }

    fn manifest_rows(&self, db: &Dolt) -> Result<ManifestInfo, String> {
        let rows = db.rows("select k, v from manifest")?;
        let value = |key: &str| {
            rows.iter()
                .find(|row| text(row, "k").as_deref() == Some(key))
                .and_then(|row| text(row, "v"))
        };
        let version = value("version").ok_or_else(|| {
            "error: the ledger's manifest table has no version row; it is not a tsk ledger"
                .to_string()
        })?;
        let version = version.trim().parse::<i64>().map_err(|_| {
            format!(
                "error: the ledger's manifest version '{}' is not a number",
                version
            )
        })?;
        Ok(ManifestInfo {
            version,
            repo_id: value("repo_id"),
        })
    }

    /// The manifest check every Dolt command makes. Versions 1 and 2 pass: it returns the
    /// version, and the caller decides what a version 1 ledger may do.
    fn check_manifest_of(&self, db: &Dolt, location: &LedgerLocation) -> Result<i64, String> {
        let info = self.manifest_rows(db)?;
        manifest::check_values(
            info.version,
            info.repo_id.as_deref(),
            location.repo_id(),
            MANIFEST_SOURCE,
            DOLT_SUPPORTED_VERSIONS,
        )
    }

    /// The manifest check of a command that needs the documents on the branch: a version
    /// 1 ledger is refused with the way out.
    fn check_current(&self, db: &Dolt, location: &LedgerLocation) -> Result<i64, String> {
        let version = self.check_manifest_of(db, location)?;
        if version == LEGACY_LAYOUT_VERSION {
            return Err(VERSION_1_REFUSAL.to_string());
        }
        Ok(version)
    }

    /// The worktree of the documents branch, when this clone has an id.
    fn docs_hint(&self) -> Option<PathBuf> {
        self.repo
            .read_clone_id()
            .ok()
            .flatten()
            .map(|id| docs_worktree_path(&self.state_root, &id))
    }

    /// The documents of an already materialised ledger.
    fn docs_tree(&self) -> Result<FileTree, String> {
        let missing = |what: String| {
            // A version 1 ledger has no documents worktree. Say so, not "fetch first".
            if let Ok(db) = self.db() {
                if self
                    .manifest_rows(&db)
                    .is_ok_and(|info| info.version == LEGACY_LAYOUT_VERSION)
                {
                    return VERSION_1_REFUSAL.to_string();
                }
            }
            format!(
                "error: {}. Run tsk ledger fetch first, which creates the documents worktree",
                what
            )
        };
        let clone_id = self.repo.read_clone_id()?.ok_or_else(|| {
            missing(format!(
                "this clone has no clone id yet ({} is absent or empty)",
                self.repo.clone_id_file().display()
            ))
        })?;
        let root = docs_worktree_path(&self.state_root, &clone_id);
        if !root.is_dir() {
            return Err(missing(format!("{} does not exist", root.display())));
        }
        Ok(FileTree { root, docs: true })
    }

    /// [`LedgerStore::refresh`], or the lenient refresh of `tsk ledger fetch` and
    /// `tsk ledger migrate-docs` when `lenient` is set: a version 1 ledger refreshes its
    /// Dolt database only and gets a note instead of the refusal.
    fn refresh_with(&self, lenient: bool, keep_local: bool) -> Result<RefreshOutcome, String> {
        let (mut outcome, version) = self.refresh_data(keep_local)?;
        if version == LEGACY_LAYOUT_VERSION {
            if !lenient {
                return Err(VERSION_1_REFUSAL.to_string());
            }
            let note = "note: this ledger stores its documents in Dolt (version 1). Run tsk ledger migrate-docs to move them to the tsk/docs branch.".to_string();
            outcome.pending_note = Some(join_notes(outcome.pending_note.take(), note));
            return Ok(outcome);
        }
        let location = self.repo.location(&self.state_root)?;
        let fetched = docs::fetch(&self.repo, &self.state_root, &location, keep_local)?;
        let dolt_unpublished = outcome.unpublished;
        for line in &fetched.pending {
            outcome.pending.push(format!("documents: {}", line));
        }
        let docs_outcome = FetchOutcome {
            location: fetched.location,
            path: fetched.path,
            commit: fetched.commit,
            pending: fetched.pending,
            unpublished: fetched.unpublished,
        };
        if !docs_outcome.pending.is_empty() {
            let note = crate::ledger::pending_note(&docs_outcome);
            outcome.pending_note = Some(join_notes(outcome.pending_note.take(), note));
        }
        // A new ledger says once that nothing is published. The documents branch is
        // named separately only when the ledger is published and its documents are not.
        if docs_outcome.unpublished && !dolt_unpublished {
            let note = crate::ledger::unpublished_note(&docs_outcome);
            outcome.unpublished_note = Some(join_notes(outcome.unpublished_note.take(), note));
            outcome.unpublished = true;
        }
        Ok(outcome)
    }

    /// Commit nothing more and push the Dolt data: fetch, merge a newer remote tip, push,
    /// with a bounded retry. `committed` says whether the caller committed local changes
    /// to the database first. `docs_changed` says the documents already moved in this
    /// push, which silences the notes that say nothing happened.
    fn push_data(
        &self,
        location: &LedgerLocation,
        db: &Dolt,
        committed: bool,
        docs_changed: bool,
        notes: &mut dyn FnMut(&str),
    ) -> Result<PushReport, String> {
        let path = db.dir().to_path_buf();
        let outcome = |result: PushResult, commit: String| PushOutcome {
            location: location.clone(),
            path: path.clone(),
            committed,
            commit,
            result,
        };
        let finish = |outcome: PushOutcome, notes: &mut dyn FnMut(&str)| {
            // The "nothing to commit" and "nothing to push" notes say the whole push did
            // nothing, which is untrue when the documents moved.
            if !docs_changed {
                for note in crate::ledger::push_notes(&outcome) {
                    notes(&note);
                }
            }
            PushReport {
                committed: outcome.committed,
                head: outcome.commit.clone(),
                already_on_remote: outcome.result == PushResult::AlreadyOnRemote,
            }
        };

        let remote_ref = format!("{}/{}", REMOTE, BRANCH);
        for attempt in 1..=MAX_ATTEMPTS {
            db.run(["fetch", REMOTE])?;
            if remote_branch_exists(db)? {
                let rows = db.rows(&format!(
                    "select dolt_merge_base('HEAD', {r}) = hashof('HEAD') as head_in_remote, \
                     dolt_merge_base('HEAD', {r}) = hashof({r}) as remote_in_head",
                    r = lit(&remote_ref)
                ));
                let (head_in_remote, remote_in_head) = match rows {
                    Ok(rows) => rows
                        .first()
                        .map(|row| (flag(row, "head_in_remote"), flag(row, "remote_in_head")))
                        .unwrap_or((false, false)),
                    Err(_) => (false, false),
                };
                if head_in_remote {
                    let tip = db.hash_of(&remote_ref)?;
                    return Ok(finish(outcome(PushResult::AlreadyOnRemote, tip), notes));
                }
                if !remote_in_head {
                    merge_remote(db, &remote_ref)?;
                }
            }

            let (args, pushed) = db.output(["push", REMOTE, BRANCH])?;
            if pushed.status.success() {
                let head = db.head_hash()?;
                return Ok(finish(
                    outcome(PushResult::Pushed { attempts: attempt }, head),
                    notes,
                ));
            }
            let stderr = String::from_utf8_lossy(&pushed.stderr).to_lowercase();
            let stale = ["rejected", "behind", "non-fast-forward"]
                .iter()
                .any(|word| stderr.contains(word));
            if !stale {
                return Err(dolt::failure_message(&args, &pushed));
            }
            if attempt < MAX_ATTEMPTS {
                notes(&crate::ledger::retry_note(
                    attempt,
                    &PushStatus {
                        flag: '!',
                        summary: "[rejected] (non-fast-forward)".to_string(),
                    },
                ));
            }
        }
        Err(format!(
            "error: {} on {} rejected the push {} times; it moved between each fetch and push.\n       \
             The commit stays in {}. Re-run tsk ledger push to try again",
            location.ref_name(),
            location.label(),
            MAX_ATTEMPTS,
            path.display()
        ))
    }
}

fn join_notes(existing: Option<String>, note: String) -> String {
    match existing {
        Some(first) => format!("{}\n{}", first, note),
        None => note,
    }
}

fn is_database(path: &Path) -> bool {
    path.join(".dolt").is_dir()
}

/// Dolt's URL for a git remote: `git+` and the git URL, with local paths made absolute
/// and the scp-like SSH form turned into a `ssh://` URL.
pub fn dolt_url(raw: &str, base: &Path) -> Result<String, String> {
    let raw = raw.trim();
    if raw.is_empty() {
        return Err("error: the ledger remote URL is empty".to_string());
    }
    for scheme in ["https://", "http://", "ssh://"] {
        if raw.starts_with(scheme) {
            return Ok(format!("git+{}", raw));
        }
    }
    if let Some(path) = raw.strip_prefix("file://") {
        return Ok(format!("git+file://{}", absolute(path, base)));
    }
    if raw.contains("://") {
        return Err(format!(
            "error: the ledger remote {} uses a scheme Dolt cannot reach; use https, ssh or a local path",
            raw
        ));
    }
    if let Some((host, path)) = raw.split_once(':') {
        if !host.is_empty() && !host.contains('/') && !host.starts_with('.') {
            return Ok(format!(
                "git+ssh://{}/{}",
                host,
                path.trim_start_matches('/')
            ));
        }
    }
    Ok(format!("git+file://{}", absolute(raw, base)))
}

fn absolute(path: &str, base: &Path) -> String {
    let joined = if Path::new(path).is_absolute() {
        PathBuf::from(path)
    } else {
        base.join(path)
    };
    std::fs::canonicalize(&joined)
        .unwrap_or(joined)
        .to_string_lossy()
        .into_owned()
}

/// The URL of the ledger's remote, as Dolt reaches it.
pub(crate) fn remote_url(repo: &Repo, location: &LedgerLocation) -> Result<String, String> {
    let base = repo
        .git
        .run_line(["rev-parse", "--show-toplevel"])
        .map(PathBuf::from)
        .unwrap_or_else(|_| repo.common_dir.clone());
    let raw = if location.repo_id().is_some() {
        location.remote().to_string()
    } else {
        repo.git
            .run_line(["remote", "get-url", location.remote()])?
    };
    dolt_url(&raw, &base)
}

/// The commit the remote ref points at, or `None` when the ref is absent.
pub(crate) fn remote_tip(git: &Git, location: &LedgerLocation) -> Result<Option<String>, String> {
    let (args, output) = git.output([
        "ls-remote",
        "--exit-code",
        location.remote(),
        location.ref_name(),
    ])?;
    match output.status.code() {
        Some(0) => Ok(Some(
            String::from_utf8_lossy(&output.stdout)
                .split_whitespace()
                .next()
                .unwrap_or("")
                .to_string(),
        )),
        Some(2) => Ok(None),
        _ => Err(crate::ledger::git::failure_message(&args, &output)),
    }
}

/// Create a new ledger database at `path`: the schema and the manifest rows. The result
/// is committed and has the remote configured, so the first push creates the branch. The
/// directory is removed again when any step fails.
pub fn create_database(
    path: &Path,
    url: &str,
    ref_name: &str,
    repo_id: Option<&str>,
) -> Result<(), String> {
    create_database_with(path, url, ref_name, repo_id, "", "Create the ledger")
}

/// [`create_database`] with `extra_sql` run in the same transaction as the schema, and
/// `message` as the commit message. The schema and the data land in one commit.
pub(crate) fn create_database_with(
    path: &Path,
    url: &str,
    ref_name: &str,
    repo_id: Option<&str>,
    extra_sql: &str,
    message: &str,
) -> Result<(), String> {
    std::fs::create_dir_all(path)
        .map_err(|e| format!("error: could not create {}: {}", path.display(), e))?;
    let build = || -> Result<(), String> {
        let db = Dolt::new(path);
        db.run(["init", "--initial-branch", BRANCH])?;
        let mut sql = dolt::init_sql(repo_id);
        sql.push_str(extra_sql);
        db.exec(&sql)?;
        db.run(["add", "-A"])?;
        db.run(["commit", "-m", message])?;
        db.run(["remote", "add", "--ref", ref_name, REMOTE, url])?;
        Ok(())
    };
    build().inspect_err(|_| {
        let _ = std::fs::remove_dir_all(path);
    })
}

fn verify_remote(db: &Dolt, url: &str, location: &LedgerLocation) -> Result<(), String> {
    let rows = db.rows(&format!(
        "select url, params from dolt_remotes where name = {}",
        lit(REMOTE)
    ))?;
    let row = rows.first().ok_or_else(|| {
        format!(
            "error: {} is a Dolt database with no '{}' remote; move it aside and re-run",
            db.dir().display(),
            REMOTE
        )
    })?;
    let found_url = text(row, "url").unwrap_or_default();
    let found_ref = row
        .get("params")
        .and_then(|p| p.get("git_ref"))
        .and_then(|r| r.as_str())
        .unwrap_or("");
    if found_url != url || found_ref != location.ref_name() {
        return Err(format!(
            "error: {} points at {} ({}), not {} ({}); move it aside and re-run",
            db.dir().display(),
            found_url,
            found_ref,
            url,
            location.ref_name()
        ));
    }
    Ok(())
}

fn pending_commits(db: &Dolt) -> Result<Vec<String>, String> {
    let rows = db.rows(&format!(
        "select commit_hash, message from dolt_log({})",
        lit(&format!("{}/{}..{}", REMOTE, BRANCH, BRANCH))
    ))?;
    Ok(rows
        .iter()
        .map(|row| {
            let hash = text(row, "commit_hash").unwrap_or_default();
            let message = text(row, "message").unwrap_or_default();
            format!(
                "{} {}",
                hash.chars().take(7).collect::<String>(),
                message.lines().next().unwrap_or("")
            )
        })
        .collect())
}

fn remote_branch_exists(db: &Dolt) -> Result<bool, String> {
    Ok(db.count(&format!(
        "select count(*) as c from dolt_remote_branches where name = {}",
        lit(REMOTE_BRANCH_NAME)
    ))? > 0)
}

fn dirty_error(path: &Path) -> String {
    format!(
        "error: {} holds uncommitted changes; refusing to reset over them.\n       \
         Push them with tsk ledger push, or discard them deliberately with: cd '{}' && dolt reset --hard\n       \
         To read the path without refreshing, use tsk ledger path",
        path.display(),
        path.display()
    )
}

impl DoltStore {
    /// Refresh the Dolt database, and return the manifest version it holds.
    pub(crate) fn refresh_data(&self, keep_local: bool) -> Result<(RefreshOutcome, i64), String> {
        dolt::ensure_identity()?;
        let location = self.repo.location(&self.state_root)?;
        let clone_id = self.repo.ensure_clone_id()?;
        let path = dolt_db_path(&self.state_root, &clone_id);
        let url = remote_url(&self.repo, &location)?;
        let remote_label = format!("{}'s {}", location.label(), location.ref_name());
        let present = is_database(&path);
        if path.exists() && !present {
            return Err(format!(
                "error: {} exists but is not a Dolt database; move it aside and re-run",
                path.display()
            ));
        }

        if remote_tip(&self.repo.git, &location)?.is_none() {
            let version = if present {
                let db = Dolt::new(&path);
                verify_remote(&db, &url, &location)?;
                self.check_manifest_of(&db, &location)?
            } else {
                create_database(&path, &url, location.ref_name(), location.repo_id())?;
                dolt::LAYOUT_VERSION
            };
            let head = Dolt::new(&path).head_hash()?;
            return Ok((
                RefreshOutcome {
                    unpublished_note: Some(crate::ledger::unpublished_text(
                        location.label(),
                        location.ref_name(),
                        &path,
                    )),
                    path,
                    remote_label,
                    remote_head: head,
                    pending: Vec::new(),
                    unpublished: true,
                    pending_note: None,
                },
                version,
            ));
        }

        let db;
        let mut dirty = false;
        let mut pending = Vec::new();
        if present {
            db = Dolt::new(&path);
            verify_remote(&db, &url, &location)?;
            dirty = db.count("select count(*) as c from dolt_status")? > 0;
            if dirty && !keep_local {
                return Err(dirty_error(&path));
            }
            db.run(["fetch", REMOTE])?;
        } else {
            self.check_remote_kind(&location)?;
            let parent = path
                .parent()
                .ok_or_else(|| format!("error: {} has no parent directory", path.display()))?;
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("error: could not create {}: {}", parent.display(), e))?;
            let name = path
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_default();
            Dolt::new(parent).run(["clone", "--ref", location.ref_name(), url.as_str(), &name])?;
            db = Dolt::new(&path);
        }
        if !remote_branch_exists(&db)? {
            return Err(format!(
                "error: {} on {} holds no Dolt branch '{}'; it is not a Dolt ledger",
                location.ref_name(),
                location.label(),
                BRANCH
            ));
        }
        let remote_head = db.hash_of(&format!("{}/{}", REMOTE, BRANCH))?;
        if present {
            pending = pending_commits(&db)?;
            if pending.is_empty() && !dirty {
                db.run(["reset", "--hard", &format!("{}/{}", REMOTE, BRANCH)])?;
            }
        }
        let version = self.check_manifest_of(&db, &location)?;
        let pending_note = (!pending.is_empty()).then(|| {
            let mut note = format!(
                "note: {} holds a commit that is not on {}:\n",
                path.display(),
                remote_label
            );
            for line in &pending {
                note.push_str(&format!("        {}\n", line));
            }
            note.push_str("      Leaving the database as it is rather than resetting over it.\n");
            note.push_str("      Push it with tsk ledger push, or discard it\n");
            note.push_str(&format!(
                "      deliberately with: cd '{}' && dolt reset --hard {}/{}",
                path.display(),
                REMOTE,
                BRANCH
            ));
            note
        });
        Ok((
            RefreshOutcome {
                path,
                remote_label,
                remote_head,
                pending,
                unpublished: false,
                pending_note,
                unpublished_note: None,
            },
            version,
        ))
    }
}

impl LedgerStore for DoltStore {
    fn refresh(&self) -> Result<RefreshOutcome, String> {
        self.refresh_with(false, false)
    }

    fn refresh_for_fetch(&self) -> Result<RefreshOutcome, String> {
        self.refresh_with(true, false)
    }

    fn refresh_keeping_local(&self) -> Result<RefreshOutcome, String> {
        self.refresh_with(false, true)
    }

    fn has_local_changes(&self) -> Result<bool, String> {
        if self.db()?.count("select count(*) as c from dolt_status")? > 0 {
            return Ok(true);
        }
        let docs = Git::new(&self.docs_tree()?.root);
        Ok(!docs.run(["status", "--porcelain=v2", "-z"])?.is_empty())
    }

    fn path(&self) -> Result<PathBuf, String> {
        let clone_id = self.repo.read_clone_id()?.ok_or_else(|| {
            format!(
                "error: this clone has no clone id yet ({} is absent or empty).\n       Run tsk ledger fetch, which mints it.",
                self.repo.clone_id_file().display()
            )
        })?;
        Ok(dolt_db_path(&self.state_root, &clone_id))
    }

    fn exists_locally(&self) -> Result<bool, String> {
        Ok(self.hint().is_some_and(|path| is_database(&path)))
    }

    fn commit_and_push(
        &self,
        message: &str,
        notes: &mut dyn FnMut(&str),
    ) -> Result<PushReport, String> {
        dolt::ensure_identity()?;
        let location = self.repo.location(&self.state_root)?;
        let db = self.db()?;
        self.check_current(&db, &location)?;
        let docs_path = self.docs_tree()?.root;

        db.run(["add", "-A"])?;
        let committed = db.count("select count(*) as c from dolt_status where staged = 1")? > 0;
        if committed {
            db.run(["commit", "-m", message])?;
        }

        // The documents go first, then the Dolt data. A record must never name a document
        // the remote lacks: a reader who finds the record can then always open the
        // document. The reverse, a document the remote holds and no record names, is
        // harmless. So when the documents push fails (rejected after the retries, or a
        // rebase conflict), this stops here and the Dolt data is not pushed. Both sides
        // keep their local commits, and the next tsk ledger push resumes. Each side
        // pushes only when it has a change or an unpushed commit: a side that is already
        // on the remote sends nothing.
        let docs_outcome = {
            let mut on_retry = |attempt: usize, status: &PushStatus| {
                notes(&crate::ledger::retry_note(attempt, status))
            };
            docs::push(&self.repo, &location, &docs_path, message, &mut on_retry)?
        };
        let docs_changed =
            docs_outcome.committed || matches!(docs_outcome.result, PushResult::Pushed { .. });

        // Every document a mission links must still exist. The documents are already
        // pushed, so this stops before the Dolt data goes and keeps both local commits.
        let docs_tree = self.docs_tree()?;
        model::check_links(
            &self.missions_in(&db, &location)?,
            &|path| docs_tree.doc_path(path).is_file(),
            "The documents are pushed. The Dolt data stays committed locally and is not pushed yet",
        )?;

        let mut report = self.push_data(&location, &db, committed, docs_changed, notes)?;
        if docs_changed {
            report.committed = report.committed || docs_outcome.committed;
            if report.already_on_remote {
                // Only the documents changed: the commit the remote now holds is theirs.
                report.head = docs_outcome.commit;
                report.already_on_remote = false;
            }
        }
        Ok(report)
    }

    fn head_id(&self) -> Result<String, String> {
        self.db()?.head_hash()
    }

    fn manifest(&self) -> Result<ManifestInfo, String> {
        self.manifest_rows(&self.db()?)
    }

    fn check_manifest(&self) -> Result<i64, String> {
        let location = self.repo.location(&self.state_root)?;
        self.check_current(&self.db()?, &location)
    }

    fn doc_get(&self, path: &str) -> Result<Option<String>, String> {
        let path = super::check_docs_path(path)?;
        self.docs_tree()?.doc_get(&path)
    }

    fn doc_put(&self, path: &str, body: &str) -> Result<(), String> {
        let path = super::check_docs_path(path)?;
        self.docs_tree()?.doc_put(&path, body)
    }

    fn doc_list(&self, prefix: &str) -> Result<Vec<String>, String> {
        self.docs_tree()?.doc_list(prefix)
    }

    fn doc_remove(&self, path: &str) -> Result<bool, String> {
        let path = super::check_docs_path(path)?;
        self.docs_tree()?.doc_remove(&path)
    }

    fn doc_exists(&self, path: &str) -> Result<bool, String> {
        match super::check_docs_path(path) {
            Ok(path) => Ok(self.docs_tree()?.doc_path(&path).is_file()),
            Err(_) => Ok(false),
        }
    }

    fn doc_path(&self, path: &str) -> Result<Option<PathBuf>, String> {
        let path = super::check_docs_path(path)?;
        let file = self.docs_tree()?.doc_path(&path);
        Ok(file.is_file().then_some(file))
    }

    fn thread_exists(&self, id: &str) -> Result<bool, String> {
        Ok(self.db()?.count(&format!(
            "select count(*) as c from threads where thread_id = {}",
            lit(id)
        ))? > 0)
    }

    fn thread_create(
        &self,
        id: &str,
        mission_id: &str,
        briefing_path: Option<&str>,
    ) -> Result<(), String> {
        let db = self.db()?;
        let location = self.repo.location(&self.state_root)?;
        // A refused write upgrades nothing: the checks come first.
        let version = self.check_current(&db, &location)?;
        if self.thread_exists(id)? {
            return Err(format!("error: threads/{} already exists", id));
        }
        let mut sql = String::new();
        if version < dolt::LAYOUT_VERSION {
            sql.push_str(&dolt::upgrade_sql(version));
        }
        sql.push_str(&format!(
            "insert into threads (thread_id, briefing_path, mission_id) values ({}, {}, {});",
            lit(id),
            briefing_path.map_or("NULL".to_string(), lit),
            lit(mission_id)
        ));
        db.exec(&sql)
    }

    fn thread_delete(&self, id: &str) -> Result<(), String> {
        self.db()?.exec(&format!(
            "delete from continuation_entries where thread_id = {t};\n\
             delete from threads where thread_id = {t};",
            t = lit(id)
        ))
    }

    fn thread_ids(&self) -> Result<Vec<String>, String> {
        let rows = self.db()?.rows("select thread_id from threads")?;
        let mut ids: Vec<String> = rows
            .iter()
            .filter_map(|row| text(row, "thread_id"))
            .collect();
        ids.sort();
        Ok(ids)
    }

    fn thread_info(&self, id: &str) -> Result<ThreadInfo, String> {
        let db = self.db()?;
        // A ledger below version 5 has no `mission_id` column.
        let column = if self.manifest_rows(&db)?.version >= dolt::LAYOUT_VERSION {
            "mission_id"
        } else {
            "null as mission_id"
        };
        let rows = db.rows(&format!(
            "select briefing_path, {} from threads where thread_id = {}",
            column,
            lit(id)
        ))?;
        Ok(rows
            .first()
            .map(|row| ThreadInfo {
                mission_id: text(row, "mission_id").filter(|m| !m.is_empty()),
                briefing_path: text(row, "briefing_path").filter(|p| !p.is_empty()),
            })
            .unwrap_or_default())
    }

    fn continuation_append(&self, id: &str, line: &str) -> Result<(), String> {
        let db = self.db()?;
        if !self.thread_exists(id)? {
            return Err(format!(
                "error: no such thread: {} (expected {})",
                id,
                self.describe_thread(id)
            ));
        }
        let next = db.count(&format!(
            "select coalesce(max(seq), 0) + 1 as n from continuation_entries where thread_id = {}",
            lit(id)
        ))?;
        db.exec(&format!(
            "insert into continuation_entries (thread_id, seq, entry) values ({}, {}, {});",
            lit(id),
            next,
            lit(line)
        ))
    }

    fn continuation_list(&self, id: &str) -> Result<Vec<String>, String> {
        let rows = self.db()?.rows(&format!(
            "select entry from continuation_entries where thread_id = {} order by seq",
            lit(id)
        ))?;
        Ok(rows
            .iter()
            .filter_map(|row| text(row, "entry"))
            .filter(|entry| !entry.trim().is_empty())
            .collect())
    }

    fn cloud_lookup(&self) -> Result<Lookup, String> {
        let rows = self.db()?.rows(
            "select session_id, thread_id, registered_at from cloud_session_bindings \
             order by registered_at, session_id",
        )?;
        let mut lookup = Lookup::default();
        for row in &rows {
            if let Some(session) = text(row, "session_id") {
                lookup.set(
                    &session,
                    CloudBinding::new(
                        &text(row, "thread_id").unwrap_or_default(),
                        &text(row, "registered_at").unwrap_or_default(),
                    ),
                );
            }
        }
        Ok(lookup)
    }

    fn cloud_bind(
        &self,
        session: &str,
        thread_id: &str,
        registered_at: &str,
    ) -> Result<(), String> {
        self.db()?.exec(&format!(
            "replace into cloud_session_bindings (session_id, thread_id, registered_at) \
             values ({}, {}, {});",
            lit(session),
            lit(thread_id),
            lit(registered_at)
        ))
    }

    fn cloud_unbind(&self, session: &str) -> Result<(), String> {
        self.db()?.exec(&format!(
            "delete from cloud_session_bindings where session_id = {};",
            lit(session)
        ))
    }

    fn cloud_unbind_thread(&self, thread_id: &str) -> Result<(), String> {
        self.db()?.exec(&format!(
            "delete from cloud_session_bindings where thread_id = {};",
            lit(thread_id)
        ))
    }

    fn events_append(&self, lines: &str) -> Result<(), String> {
        let items: Vec<&str> = lines
            .split_inclusive('\n')
            .map(|line| line.strip_suffix('\n').unwrap_or(line))
            .collect();
        if items.is_empty() {
            return Ok(());
        }
        let db = self.db()?;
        let last = db.count("select coalesce(max(position), 0) as m from external_events")?;
        let values: Vec<String> = items
            .iter()
            .enumerate()
            .map(|(index, line)| format!("({}, {})", last + 1 + index as i64, lit(line)))
            .collect();
        db.exec(&format!(
            "insert into external_events (position, envelope) values {};",
            values.join(", ")
        ))
    }

    fn events_list(&self) -> Result<Option<Vec<String>>, String> {
        let rows = self
            .db()?
            .rows("select envelope from external_events order by position")?;
        if rows.is_empty() {
            return Ok(None);
        }
        Ok(Some(
            rows.iter()
                .map(|row| text(row, "envelope").unwrap_or_default())
                .collect(),
        ))
    }

    fn events_count(&self) -> Result<u64, String> {
        Ok(self
            .db()?
            .count("select count(*) as c from external_events")?
            .max(0) as u64)
    }

    fn watermark_get(&self) -> Result<u64, String> {
        let rows = self
            .db()?
            .rows("select processed_through from watermark where id = 1")?;
        Ok(rows
            .first()
            .and_then(|row| integer(row, "processed_through"))
            .unwrap_or(0)
            .max(0) as u64)
    }

    fn watermark_set(&self, processed_through: u64, updated_at: &str) -> Result<(), String> {
        let value = i64::try_from(processed_through)
            .map_err(|_| format!("error: watermark {} is too large", processed_through))?;
        self.db()?.exec(&format!(
            "replace into watermark (id, processed_through, updated_at) values (1, {}, {});",
            value,
            lit(updated_at)
        ))
    }

    fn missions_load(&self) -> Result<Vec<Mission>, String> {
        let location = self.repo.location(&self.state_root)?;
        self.missions_in(&self.db()?, &location)
    }

    fn mission_save(&self, mission: &Mission) -> Result<(), String> {
        let location = self.repo.location(&self.state_root)?;
        let db = self.db()?;
        let version = self.check_current(&db, &location)?;
        let mut sql = String::new();
        if version < dolt::LAYOUT_VERSION {
            // The first write on an older ledger: the missing tables and columns and the
            // new version land in the same local change as the mission.
            sql.push_str(&dolt::upgrade_sql(version));
        }
        sql.push_str(&save_sql(mission));
        db.exec(&sql)
    }

    fn describe_document(&self, path: &str) -> String {
        match self.docs_hint() {
            Some(root) => root.join(path).display().to_string(),
            None => format!("document {}", path),
        }
    }

    fn describe_thread(&self, id: &str) -> String {
        self.describe(&format!("thread {}", id))
    }

    fn describe_threads(&self) -> String {
        self.describe("the threads table")
    }

    fn pending_inspect_hint(&self, path: &Path, _remote_head: &str) -> String {
        let data = format!(
            "cd '{}' && dolt log {r}/{b}..{b} && dolt diff {r}/{b} {b}",
            path.display(),
            r = REMOTE,
            b = BRANCH
        );
        match self.docs_hint() {
            Some(docs) => format!(
                "{}; for the documents: git -C '{}' log -p",
                data,
                docs.display()
            ),
            None => data,
        }
    }

    fn read_index_hint(&self, _path: &Path) -> String {
        "Read the index next with `tsk ledger doc get index.md`.".to_string()
    }
}

impl DoltStore {
    /// Every mission in `db`. A version 2 ledger has no mission tables: it has no
    /// missions. A version 1 ledger is refused.
    fn missions_in(&self, db: &Dolt, location: &LedgerLocation) -> Result<Vec<Mission>, String> {
        let version = self.check_current(db, location)?;
        if version < dolt::MISSIONS_LAYOUT_VERSION {
            return Ok(Vec::new());
        }
        // A version 3 ledger has no task, decision or question tables.
        read_missions(db, version >= dolt::TASKS_LAYOUT_VERSION)
    }

    /// A fresh clone meets a ref that holds a file ledger: stop with the way out.
    fn check_remote_kind(&self, location: &LedgerLocation) -> Result<(), String> {
        let Some(commit) = fetch_tip(&self.repo.git, location)? else {
            return Ok(());
        };
        match ref_kind(&self.repo.git, &commit)? {
            RefKind::Files => Err(format!(
                "error: {} on {} holds a file ledger, not a Dolt ledger.\n       \
                 Run tsk ledger migrate-to-dolt to convert it, or set TSK_LEDGER_BACKEND=git to keep using the file ledger",
                location.ref_name(),
                location.label()
            )),
            RefKind::Dolt => Ok(()),
            RefKind::Unknown => Err(format!(
                "error: {} on {} has no Dolt manifest and no .tsk-ledger.toml; it is not a tsk ledger",
                location.ref_name(),
                location.label()
            )),
        }
    }
}

/// A nullable text column: NULL for an empty value.
fn nullable(value: &str) -> String {
    if value.is_empty() {
        "NULL".to_string()
    } else {
        lit(value)
    }
}

/// The statements that make the tables hold `mission`: its row, and its outcomes, blockers
/// and links, with the rows it no longer has removed. A value that did not change writes
/// the same cell, so Dolt merges two clones that changed different fields.
fn save_sql(mission: &Mission) -> String {
    let id = lit(&mission.id);
    let mut sql = format!(
        "insert into missions (id, name, category, status, parent, purpose, decision_authority, \
         constraints_text, out_of_scope, next_objective, next_task, next_question) values \
         ({id}, {name}, '{category}', '{status}', {parent}, {purpose}, {authority}, \
         {constraints}, {scope}, {next}, {next_task}, {next_question}) \
         on duplicate key update name = values(name), category = values(category), \
         status = values(status), parent = values(parent), purpose = values(purpose), \
         decision_authority = values(decision_authority), \
         constraints_text = values(constraints_text), out_of_scope = values(out_of_scope), \
         next_objective = values(next_objective), next_task = values(next_task), \
         next_question = values(next_question);\n",
        id = id,
        name = lit(&mission.name),
        category = mission.category.as_str(),
        status = mission.status.as_str(),
        parent = mission
            .parent
            .as_deref()
            .map(lit)
            .unwrap_or_else(|| "NULL".to_string()),
        purpose = nullable(&mission.purpose),
        authority = nullable(&mission.decision_authority),
        constraints = nullable(&mission.constraints),
        scope = nullable(&mission.out_of_scope),
        next = mission.next_objective,
        next_task = mission.next_task,
        next_question = mission.next_question,
    );

    let seqs: Vec<String> = mission
        .objective
        .iter()
        .map(|o| o.seq.to_string())
        .collect();
    sql.push_str(&format!(
        "delete from objective_outcomes where mission_id = {}{};\n",
        id,
        if seqs.is_empty() {
            String::new()
        } else {
            format!(" and seq not in ({})", seqs.join(", "))
        }
    ));
    if !mission.objective.is_empty() {
        let rows: Vec<String> = mission
            .objective
            .iter()
            .map(|o| {
                format!(
                    "({}, {}, {}, {})",
                    id,
                    o.seq,
                    lit(&o.outcome),
                    if o.met { "true" } else { "false" }
                )
            })
            .collect();
        sql.push_str(&format!(
            "replace into objective_outcomes (mission_id, seq, outcome, met) values {};\n",
            rows.join(", ")
        ));
    }

    let kept: Vec<String> = mission.blocked_by.iter().map(|b| lit(b)).collect();
    sql.push_str(&format!(
        "delete from mission_blocks where mission_id = {}{};\n",
        id,
        if kept.is_empty() {
            String::new()
        } else {
            format!(" and blocked_by_id not in ({})", kept.join(", "))
        }
    ));
    if !kept.is_empty() {
        let rows: Vec<String> = kept.iter().map(|b| format!("({}, {})", id, b)).collect();
        sql.push_str(&format!(
            "replace into mission_blocks (mission_id, blocked_by_id) values {};\n",
            rows.join(", ")
        ));
    }

    let paths: Vec<String> = mission.documents.iter().map(|d| lit(&d.path)).collect();
    sql.push_str(&format!(
        "delete from mission_documents where mission_id = {}{};\n",
        id,
        if paths.is_empty() {
            String::new()
        } else {
            format!(" and path not in ({})", paths.join(", "))
        }
    ));
    if !paths.is_empty() {
        let rows: Vec<String> = mission
            .documents
            .iter()
            .map(|d| format!("({}, {}, '{}')", id, lit(&d.path), d.role.as_str()))
            .collect();
        sql.push_str(&format!(
            "replace into mission_documents (mission_id, path, role) values {};\n",
            rows.join(", ")
        ));
    }
    // Tasks first: blocks and decisions point at them. A task is never removed.
    if !mission.tasks.is_empty() {
        let rows: Vec<String> = mission
            .tasks
            .iter()
            .map(|t| {
                format!(
                    "({}, {}, {}, '{}')",
                    id,
                    t.seq,
                    lit(&t.description),
                    t.status.as_str()
                )
            })
            .collect();
        sql.push_str(&format!(
            "insert into tasks (mission_id, seq, description, status) values {} \
             on duplicate key update description = values(description), status = values(status);\n",
            rows.join(", ")
        ));
    }
    sql.push_str(&format!(
        "delete from task_blocks where mission_id = {};\n",
        id
    ));
    let blocks: Vec<String> = mission
        .tasks
        .iter()
        .flat_map(|t| {
            let id = &id;
            t.blocked_by
                .iter()
                .map(move |by| format!("({}, {}, {})", id, t.seq, by))
        })
        .collect();
    if !blocks.is_empty() {
        sql.push_str(&format!(
            "replace into task_blocks (mission_id, task_seq, blocked_by_seq) values {};\n",
            blocks.join(", ")
        ));
    }
    if !mission.decisions.is_empty() {
        let rows: Vec<String> = mission
            .decisions
            .iter()
            .map(|d| {
                format!(
                    "({}, {}, {}, {}, {}, {})",
                    id,
                    d.seq,
                    lit(&d.text),
                    lit(&d.decided_by),
                    lit(&d.decided_on),
                    d.task.map_or("NULL".to_string(), |t| t.to_string())
                )
            })
            .collect();
        sql.push_str(&format!(
            "insert into decisions (mission_id, seq, text, decided_by, decided_on, task_seq) values {} \
             on duplicate key update text = values(text), decided_by = values(decided_by), \
             decided_on = values(decided_on), task_seq = values(task_seq);\n",
            rows.join(", ")
        ));
    }
    if !mission.questions.is_empty() {
        let opt = |v: &Option<String>| v.as_deref().map_or("NULL".to_string(), lit);
        let rows: Vec<String> = mission
            .questions
            .iter()
            .map(|q| {
                format!(
                    "({}, {}, {}, '{}', {}, {}, {}, {})",
                    id,
                    q.seq,
                    lit(&q.text),
                    q.status.as_str(),
                    opt(&q.answer),
                    opt(&q.answered_by),
                    opt(&q.answered_on),
                    lit(&q.asked_on)
                )
            })
            .collect();
        sql.push_str(&format!(
            "insert into questions (mission_id, seq, text, status, answer, answered_by, answered_on, asked_on) values {} \
             on duplicate key update text = values(text), status = values(status), \
             answer = values(answer), answered_by = values(answered_by), \
             answered_on = values(answered_on), asked_on = values(asked_on);\n",
            rows.join(", ")
        ));
    }
    sql
}

/// Read every mission with its outcomes, blockers and links, and with its tasks,
/// decisions and questions when `with_records` says the tables exist.
fn read_missions(db: &Dolt, with_records: bool) -> Result<Vec<Mission>, String> {
    let bad = |column: &str, id: &str| {
        format!(
            "error: the missions table holds an unreadable {} for {}",
            column, id
        )
    };
    let mut missions = Vec::new();
    let counters = if with_records {
        ", next_task, next_question"
    } else {
        ""
    };
    for row in db.rows(&format!(
        "select id, name, category, status, parent, purpose, decision_authority, \
         constraints_text, out_of_scope, next_objective{} from missions order by id",
        counters
    ))? {
        let id = text(&row, "id").unwrap_or_default();
        missions.push(Mission {
            name: text(&row, "name").unwrap_or_default(),
            category: Category::parse(&text(&row, "category").unwrap_or_default())
                .map_err(|_| bad("category", &id))?,
            status: Status::parse(&text(&row, "status").unwrap_or_default())
                .map_err(|_| bad("status", &id))?,
            parent: text(&row, "parent"),
            purpose: text(&row, "purpose").unwrap_or_default(),
            decision_authority: text(&row, "decision_authority").unwrap_or_default(),
            constraints: text(&row, "constraints_text").unwrap_or_default(),
            out_of_scope: text(&row, "out_of_scope").unwrap_or_default(),
            next_objective: integer(&row, "next_objective").unwrap_or(1),
            objective: Vec::new(),
            blocked_by: Vec::new(),
            documents: Vec::new(),
            next_task: integer(&row, "next_task").unwrap_or(1),
            next_question: integer(&row, "next_question").unwrap_or(1),
            tasks: Vec::new(),
            decisions: Vec::new(),
            questions: Vec::new(),
            id,
        });
    }
    let slot = |missions: &mut Vec<Mission>, id: &str| -> Option<usize> {
        missions.iter().position(|m| m.id == id)
    };
    for row in db.rows(
        "select mission_id, seq, outcome, met from objective_outcomes order by mission_id, seq",
    )? {
        let id = text(&row, "mission_id").unwrap_or_default();
        if let Some(at) = slot(&mut missions, &id) {
            missions[at].objective.push(Outcome {
                seq: integer(&row, "seq").unwrap_or(0),
                outcome: text(&row, "outcome").unwrap_or_default(),
                met: flag(&row, "met"),
            });
        }
    }
    for row in db.rows(
        "select mission_id, blocked_by_id from mission_blocks order by mission_id, blocked_by_id",
    )? {
        let id = text(&row, "mission_id").unwrap_or_default();
        if let Some(at) = slot(&mut missions, &id) {
            missions[at]
                .blocked_by
                .push(text(&row, "blocked_by_id").unwrap_or_default());
        }
    }
    for row in
        db.rows("select mission_id, path, role from mission_documents order by mission_id, path")?
    {
        let id = text(&row, "mission_id").unwrap_or_default();
        if let Some(at) = slot(&mut missions, &id) {
            missions[at].documents.push(Document {
                path: text(&row, "path").unwrap_or_default(),
                role: Role::parse(&text(&row, "role").unwrap_or_default())
                    .map_err(|_| bad("role", &id))?,
            });
        }
    }
    if with_records {
        read_records(db, &mut missions, &bad)?;
    }
    Ok(missions)
}

/// Read every task with its blockers, and every decision and question.
fn read_records(
    db: &Dolt,
    missions: &mut [Mission],
    bad: &dyn Fn(&str, &str) -> String,
) -> Result<(), String> {
    let slot = |missions: &[Mission], id: &str| missions.iter().position(|m| m.id == id);
    for row in
        db.rows("select mission_id, seq, description, status from tasks order by mission_id, seq")?
    {
        let id = text(&row, "mission_id").unwrap_or_default();
        if let Some(at) = slot(missions, &id) {
            missions[at].tasks.push(Task {
                seq: integer(&row, "seq").unwrap_or(0),
                description: text(&row, "description").unwrap_or_default(),
                status: Status::parse(&text(&row, "status").unwrap_or_default())
                    .map_err(|_| bad("task status", &id))?,
                blocked_by: Vec::new(),
            });
        }
    }
    for row in db.rows(
        "select mission_id, task_seq, blocked_by_seq from task_blocks \
         order by mission_id, task_seq, blocked_by_seq",
    )? {
        let id = text(&row, "mission_id").unwrap_or_default();
        let task_seq = integer(&row, "task_seq").unwrap_or(0);
        if let Some(at) = slot(missions, &id) {
            if let Some(task) = missions[at].tasks.iter_mut().find(|t| t.seq == task_seq) {
                task.blocked_by
                    .push(integer(&row, "blocked_by_seq").unwrap_or(0));
            }
        }
    }
    for row in db.rows(
        "select mission_id, seq, text, decided_by, decided_on, task_seq from decisions \
         order by mission_id, seq",
    )? {
        let id = text(&row, "mission_id").unwrap_or_default();
        if let Some(at) = slot(missions, &id) {
            missions[at].decisions.push(Decision {
                seq: integer(&row, "seq").unwrap_or(0),
                text: text(&row, "text").unwrap_or_default(),
                decided_by: text(&row, "decided_by").unwrap_or_default(),
                decided_on: date(&row, "decided_on").unwrap_or_default(),
                task: integer(&row, "task_seq"),
            });
        }
    }
    for row in db.rows(
        "select mission_id, seq, text, status, answer, answered_by, answered_on, asked_on \
         from questions order by mission_id, seq",
    )? {
        let id = text(&row, "mission_id").unwrap_or_default();
        if let Some(at) = slot(missions, &id) {
            missions[at].questions.push(Question {
                seq: integer(&row, "seq").unwrap_or(0),
                text: text(&row, "text").unwrap_or_default(),
                status: QuestionStatus::parse(&text(&row, "status").unwrap_or_default())
                    .ok_or_else(|| bad("question status", &id))?,
                answer: text(&row, "answer"),
                answered_by: text(&row, "answered_by"),
                answered_on: date(&row, "answered_on"),
                asked_on: date(&row, "asked_on").unwrap_or_default(),
            });
        }
    }
    Ok(())
}

/// A `date` column as `YYYY-MM-DD`. Dolt's JSON output may carry a time part.
fn date(row: &serde_json::Value, column: &str) -> Option<String> {
    text(row, column).map(|v| v.chars().take(10).collect())
}

/// Merge the remote branch into the local one. A conflict aborts the merge and fails;
/// the local commit stays. `dolt pull` exits 0 on a conflict, so the tables are read.
fn merge_remote(db: &Dolt, remote_ref: &str) -> Result<(), String> {
    let (args, output) = db.output(["merge", remote_ref])?;
    let mut tables = Vec::new();
    for query in [
        "select `table` as t from dolt_conflicts",
        "select `table` as t from dolt_constraint_violations",
    ] {
        for row in db.rows(query)? {
            if let Some(table) = text(&row, "t") {
                if !tables.contains(&table) {
                    tables.push(table);
                }
            }
        }
    }
    if !tables.is_empty() {
        let _ = db.output(["merge", "--abort"])?;
        return Err(format!(
            "error: merging {} into {} conflicted in {}; the merge is aborted.\n       \
             Both sides changed the same row. The local commit stays in {}.\n       \
             Resolve it there with dolt, or discard it with dolt reset --hard {}, then re-run tsk ledger push",
            remote_ref,
            BRANCH,
            tables.join(", "),
            db.dir().display(),
            remote_ref
        ));
    }
    if !output.status.success() {
        return Err(dolt::failure_message(&args, &output));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn https_and_ssh_urls_gain_the_git_prefix() {
        let base = Path::new("/r");
        assert_eq!(
            dolt_url("https://github.com/o/r.git", base).unwrap(),
            "git+https://github.com/o/r.git"
        );
        assert_eq!(
            dolt_url("ssh://git@github.com/o/r", base).unwrap(),
            "git+ssh://git@github.com/o/r"
        );
    }

    #[test]
    fn scp_like_urls_become_ssh_urls() {
        assert_eq!(
            dolt_url("git@github.com:o/r.git", Path::new("/r")).unwrap(),
            "git+ssh://git@github.com/o/r.git"
        );
    }

    #[test]
    fn local_paths_and_file_urls_become_absolute_file_urls() {
        let dir = tempfile::tempdir().unwrap();
        let real = std::fs::canonicalize(dir.path()).unwrap();
        std::fs::create_dir(real.join("origin.git")).unwrap();
        let abs = real.join("origin.git");
        assert_eq!(
            dolt_url(abs.to_str().unwrap(), Path::new("/elsewhere")).unwrap(),
            format!("git+file://{}", abs.display())
        );
        assert_eq!(
            dolt_url("origin.git", &real).unwrap(),
            format!("git+file://{}", abs.display())
        );
        assert_eq!(
            dolt_url(&format!("file://{}", abs.display()), Path::new("/x")).unwrap(),
            format!("git+file://{}", abs.display())
        );
    }

    #[test]
    fn unsupported_schemes_and_empty_urls_fail() {
        assert!(dolt_url("git://host/o/r", Path::new("/r")).is_err());
        assert!(dolt_url("  ", Path::new("/r")).is_err());
    }
}