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        #[arg(
48            long,
49            conflicts_with = "allow_checksum_drift",
50            help = "If the install fails on checksum drift, and only then, re-apply the drifted \
51                    migrations (as `migrate-repair --apply`) and retry once. Any other failure \
52                    is reported as-is: repair cannot fix it"
53        )]
54        repair_drift: bool,
55    },
56    #[command(about = "Revert the most recently applied migrations for an extension")]
57    MigrateDown {
58        #[arg(help = "Extension ID")]
59        extension: String,
60        #[arg(help = "Number of migrations to revert")]
61        count: u32,
62    },
63    #[command(about = "Show migration status and history")]
64    Migrations {
65        #[command(subcommand)]
66        cmd: MigrationsCommands,
67    },
68    #[command(
69        about = "Show pending migrations (dry-run / plan, no DB writes)",
70        name = "migrate-plan"
71    )]
72    MigratePlan {
73        #[arg(help = "Filter by extension ID (default: all extensions)")]
74        extension: Option<String>,
75        #[arg(long, help = "Emit JSON instead of a text table")]
76        json: bool,
77    },
78    #[command(
79        about = "Detailed introspectable migration status (applied, pending, drift)",
80        name = "migrate-status"
81    )]
82    MigrateStatus {
83        #[arg(help = "Filter by extension ID (default: all extensions)")]
84        extension: Option<String>,
85        #[arg(long, help = "Emit JSON instead of a text table")]
86        json: bool,
87    },
88    #[command(
89        about = "Repair migration checksum drift — re-applies edited migrations in place, or with \
90                 --reconcile-only rewrites stored checksums without executing SQL",
91        name = "migrate-repair"
92    )]
93    MigrateRepair {
94        #[arg(help = "Limit repair to a single extension (default: all extensions)")]
95        extension: Option<String>,
96        #[arg(
97            long,
98            help = "Apply the repair: re-execute the SQL of each drifted migration and rewrite \
99                    its stored checksum. Without this flag, the command is a dry-run that only \
100                    lists drift."
101        )]
102        apply: bool,
103        #[arg(
104            long,
105            help = "Bookkeeping only: rewrite stored checksums to match the current migration \
106                    files without executing any SQL. Asserts the schema already matches the \
107                    edited files; nothing verifies that."
108        )]
109        reconcile_only: bool,
110        #[arg(long, help = "Emit JSON instead of a text table")]
111        json: bool,
112    },
113    #[command(
114        about = "Record a migration as already applied without running its SQL (recovers partial \
115                 state where schema is applied but tracking row is missing)",
116        name = "migrate-mark-applied"
117    )]
118    MigrateMarkApplied {
119        #[arg(long, help = "Extension ID owning the migration")]
120        extension: String,
121        #[arg(long, help = "Migration version to mark as applied")]
122        version: u32,
123        #[arg(long, help = "Emit JSON instead of a text summary")]
124        json: bool,
125    },
126    #[command(about = "Assign admin role to a user")]
127    AssignAdmin { user: String },
128    #[command(about = "Show database connection status")]
129    Status,
130    #[command(about = "Get row count for a table")]
131    Count { table_name: String },
132    #[command(about = "List all indexes")]
133    Indexes {
134        #[arg(long, help = "Filter by table name")]
135        table: Option<String>,
136    },
137    #[command(about = "Show database and table sizes")]
138    Size,
139    #[command(about = "Diff live schema against extension declarations")]
140    Doctor,
141}
142
143#[derive(Debug, Subcommand)]
144pub enum MigrationsCommands {
145    #[command(about = "Show migration status for all extensions", alias = "list")]
146    Status,
147    #[command(about = "Show migration history for an extension")]
148    History {
149        #[arg(help = "Extension ID")]
150        extension: String,
151    },
152}