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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! `drizzle generate`: writes a migration folder from schema changes.

use std::fmt::Write;
use std::path::Path;

use crate::commands::overrides;
use crate::config::{Casing, Config, Dialect, Driver, MigrationPrefix};
use crate::error::CliError;
use crate::output;
use drizzle_migrations::schema::Snapshot;

#[derive(clap::Args, Debug, Clone)]
pub struct GenerateOptions {
    /// Migration name (optional, auto-generated if not provided)
    #[arg(short, long)]
    pub name: Option<String>,

    /// Create a custom (empty) migration file for manual SQL
    #[arg(long)]
    pub custom: bool,

    /// Casing for generated identifiers (`camelCase` or `snake_case`)
    #[arg(long)]
    pub casing: Option<Casing>,

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

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

    /// Override schema path(s)
    #[arg(long, value_delimiter = ',')]
    pub schema: Option<Vec<String>>,

    /// Override output directory
    #[arg(long)]
    pub out: Option<std::path::PathBuf>,

    /// Override breakpoints setting
    #[arg(long)]
    pub breakpoints: Option<bool>,

    /// When merged migration branches change the same objects, diff against
    /// the newest snapshot instead of failing
    #[arg(long)]
    pub ignore_conflicts: bool,

    #[command(flatten)]
    pub hints: crate::commands::renames::HintArgs,
}

/// Runs `drizzle generate`: diffs the schema files against the newest
/// `snapshot.json` in the migrations folder and writes
/// `<out>/<tag>/migration.sql` plus `snapshot.json`.
///
/// After a git merge leaves several open heads in the snapshot history, the
/// diff runs against the merged state of every branch instead (see
/// [`drizzle_migrations::history::load_merge_base`]) and the new snapshot's
/// `prevIds` lists every head, as drizzle-kit does.
///
/// Prints a note and writes nothing when the schema has no tables or indexes
/// or nothing changed. With `--custom`, writes a `migration.sql` holding only
/// a comment and no snapshot, so the next diff ignores it. Also rewrites
/// `migrations.js` when bundling is enabled. Does not connect to the
/// database.
///
/// Ambiguous renames are asked about on a terminal and otherwise answered
/// from `--hints` / `--hints-file`; renames are never inferred (see
/// [`renames`](crate::commands::renames)).
///
/// # Errors
///
/// Returns [`CliError`] if `db_name` does not match the config, the folder
/// still uses the legacy `meta/_journal.json` layout (run `drizzle up`), no
/// schema files are found or they have parse errors, merged migration
/// branches conflict (unless `--ignore-conflicts`), the hints are invalid
/// ([`CliError::InvalidHints`]) or leave a rename question unanswered without
/// a terminal ([`CliError::MissingHints`]), a prompt is aborted, the diff
/// fails, or the migration folder cannot be written (including when its tag
/// already exists).
pub fn run(config: &Config, db_name: Option<&str>, opts: GenerateOptions) -> Result<(), CliError> {
    use drizzle_migrations::naming::{PrefixMode, generate_migration_tag_with_mode};

    let db = config.database(db_name)?;

    // CLI flag overrides config
    let effective_casing = opts.casing.or(db.casing);
    let effective_dialect = overrides::resolve_dialect(db, opts.dialect);
    let effective_driver = overrides::resolve_driver(db, effective_dialect, opts.driver)?;
    let effective_breakpoints = opts.breakpoints.unwrap_or(db.breakpoints);
    let out_dir = opts
        .out
        .clone()
        .unwrap_or_else(|| db.migrations_dir().to_path_buf());

    crate::commands::harness::print_db_header(config, db_name);

    println!("{}", output::heading("Generating migration..."));

    println!(
        "  {}: {}",
        output::label("Dialect"),
        effective_dialect.as_str()
    );
    if let Some(driver) = effective_driver {
        println!("  {}: {}", output::label("Driver"), driver);
    }

    // Create output directories if they don't exist
    std::fs::create_dir_all(&out_dir).map_err(|e| CliError::IoError(e.to_string()))?;

    let legacy_journal_path = out_dir.join("meta").join("_journal.json");
    if legacy_journal_path.exists() {
        return Err(CliError::Other(
            "Detected old drizzle-kit migration folders. Upgrade them before generating new migrations."
                .to_string(),
        ));
    }

    // Handle custom migration (empty migration file for manual SQL)
    if opts.custom {
        let bundle = db.bundle_enabled();
        return generate_custom_migration(
            &out_dir,
            effective_breakpoints,
            db.migrations.as_ref().and_then(|m| m.prefix),
            opts.name,
            bundle,
        );
    }

    // Parse schema files
    let parse_result = parse_schema_files(db, opts.schema.as_deref())?;

    if parse_result.tables.is_empty() && parse_result.indexes.is_empty() {
        println!(
            "{}",
            output::warning("No tables or indexes found in schema files.")
        );
        return Ok(());
    }

    println!(
        "  {} {} table(s), {} index(es)",
        output::label("Found"),
        parse_result.tables.len(),
        parse_result.indexes.len()
    );

    // Get dialect from config
    let dialect = effective_dialect.to_base();

    // Build current snapshot from parsed schema (use config dialect, not parser-detected)
    let current_snapshot = Snapshot::from_parse_result(&parse_result, dialect, effective_casing);

    // The schema the existing migrations produce: the newest snapshot, or
    // every branch's changes merged after a git merge.
    let merge_base =
        drizzle_migrations::history::load_merge_base(&out_dir, dialect, opts.ignore_conflicts)
            .map_err(|error| match error {
                drizzle_migrations::history::HistoryError::Io(error) => {
                    CliError::IoError(error.to_string())
                }
                conflicts => CliError::MigrationError(conflicts.to_string()),
            })?;

    // Ask (or read hints) about ambiguous renames, then diff with the answers.
    let options = crate::commands::renames::resolve_for_command(
        &merge_base.snapshot,
        &current_snapshot,
        drizzle_migrations::DiffOptions::new(),
        &opts.hints,
    )?;
    let mut generated = generate_diff(&merge_base.snapshot, &current_snapshot, &options)?;
    if let Some(prev_ids) = merge_base.prev_ids {
        println!(
            "  {} {} migration branches",
            output::label("Merging"),
            prev_ids.len()
        );
        generated.snapshot.set_prev_ids(prev_ids);
    }

    if generated.is_empty() {
        println!("{}", output::warning("No schema changes detected 😴"));
        return Ok(());
    }

    println!(
        "  {} {} SQL statement(s)",
        output::label("Generated"),
        generated.statements.len()
    );

    // Data-loss and review notes from the diff, as push prints them.
    if !generated.warnings.is_empty() {
        println!("{}", output::warning("Warnings:"));
        for warning in &generated.warnings {
            println!("  {} {}", output::warning("-"), warning);
        }
        println!();
    }

    let prefix_mode = db
        .migrations
        .as_ref()
        .and_then(|m| m.prefix)
        .map_or(PrefixMode::Timestamp, map_prefix_mode);

    let next_idx = next_migration_index(&out_dir)?;
    let migration_tag =
        generate_migration_tag_with_mode(prefix_mode, next_idx, opts.name.as_deref());

    let migration_dir =
        write_migration_files(&out_dir, &migration_tag, &generated, effective_breakpoints)?;

    // Regenerate {out_dir}/migrations.js bundle index when enabled.
    // Auto-enabled for driver = durable-sqlite (see `DatabaseConfig::bundle_enabled`).
    if db.bundle_enabled() {
        write_migrations_js(&out_dir)?;
    }

    println!(
        "{}",
        output::success(&format!("Migration generated: {migration_tag}"))
    );
    println!("   {}", migration_dir.display());

    Ok(())
}

/// Resolve and parse schema files.
fn parse_schema_files(
    db: &crate::config::DatabaseConfig,
    schema_override: Option<&[String]>,
) -> Result<drizzle_migrations::parser::ParseResult, CliError> {
    use drizzle_migrations::parser::SchemaParser;

    let schema_files = overrides::resolve_schema_files(db, schema_override)?;
    if schema_files.is_empty() {
        return Err(CliError::NoSchemaFiles(overrides::resolve_schema_display(
            db,
            schema_override,
        )));
    }

    println!(
        "  {} {} schema file(s)",
        output::label("Parsing"),
        schema_files.len()
    );

    let mut combined_code = String::new();
    for path in &schema_files {
        let code = std::fs::read_to_string(path)
            .map_err(|e| CliError::IoError(format!("Failed to read {}: {}", path.display(), e)))?;
        combined_code.push_str(&code);
        combined_code.push('\n');
    }

    let parse_result = SchemaParser::parse(&combined_code);
    crate::snapshot::surface_parse_diagnostics(&parse_result)?;
    Ok(parse_result)
}

/// Write migration.sql and snapshot.json to `{out_dir}/{tag}/`.
fn write_migration_files(
    out_dir: &Path,
    migration_tag: &str,
    generated: &drizzle_migrations::Plan,
    breakpoints: bool,
) -> Result<std::path::PathBuf, CliError> {
    let sql_content = if breakpoints {
        generated.statements.join("\n--> statement-breakpoint\n")
    } else {
        generated.statements.join("\n\n")
    };

    drizzle_migrations::writer::publish_migration_directory(out_dir, migration_tag, |folder| {
        std::fs::write(folder.join("migration.sql"), &sql_content)
            .map_err(|error| drizzle_migrations::MigrationError::IoError(error.to_string()))?;
        generated
            .snapshot
            .save(&folder.join("snapshot.json"))
            .map_err(|error| drizzle_migrations::MigrationError::SnapshotError(error.to_string()))
    })
    .map_err(map_migration_error)
}

/// Generate an empty custom migration for manual SQL
fn generate_custom_migration(
    out_dir: &Path,
    _breakpoints: bool,
    prefix: Option<MigrationPrefix>,
    name: Option<String>,
    bundle: bool,
) -> Result<(), CliError> {
    use drizzle_migrations::naming::{PrefixMode, generate_migration_tag_with_mode};

    let custom_name = name.unwrap_or_else(|| "custom".to_string());

    let prefix_mode = prefix.map_or(PrefixMode::Timestamp, map_prefix_mode);

    let migration_tag = generate_migration_tag_with_mode(
        prefix_mode,
        next_migration_index(out_dir)?,
        Some(&custom_name),
    );

    let sql_content = "-- Custom SQL migration file, put your code below! --\n\n";
    let migration_dir = drizzle_migrations::writer::publish_migration_directory(
        out_dir,
        &migration_tag,
        |folder| {
            std::fs::write(folder.join("migration.sql"), sql_content)
                .map_err(|error| drizzle_migrations::MigrationError::IoError(error.to_string()))
        },
    )
    .map_err(map_migration_error)?;

    // Regenerate {out_dir}/migrations.js bundle index when enabled.
    if bundle {
        write_migrations_js(out_dir)?;
    }

    println!(
        "{}",
        output::success(&format!("Custom migration created: {migration_tag}"))
    );
    println!("   {}", migration_dir.display());
    println!(
        "{}",
        output::label("   Edit the migration file to add your SQL statements.")
    );

    Ok(())
}

const fn map_prefix_mode(p: MigrationPrefix) -> drizzle_migrations::PrefixMode {
    match p {
        MigrationPrefix::Index => drizzle_migrations::PrefixMode::Index,
        MigrationPrefix::Timestamp => drizzle_migrations::PrefixMode::Timestamp,
        MigrationPrefix::Supabase => drizzle_migrations::PrefixMode::Supabase,
        MigrationPrefix::Unix => drizzle_migrations::PrefixMode::Unix,
        MigrationPrefix::None => drizzle_migrations::PrefixMode::None,
    }
}

fn next_migration_index(out_dir: &Path) -> Result<u32, CliError> {
    let entries = collect_v3_migration_tags(out_dir)?;
    let mut max_index: Option<u32> = None;

    for tag in &entries {
        let Some(prefix) = tag.split('_').next() else {
            continue;
        };

        // Index prefixes are short (`0000`); longer digit runs are timestamp
        // (14), unix (10), or millisecond (13) prefixes, not indexes.
        if prefix.len() > 5 || !prefix.chars().all(|c| c.is_ascii_digit()) {
            continue;
        }

        if let Ok(idx) = prefix.parse::<u32>() {
            max_index = Some(max_index.map_or(idx, |curr| curr.max(idx)));
        }
    }

    Ok(max_index.map_or_else(
        || u32::try_from(entries.len()).unwrap_or(u32::MAX),
        |idx| idx.saturating_add(1),
    ))
}

fn collect_v3_migration_tags(out_dir: &Path) -> Result<Vec<String>, CliError> {
    if !out_dir.exists() {
        return Ok(Vec::new());
    }

    let mut tags = Vec::new();
    for entry in std::fs::read_dir(out_dir).map_err(|e| CliError::IoError(e.to_string()))? {
        let entry = entry.map_err(|e| CliError::IoError(e.to_string()))?;
        if !entry
            .file_type()
            .map_err(|e| CliError::IoError(e.to_string()))?
            .is_dir()
        {
            continue;
        }

        let tag = entry.file_name().to_string_lossy().to_string();
        if tag == "meta" {
            continue;
        }

        if entry.path().join("migration.sql").exists() {
            tags.push(tag);
        }
    }

    tags.sort();
    Ok(tags)
}

/// Write a `migrations.js` bundle index at the root of the migrations output
/// folder.
///
/// Mirrors `drizzle-kit`'s `bundle: true` output. JS bundlers (Metro for
/// Expo/React Native, Cloudflare Workers' bundler for Durable Objects `SQLite`)
/// require static `import` statements to embed SQL text into the final JS
/// bundle; this file is the entry point.
///
/// Rust-only consumers can ignore it — our [`drizzle_migrations::MigrationDir`]
/// loader reads the `migration.sql` files directly.
///
/// # Errors
///
/// Returns [`CliError`] if the migrations directory cannot be enumerated or if
/// writing the `migrations.js` file fails.
pub fn write_migrations_js(out_dir: &Path) -> Result<(), CliError> {
    let tags = collect_v3_migration_tags(out_dir)?;

    let mut content = String::new();
    for (idx, tag) in tags.iter().enumerate() {
        let import_name = format!("m{idx:04}");
        // Forward slashes work in JS import specifiers on every platform,
        // including Windows — they are URL-style paths, not filesystem paths.
        let _ = writeln!(
            content,
            "import {import_name} from './{tag}/migration.sql';"
        );
    }

    content.push_str("\nexport default {\n  migrations: {\n");
    for (idx, tag) in tags.iter().enumerate() {
        let _ = writeln!(content, "    \"{tag}\": m{idx:04},");
    }
    content.push_str("  }\n};\n");

    let migrations_js_path = out_dir.join("migrations.js");
    std::fs::write(&migrations_js_path, content).map_err(|e| CliError::IoError(e.to_string()))?;

    Ok(())
}

/// Map migration infrastructure errors to CLI errors.
fn map_migration_error(error: drizzle_migrations::MigrationError) -> CliError {
    match error {
        drizzle_migrations::MigrationError::DialectMismatch => CliError::DialectMismatch,
        drizzle_migrations::MigrationError::NoChanges => {
            CliError::Other("No schema changes detected".to_string())
        }
        drizzle_migrations::MigrationError::ConfigError(_)
        | drizzle_migrations::MigrationError::IoError(_)
        | drizzle_migrations::MigrationError::SnapshotError(_) => {
            CliError::MigrationError(error.to_string())
        }
    }
}

/// Generate diff between two snapshots
fn generate_diff(
    prev: &drizzle_migrations::schema::Snapshot,
    current: &drizzle_migrations::schema::Snapshot,
    options: &drizzle_migrations::DiffOptions,
) -> Result<drizzle_migrations::Plan, CliError> {
    drizzle_migrations::diff_with(prev, current, options).map_err(map_migration_error)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    fn touch_migration(out_dir: &Path, tag: &str) {
        let dir = out_dir.join(tag);
        std::fs::create_dir_all(&dir).expect("mkdir migration folder");
        std::fs::write(dir.join("migration.sql"), "-- stub\n").expect("write migration.sql");
    }

    #[test]
    fn custom_migration_rejects_unsafe_name_before_writing() {
        let tmp = tempdir().expect("tempdir");
        let error = generate_custom_migration(
            tmp.path(),
            false,
            Some(MigrationPrefix::None),
            Some("../escape".to_string()),
            false,
        )
        .expect_err("unsafe name must fail");

        assert!(matches!(error, CliError::MigrationError(_)));
        assert_eq!(
            std::fs::read_dir(tmp.path()).expect("read output").count(),
            0
        );
    }

    #[test]
    fn custom_migration_is_published_as_complete_directory() {
        let tmp = tempdir().expect("tempdir");
        generate_custom_migration(
            tmp.path(),
            false,
            Some(MigrationPrefix::None),
            Some("manual_change".to_string()),
            false,
        )
        .expect("generate custom migration");

        let migration = tmp.path().join("manual_change");
        assert!(migration.join("migration.sql").is_file());
        assert_eq!(
            std::fs::read_dir(tmp.path()).expect("read output").count(),
            1
        );
    }

    #[test]
    fn migrations_js_contains_import_and_export_map_in_tag_order() {
        let tmp = tempdir().expect("tempdir");
        let out_dir = tmp.path();

        touch_migration(out_dir, "20230331141203_first");
        touch_migration(out_dir, "20230401091530_second");
        touch_migration(out_dir, "20230501111111_third");

        write_migrations_js(out_dir).expect("write migrations.js");

        let contents =
            std::fs::read_to_string(out_dir.join("migrations.js")).expect("read migrations.js");

        assert!(
            contents.contains("import m0000 from './20230331141203_first/migration.sql';"),
            "first import present"
        );
        assert!(
            contents.contains("import m0001 from './20230401091530_second/migration.sql';"),
            "second import present"
        );
        assert!(
            contents.contains("import m0002 from './20230501111111_third/migration.sql';"),
            "third import present"
        );
        assert!(
            contents.contains("\"20230331141203_first\": m0000,"),
            "first map entry"
        );
        assert!(
            contents.contains("\"20230401091530_second\": m0001,"),
            "second map entry"
        );
        assert!(
            contents.contains("\"20230501111111_third\": m0002,"),
            "third map entry"
        );
        assert!(
            contents.contains("export default {"),
            "export default present"
        );
    }

    #[test]
    fn migrations_js_is_empty_shell_when_no_migrations_exist() {
        let tmp = tempdir().expect("tempdir");
        let out_dir = tmp.path();

        write_migrations_js(out_dir).expect("write migrations.js");

        let contents =
            std::fs::read_to_string(out_dir.join("migrations.js")).expect("read migrations.js");

        assert!(!contents.contains("import "), "no imports when empty");
        assert!(
            contents.contains("export default {"),
            "export default still present"
        );
        assert!(
            contents.contains("migrations: {"),
            "migrations map still present"
        );
    }

    #[test]
    fn migrations_js_uses_forward_slashes_in_import_paths() {
        // JS import specifiers use URL-style paths (always forward slashes),
        // regardless of host filesystem separator. This guards against a
        // Windows-specific regression that upstream shipped in beta.22.
        let tmp = tempdir().expect("tempdir");
        let out_dir = tmp.path();

        touch_migration(out_dir, "20230331141203_first");

        write_migrations_js(out_dir).expect("write migrations.js");

        let contents =
            std::fs::read_to_string(out_dir.join("migrations.js")).expect("read migrations.js");

        assert!(
            !contents.contains('\\'),
            "import paths must use forward slashes even on Windows"
        );
        assert!(contents.contains("'./20230331141203_first/migration.sql'"));
    }
}