drizzle-cli 0.3.0

Command-line interface for drizzle-rs migrations
Documentation
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! `drizzle seed`: fills a live database with deterministic test data.
//!
//! The tables are read from the database itself, so no Rust schema is
//! needed. Needs a database driver feature enabled at build time.

use crate::commands::overrides::{self, ConnectionOverrides, FilterArgs};
use crate::config::{Config, Dialect, Driver};
use crate::error::CliError;
use crate::output;
use std::path::PathBuf;

#[derive(clap::Args, Debug, Clone)]
pub struct SeedOptions {
    /// RNG seed: the same seed and drizzle version give the same rows
    #[arg(long, default_value_t = 0)]
    pub seed: u64,

    /// Rows per table: `--count 50` for every table, `--count users=100`
    /// for one (repeatable). A table without a count that references a
    /// seeded table gets one row per parent row, or `--relation` rows.
    #[arg(long, value_name = "N|TABLE=N")]
    pub count: Vec<String>,

    /// Rows of CHILD per row of PARENT: `--relation users:posts=3`
    /// (repeatable). CHILD needs a foreign key to PARENT.
    #[arg(long, value_name = "PARENT:CHILD=N")]
    pub relation: Vec<String>,

    /// Leave a table out (repeatable)
    #[arg(long, value_name = "TABLE")]
    pub skip: Vec<String>,

    /// Empty the seeded tables first (children before parents)
    #[arg(long)]
    pub reset: bool,

    /// Write the SQL to FILE instead of running it (`-` for stdout)
    #[arg(long, value_name = "FILE")]
    pub out: Option<PathBuf>,

    /// Run `--reset` without asking for confirmation
    #[arg(long)]
    pub force: bool,

    /// Override dialect from config
    #[arg(long)]
    pub dialect: Option<Dialect>,

    #[command(flatten)]
    pub filters: FilterArgs,

    #[command(flatten)]
    pub connection: ConnectionOverrides,
}

/// The parsed `--count`, `--relation` and `--skip` arguments.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct SeedPlan {
    /// `--seed`.
    pub seed: u64,
    /// A bare `--count N`.
    pub default_count: Option<usize>,
    /// `--count TABLE=N`.
    pub counts: Vec<(String, usize)>,
    /// `--relation PARENT:CHILD=N`.
    pub relations: Vec<(String, String, usize)>,
    /// `--skip TABLE`.
    pub skips: Vec<String>,
    /// `--reset`.
    pub reset: bool,
}

impl SeedPlan {
    /// Parses the seed options.
    ///
    /// # Errors
    ///
    /// Returns [`CliError::Other`] for a malformed `--count` or `--relation`
    /// value, or two bare `--count`s.
    pub fn from_options(opts: &SeedOptions) -> Result<Self, CliError> {
        let number = |text: &str, flag: &str, value: &str| {
            text.trim().parse::<usize>().map_err(|_| {
                CliError::Other(format!(
                    "--{flag} {value}: `{}` is not a row count",
                    text.trim()
                ))
            })
        };
        let mut plan = Self {
            seed: opts.seed,
            reset: opts.reset,
            skips: opts.skip.clone(),
            ..Self::default()
        };
        for value in &opts.count {
            match value.rsplit_once('=') {
                Some((table, count)) if !table.trim().is_empty() => {
                    plan.counts
                        .push((table.trim().to_owned(), number(count, "count", value)?));
                }
                Some(_) => {
                    return Err(CliError::Other(format!(
                        "--count {value}: expected N or TABLE=N"
                    )));
                }
                None if plan.default_count.is_some() => {
                    return Err(CliError::Other(
                        "--count without a table was given twice".to_owned(),
                    ));
                }
                None => plan.default_count = Some(number(value, "count", value)?),
            }
        }
        for value in &opts.relation {
            let parsed = value.rsplit_once('=').and_then(|(tables, count)| {
                let (parent, child) = tables.split_once(':')?;
                (!parent.trim().is_empty() && !child.trim().is_empty())
                    .then(|| (parent.trim().to_owned(), child.trim().to_owned(), count))
            });
            let Some((parent, child, count)) = parsed else {
                return Err(CliError::Other(format!(
                    "--relation {value}: expected PARENT:CHILD=N"
                )));
            };
            plan.relations
                .push((parent, child, number(count, "relation", value)?));
        }
        Ok(plan)
    }
}

/// The SQL a seed runs, with every value written inline.
#[derive(Debug, Default)]
pub struct SeedScript {
    /// `DELETE`s (and resets) that empty the tables, children first.
    pub reset: Vec<String>,
    /// The INSERTs (and `PostgreSQL` sequence updates), parents first.
    pub inserts: Vec<String>,
    /// Rows inserted per table, in insert order.
    pub rows: Vec<(String, usize)>,
}

impl SeedScript {
    #[allow(dead_code)] // unused in a build without drivers
    fn add_rows(&mut self, table: &str, rows: usize) {
        match self.rows.last_mut() {
            Some((last, count)) if last == table => *count += rows,
            _ if rows > 0 => self.rows.push((table.to_owned(), rows)),
            _ => {}
        }
    }

    /// The whole script: one statement per line, each ending in `;`.
    #[must_use]
    pub fn to_sql(&self) -> String {
        let mut sql = String::new();
        for statement in self.reset.iter().chain(&self.inserts) {
            sql.push_str(statement);
            sql.push_str(";\n");
        }
        sql
    }
}

fn seed_error(error: drizzle_seed::SeedError) -> CliError {
    CliError::Other(format!("Seeding failed: {error}"))
}

/// Builds the script for `schema` with the dialect's `SeedConfig`
/// constructor.
#[allow(unused_macros)] // unused in a build without drivers
macro_rules! build_script {
    ($constructor:ident, $schema:expr, $plan:expr, $max_params:expr) => {{
        let plan: &SeedPlan = $plan;
        let mut config = drizzle_seed::SeedConfig::$constructor($schema).seed(plan.seed);
        if let Some(count) = plan.default_count {
            config = config.default_count(count);
        }
        if let Some(limit) = $max_params {
            config = config.max_params(limit);
        }
        for (table, count) in &plan.counts {
            config = config.count_by_name(table, *count);
        }
        for (parent, child, count) in &plan.relations {
            config = config.relation_by_name(parent, child, *count);
        }
        for table in &plan.skips {
            config = config.skip_by_name(table);
        }
        let mut script = SeedScript::default();
        if plan.reset {
            for statement in config.reset_plan().map_err(seed_error)? {
                script
                    .reset
                    .push(statement.inline_sql().map_err(seed_error)?);
            }
        }
        for statement in config.try_generate().map_err(seed_error)? {
            script.add_rows(statement.table(), statement.rows());
            script
                .inserts
                .push(statement.inline_sql().map_err(seed_error)?);
        }
        Ok(script)
    }};
}

/// Builds the seed script for `schema` in `dialect`.
///
/// `max_params` caps the values per INSERT (D1's HTTP API limits statement
/// length, so its driver passes a lower cap).
///
/// # Errors
///
/// Returns [`CliError`] when this build has no driver for `dialect`, a
/// table or column name in `plan` is unknown, or seeding fails (for example
/// a foreign-key cycle).
#[allow(unused_variables)] // every dialect arm can be compiled out
pub fn build_script(
    dialect: drizzle_types::Dialect,
    schema: &drizzle_seed::schema::Schema,
    plan: &SeedPlan,
    max_params: Option<usize>,
) -> Result<SeedScript, CliError> {
    match dialect {
        #[cfg(any(
            feature = "rusqlite",
            feature = "libsql",
            feature = "turso",
            feature = "d1-http"
        ))]
        drizzle_types::Dialect::SQLite => build_script!(sqlite, schema, plan, max_params),
        #[cfg(any(feature = "postgres-sync", feature = "tokio-postgres"))]
        drizzle_types::Dialect::PostgreSQL => build_script!(postgres, schema, plan, max_params),
        #[cfg(any(feature = "mysql-sync", feature = "mysql-async"))]
        drizzle_types::Dialect::MySQL => build_script!(mysql, schema, plan, max_params),
        #[allow(unreachable_patterns)]
        other => Err(CliError::Other(format!(
            "this drizzle build has no {other:?} driver; build it with a driver feature to seed"
        ))),
    }
}

/// Runs `drizzle seed`: reads the tables from the live database (after the
/// configured table and schema filters, without the migrations tracking
/// table), generates rows for them, and inserts them in one transaction
/// where the driver supports it. With `--out`, writes the SQL instead.
///
/// # Errors
///
/// Returns [`CliError`] if `db_name` does not match the config, an option is
/// malformed, there are no credentials or no driver for them, reading the
/// database fails, seeding fails, writing `--out` fails, a statement fails,
/// or the user declines `--reset` ([`CliError::Aborted`]).
pub fn run(config: &Config, db_name: Option<&str>, opts: &SeedOptions) -> Result<(), CliError> {
    let db = config.database(db_name)?;
    let plan = SeedPlan::from_options(opts)?;
    let effective_dialect = overrides::resolve_dialect(db, opts.dialect);
    let to_stdout = opts.out.as_deref() == Some(std::path::Path::new("-"));
    // With `--out -`, stdout carries only the SQL.
    let say = |line: String| {
        if to_stdout {
            eprintln!("{line}");
        } else {
            println!("{line}");
        }
    };

    if !to_stdout {
        crate::commands::harness::print_db_header(config, db_name);
    }
    say(output::heading("Seeding database..."));
    say(format!(
        "  {}: {}",
        output::label("Dialect"),
        effective_dialect.as_str()
    ));

    let connection = overrides::resolve_connection(db, effective_dialect, &opts.connection)?;
    let Some(connection) = connection else {
        return Err(CliError::MissingCredentials("seed"));
    };
    say(format!(
        "  {}: {}",
        output::label("Driver"),
        connection.driver
    ));

    let filters = crate::db::SnapshotFilters {
        tables: overrides::resolve_filter_list(
            opts.filters.tables_filter.as_deref(),
            db.tables_filter.as_ref(),
        ),
        schemas: overrides::resolve_schema_filters(
            effective_dialect,
            opts.filters.schema_filters.as_deref(),
            db.schema_filter.as_ref(),
        ),
        extensions: overrides::resolve_extensions_filter(
            effective_dialect,
            opts.filters.extensions_filters.as_deref(),
            db.extensions_filters.as_deref(),
        ),
        roles: None,
    };
    let snapshot = crate::db::introspect_for_seed(&connection, &filters, db.migrations_table())?;
    let schema = drizzle_seed::schema::Schema::from_snapshot(&snapshot).map_err(seed_error)?;
    if schema.tables().is_empty() {
        say(output::warning("No tables found to seed."));
        return Ok(());
    }

    let max_params = (connection.driver == Driver::D1Http).then_some(250);
    let script = build_script(effective_dialect.to_base(), &schema, &plan, max_params)?;

    if let Some(out) = &opts.out {
        let sql = script.to_sql();
        if to_stdout {
            print!("{sql}");
        } else {
            std::fs::write(out, sql).map_err(|error| {
                CliError::IoError(format!("Failed to write {}: {error}", out.display()))
            })?;
            say(format!(
                "{} Wrote {} statement(s) to {}",
                output::success("Done!"),
                script.reset.len() + script.inserts.len(),
                out.display()
            ));
        }
        return Ok(());
    }

    if plan.reset && !opts.force && !confirm_reset(&script)? {
        return Err(CliError::Aborted(
            "Seed aborted: --reset was not confirmed, so nothing was changed".into(),
        ));
    }

    let statements: Vec<String> = script
        .reset
        .iter()
        .chain(&script.inserts)
        .cloned()
        .collect();
    crate::db::execute_seed(&connection, &statements)?;

    println!();
    for (table, rows) in &script.rows {
        println!("  {} {table}: {rows} row(s)", output::success("+"));
    }
    println!();
    println!("{}", output::success("Seed complete!"));
    Ok(())
}

/// Asks before `--reset` deletes rows; refuses in a non-interactive shell.
fn confirm_reset(script: &SeedScript) -> Result<bool, CliError> {
    use std::io::{self, IsTerminal, Write};

    if !io::stdin().is_terminal() {
        return Err(CliError::Other(
            "Refusing to delete rows for --reset in non-interactive mode. Use --force.".into(),
        ));
    }
    println!(
        "{}",
        output::warning(&format!(
            "--reset deletes every row of the {} seeded table(s) first.",
            script.rows.len()
        ))
    );
    print!("Continue? [y/N]: ");
    io::stdout()
        .flush()
        .map_err(|error| CliError::IoError(error.to_string()))?;
    let mut answer = String::new();
    io::stdin()
        .read_line(&mut answer)
        .map_err(|error| CliError::IoError(error.to_string()))?;
    Ok(matches!(answer.trim(), "y" | "Y" | "yes" | "YES" | "Yes"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    #[derive(Parser)]
    struct Cli {
        #[command(flatten)]
        seed: SeedOptions,
    }

    fn plan(args: &[&str]) -> Result<SeedPlan, CliError> {
        let mut argv = vec!["drizzle"];
        argv.extend_from_slice(args);
        SeedPlan::from_options(&Cli::parse_from(argv).seed)
    }

    #[test]
    fn parses_counts_relations_and_skips() {
        let plan = plan(&[
            "--seed",
            "7",
            "--count",
            "20",
            "--count",
            "users=100",
            "--count",
            "auth.sessions = 5",
            "--relation",
            "users:posts=3",
            "--skip",
            "audit_log",
            "--reset",
        ])
        .unwrap();
        assert_eq!(
            plan,
            SeedPlan {
                seed: 7,
                default_count: Some(20),
                counts: vec![("users".into(), 100), ("auth.sessions".into(), 5)],
                relations: vec![("users".into(), "posts".into(), 3)],
                skips: vec!["audit_log".into()],
                reset: true,
            }
        );
    }

    #[test]
    fn rejects_malformed_values() {
        for args in [
            &["--count", "lots"][..],
            &["--count", "users=lots"],
            &["--count", "=5"],
            &["--count", "1", "--count", "2"],
            &["--relation", "users=3"],
            &["--relation", "users:=3"],
            &["--relation", "users:posts=x"],
        ] {
            assert!(plan(args).is_err(), "{args:?}");
        }
    }
}