Skip to content

Commit

Permalink
Fix a crash with a special case of dbgenerated() (#3515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Dec 15, 2022
1 parent ea8b7e7 commit d4c8ebe
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libs/sql-schema-describer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ impl DefaultValue {
self.constraint_name = constraint_name;
self
}

/// If the default value is the deprecated `dbgenerated()`
/// variant.
pub fn is_empty_dbgenerated(&self) -> bool {
matches!(self.kind, DefaultKind::DbGenerated(None))
}
}

fn unquote_string(val: &str) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ fn render_mysql_modify(
.unwrap_or_else(|| render_column_type(next_column));

let default = new_default
.filter(|default| !default.is_empty_dbgenerated())
.map(|default| render_default(next_column, default))
.filter(|expr| !expr.is_empty())
.map(|expression| format!(" DEFAULT {}", expression))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ fn native_type_change(types: Pair<&MySqlType>) -> Option<ColumnTypeChange> {
},

MySqlType::Time(n) => match next {
MySqlType::Time(None) if n.unwrap_or(0) == 0 => return None,
MySqlType::Time(m) if n == m => return None,
MySqlType::Time(_) => safe(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ fn schemas_with_dbgenerated_work(api: TestApi) {
.assert_no_steps();
}

#[test_connector(tags(Mysql))]
fn schemas_with_empty_dbgenerated_work_together_with_time_native_type(api: TestApi) {
// https://github.com/prisma/prisma/issues/16340

let dm1 = indoc::indoc! {r#"
model Class {
id Int @id
when DateTime @default(dbgenerated()) @db.Time
}
"#};

api.schema_push_w_datasource(dm1).send().assert_green();
api.schema_push_w_datasource(dm1)
.send()
.assert_green()
.assert_no_steps();
}

#[test_connector(tags(Mysql8, Mariadb), exclude(Vitess))]
fn schemas_with_dbgenerated_expressions_work(api: TestApi) {
let dm1 = r#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,35 @@ fn typescript_starter_schema_with_different_native_types_is_idempotent(api: Test
.assert_green()
.assert_no_steps();
}

#[test_connector(tags(Mysql))]
fn time_zero_is_idempotent(api: TestApi) {
let dm1 = indoc::indoc! {r#"
model Class {
id Int @id
when DateTime @db.Time(0)
}
"#};

api.schema_push_w_datasource(dm1).send().assert_green();
api.schema_push_w_datasource(dm1)
.send()
.assert_green()
.assert_no_steps();
}

#[test_connector(tags(Mysql))]
fn time_is_idempotent(api: TestApi) {
let dm1 = indoc::indoc! {r#"
model Class {
id Int @id
when DateTime @db.Time
}
"#};

api.schema_push_w_datasource(dm1).send().assert_green();
api.schema_push_w_datasource(dm1)
.send()
.assert_green()
.assert_no_steps();
}

0 comments on commit d4c8ebe

Please sign in to comment.