Skip to main content

spawn_db/commands/
mod.rs

1use crate::config::Config;
2use anyhow::Result;
3
4pub mod check;
5pub mod init;
6pub mod migration;
7pub mod test;
8
9pub use check::Check;
10pub use init::Init;
11pub use migration::{
12    AdoptMigration, ApplyMigration, BuildMigration, MigrationStatus, NewMigration, PinMigration,
13};
14pub use test::{BuildTest, CompareTests, ExpectTest, NewTest, RunTest};
15
16/// Telemetry information for a command.
17#[derive(Debug, Clone, Default)]
18pub struct TelemetryInfo {
19    /// A sanitized label for the command (e.g., "migration build").
20    /// Should not contain sensitive values like file paths or migration names.
21    pub label: String,
22    /// Additional safe properties to include in telemetry.
23    /// Only include non-sensitive boolean flags or enum values.
24    pub properties: Vec<(&'static str, String)>,
25}
26
27impl TelemetryInfo {
28    pub fn new(label: impl Into<String>) -> Self {
29        Self {
30            label: label.into(),
31            properties: vec![],
32        }
33    }
34
35    pub fn with_properties(mut self, properties: Vec<(&'static str, String)>) -> Self {
36        self.properties = properties;
37        self
38    }
39}
40
41/// Trait for describing commands in a telemetry-safe way.
42///
43/// Implementations should return sanitized info that doesn't
44/// contain sensitive information like file paths or migration names.
45pub trait TelemetryDescribe {
46    fn telemetry(&self) -> TelemetryInfo;
47}
48
49/// Trait for executable commands. All commands must also implement TelemetryDescribe.
50#[allow(async_fn_in_trait)]
51pub trait Command: TelemetryDescribe {
52    async fn execute(&self, config: &Config) -> Result<Outcome>;
53}
54
55pub enum Outcome {
56    AdoptedMigration,
57    AppliedMigrations,
58    BuiltMigration { content: String, pinned_warn: bool },
59    CheckFailed,
60    NewMigration(String),
61    NewTest(String),
62    PinnedMigration { hash: String },
63    Success,
64    Unimplemented,
65}