Skip to main content

systemprompt_cli/commands/infrastructure/db/
commands.rs

1//! `infra db` command dispatch.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use clap::Subcommand;
7
8#[derive(Debug, Subcommand)]
9pub enum DbCommands {
10    #[command(about = "Execute SQL query (read-only)")]
11    Query {
12        sql: String,
13        #[arg(long)]
14        limit: Option<u32>,
15        #[arg(long)]
16        offset: Option<u32>,
17        #[arg(long)]
18        format: Option<String>,
19    },
20    #[command(about = "Execute write operation (INSERT, UPDATE, DELETE)")]
21    Execute {
22        sql: String,
23        #[arg(long)]
24        format: Option<String>,
25    },
26    #[command(about = "List all tables with row counts and sizes")]
27    Tables {
28        #[arg(long, help = "Filter tables by pattern")]
29        filter: Option<String>,
30    },
31    #[command(about = "Describe table schema with columns and indexes")]
32    Describe { table_name: String },
33    #[command(about = "Show database information")]
34    Info,
35    #[command(about = "Run database migrations")]
36    Migrate {
37        #[arg(
38            long,
39            help = "Continue past migration checksum mismatches with a warning instead of \
40                    erroring (use with caution)"
41        )]
42        allow_checksum_drift: bool,
43    },
44    #[command(about = "Revert the most recently applied migrations for an extension")]
45    MigrateDown {
46        #[arg(help = "Extension ID")]
47        extension: String,
48        #[arg(help = "Number of migrations to revert")]
49        count: u32,
50    },
51    #[command(
52        about = "Squash an extension's migrations 1..=N into a baseline at version 0 (dry-run by \
53                 default)"
54    )]
55    MigrateSquash {
56        #[arg(long, help = "Extension ID whose migrations should be squashed")]
57        extension: String,
58        #[arg(
59            long,
60            help = "Squash migrations with version 1..=through into the baseline"
61        )]
62        through: u32,
63        #[arg(
64            long,
65            help = "Apply the squash (write baseline file + rewrite DB rows). Without this flag, \
66                    the command is a dry-run."
67        )]
68        apply: bool,
69    },
70    #[command(about = "Show migration status and history")]
71    Migrations {
72        #[command(subcommand)]
73        cmd: MigrationsCommands,
74    },
75    #[command(
76        about = "Show pending migrations (dry-run / plan, no DB writes)",
77        name = "migrate-plan"
78    )]
79    MigratePlan {
80        #[arg(help = "Filter by extension ID (default: all extensions)")]
81        extension: Option<String>,
82        #[arg(long, help = "Emit JSON instead of a text table")]
83        json: bool,
84    },
85    #[command(
86        about = "Detailed introspectable migration status (applied, pending, drift)",
87        name = "migrate-status"
88    )]
89    MigrateStatus {
90        #[arg(help = "Filter by extension ID (default: all extensions)")]
91        extension: Option<String>,
92        #[arg(long, help = "Emit JSON instead of a text table")]
93        json: bool,
94    },
95    #[command(
96        about = "Repair migration checksum drift — re-applies edited migrations in place (no data \
97                 loss)",
98        name = "migrate-repair"
99    )]
100    MigrateRepair {
101        #[arg(help = "Limit repair to a single extension (default: all extensions)")]
102        extension: Option<String>,
103        #[arg(
104            long,
105            help = "Apply the repair. Without this flag, the command is a dry-run that only lists \
106                    drift."
107        )]
108        apply: bool,
109        #[arg(long, help = "Emit JSON instead of a text table")]
110        json: bool,
111    },
112    #[command(
113        about = "Record a migration as already applied without running its SQL (recovers partial \
114                 state where schema is applied but tracking row is missing)",
115        name = "migrate-mark-applied"
116    )]
117    MigrateMarkApplied {
118        #[arg(long, help = "Extension ID owning the migration")]
119        extension: String,
120        #[arg(long, help = "Migration version to mark as applied")]
121        version: u32,
122        #[arg(long, help = "Emit JSON instead of a text summary")]
123        json: bool,
124    },
125    #[command(about = "Assign admin role to a user")]
126    AssignAdmin { user: String },
127    #[command(about = "Show database connection status")]
128    Status,
129    #[command(about = "Validate database schema against expected tables")]
130    Validate,
131    #[command(about = "Get row count for a table")]
132    Count { table_name: String },
133    #[command(about = "List all indexes")]
134    Indexes {
135        #[arg(long, help = "Filter by table name")]
136        table: Option<String>,
137    },
138    #[command(about = "Show database and table sizes")]
139    Size,
140    #[command(about = "Diff live schema against extension declarations")]
141    Doctor,
142}
143
144#[derive(Debug, Subcommand)]
145pub enum MigrationsCommands {
146    #[command(about = "Show migration status for all extensions", alias = "list")]
147    Status,
148    #[command(about = "Show migration history for an extension")]
149    History {
150        #[arg(help = "Extension ID")]
151        extension: String,
152    },
153}