Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash with a special case of dbgenerated() #3515

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_indepodent(api: TestApi) {
pimeys marked this conversation as resolved.
Show resolved Hide resolved
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_indepodent(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();
}