wsb 0.275.7622

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
//! `wsb migrate`: bring a machine's checkouts and state from an older wsb's
//! layout into this one, once.
//!
//! An older wsb kept a repository's settings in an uncommitted
//! `.wsb/state.json` and a database row, its board list in
//! `$PRJ/.state/projects/<p>/github.json`, and the rest of what it knew —
//! logs, extensions, the workbench layout — inside each checkout's `.wsb/`,
//! hidden from git by `info/exclude`. This wsb commits `.wsb/` whole
//! ([`crate::settings`]) and keeps everything else under `$PRJ/.state`
//! ([`crate::local_state`]).
//!
//! Every step either moves something to where it now lives or refuses, naming
//! the thing and why: nothing is guessed, and nothing that was somebody's
//! only copy is dropped without being said. A refusal leaves that item as it
//! was; the rest of the machine is still moved, and running again finishes it
//! once the refusal is dealt with.

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

use anyhow::{bail, Context, Result};
use serde::Deserialize;

use wsb_prj::settings::{relative_path, ProjectSettings, RepoSettings, VersionSettings};

use crate::annotations::{Link, Note, ProjectId, Ref};

/// What a run did, and what it would not do.
#[derive(Debug, Default)]
pub struct Report {
    pub done: Vec<String>,
    pub refused: Vec<String>,
}

impl Report {
    fn take(&mut self, what: &str, outcome: Result<Vec<String>>) {
        match outcome {
            Ok(lines) => self.done.extend(lines),
            Err(e) => self.refused.push(format!("{what}: {e:#}")),
        }
    }
}

/// Files and folders an older wsb kept in a checkout's `.wsb/`, none of which
/// this one writes there.
const OLD_ENTRIES: [&str; 14] = [
    "state.json",
    "wsb.db",
    "wsb.db-wal",
    "wsb.db-shm",
    "project.db",
    "project.db-wal",
    "project.db-shm",
    "logs",
    "templates",
    "disposable",
    "extensions",
    "backups",
    "workspace.lock",
    "workspace.writers",
];

/// The `info/exclude` lines an older wsb wrote to hide `.wsb/`.
fn is_old_exclusion(line: &str) -> bool {
    matches!(
        line.trim(),
        ".wsb/*" | "!.wsb/stamp.json" | "!.wsb/test.json" | "!.wsb/peer.json" | ".wsb" | ".wsb/" | "/.wsb" | "/.wsb/"
    )
}

/// What of an older wsb's layout the repository checked out at `repo_root`
/// still holds, one line each.
pub fn leftovers_in(repo_root: &Path) -> Result<Vec<String>> {
    let dir = wsb_prj::settings::dir_of(repo_root);
    let mut found: Vec<String> = OLD_ENTRIES
        .iter()
        .filter(|name| dir.join(name).exists())
        .map(|name| format!("{} is an older wsb's", dir.join(name).display()))
        .collect();
    if let Some(exclude) = exclude_file(repo_root)? {
        let text = read_if_there(&exclude)?.unwrap_or_default();
        if text.lines().any(is_old_exclusion) {
            found.push(format!("{} hides .wsb/, which is committed now", exclude.display()));
        }
    }
    Ok(found)
}

/// Refuse while a daemon — of any version — runs on this machine: it holds
/// the databases this moves, and a file moved under a process that writes it
/// is a file corrupted. Read from the rendezvous it publishes, by its pid
/// alone, because an older daemon's rendezvous need not read as this one's.
pub fn refuse_while_a_daemon_runs(machine: &Path) -> Result<()> {
    let path = machine.join("daemon.json");
    let Some(text) = read_if_there(&path)? else {
        return Ok(());
    };
    let pid = serde_json::from_str::<serde_json::Value>(&text)
        .ok()
        .and_then(|value| value.get("pid").and_then(|pid| pid.as_u64()))
        .and_then(|pid| u32::try_from(pid).ok());
    if let Some(pid) = pid {
        if crate::ops::process::is_alive(pid) {
            bail!(
                "the daemon (pid {pid}) is running and holds the databases this moves; stop it with \
                 `wsb daemon stop` and run `wsb migrate` again"
            );
        }
    }
    Ok(())
}

/// The machine's own state: what lived in `~/.wsb` before `$PRJ` held it,
/// the old databases brought into the one database, and the service record.
/// Refused while a daemon runs.
pub fn migrate_machine(workshop: Option<&wsb_prj::Workshop>) -> Result<Report> {
    let mut report = Report::default();
    let machine = crate::daemon::machine_dir().map_err(anyhow::Error::msg)?;
    refuse_while_a_daemon_runs(&machine)?;
    if workshop.is_some() {
        report.take("the machine's old ~/.wsb", adopt_home_state(&machine));
    }
    // Before anything writes into the workshop's database: the old ones are
    // brought into it first.
    report.take("the old databases", consolidate_databases(workshop, &machine));
    report.take("the service record", migrate_service_record(&machine));
    Ok(report)
}

/// Everything: the machine's state, then every worktree of every workspace in
/// the workshop, then the workspaces' own state, then the pins.
pub fn migrate_workshop(workshop: &wsb_prj::Workshop) -> Result<Report> {
    let mut report = migrate_machine(Some(workshop))?;
    let machine = crate::daemon::machine_dir().map_err(anyhow::Error::msg)?;

    for workspace in workshop.workspaces()? {
        for worktree in workspace.worktrees()? {
            let label = format!("{}/{}", workspace.name(), worktree.name);
            report.take(&label, migrate_checkout(&worktree.path, &machine));
        }
    }
    for workspace in workshop.workspaces()? {
        report.take(
            &format!("the state of {}", workspace.name()),
            migrate_workspace_state(workshop, &workspace),
        );
    }
    report.take("pins", migrate_pins(workshop));
    Ok(report)
}

/// One checkout outside the workshop. The machine's own state is
/// [`migrate_machine`]'s.
pub fn migrate_outside(checkout: &Path) -> Result<Report> {
    let mut report = Report::default();
    let machine = crate::daemon::machine_dir().map_err(anyhow::Error::msg)?;
    refuse_while_a_daemon_runs(&machine)?;
    report.take(&checkout.display().to_string(), migrate_checkout(checkout, &machine));
    Ok(report)
}

/// The record `wsb daemon install` keeps, as an older wsb wrote it: the
/// worktree the daemon opens was its `workspace_root`, and the daemon's flag
/// for it `--workspace`. The supervisor's own definition still says
/// `--workspace` until `wsb daemon install` renders it again, which the
/// report says.
fn migrate_service_record(machine: &Path) -> Result<Vec<String>> {
    let path = crate::daemon::service::record_path(machine);
    let Some(text) = read_if_there(&path)? else {
        return Ok(Vec::new());
    };
    let mut record: serde_json::Value =
        serde_json::from_str(&text).with_context(|| format!("{} is not JSON", path.display()))?;
    let object = record
        .as_object_mut()
        .with_context(|| format!("{} is not a service record", path.display()))?;
    let Some(root) = object.remove("workspace_root") else {
        return Ok(Vec::new());
    };
    if object.contains_key("worktree_root") {
        bail!(
            "{} names the daemon's worktree twice, as workspace_root and worktree_root; install the \
             service again with `wsb daemon install`",
            path.display()
        );
    }
    object.insert("worktree_root".to_string(), root);
    if let Some(arguments) = object.get_mut("arguments").and_then(|a| a.as_array_mut()) {
        for argument in arguments.iter_mut() {
            if argument.as_str() == Some("--workspace") {
                *argument = serde_json::Value::String("--worktree".to_string());
            }
        }
    }
    let written = format!("{}\n", serde_json::to_string_pretty(&record)?);
    let temporary = path.with_extension("json.migrating");
    std::fs::write(&temporary, written).with_context(|| format!("writing {}", temporary.display()))?;
    std::fs::rename(&temporary, &path).with_context(|| format!("replacing {}", path.display()))?;
    Ok(vec![format!(
        "rewrote {} for this wsb; render the service again with `wsb daemon install`",
        path.display()
    )])
}

// ── one checkout ──────────────────────────────────────────────────────────

/// Bring the worktree checked out at `worktree_root`, and every repository
/// of it an older wsb set up, into this layout.
fn migrate_checkout(worktree_root: &Path, machine: &Path) -> Result<Vec<String>> {
    let mut done = Vec::new();
    // The checkout's database, as `wsb.db` or by the name before that,
    // `project.db`; the newer one's version row when both hold one.
    let dir = wsb_prj::settings::dir_of(worktree_root);
    let old_dbs = [dir.join("wsb.db"), dir.join("project.db")];
    let mut row = None;
    for db in &old_dbs {
        if row.is_none() {
            row = old_project_row(db)?;
        }
    }

    // Every repository of the worktree an older wsb set up: the root and
    // each submodule holding a state file, parents first.
    let mut repos = vec![worktree_root.to_path_buf()];
    if let Ok(tree) = crate::fleet::git::SubmoduleTree::discover_allow_empty(worktree_root) {
        repos.extend(tree.top_down().iter().filter(|node| node.initialized).map(|node| node.abs_path.clone()));
    }
    for repo in &repos {
        done.extend(settings_of(repo, worktree_root, row.as_ref())?);
        done.extend(unhide(repo)?);
    }

    let state = crate::local_state::LocalState::of(worktree_root)?;
    for db in &old_dbs {
        done.extend(move_database(worktree_root, db, &state)?);
    }
    for repo in &repos {
        done.extend(move_local_files(repo, &state, machine)?);
    }
    Ok(done)
}

/// The project row an older wsb kept in a worktree's database: its version
/// settings.
#[derive(Debug, Clone)]
struct OldProjectRow {
    major: u32,
    version_file: String,
    detect_manifests: bool,
    manifests: Vec<String>,
}

fn old_project_row(db: &Path) -> Result<Option<OldProjectRow>> {
    if !db.exists() {
        return Ok(None);
    }
    let conn = rusqlite::Connection::open_with_flags(db, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
        .with_context(|| format!("opening {}", db.display()))?;
    let has: bool = conn.query_row(
        "SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'projects')",
        [],
        |row| row.get(0),
    )?;
    if !has {
        return Ok(None);
    }
    let mut rows = conn.prepare("SELECT major_version, version_file, auto_detect_project_files, project_files FROM projects")?;
    let found: Vec<(i64, String, i64, Option<String>)> = rows
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)))?
        .collect::<rusqlite::Result<_>>()?;
    match found.as_slice() {
        [] => Ok(None),
        [(major, version_file, detect, manifests)] => Ok(Some(OldProjectRow {
            major: u32::try_from(*major).with_context(|| format!("{} holds a major of {major}", db.display()))?,
            version_file: version_file.clone(),
            detect_manifests: *detect != 0,
            manifests: match manifests {
                Some(text) => serde_json::from_str(text)
                    .with_context(|| format!("{} holds project files that are not a list: {text}", db.display()))?,
                None => Vec::new(),
            },
        })),
        several => bail!(
            "{} holds {} project rows; an older wsb only ever wrote one, so which is meant cannot be told",
            db.display(),
            several.len()
        ),
    }
}

/// An older wsb's `.wsb/state.json`, as it last wrote it.
#[derive(Debug, Deserialize)]
struct OldState {
    #[serde(default)]
    version_suffix: Option<String>,
    #[serde(default)]
    project_name: Option<String>,
    #[serde(default)]
    wstemplate_entries: Vec<OldEntry>,
    #[serde(default)]
    ignored_submodules: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct OldEntry {
    alias: String,
    root: PathBuf,
}

/// Write the repository's `.wsb/repo.json` (and, at the worktree root,
/// `.wsb/project.json`) from what an older wsb kept, and remove its state
/// file. A repository whose `repo.json` is already there is left alone when
/// the two agree, and refused when they do not.
fn settings_of(repo: &Path, worktree_root: &Path, row: Option<&OldProjectRow>) -> Result<Vec<String>> {
    let state_path = wsb_prj::settings::dir_of(repo).join("state.json");
    let Some(text) = read_if_there(&state_path)? else {
        return Ok(Vec::new());
    };
    let old: OldState =
        serde_json::from_str(&text).with_context(|| format!("{} is not a state file wsb wrote", state_path.display()))?;
    let default_name = wsb_prj::repository_name(repo)?;
    let (alias, templates_root) = match old.wstemplate_entries.as_slice() {
        [] => (crate::state::WstemplateEngine::derive_alias(Path::new(&default_name)), ".".to_string()),
        [entry] => {
            let root = if entry.root.is_absolute() {
                relative_path(&entry.root, repo)?
            } else {
                entry.root.to_string_lossy().replace('\\', "/")
            };
            (entry.alias.clone(), root)
        }
        several => bail!("{} names {} template entries; a repository has one", state_path.display(), several.len()),
    };
    // The major an older wsb used: the database row's when it was set, else
    // what the repository's own version file starts with — exactly the
    // fallback it calculated with.
    let from_file = crate::state::read_major_version_from_file(repo);
    let major = match row {
        Some(row) if row.major > 0 => row.major,
        _ => from_file.unwrap_or(0),
    };
    let version = VersionSettings {
        major,
        file: row.map(|r| r.version_file.clone()).unwrap_or_else(|| VersionSettings::DEFAULT_FILE.to_string()),
        suffix: old.version_suffix.filter(|suffix| !suffix.is_empty()),
        detect_manifests: row.map(|r| r.detect_manifests).unwrap_or(true),
        manifests: row.map(|r| r.manifests.clone()).unwrap_or_default(),
    };
    let wanted = RepoSettings {
        name: old.project_name.filter(|name| !name.trim().is_empty()).unwrap_or(default_name),
        alias,
        templates_root,
        version,
    };
    let mut done = Vec::new();
    match RepoSettings::load(repo)? {
        Some(held) if held == wanted => {}
        Some(held) => bail!(
            "{} is already there and says {:?}, while {} says {:?}; keep one, remove the other, and run again",
            RepoSettings::path(repo).display(),
            held,
            state_path.display(),
            wanted
        ),
        None => {
            wanted.save(repo)?;
            done.push(format!("wrote {} (commit it)", RepoSettings::path(repo).display()));
        }
    }
    if repo == worktree_root && !old.ignored_submodules.is_empty() {
        let mut project = ProjectSettings::load(repo)?;
        if project.ignored_submodules != old.ignored_submodules {
            if !project.ignored_submodules.is_empty() {
                bail!(
                    "{} already ignores {:?}, while {} ignores {:?}; keep one and run again",
                    ProjectSettings::path(repo).display(),
                    project.ignored_submodules,
                    state_path.display(),
                    old.ignored_submodules
                );
            }
            project.ignored_submodules = old.ignored_submodules;
            project.save(repo)?;
            done.push(format!("wrote the ignored submodules into {} (commit it)", ProjectSettings::path(repo).display()));
        }
    }
    std::fs::remove_file(&state_path).with_context(|| format!("removing {}", state_path.display()))?;
    done.push(format!("removed {}", state_path.display()));
    Ok(done)
}

/// Take the lines an older wsb wrote to hide `.wsb/` out of the repository's
/// exclude file, keeping every other line.
fn unhide(repo: &Path) -> Result<Vec<String>> {
    let Some(exclude) = exclude_file(repo)? else {
        return Ok(Vec::new());
    };
    let Some(text) = read_if_there(&exclude)? else {
        return Ok(Vec::new());
    };
    if !text.lines().any(is_old_exclusion) {
        return Ok(Vec::new());
    }
    let kept: Vec<&str> = text.lines().filter(|line| !is_old_exclusion(line)).collect();
    let mut rewritten = kept.join("\n");
    if !rewritten.is_empty() {
        rewritten.push('\n');
    }
    std::fs::write(&exclude, rewritten).with_context(|| format!("writing {}", exclude.display()))?;
    Ok(vec![format!("{} no longer hides .wsb/", exclude.display())])
}

/// The exclude file git reads for the checkout at `root`, or `None` for a
/// folder that is no repository.
fn exclude_file(root: &Path) -> Result<Option<PathBuf>> {
    if !root.join(".git").exists() {
        return Ok(None);
    }
    let output = crate::git_backend::git_command()
        .arg("-C")
        .arg(root)
        .args(["rev-parse", "--path-format=absolute", "--git-path", "info/exclude"])
        .output()
        .with_context(|| format!("cannot run git in {}", root.display()))?;
    if !output.status.success() {
        bail!(
            "git cannot say where {}'s exclude file is: {}",
            root.display(),
            String::from_utf8_lossy(&output.stderr).trim()
        );
    }
    Ok(Some(PathBuf::from(String::from_utf8_lossy(&output.stdout).trim())))
}

/// Move the worktree's layout out of the database an older wsb kept in its
/// checkout into the workshop's, under the worktree's key; say what else
/// the old one held that this wsb does not keep; and remove it.
fn move_database(worktree_root: &Path, old_db: &Path, state: &crate::local_state::LocalState) -> Result<Vec<String>> {
    if !old_db.exists() {
        return Ok(Vec::new());
    }
    let mut done = Vec::new();
    let old = rusqlite::Connection::open_with_flags(old_db, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
        .with_context(|| format!("opening {}", old_db.display()))?;
    let table_exists = |name: &str| -> Result<bool> {
        Ok(old.query_row(
            "SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1)",
            [name],
            |row| row.get(0),
        )?)
    };
    if table_exists("workbench_state")? {
        let rows: Vec<(String, String)> = old
            .prepare("SELECT workspace_root, state_json FROM workbench_state")?
            .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
            .collect::<rusqlite::Result<_>>()?;
        let here = crate::platform::canonicalize(worktree_root).unwrap_or_else(|_| worktree_root.to_path_buf());
        let layout = match rows.as_slice() {
            [] => None,
            [(_, layout)] => Some(layout.clone()),
            several => several
                .iter()
                .find(|(root, _)| Path::new(root) == here || Path::new(root) == worktree_root)
                .map(|(_, layout)| layout.clone()),
        };
        if let Some(layout) = layout {
            // What a worktree keeps of its own is which folders its explorer
            // had open; its tabs belonged to a workbench that showed one
            // worktree at a time, and the machine's one layout starts afresh.
            let old: serde_json::Value = serde_json::from_str(&layout)
                .with_context(|| format!("{} holds a workbench layout that is not JSON", old_db.display()))?;
            let folders = old.get("expanded_paths").cloned().unwrap_or_else(|| serde_json::json!([]));
            let kept = serde_json::json!({ "expanded_paths": folders }).to_string();
            crate::store::open_for_migration()?.get_connection()?.execute(
                "INSERT INTO workbench_state (worktree, state_json) VALUES (?1, ?2) \
                 ON CONFLICT(worktree) DO NOTHING",
                rusqlite::params![state.key(), &kept],
            )?;
            done.push(format!("kept the explorer's open folders in the workshop's database, as {}", state.key()));
        }
    }
    // What this wsb does not keep, counted, so dropping it is said.
    for table in [
        "notes", "note_links", "path_notes", "features", "tasks", "milestones", "directives", "dependencies", "surveys",
        "work_sessions", "session_continuity_states", "session_knowledge_transfers",
    ] {
        if table_exists(table)? {
            let count: i64 = old.query_row(&format!("SELECT COUNT(*) FROM {table}"), [], |row| row.get(0))?;
            if count > 0 {
                done.push(format!("dropped {count} row(s) of {table}, which this wsb does not keep"));
            }
        }
    }
    drop(old);
    remove_database_files(old_db)?;
    done.push(format!("removed {} (its test catalogue is rebuilt by the next scan)", old_db.display()));
    Ok(done)
}

/// Remove a database file and the journal files beside it.
fn remove_database_files(db: &Path) -> Result<()> {
    for suffix in ["", "-wal", "-shm"] {
        let path = PathBuf::from(format!("{}{suffix}", db.display()));
        if path.exists() {
            std::fs::remove_file(&path).with_context(|| format!("removing {}", path.display()))?;
        }
    }
    Ok(())
}

// ── the old databases ──────────────────────────────────────────────────────

/// Bring the databases an older wsb kept apart into the workshop's one.
///
/// * The machine's sessions database, `<machine dir>/wsb.db`, becomes the
///   workshop's by being moved into place — its schema is the workshop
///   database's first two migrations. When the workshop's database already
///   exists too, the two are refused rather than merged: the sessions' rows
///   are numbered per file, and stars and pins name them, so neither file
///   can be poured into the other without losing what is only in it.
/// * The GitHub mirror, `$PRJ/.state/github.db`, and the peers,
///   `<machine dir>/machine.db`, have every table copied in.
fn consolidate_databases(workshop: Option<&wsb_prj::Workshop>, machine: &Path) -> Result<Vec<String>> {
    let mut done = Vec::new();
    let store = crate::store::path()?;
    let old_sessions = machine.join("wsb.db");
    if old_sessions.exists() && old_sessions != store {
        if store.exists() && holds_nothing(&store)? {
            // Made by a newer wsb run before `wsb migrate`: it holds no row,
            // so there is nothing in it to choose against.
            remove_database_files(&store)?;
            done.push(format!("removed {}, made empty before the migration", store.display()));
        }
        if store.exists() {
            bail!(
                "{} and {} both exist: the first is the machine's sessions as an older wsb kept them, the \
                 second the workshop's database a newer one made before `wsb migrate` ran. Which holds what \
                 cannot be merged without losing stars and pins: stop the daemon, move {} aside (its \
                 sessions are read again from their transcripts), and run `wsb migrate` again",
                old_sessions.display(),
                store.display(),
                store.display()
            );
        }
        let conn = rusqlite::Connection::open(&old_sessions)
            .with_context(|| format!("opening {}", old_sessions.display()))?;
        conn.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(()))?;
        drop(conn);
        if let Some(parent) = store.parent() {
            std::fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
        }
        std::fs::rename(&old_sessions, &store)
            .with_context(|| format!("moving {} to {}", old_sessions.display(), store.display()))?;
        remove_database_files(&old_sessions)?;
        done.push(format!("moved {} to {}", old_sessions.display(), store.display()));
    }
    // Opened, so the schema — the mirror's tables among it — is in place
    // before anything is copied into it.
    crate::store::open_for_migration()?;
    wsb_github::Mirror::open(&store)?;
    let mut others = vec![machine.join("machine.db")];
    if let Some(workshop) = workshop {
        others.push(workshop.state_dir().join("github.db"));
    }
    for old in others {
        if old.exists() && old != store {
            let copied = copy_tables(&store, &old)?;
            remove_database_files(&old)?;
            done.push(format!("copied {copied} row(s) of {} into {}", old.display(), store.display()));
        }
    }
    Ok(done)
}

/// Whether the database at `db` holds no row in any table but its schema's
/// own records — a full-text index's internal tables keep their bookkeeping
/// even when it indexes nothing.
fn holds_nothing(db: &Path) -> Result<bool> {
    let conn = rusqlite::Connection::open(db).with_context(|| format!("opening {}", db.display()))?;
    let tables: Vec<String> = conn
        .prepare(
            "SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' \
             AND name NOT IN ('refinery_schema_history', 'github_schema') \
             AND name NOT LIKE '%\\_fts\\_%' ESCAPE '\\'",
        )?
        .query_map([], |row| row.get(0))?
        .collect::<rusqlite::Result<_>>()?;
    for table in tables {
        let rows: i64 = conn.query_row(&format!("SELECT COUNT(*) FROM \"{table}\""), [], |row| row.get(0))?;
        if rows > 0 {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Columns an older wsb named otherwise: a peer's name for a worktree was a
/// "workspace id".
const RENAMED_COLUMNS: [(&str, &str); 1] = [("workspace_id", "shared_worktree")];

/// Copy every table of the database at `old` into the store, column by name.
///
/// A table the store does not define is made as `old` defines it. A column
/// of `old` lands in the store's column of the same name, or the name it
/// has now ([`RENAMED_COLUMNS`]); one with neither is refused, naming it,
/// rather than dropped. Rows the store already holds are kept. How many rows
/// were copied.
fn copy_tables(store: &Path, old: &Path) -> Result<usize> {
    let conn = rusqlite::Connection::open(store).with_context(|| format!("opening {}", store.display()))?;
    conn.busy_timeout(std::time::Duration::from_secs(30))?;
    conn.execute("ATTACH DATABASE ?1 AS old", [old.to_string_lossy()])
        .with_context(|| format!("opening {}", old.display()))?;
    let held: Vec<(String, String)> = conn
        .prepare(
            "SELECT name, sql FROM old.sqlite_master WHERE type = 'table' \
             AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'refinery_%' AND sql IS NOT NULL",
        )?
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
        .collect::<rusqlite::Result<_>>()?;
    let columns = |schema: &str, table: &str| -> Result<Vec<String>> {
        Ok(conn
            .prepare("SELECT name FROM pragma_table_info(?1, ?2)")?
            .query_map([table, schema], |row| row.get(0))?
            .collect::<rusqlite::Result<_>>()?)
    };
    let mut copied = 0;
    for (name, sql) in held {
        let present: bool = conn.query_row(
            "SELECT EXISTS (SELECT 1 FROM main.sqlite_master WHERE type = 'table' AND name = ?1)",
            [&name],
            |row| row.get(0),
        )?;
        if !present {
            conn.execute_batch(&sql)
                .with_context(|| format!("making {name} as {} defines it", old.display()))?;
        }
        let here = columns("main", &name)?;
        let mut from = Vec::new();
        let mut to = Vec::new();
        for column in columns("old", &name)? {
            let now = RENAMED_COLUMNS
                .iter()
                .find(|(was, _)| *was == column)
                .map_or(column.as_str(), |(_, now)| now);
            if !here.iter().any(|known| known == now) {
                bail!(
                    "{}'s table {name} has a column {column} the store's {name} has no place for; \
                     nothing was copied from it",
                    old.display()
                );
            }
            from.push(format!("\"{column}\""));
            to.push(format!("\"{now}\""));
        }
        copied += conn
            .execute(
                &format!(
                    "INSERT OR IGNORE INTO main.\"{name}\" ({}) SELECT {} FROM old.\"{name}\"",
                    to.join(", "),
                    from.join(", ")
                ),
                [],
            )
            .with_context(|| format!("copying {name} from {}", old.display()))?;
    }
    conn.execute("DETACH DATABASE old", [])?;
    Ok(copied)
}

/// Move what else an older wsb kept in a checkout's `.wsb/` to where it lives
/// now: logs and backups into the worktree's local state, extensions into the
/// machine's. A lock is removed only when nobody holds it; templates only
/// when there are none in it.
fn move_local_files(repo: &Path, state: &crate::local_state::LocalState, machine: &Path) -> Result<Vec<String>> {
    let dir = wsb_prj::settings::dir_of(repo);
    let mut done = Vec::new();

    // Where an older wsb wrote its logs before `disposable/logs`.
    let logs = dir.join("logs");
    if logs.is_dir() {
        merge_into(&logs, &state.logs())?;
        done.push(format!("moved {} to {}", logs.display(), state.logs().display()));
    }

    let disposable = dir.join("disposable");
    if disposable.is_dir() {
        let logs = disposable.join("logs");
        if logs.is_dir() {
            merge_into(&logs, &state.logs())?;
            done.push(format!("moved {} to {}", logs.display(), state.logs().display()));
        }
        let left = entries(&disposable)?;
        if !left.is_empty() {
            bail!(
                "{} holds {} as well as logs; it is not something to move for you: move it out of .wsb/ and run again",
                disposable.display(),
                left.join(", ")
            );
        }
        std::fs::remove_dir(&disposable).with_context(|| format!("removing {}", disposable.display()))?;
    }

    let backups = dir.join("backups");
    if backups.is_dir() {
        let held = entries(&backups)?.len();
        std::fs::remove_dir_all(&backups).with_context(|| format!("removing {}", backups.display()))?;
        done.push(format!(
            "removed {} ({held} backup(s) of an older wsb's checkout database, which nothing reads any more)",
            backups.display()
        ));
    }

    let extensions = dir.join("extensions");
    if extensions.is_dir() {
        let target = machine.join(crate::extensions::DIR);
        merge_into(&extensions, &target)?;
        done.push(format!("moved {} to the machine's {}", extensions.display(), target.display()));
    }

    let templates = dir.join("templates");
    if templates.is_dir() {
        let held: Vec<String> = entries(&templates)?
            .into_iter()
            .filter(|name| name != "templates.json")
            .collect();
        if !held.is_empty() {
            bail!(
                "{} holds .tera templates ({}), which this wsb does not render: make each a .wstemplate beside \
                 its output, remove the folder, and run again",
                templates.display(),
                held.join(", ")
            );
        }
        std::fs::remove_dir_all(&templates).with_context(|| format!("removing {}", templates.display()))?;
        done.push(format!("removed {}, which held no templates", templates.display()));
    }

    let lock = dir.join("workspace.lock");
    if lock.exists() {
        bail!(
            "{} is held — a snapshot may be under way. Run again once it is released, or delete it if its holder is gone",
            lock.display()
        );
    }
    let writers = dir.join("workspace.writers");
    if writers.is_dir() {
        let live = entries(&writers)?;
        if !live.is_empty() {
            bail!("{} registers running writers ({}); run again once they finish", writers.display(), live.join(", "));
        }
        std::fs::remove_dir(&writers).with_context(|| format!("removing {}", writers.display()))?;
    }
    Ok(done)
}

// ── the workspaces' and the machine's state ────────────────────────────────

/// `$PRJ/.state/projects/<p>/` becomes `$PRJ/.state/workspaces/<p>/`; the
/// board list kept there, or in the workspace folder by an older wsb still,
/// goes into the main worktree's committed `.wsb/project.json`.
fn migrate_workspace_state(workshop: &wsb_prj::Workshop, workspace: &wsb_prj::Workspace) -> Result<Vec<String>> {
    let mut done = Vec::new();
    let old = workshop.state_dir().join("projects").join(workspace.name());
    let new = workspace.state_dir();
    let main = workspace.main_worktree();
    for github in [old.join("github.json"), workspace.dir().join(".wsb").join("github.json")] {
        if let Some(text) = read_if_there(&github)? {
            #[derive(Deserialize)]
            #[serde(deny_unknown_fields)]
            struct OldBoards {
                #[serde(default)]
                boards: Vec<String>,
            }
            let listed: OldBoards =
                serde_json::from_str(&text).with_context(|| format!("{} is not a board list", github.display()))?;
            let mut project = ProjectSettings::load(&main)?;
            let before = project.boards.len();
            for board in listed.boards {
                if !project.boards.iter().any(|held| held.eq_ignore_ascii_case(&board)) {
                    project.boards.push(board);
                }
            }
            if project.boards.len() != before {
                project.save(&main)?;
                done.push(format!(
                    "wrote {}'s boards into {} (commit it)",
                    workspace.name(),
                    ProjectSettings::path(&main).display()
                ));
            }
            std::fs::remove_file(&github).with_context(|| format!("removing {}", github.display()))?;
            done.push(format!("removed {}", github.display()));
        }
    }
    let stray = workspace.dir().join(".wsb");
    if stray.is_dir() {
        merge_into(&stray, &new)?;
        done.push(format!("moved what was left in {} to {}", stray.display(), new.display()));
    }
    if old.is_dir() {
        merge_into(&old, &new)?;
        done.push(format!("moved {} to {}", old.display(), new.display()));
    }
    // The folder that held every workspace's state under the old name goes
    // with its last workspace.
    let projects = workshop.state_dir().join("projects");
    if projects.is_dir() && entries(&projects)?.is_empty() {
        std::fs::remove_dir(&projects).with_context(|| format!("removing {}", projects.display()))?;
    }
    Ok(done)
}

/// The machine's state from `~/.wsb`, where it lived before `$PRJ` held it.
fn adopt_home_state(machine: &Path) -> Result<Vec<String>> {
    if std::env::var_os(crate::daemon::MACHINE_DIR_ENV).is_some_and(|v| !v.is_empty()) {
        return Ok(Vec::new());
    }
    let home = crate::daemon::home_dir().map_err(anyhow::Error::msg)?;
    crate::daemon::adopt_old_machine_dir(&home, machine).map_err(anyhow::Error::msg)
}

/// Every pin an older wsb kept becomes a committed link: the message, linked
/// to the project it was pinned in, in that project's main worktree; its
/// note, a note linked to the message.
fn migrate_pins(workshop: &wsb_prj::Workshop) -> Result<Vec<String>> {
    let conn = crate::store::open_for_migration()?.get_connection()?;
    let exists: bool = conn.query_row(
        "SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'pins_to_migrate')",
        [],
        |row| row.get(0),
    )?;
    if !exists {
        return Ok(Vec::new());
    }
    type Pin = (String, String, Option<String>, String, Option<String>);
    let pins: Vec<Pin> = conn
        .prepare("SELECT session_id, agent, project, message_uuid, note FROM pins_to_migrate")?
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?, row.get(4)?)))?
        .collect::<rusqlite::Result<_>>()?;
    let mut done = Vec::new();
    let mut by_project: BTreeMap<String, Vec<Pin>> = BTreeMap::new();
    for pin in pins {
        let Some(project) = pin.2.clone() else {
            bail!(
                "a pin on session {} was made in no project, so there is no project to commit it in; \
                 unpin it with an older wsb, or keep that wsb to read it",
                pin.0
            );
        };
        by_project.entry(project).or_default().push(pin);
    }
    let machine_name = crate::daemon::machine_name().map_err(anyhow::Error::msg)?;
    for (project, pins) in by_project {
        let workspace = workshop
            .workspace(&project)
            .with_context(|| format!("{} pin(s) were made in {project}, which is not in this workshop", pins.len()))?;
        let main = workspace.main_worktree();
        let id = ProjectId::of_checkout(&main)?;
        for (session, agent, _, uuid, note) in &pins {
            let message = Ref::Message { agent: agent.clone(), session: session.clone(), uuid: uuid.clone() };
            Link::new(message.clone(), Ref::Project(id.clone()), Some(crate::annotations::links::PINNED.to_string()))?
                .write(&main)?;
            if let Some(text) = note.as_deref().filter(|text| !text.trim().is_empty()) {
                let note = Note::new("Pinned message", text, &machine_name)?;
                note.write(&main)?;
                Link::new(Ref::Note { id: note.id.clone() }, message, None)?.write(&main)?;
            }
        }
        done.push(format!("wrote {} pin(s) as links into {} (commit them)", pins.len(), main.display()));
    }
    conn.execute("DROP TABLE pins_to_migrate", [])?;
    Ok(done)
}

// ── files ─────────────────────────────────────────────────────────────────

fn read_if_there(path: &Path) -> Result<Option<String>> {
    match std::fs::read_to_string(path) {
        Ok(text) => Ok(Some(text)),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
    }
}

fn entries(dir: &Path) -> Result<Vec<String>> {
    let mut names = Vec::new();
    for entry in std::fs::read_dir(dir).with_context(|| format!("cannot read {}", dir.display()))? {
        names.push(entry.with_context(|| format!("cannot read {}", dir.display()))?.file_name().to_string_lossy().into_owned());
    }
    names.sort();
    Ok(names)
}

/// Move everything in `from` into `to`, then remove `from`. A file already in
/// `to` is kept when it is the same, and refused when it differs: which one
/// is meant is not something to guess. Refused before anything moves.
fn merge_into(from: &Path, to: &Path) -> Result<()> {
    fn clashes(from: &Path, to: &Path, found: &mut Vec<String>) -> Result<()> {
        for name in entries(from)? {
            let (source, target) = (from.join(&name), to.join(&name));
            if source.is_dir() && target.is_dir() {
                clashes(&source, &target, found)?;
            } else if target.exists() {
                let same = source.is_file()
                    && target.is_file()
                    && std::fs::read(&source).ok() == std::fs::read(&target).ok();
                if !same {
                    found.push(format!("{} and {}", source.display(), target.display()));
                }
            }
        }
        Ok(())
    }
    fn moved(from: &Path, to: &Path) -> Result<()> {
        std::fs::create_dir_all(to).with_context(|| format!("cannot create {}", to.display()))?;
        for name in entries(from)? {
            let (source, target) = (from.join(&name), to.join(&name));
            if source.is_dir() && target.is_dir() {
                moved(&source, &target)?;
            } else if target.exists() {
                std::fs::remove_file(&source).with_context(|| format!("removing {}", source.display()))?;
            } else if std::fs::rename(&source, &target).is_err() {
                // Another filesystem: copied, then removed.
                copy_tree(&source, &target)?;
                remove_any(&source)?;
            }
        }
        std::fs::remove_dir(from).with_context(|| format!("removing {}", from.display()))
    }
    let mut found = Vec::new();
    if to.exists() {
        clashes(from, to, &mut found)?;
    }
    if !found.is_empty() {
        bail!("both hold different files: {}; keep one of each and run again", found.join("; "));
    }
    moved(from, to)
}

fn copy_tree(from: &Path, to: &Path) -> Result<()> {
    if from.is_dir() {
        std::fs::create_dir_all(to).with_context(|| format!("cannot create {}", to.display()))?;
        for name in entries(from)? {
            copy_tree(&from.join(&name), &to.join(&name))?;
        }
        Ok(())
    } else {
        std::fs::copy(from, to).with_context(|| format!("copying {} to {}", from.display(), to.display()))?;
        Ok(())
    }
}

fn remove_any(path: &Path) -> Result<()> {
    if path.is_dir() {
        std::fs::remove_dir_all(path)
    } else {
        std::fs::remove_file(path)
    }
    .with_context(|| format!("removing {}", path.display()))
}

#[cfg(test)]
mod tests;