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    },
18    #[command(about = "Execute write operation (INSERT, UPDATE, DELETE)")]
19    Execute { sql: String },
20    #[command(about = "List all tables with row counts and sizes")]
21    Tables {
22        #[arg(long, help = "Filter tables by pattern")]
23        filter: Option<String>,
24    },
25    #[command(about = "Describe table schema with columns and indexes")]
26    Describe { table_name: String },
27    #[command(about = "Show database information")]
28    Info,
29    #[command(about = "Run database migrations")]
30    Migrate {
31        #[arg(
32            long,
33            help = "Continue past migration checksum mismatches with a warning instead of \
34                    erroring (use with caution)"
35        )]
36        allow_checksum_drift: bool,
37    },
38    #[command(about = "Revert the most recently applied migrations for an extension")]
39    MigrateDown {
40        #[arg(help = "Extension ID")]
41        extension: String,
42        #[arg(help = "Number of migrations to revert")]
43        count: u32,
44    },
45    #[command(about = "Show migration status and history")]
46    Migrations {
47        #[command(subcommand)]
48        cmd: MigrationsCommands,
49    },
50    #[command(
51        about = "Show pending migrations (dry-run / plan, no DB writes)",
52        name = "migrate-plan"
53    )]
54    MigratePlan {
55        #[arg(help = "Filter by extension ID (default: all extensions)")]
56        extension: Option<String>,
57        #[arg(long, help = "Emit JSON instead of a text table")]
58        json: bool,
59    },
60    #[command(
61        about = "Detailed introspectable migration status (applied, pending, drift)",
62        name = "migrate-status"
63    )]
64    MigrateStatus {
65        #[arg(help = "Filter by extension ID (default: all extensions)")]
66        extension: Option<String>,
67        #[arg(long, help = "Emit JSON instead of a text table")]
68        json: bool,
69    },
70    #[command(
71        about = "Repair migration checksum drift — re-applies edited migrations in place, or with \
72                 --reconcile-only rewrites stored checksums without executing SQL",
73        name = "migrate-repair"
74    )]
75    MigrateRepair {
76        #[arg(help = "Limit repair to a single extension (default: all extensions)")]
77        extension: Option<String>,
78        #[arg(
79            long,
80            help = "Apply the repair: re-execute the SQL of each drifted migration and rewrite \
81                    its stored checksum. Without this flag, the command is a dry-run that only \
82                    lists drift."
83        )]
84        apply: bool,
85        #[arg(
86            long,
87            help = "Bookkeeping only: rewrite stored checksums to match the current migration \
88                    files without executing any SQL. Asserts the schema already matches the \
89                    edited files; nothing verifies that."
90        )]
91        reconcile_only: bool,
92        #[arg(long, help = "Emit JSON instead of a text table")]
93        json: bool,
94    },
95    #[command(
96        about = "Record a migration as already applied without running its SQL (recovers partial \
97                 state where schema is applied but tracking row is missing)",
98        name = "migrate-mark-applied"
99    )]
100    MigrateMarkApplied {
101        #[arg(long, help = "Extension ID owning the migration")]
102        extension: String,
103        #[arg(long, help = "Migration version to mark as applied")]
104        version: u32,
105        #[arg(long, help = "Emit JSON instead of a text summary")]
106        json: bool,
107    },
108    #[command(about = "Assign admin role to a user")]
109    AssignAdmin { user: String },
110    #[command(about = "Show database connection status")]
111    Status,
112    #[command(about = "Get row count for a table")]
113    Count { table_name: String },
114    #[command(about = "List all indexes")]
115    Indexes {
116        #[arg(long, help = "Filter by table name")]
117        table: Option<String>,
118    },
119    #[command(about = "Show database and table sizes")]
120    Size,
121    #[command(about = "Diff live schema against extension declarations")]
122    Doctor,
123}
124
125#[derive(Debug, Subcommand)]
126pub enum MigrationsCommands {
127    #[command(about = "Show migration status for all extensions", alias = "list")]
128    Status,
129    #[command(about = "Show migration history for an extension")]
130    History {
131        #[arg(help = "Extension ID")]
132        extension: String,
133    },
134}