1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use std::collections::HashMap;
use clap::{Parser, Subcommand};
use clap_complete::Shell;
#[derive(Parser, Debug)]
#[command(name = "seedgen")]
#[command(about = "Zero-config database seed data generator", long_about = None)]
#[command(version)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// PostgreSQL connection URL.
#[arg(long, short = 'u', env = "DATABASE_URL", global = true)]
pub url: Option<String>,
/// Increase verbosity (-v, -vv, -vvv).
#[arg(long, short, action = clap::ArgAction::Count, global = true)]
pub verbose: u8,
/// Suppress output except errors.
#[arg(long, short, global = true)]
pub quiet: bool,
/// Disable colored output.
#[arg(long, global = true)]
pub no_color: bool,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Introspect database schema and print it.
Introspect {
/// Output format: `table`, `json`, or `yaml`.
#[arg(long, default_value = "table")]
format: String,
/// Write to this file instead of stdout.
#[arg(long)]
output: Option<String>,
/// Comma-separated list of tables to include.
#[arg(long, value_delimiter = ',')]
include: Option<Vec<String>>,
/// Comma-separated list of tables to exclude.
#[arg(long, value_delimiter = ',')]
exclude: Option<Vec<String>>,
},
/// Generate and insert seed data.
Generate {
/// Seed for deterministic output. Defaults to time-based.
#[arg(long)]
seed: Option<u64>,
/// Default rows per table.
#[arg(long, default_value = "10")]
rows: usize,
/// Built-in scenario name: ecommerce, saas, blog, social.
#[arg(long)]
scenario: Option<String>,
/// Custom scenario YAML file.
#[arg(long, short = 'f')]
file: Option<String>,
/// Per-table row counts, e.g. `users=100,orders=500`.
#[arg(long, value_parser = parse_entities)]
entities: Option<HashMap<String, usize>>,
/// Write generated SQL/JSON to this file instead of inserting.
#[arg(long)]
output: Option<String>,
/// Output format when --output is set: `sql`, `json`, `copy`.
#[arg(long, default_value = "sql")]
format: String,
/// Use the COPY protocol for faster inserts.
#[arg(long)]
fast: bool,
/// Print the generation plan without inserting.
#[arg(long)]
dry_run: bool,
/// Locale for fake data: en, id, ja, de, fr.
#[arg(long, default_value = "en")]
locale: String,
/// Comma-separated list of tables to include.
#[arg(long, value_delimiter = ',')]
include: Option<Vec<String>>,
/// Comma-separated list of tables to exclude.
#[arg(long, value_delimiter = ',')]
exclude: Option<Vec<String>>,
/// TRUNCATE all target tables before inserting.
#[arg(long)]
truncate_first: bool,
/// Generate from a production profile file (statistics-driven).
#[arg(long)]
profile: Option<String>,
/// Scale factor for `--profile` generation (0 < scale <= 1).
#[arg(long, default_value = "1.0")]
scale: f64,
},
/// Profile a database's statistics (read-only, aggregate-only).
Profile {
/// Write the profile to this file.
#[arg(long, default_value = "prod-profile.yaml")]
output: String,
/// Profile format: `yaml` or `json`.
#[arg(long, default_value = "yaml")]
format: String,
/// Max distinct values before a column is treated as high-cardinality.
#[arg(long, default_value = "50")]
cardinality_threshold: usize,
/// Columns to exclude (`table.column`), comma-separated.
#[arg(long, value_delimiter = ',')]
exclude: Option<Vec<String>>,
/// Columns to force-include even if sensitive (`table.column`).
#[arg(long, value_delimiter = ',')]
include: Option<Vec<String>>,
/// Refuse to profile when connected as a superuser.
#[arg(long)]
strict_security: bool,
/// Skip hourly-density capture for timestamp columns.
#[arg(long)]
no_hourly: bool,
/// Skip monthly-density capture for timestamp columns.
#[arg(long)]
no_monthly: bool,
/// Print the queries that would run, without executing them.
#[arg(long)]
dry_run_queries: bool,
/// Print the queries as a runnable SQL file, without executing them.
#[arg(long)]
export_queries: bool,
/// Build the profile offline from an externally-collected results file
/// (no database connection needed).
#[arg(long)]
import_results: Option<String>,
},
/// TRUNCATE tables in safe order.
Reset {
/// Required safety flag.
#[arg(long)]
confirm: bool,
/// Only truncate these tables (comma-separated).
#[arg(long, value_delimiter = ',')]
only: Option<Vec<String>>,
/// Use TRUNCATE CASCADE.
#[arg(long, default_value = "true")]
cascade: bool,
},
/// Validate a scenario YAML file against the schema.
Validate {
/// Scenario YAML file to validate.
#[arg(long, short = 'f')]
file: String,
},
/// Start the MCP server.
McpServer {
/// Transport: stdio or http.
#[arg(long, default_value = "stdio")]
transport: String,
/// Port for the HTTP transport.
#[arg(long, default_value = "3100")]
port: u16,
},
/// Print shell completion script.
Completions {
/// Shell to generate completions for.
shell: Shell,
},
}
fn parse_entities(s: &str) -> Result<HashMap<String, usize>, String> {
let mut map = HashMap::new();
for pair in s.split(',') {
let (k, v) = pair
.split_once('=')
.ok_or_else(|| format!("expected `name=count`, got `{pair}`"))?;
let k = k.trim();
let count: usize = v
.trim()
.parse()
.map_err(|e| format!("invalid count for `{k}`: {e}"))?;
if k.is_empty() {
return Err("empty table name".into());
}
map.insert(k.to_string(), count);
}
Ok(map)
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn test_cli_builds_without_panic() {
Cli::command().debug_assert();
}
#[test]
fn test_parse_entities_simple() {
let m = parse_entities("users=100,orders=500").unwrap();
assert_eq!(m.get("users"), Some(&100));
assert_eq!(m.get("orders"), Some(&500));
}
#[test]
fn test_parse_entities_with_whitespace() {
let m = parse_entities("users = 50, products = 200").unwrap();
assert_eq!(m.get("users"), Some(&50));
assert_eq!(m.get("products"), Some(&200));
}
#[test]
fn test_parse_entities_missing_equals_errors() {
assert!(parse_entities("users 100").is_err());
}
#[test]
fn test_parse_entities_invalid_count_errors() {
assert!(parse_entities("users=abc").is_err());
}
#[test]
fn test_parse_entities_empty_name_errors() {
assert!(parse_entities("=100").is_err());
}
}