autumn-cli 0.9.0

CLI tool for the Autumn web framework
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
//! `autumn fleet` (issue #3232) through the real binary, against a temp fleet
//! root.
//!
//! The `SQLite` build runs every subcommand. The default (Postgres) build only
//! checks that the command tells the operator which build to use.
//!
//!   `cargo test -p autumn-cli --no-default-features --features sqlite --test cli_tests fleet_cli`

use std::path::Path;
use std::process::Command;

const fn autumn_bin() -> &'static str {
    env!("CARGO_BIN_EXE_autumn")
}

fn run_autumn(dir: &Path, args: &[&str]) -> (String, String, Option<i32>) {
    let output = Command::new(autumn_bin())
        .args(args)
        .current_dir(dir)
        .env("AUTUMN_ENV", "dev")
        .env("AUTUMN_MANIFEST_DIR", dir)
        .output()
        .expect("failed to run autumn");
    (
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
        output.status.code(),
    )
}

#[cfg(not(feature = "sqlite"))]
#[test]
fn fleet_in_the_postgres_build_names_the_sqlite_build() {
    let tmp = tempfile::tempdir().unwrap();
    let (_, stderr, code) = run_autumn(tmp.path(), &["fleet", "list"]);
    assert_eq!(code, Some(1), "{stderr}");
    assert!(stderr.contains("--features sqlite"), "{stderr}");
}

#[cfg(feature = "sqlite")]
mod sqlite {
    use std::path::{Path, PathBuf};
    use std::sync::Arc;
    use std::time::Duration;

    use autumn_web::config::DatabaseFleetConfig;
    use autumn_web::db::fleet::DatabaseFleet;
    use autumn_web::db::fleet_replication::FleetReplication;
    use autumn_web::fleet_layout::FleetMode;
    use autumn_web::replication::{FileDestination, ReplicaDestination, ReplicationSettings};
    use diesel::connection::SimpleConnection as _;
    use diesel::{Connection as _, QueryableByName, RunQueryDsl as _, SqliteConnection};

    use super::run_autumn;

    fn run_ok(dir: &Path, args: &[&str]) -> (String, String) {
        let (stdout, stderr, code) = run_autumn(dir, args);
        assert_eq!(
            code,
            Some(0),
            "autumn {args:?} failed\nstdout: {stdout}\nstderr: {stderr}"
        );
        (stdout, stderr)
    }

    fn run_err(dir: &Path, args: &[&str]) -> String {
        let (stdout, stderr, code) = run_autumn(dir, args);
        assert_eq!(
            code,
            Some(1),
            "autumn {args:?} must fail\nstdout: {stdout}\nstderr: {stderr}"
        );
        stderr
    }

    /// A project directory: `autumn.toml`, a `migrations/` directory with one
    /// app migration, and a tenant fleet under `fleet/` (relative, as an app
    /// usually writes it).
    struct App {
        _tmp: tempfile::TempDir,
        dir: PathBuf,
    }

    impl App {
        fn new(extra_toml: &str) -> Self {
            let tmp = tempfile::tempdir().unwrap();
            let dir = tmp.path().join("app");
            let migration = dir.join("migrations/20260101000000_create_notes");
            std::fs::create_dir_all(&migration).unwrap();
            std::fs::write(
                migration.join("up.sql"),
                "CREATE TABLE notes (body TEXT NOT NULL);",
            )
            .unwrap();
            std::fs::write(migration.join("down.sql"), "DROP TABLE notes;").unwrap();
            std::fs::write(
                dir.join("autumn.toml"),
                format!(
                    "[database]\nurl = \"sqlite://{}\"\n\n[database.fleet]\nmode = \"tenant\"\n\
                     root = \"fleet\"\n\n{extra_toml}",
                    tmp.path().join("control.db").display()
                ),
            )
            .unwrap();
            Self { _tmp: tmp, dir }
        }

        fn root(&self) -> PathBuf {
            self.dir.join("fleet")
        }

        fn config(&self) -> DatabaseFleetConfig {
            DatabaseFleetConfig {
                mode: FleetMode::Tenant,
                root: self.root().display().to_string(),
                path: None,
                max_open: 8,
                pool_size: 2,
                create_on_demand: None,
                idle_close_secs: 0,
                restore_missing: false,
                ownership: None,
            }
        }

        /// A fleet as the app builds it: its migrations and the framework sets.
        fn fleet(&self) -> DatabaseFleet {
            let source =
                diesel_migrations::FileBasedMigrations::from_path(self.dir.join("migrations"))
                    .unwrap();
            DatabaseFleet::builder(self.config())
                .migration_source("app", source)
                .framework_migrations()
                .build()
                .unwrap()
        }

        /// A fleet with no migrations: databases that `fleet migrate` must
        /// bring up to date.
        fn bare_fleet(&self) -> DatabaseFleet {
            DatabaseFleet::builder(self.config()).build().unwrap()
        }

        fn path(&self, tenant: &str) -> PathBuf {
            let fleet = self.bare_fleet();
            fleet.path_of(&fleet.key_for(tenant).unwrap())
        }

        fn provision(&self, fleet: &DatabaseFleet, tenants: &[&str], rows: usize) {
            runtime().block_on(async {
                for tenant in tenants {
                    let key = fleet.key_for(tenant).unwrap();
                    fleet.provision(&key).await.unwrap();
                }
                fleet.close_all().await;
            });
            for tenant in tenants {
                for n in 0..rows {
                    exec(
                        &self.path(tenant),
                        &format!("INSERT INTO notes VALUES ('{n}')"),
                    );
                }
            }
        }
    }

    fn runtime() -> tokio::runtime::Runtime {
        tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .unwrap()
    }

    fn connect(path: &Path) -> SqliteConnection {
        SqliteConnection::establish(path.to_str().unwrap()).unwrap()
    }

    fn exec(path: &Path, sql: &str) {
        connect(path).batch_execute(sql).unwrap();
    }

    fn count_notes(path: &Path) -> i64 {
        #[derive(QueryableByName)]
        struct Count {
            #[diesel(sql_type = diesel::sql_types::BigInt)]
            n: i64,
        }
        diesel::sql_query("SELECT COUNT(*) AS n FROM notes")
            .get_result::<Count>(&mut connect(path))
            .unwrap()
            .n
    }

    fn has_table(path: &Path, table: &str) -> bool {
        connect(path)
            .batch_execute(&format!("SELECT 1 FROM {table}"))
            .is_ok()
    }

    #[test]
    fn fleet_list_reports_every_database() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme", "globex"], 1);

        let (stdout, _) = run_ok(&app.dir, &["fleet", "list"]);
        assert!(stdout.contains("tenant:acme"), "{stdout}");
        assert!(stdout.contains("tenant:globex"), "{stdout}");

        let (stdout, _) = run_ok(&app.dir, &["fleet", "list", "--open", "--json"]);
        let rows: Vec<serde_json::Value> = serde_json::from_str(&stdout).expect(&stdout);
        assert_eq!(rows.len(), 2, "{stdout}");
        let acme = &rows[0];
        assert_eq!(acme["key"], "tenant:acme");
        assert_eq!(acme["identity"], "tenant:acme");
        assert!(acme["bytes"].as_u64().unwrap() > 0, "{stdout}");
        assert_eq!(acme["migration_version"], "20260101000000", "{stdout}");
        assert_eq!(acme["pending"], 0, "{stdout}");
        assert!(
            Path::new(acme["path"].as_str().unwrap()).ends_with("acme.db"),
            "{stdout}"
        );

        // Without --open, nothing is read from the files.
        let (stdout, _) = run_ok(&app.dir, &["fleet", "list", "--json"]);
        let rows: Vec<serde_json::Value> = serde_json::from_str(&stdout).expect(&stdout);
        assert!(rows[0].get("identity").is_none(), "{stdout}");

        // A file that is not a database: its row has the error, and the
        // command exits non-zero after it prints every row.
        let junk = app.path("initech");
        std::fs::create_dir_all(junk.parent().unwrap()).unwrap();
        std::fs::write(&junk, b"not a database, not at all, no").unwrap();
        let (stdout, stderr, code) = run_autumn(&app.dir, &["fleet", "list", "--open"]);
        assert_eq!(code, Some(1), "{stderr}");
        assert!(stdout.contains("tenant:acme"), "{stdout}");
        assert!(stdout.contains("tenant:initech"), "{stdout}");
        assert!(stderr.contains("1 of 3"), "{stderr}");
    }

    #[test]
    fn fleet_refuses_a_missing_root_and_creates_nothing() {
        let app = App::new("[replication]\nenabled = true\npath = \"replicas\"\n");
        let dest = app.dir.join("copy.db");
        for args in [
            vec!["fleet", "list"],
            vec!["fleet", "migrate"],
            vec!["fleet", "backup", "tenant:acme", dest.to_str().unwrap()],
            vec!["fleet", "delete", "tenant:acme", "--force"],
            vec!["fleet", "restore", "tenant:acme", "--force"],
        ] {
            let stderr = run_err(&app.dir, &args);
            assert!(stderr.contains("does not exist"), "{args:?}: {stderr}");
            assert!(!app.root().exists(), "{args:?} created the fleet root");
        }
    }

    #[test]
    fn fleet_resolves_the_profile_overlay() {
        let app = App::new("[profile.staging.database.fleet]\nroot = \"staging-fleet\"\n");
        std::fs::create_dir_all(app.dir.join("staging-fleet")).unwrap();
        let output = std::process::Command::new(super::autumn_bin())
            .args(["fleet", "list", "--profile", "staging"])
            .current_dir(&app.dir)
            .env_remove("AUTUMN_ENV")
            .env_remove("AUTUMN_PROFILE")
            .env("AUTUMN_MANIFEST_DIR", &app.dir)
            .output()
            .unwrap();
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(output.status.success(), "{stderr}");
        assert!(stderr.contains("staging-fleet"), "{stderr}");
    }

    #[test]
    fn fleet_needs_a_fleet_section() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("autumn.toml"),
            "[database]\nurl = \"sqlite://control.db\"\n",
        )
        .unwrap();
        let stderr = run_err(tmp.path(), &["fleet", "list"]);
        assert!(stderr.contains("[database.fleet]"), "{stderr}");
    }

    #[test]
    fn fleet_migrate_applies_pending_migrations_to_every_database() {
        let app = App::new("");
        let bare = app.bare_fleet();
        runtime().block_on(async {
            for tenant in ["acme", "globex", "initech"] {
                bare.provision(&bare.key_for(tenant).unwrap())
                    .await
                    .unwrap();
            }
            bare.close_all().await;
        });

        // One key first.
        let (_, stderr) = run_ok(&app.dir, &["fleet", "migrate", "--key", "tenant:acme"]);
        assert!(stderr.contains("tenant:acme"), "{stderr}");
        assert!(has_table(&app.path("acme"), "notes"));
        assert!(!has_table(&app.path("globex"), "notes"));

        // Then the rest, two at a time.
        run_ok(&app.dir, &["fleet", "migrate", "--concurrency", "2"]);
        for tenant in ["acme", "globex", "initech"] {
            let path = app.path(tenant);
            assert!(has_table(&path, "notes"), "{tenant}");
            assert!(has_table(&path, "_autumn_version_history"), "{tenant}");
            assert!(
                !has_table(&path, "api_tokens"),
                "{tenant}: control-plane tables stay on the control database"
            );
        }

        // Boot finds nothing left to apply.
        runtime().block_on(async {
            let boot = DatabaseFleet::builder(app.config())
                .migration_source(
                    "app",
                    diesel_migrations::FileBasedMigrations::from_path(app.dir.join("migrations"))
                        .unwrap(),
                )
                .framework_migrations()
                .apply_migrations_on_open(false)
                .build()
                .unwrap();
            boot.open(&boot.key_for("globex").unwrap()).await.unwrap();
            boot.close_all().await;
        });
    }

    #[test]
    fn fleet_migrate_reports_each_failure_and_exits_non_zero() {
        let app = App::new("");
        app.provision(&app.bare_fleet(), &["acme"], 0);
        // A file copied by hand onto another key's path.
        let copy = app.path("globex");
        std::fs::create_dir_all(copy.parent().unwrap()).unwrap();
        std::fs::copy(app.path("acme"), &copy).unwrap();

        let stderr = run_err(&app.dir, &["fleet", "migrate"]);
        assert!(
            stderr.contains("tenant:globex identifies itself as tenant:acme"),
            "{stderr}"
        );
        assert!(stderr.contains("1 of 2"), "{stderr}");
        assert!(has_table(&app.path("acme"), "notes"), "the others migrate");
    }

    #[test]
    fn fleet_migrate_refuses_a_version_boot_could_record_differently() {
        let app = App::new("");
        app.provision(&app.bare_fleet(), &["acme"], 0);
        // The version of the framework's derivations migration.
        let shared = app.dir.join("migrations/20260907101530_create_tags");
        std::fs::create_dir_all(&shared).unwrap();
        std::fs::write(shared.join("up.sql"), "CREATE TABLE tags (id INTEGER);").unwrap();
        std::fs::write(shared.join("down.sql"), "DROP TABLE tags;").unwrap();

        let stderr = run_err(&app.dir, &["fleet", "migrate"]);
        assert!(stderr.contains("20260907101530_create_tags"), "{stderr}");
        assert!(stderr.contains("AUTUMN_MIGRATE=1"), "{stderr}");
        assert!(
            !has_table(&app.path("acme"), "notes"),
            "nothing was applied"
        );
    }

    #[test]
    fn fleet_migrate_refuses_a_database_another_set_migrated() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme", "globex"], 0);
        // A plugin set recorded this version at boot.
        exec(
            &app.path("globex"),
            "INSERT INTO __diesel_schema_migrations (version) VALUES ('20990101000000')",
        );

        let stderr = run_err(&app.dir, &["fleet", "migrate"]);
        assert!(stderr.contains("tenant:globex: 20990101000000"), "{stderr}");
        assert!(stderr.contains("AUTUMN_MIGRATE=1"), "{stderr}");
        // Only the clean database, by key.
        run_ok(&app.dir, &["fleet", "migrate", "--key", "tenant:acme"]);
    }

    #[test]
    fn fleet_refuses_a_database_config_boot_refuses() {
        let app =
            App::new("[[database.shards]]\nname = \"s1\"\nprimary_url = \"sqlite://s1.db\"\n");
        std::fs::create_dir_all(app.root()).unwrap();
        let stderr = run_err(&app.dir, &["fleet", "list"]);
        assert!(
            stderr.contains("Invalid [database] configuration"),
            "{stderr}"
        );
        assert!(stderr.contains("database.shards"), "{stderr}");
    }

    #[test]
    fn fleet_migrate_stops_when_a_database_cannot_be_checked() {
        let app = App::new("");
        app.provision(&app.bare_fleet(), &["acme"], 0);
        let junk = app.path("initech");
        std::fs::create_dir_all(junk.parent().unwrap()).unwrap();
        std::fs::write(&junk, b"not a database, not at all, no").unwrap();

        let stderr = run_err(&app.dir, &["fleet", "migrate"]);
        assert!(
            stderr.contains("could not check tenant:initech"),
            "{stderr}"
        );
        assert!(
            !has_table(&app.path("acme"), "notes"),
            "nothing was migrated"
        );
        let stderr = run_err(&app.dir, &["fleet", "migrate", "--key", "tenant:globex"]);
        assert!(stderr.contains("tenant:globex does not exist"), "{stderr}");
    }

    #[test]
    fn fleet_refuses_a_control_database_inside_the_root() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("fleet");
        std::fs::create_dir(&root).unwrap();
        std::fs::write(
            tmp.path().join("autumn.toml"),
            format!(
                "[database]\nurl = \"sqlite://{}\"\n\n[database.fleet]\nmode = \"tenant\"\n\
                 root = \"fleet\"\n",
                root.join("control.db").display()
            ),
        )
        .unwrap();
        let stderr = run_err(tmp.path(), &["fleet", "list"]);
        assert!(stderr.contains("control database"), "{stderr}");
    }

    /// A default ACL on the directory overrides the umask. The copy must
    /// still give no other user access.
    #[cfg(target_os = "linux")]
    #[test]
    fn fleet_backup_ignores_a_default_acl() {
        use std::os::unix::fs::PermissionsExt as _;
        let app = App::new("");
        app.provision(&app.fleet(), &["acme"], 1);
        let out = app.dir.join("exports");
        std::fs::create_dir(&out).unwrap();
        let acl = std::process::Command::new("setfacl")
            .args(["-d", "-m", "u:nobody:r"])
            .arg(&out)
            .status();
        if !acl.is_ok_and(|s| s.success()) {
            eprintln!("skipped: setfacl is not available here");
            return;
        }
        let dest = out.join("acme.db");

        run_ok(
            &app.dir,
            &["fleet", "backup", "tenant:acme", dest.to_str().unwrap()],
        );
        // On a file with an ACL the group bits are the ACL mask: `---`
        // stops the inherited `user:nobody:r--` entry.
        let mode = std::fs::metadata(&dest).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o600, "only the owner reads the copy");
    }

    /// A symlinked directory on the way is resolved once, at the check; the
    /// copy goes to the directory that was checked.
    #[cfg(unix)]
    #[test]
    fn fleet_backup_resolves_a_symlinked_directory() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme"], 2);
        let real = app.dir.join("exports-real");
        std::fs::create_dir(&real).unwrap();
        let via = app.dir.join("exports-link");
        std::os::unix::fs::symlink(&real, &via).unwrap();
        let dest = via.join("acme.db");

        run_ok(
            &app.dir,
            &["fleet", "backup", "tenant:acme", dest.to_str().unwrap()],
        );
        assert_eq!(count_notes(&real.join("acme.db")), 2);
    }

    #[test]
    fn fleet_backup_writes_a_copy() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme"], 3);
        let dest = app.dir.join("acme-export.db");

        // `tenant/<id>` is accepted as well as the canonical `tenant:<id>`.
        run_ok(
            &app.dir,
            &["fleet", "backup", "tenant/acme", dest.to_str().unwrap()],
        );
        assert_eq!(count_notes(&dest), 3);
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt as _;
            let mode = std::fs::metadata(&dest).unwrap().permissions().mode();
            assert_eq!(mode & 0o777, 0o600, "only the owner reads the copy");
        }

        let stderr = run_err(
            &app.dir,
            &["fleet", "backup", "tenant:acme", dest.to_str().unwrap()],
        );
        assert!(stderr.contains("already exists"), "{stderr}");

        // A file or a dangling symlink another user put at the path is never
        // written through.
        let planted = app.dir.join("planted.db");
        std::fs::write(&planted, b"").unwrap();
        let stderr = run_err(
            &app.dir,
            &["fleet", "backup", "tenant:acme", planted.to_str().unwrap()],
        );
        assert!(stderr.contains("already exists"), "{stderr}");
        assert_eq!(std::fs::metadata(&planted).unwrap().len(), 0);
        #[cfg(unix)]
        {
            let link = app.dir.join("link.db");
            let target = app.dir.join("target.db");
            std::os::unix::fs::symlink(&target, &link).unwrap();
            let stderr = run_err(
                &app.dir,
                &["fleet", "backup", "tenant:acme", link.to_str().unwrap()],
            );
            assert!(stderr.contains("already exists"), "{stderr}");
            assert!(!target.exists(), "the link was not followed");
        }
        // Another user could swap the staging directory in a shared
        // directory without the sticky bit.
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt as _;
            let shared = app.dir.join("shared");
            std::fs::create_dir(&shared).unwrap();
            std::fs::set_permissions(&shared, std::fs::Permissions::from_mode(0o777)).unwrap();
            let into = shared.join("acme.db");
            let stderr = run_err(
                &app.dir,
                &["fleet", "backup", "tenant:acme", into.to_str().unwrap()],
            );
            assert!(stderr.contains("writable by other users"), "{stderr}");
            assert!(!into.exists());
            // A private directory under the shared one: the shared one can
            // still rename it.
            let private = shared.join("private");
            std::fs::create_dir(&private).unwrap();
            let nested = private.join("acme.db");
            let stderr = run_err(
                &app.dir,
                &["fleet", "backup", "tenant:acme", nested.to_str().unwrap()],
            );
            assert!(stderr.contains("writable by other users"), "{stderr}");
            assert!(!nested.exists());
            // The sticky bit (as on /tmp) stops the swap.
            std::fs::set_permissions(&shared, std::fs::Permissions::from_mode(0o1777)).unwrap();
            run_ok(
                &app.dir,
                &["fleet", "backup", "tenant:acme", into.to_str().unwrap()],
            );
            assert_eq!(count_notes(&into), 3);
        }
        let leftovers: Vec<_> = std::fs::read_dir(&app.dir)
            .unwrap()
            .filter_map(Result::ok)
            .filter(|e| {
                e.file_name()
                    .to_string_lossy()
                    .starts_with(".autumn-fleet-backup")
            })
            .collect();
        assert!(leftovers.is_empty(), "the staging directory is removed");

        let other = app.dir.join("missing.db");
        let stderr = run_err(
            &app.dir,
            &["fleet", "backup", "tenant:initech", other.to_str().unwrap()],
        );
        assert!(stderr.contains("does not exist"), "{stderr}");
        assert!(!app.path("initech").exists(), "backup never creates");
        assert!(!other.exists());

        let stderr = run_err(
            &app.dir,
            &["fleet", "backup", "Tenant:ACME", other.to_str().unwrap()],
        );
        assert!(stderr.contains("not a database name"), "{stderr}");
    }

    #[test]
    fn fleet_delete_needs_force_and_removes_the_database() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme", "globex"], 1);
        let path = app.path("acme");

        let stderr = run_err(&app.dir, &["fleet", "delete", "tenant:acme"]);
        assert!(stderr.contains("--force"), "{stderr}");
        assert!(path.is_file());

        run_ok(&app.dir, &["fleet", "delete", "tenant:acme", "--force"]);
        assert!(!path.exists());
        assert!(app.path("globex").is_file(), "only the named database");

        let stderr = run_err(&app.dir, &["fleet", "delete", "tenant:acme", "--force"]);
        assert!(stderr.contains("does not exist"), "{stderr}");
    }

    #[test]
    fn fleet_delete_refuses_a_database_open_elsewhere() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme"], 1);
        let path = app.path("acme");
        // A live app: another process with the file open.
        let mut live = connect(&path);
        live.batch_execute("SELECT 1 FROM notes").unwrap();

        let stderr = run_err(&app.dir, &["fleet", "delete", "tenant:acme", "--force"]);
        assert!(stderr.contains("open in another process"), "{stderr}");
        assert!(path.is_file(), "nothing was removed");

        // The app closes the database: the delete goes through.
        drop(live);
        run_ok(&app.dir, &["fleet", "delete", "tenant:acme", "--force"]);
        assert!(!path.exists());
    }

    /// The `fleet_replication` setup: a fleet that ships to a local replica
    /// directory, under the root `autumn.toml`'s `[replication]` resolves to.
    fn replicated_fleet(app: &App, replicas: &Path) -> (DatabaseFleet, Arc<FleetReplication>) {
        let fleet = app.fleet();
        let destination: Arc<dyn ReplicaDestination> =
            Arc::new(FileDestination::new(replicas).unwrap());
        let settings = ReplicationSettings {
            database_path: PathBuf::new(),
            root: autumn_web::replication::segment::root_prefix(Some("app"), "dev"),
            sync_interval: Duration::from_secs(1),
            snapshot_interval: Duration::from_secs(3600),
            max_wal_bytes: 4 * 1024 * 1024,
            retention: Duration::from_secs(24 * 3600),
            verify_interval: None,
        };
        let replication = Arc::new(FleetReplication::new(
            destination,
            settings,
            Duration::from_secs(30),
            false,
            Arc::new(autumn_web::time::SystemClock),
        ));
        assert!(fleet.install_replication(Arc::clone(&replication)).is_ok());
        (fleet, replication)
    }

    #[test]
    fn fleet_restore_rebuilds_a_database_from_its_replica() {
        let tmp = tempfile::tempdir().unwrap();
        let replicas = tmp.path().join("replicas");
        let app = App::new(&format!(
            "[replication]\nenabled = true\npath = \"{}\"\nprefix = \"app\"\n",
            replicas.display()
        ));
        let (fleet, replication) = replicated_fleet(&app, &replicas);
        let rt = runtime();
        let before_second = rt.block_on(async {
            let key = fleet.key_for("acme").unwrap();
            let db = fleet.provision(&key).await.unwrap();
            // Written while the fleet holds the database open, so the
            // replicator ships the frames.
            exec(db.path(), "INSERT INTO notes VALUES ('first')");
            let ticker = Arc::clone(&replication);
            tokio::task::spawn_blocking(move || ticker.tick_all())
                .await
                .unwrap();
            // A restore plans by millisecond timestamps.
            tokio::time::sleep(Duration::from_millis(100)).await;
            let between = chrono::Utc::now();
            tokio::time::sleep(Duration::from_millis(100)).await;
            exec(db.path(), "INSERT INTO notes VALUES ('second')");
            drop(db);
            // Closing ships the last frames.
            fleet.close_all().await;
            between
        });
        let path = app.path("acme");
        assert_eq!(count_notes(&path), 2);
        exec(&path, "DELETE FROM notes");

        // Destructive: needs --force.
        let stderr = run_err(&app.dir, &["fleet", "restore", "tenant:acme"]);
        assert!(stderr.contains("--force"), "{stderr}");
        assert_eq!(count_notes(&path), 0, "nothing was restored");

        let (_, stderr) = run_ok(&app.dir, &["fleet", "restore", "tenant:acme", "--force"]);
        assert!(stderr.contains("integrity verified"), "{stderr}");
        assert_eq!(count_notes(&path), 2);

        // Point in time.
        let at = before_second.to_rfc3339();
        run_ok(
            &app.dir,
            &[
                "fleet",
                "restore",
                "tenant:acme",
                "--timestamp",
                &at,
                "--force",
            ],
        );
        assert_eq!(count_notes(&path), 1);

        // A lost volume: the file is gone, the replica is not. The operator
        // makes the new root.
        std::fs::remove_dir_all(app.root()).unwrap();
        std::fs::create_dir(app.root()).unwrap();
        run_ok(&app.dir, &["fleet", "restore", "tenant:acme", "--force"]);
        assert_eq!(count_notes(&path), 2);

        let stderr = run_err(
            &app.dir,
            &[
                "fleet",
                "restore",
                "tenant:acme",
                "--timestamp",
                "yesterday",
                "--force",
            ],
        );
        assert!(stderr.contains("RFC 3339"), "{stderr}");

        let stderr = run_err(&app.dir, &["fleet", "restore", "tenant:globex", "--force"]);
        assert!(
            stderr.contains("tenant:globex has no replica under app/dev/fleet/tenant/globex"),
            "{stderr}"
        );
    }

    #[test]
    fn fleet_restore_refuses_a_database_open_elsewhere() {
        let tmp = tempfile::tempdir().unwrap();
        let replicas = tmp.path().join("replicas");
        let app = App::new(&format!(
            "[replication]\nenabled = true\npath = \"{}\"\nprefix = \"app\"\n",
            replicas.display()
        ));
        let (fleet, _) = replicated_fleet(&app, &replicas);
        runtime().block_on(async {
            fleet
                .provision(&fleet.key_for("acme").unwrap())
                .await
                .unwrap();
            fleet.close_all().await;
        });
        let path = app.path("acme");
        let mut live = connect(&path);
        live.batch_execute("SELECT 1 FROM notes").unwrap();

        let stderr = run_err(&app.dir, &["fleet", "restore", "tenant:acme", "--force"]);
        assert!(stderr.contains("open in another process"), "{stderr}");

        drop(live);
        run_ok(&app.dir, &["fleet", "restore", "tenant:acme", "--force"]);
    }

    #[test]
    fn fleet_restore_refuses_two_replica_destinations() {
        for enabled in [true, false] {
            let app = App::new(&format!(
                "[replication]\nenabled = {enabled}\npath = \"replicas\"\n\n\
                 [replication.s3]\nbucket = \"b\"\naccess_key_id_env = \"K\"\n\
                 secret_access_key_env = \"S\"\n"
            ));
            app.provision(&app.fleet(), &["acme"], 1);
            let stderr = run_err(&app.dir, &["fleet", "restore", "tenant:acme", "--force"]);
            assert!(
                stderr.contains("both `s3` and `path`"),
                "{enabled}: {stderr}"
            );
            assert_eq!(count_notes(&app.path("acme")), 1, "nothing was restored");
        }
    }

    #[test]
    fn fleet_restore_needs_replication() {
        let app = App::new("");
        app.provision(&app.fleet(), &["acme"], 1);
        let stderr = run_err(&app.dir, &["fleet", "restore", "tenant:acme", "--force"]);
        assert!(stderr.contains("[replication]"), "{stderr}");
        assert_eq!(count_notes(&app.path("acme")), 1);
    }
}