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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! `drizzle import` on real drizzle-kit output.
//!
//! `fixtures/import/{stable,beta}/<dialect>` were written by drizzle-kit
//! 0.31.11 (journal layout) and 1.0.0-rc.4 (folder layout) from the schemas
//! in `fixtures/import/ts`, each with two migrations (`init`, then `full`).
//!
//! The round-trip tests import the newest snapshot, compare the Rust with
//! `fixtures/import/expected/*.rs` (which the root crate's
//! `imported_schemas` test compiles with the real macros), parse it the way
//! `drizzle generate` does, and diff it against the snapshot: any planned
//! statement means the generated schema lost or changed something.
//!
//! Set `UPDATE_IMPORT_GOLDEN=1` to rewrite the expected files.

use assert_cmd::Command;
use assert_cmd::cargo::cargo_bin_cmd;
use drizzle_migrations::parser::SchemaParser;
use drizzle_migrations::schema::Snapshot;
use drizzle_migrations::upgrade::upgrade_to_latest;
use drizzle_types::Dialect;
use predicates::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::tempdir;

fn fixtures() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/import")
}

fn drizzle(dir: &Path) -> Command {
    let mut cmd = cargo_bin_cmd!("drizzle");
    cmd.current_dir(dir).env("NO_COLOR", "1");
    cmd
}

/// Copies a fixture folder so a test can change it.
fn copy_dir(from: &Path, to: &Path) {
    fs::create_dir_all(to).expect("create copy");
    for entry in fs::read_dir(from).expect("read fixture") {
        let entry = entry.expect("entry");
        let target = to.join(entry.file_name());
        if entry.path().is_dir() {
            copy_dir(&entry.path(), &target);
        } else {
            fs::copy(entry.path(), &target).expect("copy file");
        }
    }
}

/// The newest snapshot of a fixture folder, upgraded and loaded.
fn newest_snapshot(dir: &Path, dialect: Dialect) -> Snapshot {
    let path = if dir.join("meta/_journal.json").exists() {
        dir.join("meta/0001_snapshot.json")
    } else {
        let mut folders: Vec<_> = fs::read_dir(dir)
            .expect("read fixture")
            .map(|e| e.expect("entry").path())
            .filter(|p| p.join("snapshot.json").exists())
            .collect();
        folders.sort();
        folders
            .pop()
            .expect("a migration folder")
            .join("snapshot.json")
    };
    let json: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(&path).expect("read snapshot")).expect("json");
    let upgraded = upgrade_to_latest(json, dialect);
    match dialect {
        Dialect::SQLite => Snapshot::Sqlite(serde_json::from_value(upgraded).expect("sqlite")),
        Dialect::PostgreSQL => {
            Snapshot::Postgres(serde_json::from_value(upgraded).expect("postgres"))
        }
        Dialect::MySQL => Snapshot::MySQL(serde_json::from_value(upgraded).expect("mysql")),
    }
}

/// Imports `fixtures/import/<rel>`, checks the code against the expected
/// file, and asserts that the generated schema diffs clean against the
/// imported snapshot.
fn assert_round_trip(rel: &str, dialect: Dialect) {
    let output = drizzle(&fixtures())
        .args(["import", rel, "--out", "-"])
        .output()
        .expect("run drizzle import");
    assert!(
        output.status.success(),
        "drizzle import failed:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let code = String::from_utf8(output.stdout).expect("utf-8 schema");

    let expected_path = fixtures()
        .join("expected")
        .join(format!("{}.rs", rel.replace('/', "_")));
    if std::env::var_os("UPDATE_IMPORT_GOLDEN").is_some() {
        fs::write(&expected_path, &code).expect("write expected schema");
    }
    let expected = fs::read_to_string(&expected_path).expect("read expected schema");
    assert_eq!(
        code, expected,
        "generated schema for {rel} changed; rerun with UPDATE_IMPORT_GOLDEN=1 if intended"
    );

    let parsed = SchemaParser::parse(&code);
    assert!(
        parsed.errors.is_empty(),
        "generated schema did not parse:\n{code}\nerrors: {:#?}",
        parsed.errors
    );
    let regenerated = Snapshot::from_parse_result(&parsed, dialect, None);
    let imported = newest_snapshot(&fixtures().join(rel), dialect);
    let plan = drizzle_migrations::diff(&imported, &regenerated).expect("diff");
    assert!(
        plan.statements.is_empty(),
        "generated schema differs from the snapshot in {rel}:\n{code}\nplanned: {:#?}",
        plan.statements
    );
}

#[test]
fn sqlite_stable_round_trips() {
    assert_round_trip("stable/sqlite", Dialect::SQLite);
}

#[test]
fn sqlite_folder_layout_round_trips() {
    assert_round_trip("beta/sqlite", Dialect::SQLite);
}

#[test]
fn postgres_stable_round_trips() {
    assert_round_trip("stable/postgres", Dialect::PostgreSQL);
}

#[test]
fn postgres_folder_layout_round_trips() {
    assert_round_trip("beta/postgres", Dialect::PostgreSQL);
}

#[test]
fn mysql_stable_round_trips() {
    assert_round_trip("stable/mysql", Dialect::MySQL);
}

#[test]
fn mysql_folder_layout_round_trips() {
    assert_round_trip("beta/mysql", Dialect::MySQL);
}

// =============================================================================
// Inputs
// =============================================================================

#[test]
fn folder_layout_uses_the_newest_migration() {
    drizzle(&fixtures())
        .args(["import", "beta/postgres", "--out", "-"])
        .assert()
        .success()
        .stderr(predicate::str::contains("_full/snapshot.json"))
        .stdout(predicate::str::contains("pub struct PostTagVotes"));
}

#[test]
fn journal_layout_uses_the_last_journal_entry() {
    drizzle(&fixtures())
        .args(["import", "stable/sqlite", "--out", "-"])
        .assert()
        .success()
        .stderr(predicate::str::contains("meta/0001_snapshot.json"))
        .stderr(predicate::str::contains("drizzle up --out stable/sqlite"))
        .stdout(predicate::str::contains("pub struct Posts"));
}

#[test]
fn single_snapshot_file_is_imported() {
    // The first migration only has `users`.
    drizzle(&fixtures())
        .args([
            "import",
            "stable/postgres/meta/0000_snapshot.json",
            "--out",
            "-",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("pub struct Users"))
        .stdout(predicate::str::contains("pub struct Posts").not())
        // The file sits in a journal-layout folder: still point at `drizzle up`.
        .stderr(predicate::str::contains("drizzle up --out stable/postgres"));
}

#[test]
fn stdout_output_keeps_messages_on_stderr() {
    let output = drizzle(&fixtures())
        .args(["import", "beta/mysql", "--out", "-"])
        .output()
        .expect("run");
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).expect("utf-8");
    assert!(
        stdout.starts_with("//! Auto-generated MySQL schema"),
        "{stdout}"
    );
    assert!(!stdout.contains("Next steps"), "{stdout}");
    let stderr = String::from_utf8(output.stderr).expect("utf-8");
    assert!(stderr.contains("Next steps"), "{stderr}");
}

#[test]
fn missing_path_and_empty_folder_are_reported() {
    let dir = tempdir().expect("temp dir");
    drizzle(dir.path())
        .args(["import", "nope"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("nope does not exist"));

    fs::create_dir(dir.path().join("empty")).expect("mkdir");
    drizzle(dir.path())
        .args(["import", "empty"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("No drizzle-kit snapshots found"));
}

// =============================================================================
// Dialects and versions
// =============================================================================

#[test]
fn dialect_comes_from_the_snapshot_or_the_flag() {
    // drizzle-kit 0.x writes `postgresql`, 1.x writes `postgres`.
    for rel in ["stable/postgres", "beta/postgres"] {
        drizzle(&fixtures())
            .args(["import", rel, "--out", "-"])
            .assert()
            .success()
            .stderr(predicate::str::contains("Dialect: postgresql"));
    }

    // A SQLite snapshot can be imported for Turso.
    drizzle(&fixtures())
        .args(["import", "beta/sqlite", "--out", "-", "--dialect", "turso"])
        .assert()
        .success()
        .stderr(predicate::str::contains("Dialect: turso"))
        .stdout(predicate::str::contains("#[SQLiteTable"));

    // ...but not as another database.
    drizzle(&fixtures())
        .args(["import", "beta/sqlite", "--out", "-", "--dialect", "mysql"])
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "is a sqlite snapshot; --dialect mysql does not match it",
        ));
}

#[test]
fn dialect_flag_covers_snapshots_without_one() {
    let dir = tempdir().expect("temp dir");
    let mut json: serde_json::Value = serde_json::from_str(
        &fs::read_to_string(fixtures().join("stable/sqlite/meta/0000_snapshot.json"))
            .expect("read"),
    )
    .expect("json");
    json.as_object_mut().expect("object").remove("dialect");
    let path = dir.path().join("snapshot.json");
    fs::write(&path, json.to_string()).expect("write");

    drizzle(dir.path())
        .args(["import", "snapshot.json", "--out", "-"])
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "has no \"dialect\" field; pass --dialect",
        ));
    drizzle(dir.path())
        .args([
            "import",
            "snapshot.json",
            "--out",
            "-",
            "--dialect",
            "sqlite",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("pub struct Users"));
}

#[test]
fn unsupported_dialects_and_versions_are_refused() {
    let dir = tempdir().expect("temp dir");
    let write = |name: &str, json: serde_json::Value| {
        fs::write(dir.path().join(name), json.to_string()).expect("write");
    };
    write(
        "singlestore.json",
        serde_json::json!({ "version": "1", "dialect": "singlestore", "tables": {} }),
    );
    write(
        "old.json",
        serde_json::json!({ "version": "3", "dialect": "sqlite", "tables": {} }),
    );
    write(
        "future.json",
        serde_json::json!({ "version": "99", "dialect": "postgresql", "ddl": [] }),
    );

    drizzle(dir.path())
        .args(["import", "singlestore.json", "--out", "-"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("is a \"singlestore\" snapshot"));
    drizzle(dir.path())
        .args(["import", "old.json", "--out", "-"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("at version 3"))
        .stderr(predicate::str::contains("npx drizzle-kit up"));
    drizzle(dir.path())
        .args(["import", "future.json", "--out", "-"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("at version 99"))
        .stderr(predicate::str::contains("update drizzle-cli"));
}

// =============================================================================
// Output file
// =============================================================================

#[test]
fn writes_src_schema_rs_without_a_config_and_refuses_to_overwrite() {
    let dir = tempdir().expect("temp dir");
    let fixture = fixtures().join("beta/sqlite");

    drizzle(dir.path())
        .arg("import")
        .arg(&fixture)
        .assert()
        .success()
        .stdout(predicate::str::contains("Output: src/schema.rs"))
        .stdout(predicate::str::contains("Found 6 table(s)"));
    let schema = dir.path().join("src/schema.rs");
    assert!(
        fs::read_to_string(&schema)
            .expect("schema written")
            .contains("pub struct AuditLog")
    );

    fs::write(&schema, "// mine\n").expect("overwrite");
    drizzle(dir.path())
        .arg("import")
        .arg(&fixture)
        .assert()
        .failure()
        .stderr(predicate::str::contains("src/schema.rs already exists"))
        .stderr(predicate::str::contains("--force"));
    assert_eq!(fs::read_to_string(&schema).expect("read"), "// mine\n");

    drizzle(dir.path())
        .arg("import")
        .arg(&fixture)
        .arg("--force")
        .assert()
        .success();
    assert!(
        fs::read_to_string(&schema)
            .expect("read")
            .contains("pub struct AuditLog")
    );
}

#[test]
fn config_supplies_the_schema_path_and_casing() {
    let dir = tempdir().expect("temp dir");
    fs::write(
        dir.path().join("drizzle.config.toml"),
        "dialect = \"sqlite\"\nschema = \"db/schema.rs\"\nout = \"./drizzle\"\n\n[introspect]\ncasing = \"camel\"\n",
    )
    .expect("write config");

    drizzle(dir.path())
        .arg("import")
        .arg(fixtures().join("beta/sqlite"))
        .assert()
        .success()
        // `out` in the config points elsewhere: say how to use the folder.
        .stdout(predicate::str::contains("Set `out = "));
    let schema = fs::read_to_string(dir.path().join("db/schema.rs")).expect("schema written");
    assert!(schema.contains("pub displayName: String"), "{schema}");

    // --out and --casing win over the config.
    drizzle(dir.path())
        .arg("import")
        .arg(fixtures().join("beta/sqlite"))
        .args(["--out", "other.rs", "--casing", "preserve"])
        .assert()
        .success();
    let schema = fs::read_to_string(dir.path().join("other.rs")).expect("schema written");
    assert!(schema.contains("pub display_name: String"), "{schema}");
    assert!(schema.contains("pub createdAt: i64"), "{schema}");
}

#[test]
fn schema_name_flag_names_the_schema_struct() {
    drizzle(&fixtures())
        .args([
            "import",
            "beta/sqlite",
            "--out",
            "-",
            "--schema-name",
            "AppSchema",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("pub struct AppSchema {"));
}

// =============================================================================
// Migration history
// =============================================================================

#[test]
fn next_steps_cover_config_history_and_manual_ports() {
    let dir = tempdir().expect("temp dir");
    let output = drizzle(dir.path())
        .arg("import")
        .arg(fixtures().join("beta/postgres"))
        .output()
        .expect("run");
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).expect("utf-8");
    for expected in [
        "Create drizzle.config.toml",
        "dialect = \"postgresql\"",
        "schema = \"src/schema.rs\"",
        "__drizzle_migrations",
        "drizzle generate",
        "relations()",
        "$type<T>()",
        "$defaultFn",
        "customType()",
    ] {
        assert!(
            stdout.contains(expected),
            "missing `{expected}` in:\n{stdout}"
        );
    }
    // The folder layout needs no conversion.
    assert!(!stdout.contains("drizzle up"), "{stdout}");
    assert!(!dir.path().join("drizzle.config.toml").exists());
}

#[test]
fn upgrade_flag_converts_the_journal_layout_in_place() {
    let dir = tempdir().expect("temp dir");
    let migrations = dir.path().join("drizzle");
    copy_dir(&fixtures().join("stable/mysql"), &migrations);

    drizzle(dir.path())
        .args(["import", "drizzle", "--upgrade"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Converted 2 migration(s)"))
        .stdout(predicate::str::contains("drizzle up").not());

    assert!(!migrations.join("meta").exists());
    // `drizzle up` names each folder `<journal time, UTC>_<tag without index>`.
    let folder = |tag: &str| {
        fs::read_dir(&migrations)
            .expect("read migrations")
            .map(|entry| entry.expect("entry").path())
            .find(|path| {
                path.file_name()
                    .and_then(|name| name.to_str())
                    .and_then(|name| name.strip_suffix(tag))
                    .and_then(|stamp| stamp.strip_suffix('_'))
                    .is_some_and(|stamp| {
                        stamp.len() == 14 && stamp.bytes().all(|b| b.is_ascii_digit())
                    })
            })
            .unwrap_or_else(|| panic!("no converted folder for {tag}"))
    };
    assert!(folder("init").join("migration.sql").exists());
    assert!(folder("full").join("snapshot.json").exists());
    assert!(dir.path().join("src/schema.rs").exists());

    // `--upgrade` cannot share stdout with the schema.
    drizzle(dir.path())
        .args(["import", "drizzle", "--upgrade", "--out", "-"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("--upgrade cannot be combined"));
}

#[test]
fn init_config_writes_a_config_for_the_imported_project() {
    let dir = tempdir().expect("temp dir");
    copy_dir(&fixtures().join("beta/sqlite"), &dir.path().join("drizzle"));

    drizzle(dir.path())
        .args(["import", "drizzle", "--init-config"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Created drizzle.config.toml"));
    let config = fs::read_to_string(dir.path().join("drizzle.config.toml")).expect("config");
    assert!(config.contains("dialect = \"sqlite\""), "{config}");
    assert!(config.contains("schema = \"src/schema.rs\""), "{config}");
    assert!(config.contains("out = \"drizzle\""), "{config}");

    // The written config is valid: `drizzle status` reads the history.
    drizzle(dir.path())
        .arg("status")
        .assert()
        .success()
        .stdout(predicate::str::contains("_full"));

    // An existing config is left alone.
    drizzle(dir.path())
        .args(["import", "drizzle", "--init-config", "--force"])
        .assert()
        .success()
        .stdout(predicate::str::contains("a config already exists"));
}

/// The whole adoption path: import, point the config at the existing
/// folder (converting the journal layout), and `drizzle generate` reads
/// drizzle-kit's last snapshot as the previous state and finds nothing to do.
#[test]
fn generate_after_import_finds_no_changes() {
    for rel in [
        "stable/sqlite",
        "beta/sqlite",
        "stable/postgres",
        "beta/postgres",
        "stable/mysql",
        "beta/mysql",
    ] {
        let dir = tempdir().expect("temp dir");
        copy_dir(&fixtures().join(rel), &dir.path().join("drizzle"));

        drizzle(dir.path())
            .args(["import", "drizzle", "--init-config", "--upgrade"])
            .assert()
            .success();
        drizzle(dir.path())
            .arg("generate")
            .assert()
            .success()
            .stdout(predicate::str::contains("No schema changes"));
        let migrations = fs::read_dir(dir.path().join("drizzle"))
            .expect("read migrations")
            .count();
        assert_eq!(migrations, 2, "{rel}: generate must not add a migration");
    }
}

/// A database drizzle-orm 0.x migrated: its migrator ran each `NNNN_tag.sql`
/// and recorded `sha256(file)` with the journal's `when` in
/// `__drizzle_migrations`. After `drizzle import --upgrade`, `drizzle migrate`
/// must see both migrations as applied and run nothing.
#[cfg(feature = "rusqlite")]
#[test]
fn migrate_keeps_drizzle_orm_history() {
    use sha2::{Digest, Sha256};

    let dir = tempdir().expect("temp dir");
    let migrations = dir.path().join("drizzle");
    copy_dir(&fixtures().join("stable/sqlite"), &migrations);

    let journal: serde_json::Value = serde_json::from_str(
        &fs::read_to_string(migrations.join("meta/_journal.json")).expect("journal"),
    )
    .expect("json");
    let conn = rusqlite::Connection::open(dir.path().join("dev.db")).expect("open db");
    // drizzle-orm's SQLite migrator table.
    conn.execute(
        "CREATE TABLE IF NOT EXISTS \"__drizzle_migrations\" (id SERIAL PRIMARY KEY, hash text NOT NULL, created_at numeric)",
        [],
    )
    .expect("tracking table");
    for (idx, entry) in journal["entries"]
        .as_array()
        .expect("entries")
        .iter()
        .enumerate()
    {
        let tag = entry["tag"].as_str().expect("tag");
        let sql = fs::read_to_string(migrations.join(format!("{tag}.sql"))).expect("sql");
        // Only the tracking rows matter here. (drizzle-kit 0.31's rebuild of
        // `users` in 0001 copies the generated column, which SQLite rejects.)
        if idx == 0 {
            for statement in sql.split("--> statement-breakpoint") {
                conn.execute_batch(statement).expect("apply migration");
            }
        }
        let hash = Sha256::digest(sql.as_bytes())
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect::<String>();
        conn.execute(
            "INSERT INTO \"__drizzle_migrations\" (\"hash\", \"created_at\") VALUES (?1, ?2)",
            rusqlite::params![hash, entry["when"].as_i64().expect("when")],
        )
        .expect("record migration");
    }
    drop(conn);

    drizzle(dir.path())
        .args(["import", "drizzle", "--upgrade", "--init-config"])
        .assert()
        .success();
    drizzle(dir.path())
        .args(["migrate", "--plan"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Applied migrations: 2"))
        .stdout(predicate::str::contains("Pending migrations: 0"));
    drizzle(dir.path())
        .arg("migrate")
        .assert()
        .success()
        .stdout(predicate::str::contains("No pending migrations."));

    let conn = rusqlite::Connection::open(dir.path().join("dev.db")).expect("reopen db");
    let recorded: i64 = conn
        .query_row("SELECT COUNT(*) FROM \"__drizzle_migrations\"", [], |row| {
            row.get(0)
        })
        .expect("count");
    assert_eq!(recorded, 2, "no migration may run twice");
}