1use crate::ir::{Check, Column, EnumType, Extra, ForeignKey, Schema, Table, Unique};
8use crate::naming;
9use crate::parse::quote;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub(crate) struct Op {
14 pub(crate) up: String,
15 pub(crate) down: String,
17 pub(crate) destructive: Option<String>,
19}
20
21impl Op {
22 fn new(up: impl Into<String>, down: impl Into<String>) -> Self {
23 Self {
24 up: up.into(),
25 down: down.into(),
26 destructive: None,
27 }
28 }
29
30 fn destructive(mut self, why: impl Into<String>) -> Self {
31 self.destructive = Some(why.into());
32 self
33 }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
38pub enum Rename {
39 Table {
41 from: String,
43 to: String,
45 },
46 Column {
48 table: String,
50 from: String,
52 to: String,
54 },
55}
56
57impl Rename {
58 pub fn parse(spec: &str) -> Option<Self> {
60 let (from, to) = spec.split_once('=')?;
61 let (from, to) = (from.trim(), to.trim());
62 if from.is_empty() || to.is_empty() || to.contains('.') {
63 return None;
64 }
65 Some(match from.rsplit_once('.') {
66 Some((table, column)) => Rename::Column {
67 table: table.to_owned(),
68 from: column.to_owned(),
69 to: to.to_owned(),
70 },
71 None => Rename::Table {
72 from: from.to_owned(),
73 to: to.to_owned(),
74 },
75 })
76 }
77}
78
79pub(crate) fn diff(old: &Schema, new: &Schema, renames: &[Rename]) -> Result<Vec<Op>, String> {
82 let mut old = old.clone();
83 let mut ops = Vec::new();
84
85 for extra in old.extras.iter().rev().filter(|e| !has_extra(new, e)) {
87 ops.push(Op::new(&extra.down, &extra.up).destructive_if(
88 extra.down.trim().is_empty(),
89 "removes a statement that has no down SQL",
90 ));
91 }
92 for extra in new
93 .extras
94 .iter()
95 .filter(|e| e.before_tables && !has_extra(&old, e))
96 {
97 ops.push(Op::new(&extra.up, &extra.down));
98 }
99
100 for e in &new.enums {
102 match old.enums.iter().find(|o| o.name == e.name) {
103 None => ops.push(Op::new(
104 create_enum(e),
105 format!("DROP TYPE {}", quote(&e.name)),
106 )),
107 Some(o) => ops.extend(enum_changes(o, e)?),
108 }
109 }
110
111 for rename in renames {
113 ops.push(apply_rename(&mut old, rename)?);
114 }
115
116 let enum_names: Vec<&str> = new
117 .enums
118 .iter()
119 .chain(&old.enums)
120 .map(|e| e.name.as_str())
121 .collect();
122 let ty = |sql_type: &str| render_type(sql_type, &enum_names);
123
124 let mut drop_constraints = Vec::new();
125 let mut table_changes = Vec::new();
126 let mut add_constraints = Vec::new();
127 let mut add_fks = Vec::new();
128
129 for table in &new.tables {
130 let Some(before) = old.tables.iter().find(|t| t.name == table.name) else {
131 table_changes.push(Op::new(
132 create_table(table, &ty),
133 format!("DROP TABLE {}", quote(&table.name)),
134 ));
135 for fk in &table.foreign_keys {
136 add_fks.push(add_fk(table, fk));
137 }
138 continue;
139 };
140 let t = quote(&table.name);
141 if before.primary_key != table.primary_key {
143 if !before.primary_key.is_empty() {
144 drop_constraints.push(
145 Op::new(
146 format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&pkey(before))),
147 format!(
148 "ALTER TABLE {t} ADD CONSTRAINT {} PRIMARY KEY ({})",
149 quote(&pkey(before)),
150 columns(&before.primary_key)
151 ),
152 )
153 .destructive(format!("changes the primary key of `{}`", table.name)),
154 );
155 }
156 if !table.primary_key.is_empty() {
157 add_constraints.push(Op::new(
158 format!(
159 "ALTER TABLE {t} ADD CONSTRAINT {} PRIMARY KEY ({})",
160 quote(&pkey(table)),
161 columns(&table.primary_key)
162 ),
163 format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&pkey(table))),
164 ));
165 }
166 }
167 for fk in before
168 .foreign_keys
169 .iter()
170 .filter(|fk| !table.foreign_keys.contains(fk))
171 {
172 let mut op = add_fk(before, fk);
173 std::mem::swap(&mut op.up, &mut op.down);
174 drop_constraints.push(op);
175 }
176 for fk in table
177 .foreign_keys
178 .iter()
179 .filter(|fk| !before.foreign_keys.contains(fk))
180 {
181 add_fks.push(add_fk(table, fk));
182 }
183 for u in before.uniques.iter().filter(|u| !table.uniques.contains(u)) {
184 let mut op = add_unique(before, u);
185 std::mem::swap(&mut op.up, &mut op.down);
186 drop_constraints.push(op);
187 }
188 for u in table.uniques.iter().filter(|u| !before.uniques.contains(u)) {
189 add_constraints.push(add_unique(table, u));
190 }
191 for c in before.checks.iter().filter(|c| !table.checks.contains(c)) {
192 let mut op = add_check(before, c);
193 std::mem::swap(&mut op.up, &mut op.down);
194 drop_constraints.push(op);
195 }
196 for c in table.checks.iter().filter(|c| !before.checks.contains(c)) {
197 add_constraints.push(add_check(table, c));
198 }
199 for column in before
201 .columns
202 .iter()
203 .filter(|c| table.column(&c.name).is_none())
204 {
205 table_changes.push(
206 Op::new(
207 format!("ALTER TABLE {t} DROP COLUMN {}", quote(&column.name)),
208 format!("ALTER TABLE {t} ADD COLUMN {}", column_def(column, &ty)),
209 )
210 .destructive(format!(
211 "drops column `{}.{}` and its data",
212 table.name, column.name
213 )),
214 );
215 }
216 for column in &table.columns {
217 match before.column(&column.name) {
218 None => {
219 let op = Op::new(
220 format!("ALTER TABLE {t} ADD COLUMN {}", column_def(column, &ty)),
221 format!("ALTER TABLE {t} DROP COLUMN {}", quote(&column.name)),
222 );
223 let needs_value = !column.nullable && !column.has_database_value();
224 table_changes.push(op.destructive_if(
225 needs_value,
226 format!(
227 "adds NOT NULL column `{}.{}` without a DEFAULT, which fails when the table has rows",
228 table.name, column.name
229 ),
230 ));
231 }
232 Some(old_column) => {
233 table_changes.extend(column_changes(table, old_column, column, &ty)?)
234 }
235 }
236 }
237 }
238
239 let mut drop_indexes = Vec::new();
241 let mut create_indexes = Vec::new();
242 for index in &old.indexes {
243 if !new.indexes.contains(index) {
244 drop_indexes.push(Op::new(
245 format!(
246 "DROP INDEX IF EXISTS {}",
247 quote(&index_name(&index.table, &index.name))
248 ),
249 index.sql.clone(),
250 ));
251 }
252 }
253 for index in &new.indexes {
254 if !old.indexes.contains(index) {
255 create_indexes.push(Op::new(
256 index.sql.clone(),
257 format!(
258 "DROP INDEX IF EXISTS {}",
259 quote(&index_name(&index.table, &index.name))
260 ),
261 ));
262 }
263 }
264
265 let removed: Vec<&Table> = old
267 .tables
268 .iter()
269 .filter(|o| !new.tables.iter().any(|t| t.name == o.name))
270 .collect();
271 let mut drop_tables = Vec::new();
272 for table in &removed {
273 for fk in &table.foreign_keys {
274 let mut op = add_fk(table, fk);
275 std::mem::swap(&mut op.up, &mut op.down);
276 drop_constraints.push(op);
277 }
278 }
279 for table in removed.iter().rev() {
280 drop_tables.push(
281 Op::new(
282 format!("DROP TABLE {}", quote(&table.name)),
283 create_table(table, &ty),
284 )
285 .destructive(format!("drops table `{}` and its data", table.name)),
286 );
287 }
288
289 ops.extend(drop_constraints);
290 ops.extend(drop_indexes);
291 ops.extend(table_changes);
292 ops.extend(add_constraints);
293 ops.extend(add_fks);
294 ops.extend(create_indexes);
295 ops.extend(drop_tables);
296
297 for e in old
298 .enums
299 .iter()
300 .filter(|o| !new.enums.iter().any(|e| e.name == o.name))
301 {
302 ops.push(
303 Op::new(format!("DROP TYPE {}", quote(&e.name)), create_enum(e))
304 .destructive(format!("drops enum type `{}`", e.name)),
305 );
306 }
307 for extra in new
308 .extras
309 .iter()
310 .filter(|e| !e.before_tables && !has_extra(&old, e))
311 {
312 ops.push(Op::new(&extra.up, &extra.down));
313 }
314 Ok(ops)
315}
316
317impl Op {
318 fn destructive_if(self, condition: bool, why: impl Into<String>) -> Self {
319 if condition {
320 self.destructive(why)
321 } else {
322 self
323 }
324 }
325}
326
327fn has_extra(schema: &Schema, extra: &Extra) -> bool {
328 schema.extras.iter().any(|e| e.up == extra.up)
329}
330
331fn enum_changes(old: &EnumType, new: &EnumType) -> Result<Vec<Op>, String> {
332 let kept: Vec<&String> = new
333 .values
334 .iter()
335 .filter(|v| old.values.contains(v))
336 .collect();
337 if kept.len() != old.values.len() || kept.iter().zip(&old.values).any(|(a, b)| *a != b) {
338 return Err(format!(
339 "enum `{}` lost or reordered values; PostgreSQL can only add values. \
340 Write that change as a hand-made migration",
341 old.name
342 ));
343 }
344 let mut ops = Vec::new();
345 for (i, value) in new.values.iter().enumerate() {
346 if old.values.contains(value) {
347 continue;
348 }
349 let position = match new.values[..i]
350 .iter()
351 .rev()
352 .find(|v| old.values.contains(v))
353 {
354 Some(prev) => format!(" AFTER {}", literal(prev)),
355 None => match new.values[i + 1..].iter().find(|v| old.values.contains(v)) {
356 Some(next) => format!(" BEFORE {}", literal(next)),
357 None => String::new(),
358 },
359 };
360 ops.push(Op::new(
362 format!(
363 "ALTER TYPE {} ADD VALUE {}{position}",
364 quote(&new.name),
365 literal(value)
366 ),
367 "",
368 ));
369 }
370 Ok(ops)
371}
372
373fn column_changes(
374 table: &Table,
375 old: &Column,
376 new: &Column,
377 ty: &dyn Fn(&str) -> String,
378) -> Result<Vec<Op>, String> {
379 let t = quote(&table.name);
380 let c = quote(&new.name);
381 let mut ops = Vec::new();
382 if old.generated != new.generated || old.identity != new.identity {
383 return Err(format!(
384 "`{}.{}` changed its GENERATED / IDENTITY definition; write that change as a hand-made migration",
385 table.name, new.name
386 ));
387 }
388 if old.sql_type != new.sql_type {
389 ops.push(
390 Op::new(
391 format!(
392 "ALTER TABLE {t} ALTER COLUMN {c} TYPE {} USING {c}::{}",
393 ty(&new.sql_type),
394 ty(&new.sql_type)
395 ),
396 format!(
397 "ALTER TABLE {t} ALTER COLUMN {c} TYPE {} USING {c}::{}",
398 ty(&old.sql_type),
399 ty(&old.sql_type)
400 ),
401 )
402 .destructive(format!(
403 "changes the type of `{}.{}` from {} to {}",
404 table.name, new.name, old.sql_type, new.sql_type
405 )),
406 );
407 }
408 if old.default != new.default {
409 let set = |d: &Option<String>| match d {
410 Some(d) => format!("ALTER TABLE {t} ALTER COLUMN {c} SET DEFAULT {d}"),
411 None => format!("ALTER TABLE {t} ALTER COLUMN {c} DROP DEFAULT"),
412 };
413 ops.push(Op::new(set(&new.default), set(&old.default)));
414 }
415 if old.nullable != new.nullable {
416 let set = format!("ALTER TABLE {t} ALTER COLUMN {c} SET NOT NULL");
417 let drop = format!("ALTER TABLE {t} ALTER COLUMN {c} DROP NOT NULL");
418 ops.push(if new.nullable {
419 Op::new(drop, set)
420 } else {
421 Op::new(set, drop).destructive(format!(
422 "makes `{}.{}` NOT NULL, which fails when it holds NULLs",
423 table.name, new.name
424 ))
425 });
426 }
427 Ok(ops)
428}
429
430fn apply_rename(old: &mut Schema, rename: &Rename) -> Result<Op, String> {
431 match rename {
432 Rename::Table { from, to } => {
433 let schema_prefix = from.rsplit_once('.').map(|(s, _)| format!("{s}."));
434 let new_name = format!("{}{to}", schema_prefix.unwrap_or_default());
435 let table = old
436 .tables
437 .iter_mut()
438 .find(|t| &t.name == from)
439 .ok_or_else(|| format!("--rename: the previous schema has no table `{from}`"))?;
440 table.name.clone_from(&new_name);
441 for t in &mut old.tables {
442 for fk in &mut t.foreign_keys {
443 if &fk.ref_table == from {
444 fk.ref_table.clone_from(&new_name);
445 }
446 }
447 }
448 for index in &mut old.indexes {
449 if &index.table == from {
450 index.table.clone_from(&new_name);
451 }
452 }
453 Ok(Op::new(
454 format!("ALTER TABLE {} RENAME TO {}", quote(from), quote(to)),
455 format!(
456 "ALTER TABLE {} RENAME TO {}",
457 quote(&new_name),
458 quote(naming::unqualified(from))
459 ),
460 ))
461 }
462 Rename::Column { table, from, to } => {
463 let t = old
464 .tables
465 .iter_mut()
466 .find(|t| &t.name == table)
467 .ok_or_else(|| format!("--rename: the previous schema has no table `{table}`"))?;
468 let column = t
469 .columns
470 .iter_mut()
471 .find(|c| &c.name == from)
472 .ok_or_else(|| format!("--rename: `{table}` has no column `{from}`"))?;
473 column.name.clone_from(to);
474 let rename = |cols: &mut Vec<String>| {
475 for c in cols.iter_mut().filter(|c| *c == from) {
476 c.clone_from(to);
477 }
478 };
479 rename(&mut t.primary_key);
480 t.uniques.iter_mut().for_each(|u| rename(&mut u.columns));
481 t.foreign_keys
482 .iter_mut()
483 .for_each(|fk| rename(&mut fk.columns));
484 let table_name = t.name.clone();
485 for other in &mut old.tables {
486 for fk in other
487 .foreign_keys
488 .iter_mut()
489 .filter(|fk| fk.ref_table == table_name)
490 {
491 rename(&mut fk.ref_columns);
492 }
493 }
494 Ok(Op::new(
495 format!(
496 "ALTER TABLE {} RENAME COLUMN {} TO {}",
497 quote(table),
498 quote(from),
499 quote(to)
500 ),
501 format!(
502 "ALTER TABLE {} RENAME COLUMN {} TO {}",
503 quote(table),
504 quote(to),
505 quote(from)
506 ),
507 ))
508 }
509 }
510}
511
512pub(crate) fn create_enum(e: &EnumType) -> String {
513 let values: Vec<String> = e.values.iter().map(|v| literal(v)).collect();
514 format!(
515 "CREATE TYPE {} AS ENUM ({})",
516 quote(&e.name),
517 values.join(", ")
518 )
519}
520
521pub(crate) fn create_table(table: &Table, ty: &dyn Fn(&str) -> String) -> String {
523 let mut lines: Vec<String> = table.columns.iter().map(|c| column_def(c, ty)).collect();
524 if !table.primary_key.is_empty() {
525 lines.push(format!(
526 "CONSTRAINT {} PRIMARY KEY ({})",
527 quote(&pkey(table)),
528 columns(&table.primary_key)
529 ));
530 }
531 for u in &table.uniques {
532 lines.push(format!(
533 "CONSTRAINT {} UNIQUE ({})",
534 quote(&u.name),
535 columns(&u.columns)
536 ));
537 }
538 for c in &table.checks {
539 lines.push(format!("CONSTRAINT {} CHECK ({})", quote(&c.name), c.expr));
540 }
541 format!(
542 "CREATE TABLE {} (\n {}\n)",
543 quote(&table.name),
544 lines.join(",\n ")
545 )
546}
547
548fn column_def(c: &Column, ty: &dyn Fn(&str) -> String) -> String {
549 let mut def = format!("{} {}", quote(&c.name), ty(&c.sql_type));
550 if let Some(expr) = &c.generated {
551 def.push_str(&format!(" GENERATED ALWAYS AS ({expr}) STORED"));
552 }
553 if let Some(kind) = &c.identity {
554 def.push_str(&format!(" GENERATED {kind} AS IDENTITY"));
555 }
556 if !c.nullable {
557 def.push_str(" NOT NULL");
558 }
559 if let Some(d) = &c.default {
560 def.push_str(&format!(" DEFAULT {d}"));
561 }
562 def
563}
564
565fn add_fk(table: &Table, fk: &ForeignKey) -> Op {
566 let t = quote(&table.name);
567 let mut up = format!(
568 "ALTER TABLE {t} ADD CONSTRAINT {} FOREIGN KEY ({}) REFERENCES {} ({})",
569 quote(&fk.name),
570 columns(&fk.columns),
571 quote(&fk.ref_table),
572 columns(&fk.ref_columns)
573 );
574 if let Some(action) = &fk.on_delete {
575 up.push_str(&format!(" ON DELETE {action}"));
576 }
577 if let Some(action) = &fk.on_update {
578 up.push_str(&format!(" ON UPDATE {action}"));
579 }
580 Op::new(
581 up,
582 format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&fk.name)),
583 )
584}
585
586fn add_unique(table: &Table, u: &Unique) -> Op {
587 let t = quote(&table.name);
588 Op::new(
589 format!(
590 "ALTER TABLE {t} ADD CONSTRAINT {} UNIQUE ({})",
591 quote(&u.name),
592 columns(&u.columns)
593 ),
594 format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&u.name)),
595 )
596}
597
598fn add_check(table: &Table, c: &Check) -> Op {
599 let t = quote(&table.name);
600 Op::new(
601 format!(
602 "ALTER TABLE {t} ADD CONSTRAINT {} CHECK ({})",
603 quote(&c.name),
604 c.expr
605 ),
606 format!("ALTER TABLE {t} DROP CONSTRAINT {}", quote(&c.name)),
607 )
608}
609
610fn pkey(table: &Table) -> String {
611 format!("{}_pkey", naming::unqualified(&table.name))
612}
613
614fn index_name(table: &str, index: &str) -> String {
616 match table.rsplit_once('.') {
617 Some((schema, _)) if !index.contains('.') => format!("{schema}.{index}"),
618 _ => index.to_owned(),
619 }
620}
621
622fn columns(cols: &[String]) -> String {
623 cols.iter().map(|c| quote(c)).collect::<Vec<_>>().join(", ")
624}
625
626fn render_type(sql_type: &str, enums: &[&str]) -> String {
627 let (base, dims) = match sql_type.find("[]") {
628 Some(i) => (&sql_type[..i], &sql_type[i..]),
629 None => (sql_type, ""),
630 };
631 if enums.contains(&base) {
632 format!("{}{dims}", quote(base))
633 } else {
634 sql_type.to_owned()
635 }
636}
637
638fn literal(value: &str) -> String {
640 format!("'{}'", value.replace('\'', "''"))
641}
642
643pub(crate) fn scripts(ops: &[Op]) -> (String, String) {
646 let up = ops
647 .iter()
648 .map(|op| format!("{};\n", op.up))
649 .collect::<String>();
650 let down = if ops.iter().any(|op| op.down.trim().is_empty()) {
651 String::new()
652 } else {
653 ops.iter()
654 .rev()
655 .map(|op| format!("{};\n", op.down))
656 .collect()
657 };
658 (up, down)
659}
660
661#[cfg(test)]
662mod tests {
663 use super::*;
664 use crate::parse::{SourceFile, parse};
665
666 fn schema(text: &str) -> Schema {
667 parse(&[SourceFile {
668 path: "db/app.sql".into(),
669 module: "app".into(),
670 text: text.into(),
671 }])
672 .unwrap()
673 .schema
674 }
675
676 const V1: &str = "
677 CREATE TYPE role AS ENUM ('admin', 'member');
678 CREATE TABLE users (id BIGSERIAL PRIMARY KEY, email TEXT NOT NULL UNIQUE, role role NOT NULL DEFAULT 'member');
679 CREATE TABLE posts (id BIGSERIAL PRIMARY KEY, user_id BIGINT NOT NULL REFERENCES users, title TEXT NOT NULL);
680 CREATE INDEX posts_user_idx ON posts (user_id);";
681
682 #[test]
683 fn init_creates_everything_and_undoes_it() {
684 let ops = diff(&Schema::default(), &schema(V1), &[]).unwrap();
685 let (up, down) = scripts(&ops);
686 let create_users = up.find("CREATE TABLE \"users\"").unwrap();
687 let fk = up
688 .find("FOREIGN KEY (\"user_id\") REFERENCES \"users\" (\"id\")")
689 .unwrap();
690 assert!(up.starts_with("CREATE TYPE \"role\" AS ENUM ('admin', 'member');"));
691 assert!(create_users < fk, "{up}");
692 assert!(
693 up.contains("\"role\" \"role\" NOT NULL DEFAULT 'member'"),
694 "{up}"
695 );
696 assert!(
697 up.ends_with("CREATE INDEX posts_user_idx ON posts(user_id);\n"),
698 "{up}"
699 );
700 assert!(
701 down.starts_with("DROP INDEX IF EXISTS \"posts_user_idx\";"),
702 "{down}"
703 );
704 assert!(down.ends_with("DROP TYPE \"role\";\n"), "{down}");
705 assert!(ops.iter().all(|op| op.destructive.is_none()));
706 }
707
708 #[test]
709 fn no_changes_no_steps() {
710 assert!(diff(&schema(V1), &schema(V1), &[]).unwrap().is_empty());
711 }
712
713 #[test]
714 fn column_and_enum_changes() {
715 let v2 = V1
716 .replace("'admin', 'member'", "'admin', 'editor', 'member'")
717 .replace(
718 "title TEXT NOT NULL",
719 "title TEXT, slug TEXT NOT NULL DEFAULT ''",
720 );
721 let ops = diff(&schema(V1), &schema(&v2), &[]).unwrap();
722 let ups: Vec<&str> = ops.iter().map(|op| op.up.as_str()).collect();
723 assert_eq!(
724 ups,
725 [
726 "ALTER TYPE \"role\" ADD VALUE 'editor' AFTER 'admin'",
727 "ALTER TABLE \"posts\" ALTER COLUMN \"title\" DROP NOT NULL",
728 "ALTER TABLE \"posts\" ADD COLUMN \"slug\" TEXT NOT NULL DEFAULT ''",
729 ]
730 );
731 assert!(scripts(&ops).1.is_empty());
733 }
734
735 #[test]
736 fn destructive_steps_are_flagged() {
737 let v2 = V1.replace(", title TEXT NOT NULL", ", body TEXT NOT NULL");
738 let ops = diff(&schema(V1), &schema(&v2), &[]).unwrap();
739 let reasons: Vec<&str> = ops
740 .iter()
741 .filter_map(|op| op.destructive.as_deref())
742 .collect();
743 assert_eq!(reasons.len(), 2, "{reasons:?}");
744 assert!(reasons[0].contains("drops column `posts.title`"));
745 assert!(reasons[1].contains("without a DEFAULT"));
746
747 let rename = [Rename::parse("posts.title=body").unwrap()];
749 let ops = diff(&schema(V1), &schema(&v2), &rename).unwrap();
750 assert_eq!(ops.len(), 1);
751 assert_eq!(
752 ops[0].up,
753 "ALTER TABLE \"posts\" RENAME COLUMN \"title\" TO \"body\""
754 );
755 }
756
757 #[test]
758 fn removed_enum_values_are_refused() {
759 let v2 = V1.replace("'admin', 'member'", "'member'");
760 assert!(
761 diff(&schema(V1), &schema(&v2), &[])
762 .unwrap_err()
763 .contains("lost or reordered")
764 );
765 }
766}