rok-db-codegen 0.4.0

Generate a rok-db crate (models, typed queries and migrations) from .sql files
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Migrations: the SQL that turns one schema into another, with its undo.
//!
//! The first migration is the difference from an empty schema. Tables are
//! created without foreign keys, which are added once every table exists, so
//! reference cycles need no special order.

use crate::ir::{Check, Column, EnumType, Extra, ForeignKey, Schema, Table, Unique};
use crate::naming;
use crate::parse::quote;

/// One step of a migration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Op {
    pub(crate) up: String,
    /// Empty when the step can't be undone.
    pub(crate) down: String,
    /// Why the step may lose data or fail on existing rows.
    pub(crate) destructive: Option<String>,
}

impl Op {
    fn new(up: impl Into<String>, down: impl Into<String>) -> Self {
        Self {
            up: up.into(),
            down: down.into(),
            destructive: None,
        }
    }

    fn destructive(mut self, why: impl Into<String>) -> Self {
        self.destructive = Some(why.into());
        self
    }
}

/// A rename the user asked for (`--rename`), applied before diffing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Rename {
    /// `old=new` for a table.
    Table {
        /// Previous name (schema-qualified when the table is).
        from: String,
        /// New name, without a schema.
        to: String,
    },
    /// `table.old=new` for a column.
    Column {
        /// The table, by its previous name.
        table: String,
        /// Previous column name.
        from: String,
        /// New column name.
        to: String,
    },
}

impl Rename {
    /// Parse `old=new` or `table.old=new`.
    pub fn parse(spec: &str) -> Option<Self> {
        let (from, to) = spec.split_once('=')?;
        let (from, to) = (from.trim(), to.trim());
        if from.is_empty() || to.is_empty() || to.contains('.') {
            return None;
        }
        Some(match from.rsplit_once('.') {
            Some((table, column)) => Rename::Column {
                table: table.to_owned(),
                from: column.to_owned(),
                to: to.to_owned(),
            },
            None => Rename::Table {
                from: from.to_owned(),
                to: to.to_owned(),
            },
        })
    }
}

/// The steps from `old` to `new`, in an order that works on a live
/// database; their undo runs in reverse.
pub(crate) fn diff(old: &Schema, new: &Schema, renames: &[Rename]) -> Result<Vec<Op>, String> {
    let mut old = old.clone();
    let mut ops = Vec::new();

    // Removed extras go first (views may depend on columns that change).
    for extra in old.extras.iter().rev().filter(|e| !has_extra(new, e)) {
        ops.push(Op::new(&extra.down, &extra.up).destructive_if(
            extra.down.trim().is_empty(),
            "removes a statement that has no down SQL",
        ));
    }
    for extra in new
        .extras
        .iter()
        .filter(|e| e.before_tables && !has_extra(&old, e))
    {
        ops.push(Op::new(&extra.up, &extra.down));
    }

    // Enums.
    for e in &new.enums {
        match old.enums.iter().find(|o| o.name == e.name) {
            None => ops.push(Op::new(
                create_enum(e),
                format!("DROP TYPE {}", quote(&e.name)),
            )),
            Some(o) => ops.extend(enum_changes(o, e)?),
        }
    }

    // Renames.
    for rename in renames {
        ops.push(apply_rename(&mut old, rename)?);
    }

    let enum_names: Vec<&str> = new
        .enums
        .iter()
        .chain(&old.enums)
        .map(|e| e.name.as_str())
        .collect();
    let ty = |sql_type: &str| render_type(sql_type, &enum_names);

    let mut drop_constraints = Vec::new();
    let mut table_changes = Vec::new();
    let mut add_constraints = Vec::new();
    let mut add_fks = Vec::new();

    for table in &new.tables {
        let Some(before) = old.tables.iter().find(|t| t.name == table.name) else {
            table_changes.push(Op::new(
                create_table(table, &ty),
                format!("DROP TABLE {}", quote(&table.name)),
            ));
            for fk in &table.foreign_keys {
                add_fks.push(add_fk(table, fk));
            }
            continue;
        };
        let t = quote(&table.name);
        // Constraints that went away or changed are dropped first.
        if before.primary_key != table.primary_key {
            if !before.primary_key.is_empty() {
                drop_constraints.push(
                    Op::new(
                        format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&pkey(before))),
                        format!(
                            "ALTER TABLE {t} ADD CONSTRAINT {} PRIMARY KEY ({})",
                            quote(&pkey(before)),
                            columns(&before.primary_key)
                        ),
                    )
                    .destructive(format!("changes the primary key of `{}`", table.name)),
                );
            }
            if !table.primary_key.is_empty() {
                add_constraints.push(Op::new(
                    format!(
                        "ALTER TABLE {t} ADD CONSTRAINT {} PRIMARY KEY ({})",
                        quote(&pkey(table)),
                        columns(&table.primary_key)
                    ),
                    format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&pkey(table))),
                ));
            }
        }
        for fk in before
            .foreign_keys
            .iter()
            .filter(|fk| !table.foreign_keys.contains(fk))
        {
            let mut op = add_fk(before, fk);
            std::mem::swap(&mut op.up, &mut op.down);
            drop_constraints.push(op);
        }
        for fk in table
            .foreign_keys
            .iter()
            .filter(|fk| !before.foreign_keys.contains(fk))
        {
            add_fks.push(add_fk(table, fk));
        }
        for u in before.uniques.iter().filter(|u| !table.uniques.contains(u)) {
            let mut op = add_unique(before, u);
            std::mem::swap(&mut op.up, &mut op.down);
            drop_constraints.push(op);
        }
        for u in table.uniques.iter().filter(|u| !before.uniques.contains(u)) {
            add_constraints.push(add_unique(table, u));
        }
        for c in before.checks.iter().filter(|c| !table.checks.contains(c)) {
            let mut op = add_check(before, c);
            std::mem::swap(&mut op.up, &mut op.down);
            drop_constraints.push(op);
        }
        for c in table.checks.iter().filter(|c| !before.checks.contains(c)) {
            add_constraints.push(add_check(table, c));
        }
        // Columns.
        for column in before
            .columns
            .iter()
            .filter(|c| table.column(&c.name).is_none())
        {
            table_changes.push(
                Op::new(
                    format!("ALTER TABLE {t} DROP COLUMN {}", quote(&column.name)),
                    format!("ALTER TABLE {t} ADD COLUMN {}", column_def(column, &ty)),
                )
                .destructive(format!(
                    "drops column `{}.{}` and its data",
                    table.name, column.name
                )),
            );
        }
        for column in &table.columns {
            match before.column(&column.name) {
                None => {
                    let op = Op::new(
                        format!("ALTER TABLE {t} ADD COLUMN {}", column_def(column, &ty)),
                        format!("ALTER TABLE {t} DROP COLUMN {}", quote(&column.name)),
                    );
                    let needs_value = !column.nullable && !column.has_database_value();
                    table_changes.push(op.destructive_if(
                        needs_value,
                        format!(
                            "adds NOT NULL column `{}.{}` without a DEFAULT, which fails when the table has rows",
                            table.name, column.name
                        ),
                    ));
                }
                Some(old_column) => {
                    table_changes.extend(column_changes(table, old_column, column, &ty)?)
                }
            }
        }
    }

    // Indexes: dropped before columns change, created after.
    let mut drop_indexes = Vec::new();
    let mut create_indexes = Vec::new();
    for index in &old.indexes {
        if !new.indexes.contains(index) {
            drop_indexes.push(Op::new(
                format!(
                    "DROP INDEX IF EXISTS {}",
                    quote(&index_name(&index.table, &index.name))
                ),
                index.sql.clone(),
            ));
        }
    }
    for index in &new.indexes {
        if !old.indexes.contains(index) {
            create_indexes.push(Op::new(
                index.sql.clone(),
                format!(
                    "DROP INDEX IF EXISTS {}",
                    quote(&index_name(&index.table, &index.name))
                ),
            ));
        }
    }

    // Removed tables: their foreign keys first, then the tables.
    let removed: Vec<&Table> = old
        .tables
        .iter()
        .filter(|o| !new.tables.iter().any(|t| t.name == o.name))
        .collect();
    let mut drop_tables = Vec::new();
    for table in &removed {
        for fk in &table.foreign_keys {
            let mut op = add_fk(table, fk);
            std::mem::swap(&mut op.up, &mut op.down);
            drop_constraints.push(op);
        }
    }
    for table in removed.iter().rev() {
        drop_tables.push(
            Op::new(
                format!("DROP TABLE {}", quote(&table.name)),
                create_table(table, &ty),
            )
            .destructive(format!("drops table `{}` and its data", table.name)),
        );
    }

    ops.extend(drop_constraints);
    ops.extend(drop_indexes);
    ops.extend(table_changes);
    ops.extend(add_constraints);
    ops.extend(add_fks);
    ops.extend(create_indexes);
    ops.extend(drop_tables);

    for e in old
        .enums
        .iter()
        .filter(|o| !new.enums.iter().any(|e| e.name == o.name))
    {
        ops.push(
            Op::new(format!("DROP TYPE {}", quote(&e.name)), create_enum(e))
                .destructive(format!("drops enum type `{}`", e.name)),
        );
    }
    for extra in new
        .extras
        .iter()
        .filter(|e| !e.before_tables && !has_extra(&old, e))
    {
        ops.push(Op::new(&extra.up, &extra.down));
    }
    Ok(ops)
}

impl Op {
    fn destructive_if(self, condition: bool, why: impl Into<String>) -> Self {
        if condition {
            self.destructive(why)
        } else {
            self
        }
    }
}

fn has_extra(schema: &Schema, extra: &Extra) -> bool {
    schema.extras.iter().any(|e| e.up == extra.up)
}

fn enum_changes(old: &EnumType, new: &EnumType) -> Result<Vec<Op>, String> {
    let kept: Vec<&String> = new
        .values
        .iter()
        .filter(|v| old.values.contains(v))
        .collect();
    if kept.len() != old.values.len() || kept.iter().zip(&old.values).any(|(a, b)| *a != b) {
        return Err(format!(
            "enum `{}` lost or reordered values; PostgreSQL can only add values. \
             Write that change as a hand-made migration",
            old.name
        ));
    }
    let mut ops = Vec::new();
    for (i, value) in new.values.iter().enumerate() {
        if old.values.contains(value) {
            continue;
        }
        let position = match new.values[..i]
            .iter()
            .rev()
            .find(|v| old.values.contains(v))
        {
            Some(prev) => format!(" AFTER {}", literal(prev)),
            None => match new.values[i + 1..].iter().find(|v| old.values.contains(v)) {
                Some(next) => format!(" BEFORE {}", literal(next)),
                None => String::new(),
            },
        };
        // PostgreSQL can't remove an enum value, so this step has no undo.
        ops.push(Op::new(
            format!(
                "ALTER TYPE {} ADD VALUE {}{position}",
                quote(&new.name),
                literal(value)
            ),
            "",
        ));
    }
    Ok(ops)
}

fn column_changes(
    table: &Table,
    old: &Column,
    new: &Column,
    ty: &dyn Fn(&str) -> String,
) -> Result<Vec<Op>, String> {
    let t = quote(&table.name);
    let c = quote(&new.name);
    let mut ops = Vec::new();
    if old.generated != new.generated || old.identity != new.identity {
        return Err(format!(
            "`{}.{}` changed its GENERATED / IDENTITY definition; write that change as a hand-made migration",
            table.name, new.name
        ));
    }
    if old.sql_type != new.sql_type {
        ops.push(
            Op::new(
                format!(
                    "ALTER TABLE {t} ALTER COLUMN {c} TYPE {} USING {c}::{}",
                    ty(&new.sql_type),
                    ty(&new.sql_type)
                ),
                format!(
                    "ALTER TABLE {t} ALTER COLUMN {c} TYPE {} USING {c}::{}",
                    ty(&old.sql_type),
                    ty(&old.sql_type)
                ),
            )
            .destructive(format!(
                "changes the type of `{}.{}` from {} to {}",
                table.name, new.name, old.sql_type, new.sql_type
            )),
        );
    }
    if old.default != new.default {
        let set = |d: &Option<String>| match d {
            Some(d) => format!("ALTER TABLE {t} ALTER COLUMN {c} SET DEFAULT {d}"),
            None => format!("ALTER TABLE {t} ALTER COLUMN {c} DROP DEFAULT"),
        };
        ops.push(Op::new(set(&new.default), set(&old.default)));
    }
    if old.nullable != new.nullable {
        let set = format!("ALTER TABLE {t} ALTER COLUMN {c} SET NOT NULL");
        let drop = format!("ALTER TABLE {t} ALTER COLUMN {c} DROP NOT NULL");
        ops.push(if new.nullable {
            Op::new(drop, set)
        } else {
            Op::new(set, drop).destructive(format!(
                "makes `{}.{}` NOT NULL, which fails when it holds NULLs",
                table.name, new.name
            ))
        });
    }
    Ok(ops)
}

fn apply_rename(old: &mut Schema, rename: &Rename) -> Result<Op, String> {
    match rename {
        Rename::Table { from, to } => {
            let schema_prefix = from.rsplit_once('.').map(|(s, _)| format!("{s}."));
            let new_name = format!("{}{to}", schema_prefix.unwrap_or_default());
            let table = old
                .tables
                .iter_mut()
                .find(|t| &t.name == from)
                .ok_or_else(|| format!("--rename: the previous schema has no table `{from}`"))?;
            table.name.clone_from(&new_name);
            for t in &mut old.tables {
                for fk in &mut t.foreign_keys {
                    if &fk.ref_table == from {
                        fk.ref_table.clone_from(&new_name);
                    }
                }
            }
            for index in &mut old.indexes {
                if &index.table == from {
                    index.table.clone_from(&new_name);
                }
            }
            Ok(Op::new(
                format!("ALTER TABLE {} RENAME TO {}", quote(from), quote(to)),
                format!(
                    "ALTER TABLE {} RENAME TO {}",
                    quote(&new_name),
                    quote(naming::unqualified(from))
                ),
            ))
        }
        Rename::Column { table, from, to } => {
            let t = old
                .tables
                .iter_mut()
                .find(|t| &t.name == table)
                .ok_or_else(|| format!("--rename: the previous schema has no table `{table}`"))?;
            let column = t
                .columns
                .iter_mut()
                .find(|c| &c.name == from)
                .ok_or_else(|| format!("--rename: `{table}` has no column `{from}`"))?;
            column.name.clone_from(to);
            let rename = |cols: &mut Vec<String>| {
                for c in cols.iter_mut().filter(|c| *c == from) {
                    c.clone_from(to);
                }
            };
            rename(&mut t.primary_key);
            t.uniques.iter_mut().for_each(|u| rename(&mut u.columns));
            t.foreign_keys
                .iter_mut()
                .for_each(|fk| rename(&mut fk.columns));
            let table_name = t.name.clone();
            for other in &mut old.tables {
                for fk in other
                    .foreign_keys
                    .iter_mut()
                    .filter(|fk| fk.ref_table == table_name)
                {
                    rename(&mut fk.ref_columns);
                }
            }
            Ok(Op::new(
                format!(
                    "ALTER TABLE {} RENAME COLUMN {} TO {}",
                    quote(table),
                    quote(from),
                    quote(to)
                ),
                format!(
                    "ALTER TABLE {} RENAME COLUMN {} TO {}",
                    quote(table),
                    quote(to),
                    quote(from)
                ),
            ))
        }
    }
}

pub(crate) fn create_enum(e: &EnumType) -> String {
    let values: Vec<String> = e.values.iter().map(|v| literal(v)).collect();
    format!(
        "CREATE TYPE {} AS ENUM ({})",
        quote(&e.name),
        values.join(", ")
    )
}

/// `CREATE TABLE` without foreign keys (they are added separately).
pub(crate) fn create_table(table: &Table, ty: &dyn Fn(&str) -> String) -> String {
    let mut lines: Vec<String> = table.columns.iter().map(|c| column_def(c, ty)).collect();
    if !table.primary_key.is_empty() {
        lines.push(format!(
            "CONSTRAINT {} PRIMARY KEY ({})",
            quote(&pkey(table)),
            columns(&table.primary_key)
        ));
    }
    for u in &table.uniques {
        lines.push(format!(
            "CONSTRAINT {} UNIQUE ({})",
            quote(&u.name),
            columns(&u.columns)
        ));
    }
    for c in &table.checks {
        lines.push(format!("CONSTRAINT {} CHECK ({})", quote(&c.name), c.expr));
    }
    format!(
        "CREATE TABLE {} (\n    {}\n)",
        quote(&table.name),
        lines.join(",\n    ")
    )
}

fn column_def(c: &Column, ty: &dyn Fn(&str) -> String) -> String {
    let mut def = format!("{} {}", quote(&c.name), ty(&c.sql_type));
    if let Some(expr) = &c.generated {
        def.push_str(&format!(" GENERATED ALWAYS AS ({expr}) STORED"));
    }
    if let Some(kind) = &c.identity {
        def.push_str(&format!(" GENERATED {kind} AS IDENTITY"));
    }
    if !c.nullable {
        def.push_str(" NOT NULL");
    }
    if let Some(d) = &c.default {
        def.push_str(&format!(" DEFAULT {d}"));
    }
    def
}

fn add_fk(table: &Table, fk: &ForeignKey) -> Op {
    let t = quote(&table.name);
    let mut up = format!(
        "ALTER TABLE {t} ADD CONSTRAINT {} FOREIGN KEY ({}) REFERENCES {} ({})",
        quote(&fk.name),
        columns(&fk.columns),
        quote(&fk.ref_table),
        columns(&fk.ref_columns)
    );
    if let Some(action) = &fk.on_delete {
        up.push_str(&format!(" ON DELETE {action}"));
    }
    if let Some(action) = &fk.on_update {
        up.push_str(&format!(" ON UPDATE {action}"));
    }
    Op::new(
        up,
        format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&fk.name)),
    )
}

fn add_unique(table: &Table, u: &Unique) -> Op {
    let t = quote(&table.name);
    Op::new(
        format!(
            "ALTER TABLE {t} ADD CONSTRAINT {} UNIQUE ({})",
            quote(&u.name),
            columns(&u.columns)
        ),
        format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&u.name)),
    )
}

fn add_check(table: &Table, c: &Check) -> Op {
    let t = quote(&table.name);
    Op::new(
        format!(
            "ALTER TABLE {t} ADD CONSTRAINT {} CHECK ({})",
            quote(&c.name),
            c.expr
        ),
        format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&c.name)),
    )
}

fn pkey(table: &Table) -> String {
    format!("{}_pkey", naming::unqualified(&table.name))
}

/// An index lives in its table's schema.
fn index_name(table: &str, index: &str) -> String {
    match table.rsplit_once('.') {
        Some((schema, _)) if !index.contains('.') => format!("{schema}.{index}"),
        _ => index.to_owned(),
    }
}

fn columns(cols: &[String]) -> String {
    cols.iter().map(|c| quote(c)).collect::<Vec<_>>().join(", ")
}

fn render_type(sql_type: &str, enums: &[&str]) -> String {
    let (base, dims) = match sql_type.find("[]") {
        Some(i) => (&sql_type[..i], &sql_type[i..]),
        None => (sql_type, ""),
    };
    if enums.contains(&base) {
        format!("{}{dims}", quote(base))
    } else {
        sql_type.to_owned()
    }
}

/// A SQL string literal.
fn literal(value: &str) -> String {
    format!("'{}'", value.replace('\'', "''"))
}

/// Join steps into one migration's `up` and `down` scripts. `down` is empty
/// when a step can't be undone.
pub(crate) fn scripts(ops: &[Op]) -> (String, String) {
    let up = ops
        .iter()
        .map(|op| format!("{};\n", op.up))
        .collect::<String>();
    let down = if ops.iter().any(|op| op.down.trim().is_empty()) {
        String::new()
    } else {
        ops.iter()
            .rev()
            .map(|op| format!("{};\n", op.down))
            .collect()
    };
    (up, down)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::{SourceFile, parse};

    fn schema(text: &str) -> Schema {
        parse(&[SourceFile {
            path: "db/app.sql".into(),
            module: "app".into(),
            text: text.into(),
        }])
        .unwrap()
        .schema
    }

    const V1: &str = "
        CREATE TYPE role AS ENUM ('admin', 'member');
        CREATE TABLE users (id BIGSERIAL PRIMARY KEY, email TEXT NOT NULL UNIQUE, role role NOT NULL DEFAULT 'member');
        CREATE TABLE posts (id BIGSERIAL PRIMARY KEY, user_id BIGINT NOT NULL REFERENCES users, title TEXT NOT NULL);
        CREATE INDEX posts_user_idx ON posts (user_id);";

    #[test]
    fn init_creates_everything_and_undoes_it() {
        let ops = diff(&Schema::default(), &schema(V1), &[]).unwrap();
        let (up, down) = scripts(&ops);
        let create_users = up.find("CREATE TABLE \"users\"").unwrap();
        let fk = up
            .find("FOREIGN KEY (\"user_id\") REFERENCES \"users\" (\"id\")")
            .unwrap();
        assert!(up.starts_with("CREATE TYPE \"role\" AS ENUM ('admin', 'member');"));
        assert!(create_users < fk, "{up}");
        assert!(
            up.contains("\"role\" \"role\" NOT NULL DEFAULT 'member'"),
            "{up}"
        );
        assert!(
            up.ends_with("CREATE INDEX posts_user_idx ON posts(user_id);\n"),
            "{up}"
        );
        assert!(
            down.starts_with("DROP INDEX IF EXISTS \"posts_user_idx\";"),
            "{down}"
        );
        assert!(down.ends_with("DROP TYPE \"role\";\n"), "{down}");
        assert!(ops.iter().all(|op| op.destructive.is_none()));
    }

    #[test]
    fn no_changes_no_steps() {
        assert!(diff(&schema(V1), &schema(V1), &[]).unwrap().is_empty());
    }

    #[test]
    fn column_and_enum_changes() {
        let v2 = V1
            .replace("'admin', 'member'", "'admin', 'editor', 'member'")
            .replace(
                "title TEXT NOT NULL",
                "title TEXT, slug TEXT NOT NULL DEFAULT ''",
            );
        let ops = diff(&schema(V1), &schema(&v2), &[]).unwrap();
        let ups: Vec<&str> = ops.iter().map(|op| op.up.as_str()).collect();
        assert_eq!(
            ups,
            [
                "ALTER TYPE \"role\" ADD VALUE 'editor' AFTER 'admin'",
                "ALTER TABLE \"posts\" ALTER COLUMN \"title\" DROP NOT NULL",
                "ALTER TABLE \"posts\" ADD COLUMN \"slug\" TEXT NOT NULL DEFAULT ''",
            ]
        );
        // Adding an enum value can't be undone.
        assert!(scripts(&ops).1.is_empty());
    }

    #[test]
    fn destructive_steps_are_flagged() {
        let v2 = V1.replace(", title TEXT NOT NULL", ", body TEXT NOT NULL");
        let ops = diff(&schema(V1), &schema(&v2), &[]).unwrap();
        let reasons: Vec<&str> = ops
            .iter()
            .filter_map(|op| op.destructive.as_deref())
            .collect();
        assert_eq!(reasons.len(), 2, "{reasons:?}");
        assert!(reasons[0].contains("drops column `posts.title`"));
        assert!(reasons[1].contains("without a DEFAULT"));

        // ...unless it is a rename.
        let rename = [Rename::parse("posts.title=body").unwrap()];
        let ops = diff(&schema(V1), &schema(&v2), &rename).unwrap();
        assert_eq!(ops.len(), 1);
        assert_eq!(
            ops[0].up,
            "ALTER TABLE \"posts\" RENAME COLUMN \"title\" TO \"body\""
        );
    }

    #[test]
    fn removed_enum_values_are_refused() {
        let v2 = V1.replace("'admin', 'member'", "'member'");
        assert!(
            diff(&schema(V1), &schema(&v2), &[])
                .unwrap_err()
                .contains("lost or reordered")
        );
    }
}