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