Skip to content

Commit 25ea8ac

Browse files
committed
refactor(migrations): read the database schema into the model
* Read tables, columns, defaults, primary and foreign keys and the server version on PostgreSQL, SQL Server and MySQL * Read the default schema and the asked ones, leave Canyon's own tables out * Keep collation, comment, `ON UPDATE` and `INVISIBLE` when a column is written again * Move `CURRENT_TIMESTAMP` defaults to the new precision on a type change * Never rewrite generated columns * Escape MySQL comments for the `sql_mode` * Test introspection and column rewrites on the three databases
1 parent 16ac301 commit 25ea8ac

49 files changed

Lines changed: 2944 additions & 42 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎canyon_migrations/src/dialect/fixtures.rs‎

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub fn ctx<'a>(actual: &'a SchemaModel, default_schema: &'a str) -> Ctx<'a> {
4949
patch: 0,
5050
},
5151
default_schema,
52+
no_backslash_escapes: false,
5253
actual,
5354
}
5455
}
@@ -105,6 +106,7 @@ pub fn all_ops() -> Vec<(&'static str, Op)> {
105106
ty: Logical::Known(SqlType::BigInt),
106107
nullable: true,
107108
default: None,
109+
extra: ColumnExtra::default(),
108110
};
109111
let pk = PrimaryKey {
110112
name: None,
@@ -256,7 +258,12 @@ pub fn rebuild_op() -> Op {
256258

257259
/// The cases the happy-path fixtures miss: defaults where they get in the way, names
258260
/// that need quoting, schemas. Each dialect snapshots all of them.
259-
pub fn edge_cases(raw_int: &str, raw_text: &str) -> Vec<(&'static str, Op, SchemaModel)> {
261+
/// `collation` is a text collation as the dialect's catalog reports it.
262+
pub fn edge_cases(
263+
raw_int: &str,
264+
raw_text: &str,
265+
collation: &str,
266+
) -> Vec<(&'static str, Op, SchemaModel)> {
260267
let model = |t: TableDef| SchemaModel::from_tables(vec![t]).unwrap();
261268
let with_default = |col: usize, default: ColumnDefault| {
262269
let mut t = actual_player(raw_int, raw_text, None);
@@ -293,6 +300,7 @@ pub fn edge_cases(raw_int: &str, raw_text: &str) -> Vec<(&'static str, Op, Schem
293300
ty: Logical::Known(SqlType::BigInt),
294301
nullable: false, // the desired side; the column is still NULL
295302
default: None,
303+
extra: ColumnExtra::default(),
296304
},
297305
},
298306
model(actual_player(raw_int, raw_text, None)),
@@ -333,5 +341,160 @@ pub fn edge_cases(raw_int: &str, raw_text: &str) -> Vec<(&'static str, Op, Schem
333341
},
334342
SchemaModel::default(),
335343
),
344+
(
345+
"drop_not_null_keeps_extras",
346+
Op::DropNotNull {
347+
table: q("player"),
348+
column: id("name"),
349+
},
350+
model(with_extras(
351+
actual_player(raw_int, raw_text, None),
352+
collation,
353+
)),
354+
),
355+
(
356+
"alter_type_to_text_keeps_collation",
357+
Op::AlterColumnType {
358+
table: q("player"),
359+
column: desired("name", SqlType::Text { max_len: Some(200) }),
360+
},
361+
model(with_extras(
362+
actual_player(raw_int, raw_text, None),
363+
collation,
364+
)),
365+
),
366+
(
367+
"alter_type_to_int_drops_collation",
368+
Op::AlterColumnType {
369+
table: q("player"),
370+
column: desired("name", SqlType::BigInt),
371+
},
372+
model(with_extras(
373+
actual_player(raw_int, raw_text, None),
374+
collation,
375+
)),
376+
),
377+
(
378+
"generated_column_is_not_rewritten",
379+
Op::SetNotNull {
380+
table: q("player"),
381+
column: id("age"),
382+
},
383+
model({
384+
let mut t = actual_player(raw_int, raw_text, None);
385+
t.columns[2].extra.generated = true;
386+
t
387+
}),
388+
),
389+
(
390+
"rebuild_keeps_collation",
391+
rebuild_op_from(with_extras(player(), collation)),
392+
SchemaModel::default(),
393+
),
394+
(
395+
"rebuild_renamed_keeps_collation",
396+
{
397+
let mut from = with_extras(player(), collation);
398+
from.columns[1].name = id("nick");
399+
from.primary_key.as_mut().unwrap().generated = None;
400+
let to = player();
401+
let column_map = to
402+
.columns
403+
.iter()
404+
.map(|c| {
405+
let old = if c.name.as_str() == "name" {
406+
id("nick")
407+
} else {
408+
c.name.clone()
409+
};
410+
(old, c.name.clone())
411+
})
412+
.collect();
413+
Op::RebuildTable {
414+
from,
415+
to,
416+
column_map,
417+
}
418+
},
419+
SchemaModel::default(),
420+
),
421+
(
422+
"rebuild_to_int_drops_collation",
423+
match rebuild_op_from(with_extras(player(), collation)) {
424+
Op::RebuildTable {
425+
from,
426+
mut to,
427+
column_map,
428+
} => {
429+
to.columns[1].ty = Logical::Known(SqlType::BigInt);
430+
Op::RebuildTable {
431+
from,
432+
to,
433+
column_map,
434+
}
435+
}
436+
_ => unreachable!(),
437+
},
438+
SchemaModel::default(),
439+
),
440+
(
441+
"set_not_null_keeps_on_update",
442+
Op::SetNotNull {
443+
table: q("stamp"),
444+
column: id("ts"),
445+
},
446+
model(stamp()),
447+
),
448+
(
449+
"alter_timestamp_precision_moves_current_timestamp",
450+
Op::AlterColumnType {
451+
table: q("stamp"),
452+
column: desired("ts", SqlType::Timestamp { precision: 3 }),
453+
},
454+
model(stamp()),
455+
),
456+
(
457+
"alter_timestamp_to_text_keeps_the_default",
458+
Op::AlterColumnType {
459+
table: q("stamp"),
460+
column: desired("ts", SqlType::Text { max_len: Some(40) }),
461+
},
462+
model(stamp()),
463+
),
336464
]
337465
}
466+
467+
/// `name` with what a catalog reports besides its type: `collation` as the dialect's
468+
/// catalog names it, and a comment.
469+
fn with_extras(mut t: TableDef, collation: &str) -> TableDef {
470+
t.columns[1].extra = ColumnExtra {
471+
collation: Some(CatalogSql::new(collation)),
472+
comment: Some("it's \\ x".into()),
473+
..ColumnExtra::default()
474+
};
475+
t
476+
}
477+
478+
/// `stamp (ts datetime(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6))`
479+
/// as MySQL reports it (only MySQL has ON UPDATE; the others snapshot it all the same).
480+
fn stamp() -> TableDef {
481+
let mut t = TableDef::builder("Stamp", "stamp")
482+
.column("ts", SqlType::Timestamp { precision: 6 }, true)
483+
.build()
484+
.unwrap();
485+
t.entity = None;
486+
let ts = &mut t.columns[0];
487+
ts.default = Some(expr("CURRENT_TIMESTAMP(6)"));
488+
ts.extra.on_update = Some(CatalogSql::new("CURRENT_TIMESTAMP(6)"));
489+
t
490+
}
491+
492+
fn desired(name: &str, ty: SqlType) -> ColumnDef {
493+
ColumnDef {
494+
name: id(name),
495+
ty: Logical::Known(ty),
496+
nullable: false,
497+
default: None,
498+
extra: ColumnExtra::default(),
499+
}
500+
}

0 commit comments

Comments
 (0)