Skip to main content

spawn_db/commands/
mod.rs

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