-
Notifications
You must be signed in to change notification settings - Fork 296
fix: prevent breaking default expr in SQLite #5748
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -270,6 +270,18 @@ impl<'a> SqlSchemaDescriber<'a> { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Wraps the given string in parentheses if it is not already. | ||||||
| /// The result of `PRAGMA table_info` removes parentheses from default values, | ||||||
| /// so we need to add parentheses when generating prisma schema. | ||||||
| /// See <https://github.com/prisma/prisma/issues/29064> | ||||||
| fn wrap_in_parentheses(s: &str) -> Cow<'_, str> { | ||||||
| if s[0..1] != *"(" && s[s.len() - 1..] == *")" { | ||||||
ken0x0a marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| format!("({})", s).into() | ||||||
| } else { | ||||||
| s.into() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| async fn push_columns( | ||||||
| table_name: &str, | ||||||
| container_id: Either<TableId, ViewId>, | ||||||
|
|
@@ -306,26 +318,26 @@ async fn push_columns( | |||||
| Some(match &tpe.family { | ||||||
| ColumnTypeFamily::Int => match SqlSchemaDescriber::parse_int(&default_string) { | ||||||
| Some(int_value) => DefaultValue::value(int_value), | ||||||
| None => DefaultValue::db_generated(default_string), | ||||||
| None => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), | ||||||
| }, | ||||||
| ColumnTypeFamily::BigInt => match SqlSchemaDescriber::parse_big_int(&default_string) { | ||||||
| Some(int_value) => DefaultValue::value(int_value), | ||||||
| None => DefaultValue::db_generated(default_string), | ||||||
| None => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), | ||||||
| }, | ||||||
| ColumnTypeFamily::Float => match SqlSchemaDescriber::parse_float(&default_string) { | ||||||
| Some(float_value) => DefaultValue::value(float_value), | ||||||
| None => DefaultValue::db_generated(default_string), | ||||||
| None => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), | ||||||
| }, | ||||||
| ColumnTypeFamily::Decimal => match SqlSchemaDescriber::parse_float(&default_string) { | ||||||
| Some(float_value) => DefaultValue::value(float_value), | ||||||
| None => DefaultValue::db_generated(default_string), | ||||||
| None => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), | ||||||
| }, | ||||||
| ColumnTypeFamily::Boolean => match SqlSchemaDescriber::parse_int(&default_string) { | ||||||
| Some(PrismaValue::Int(1)) => DefaultValue::value(true), | ||||||
| Some(PrismaValue::Int(0)) => DefaultValue::value(false), | ||||||
| _ => match SqlSchemaDescriber::parse_bool(&default_string) { | ||||||
| Some(bool_value) => DefaultValue::value(bool_value), | ||||||
| None => DefaultValue::db_generated(default_string), | ||||||
| None => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), | ||||||
| }, | ||||||
| }, | ||||||
| ColumnTypeFamily::String => { | ||||||
|
|
@@ -335,7 +347,7 @@ async fn push_columns( | |||||
| "current_timestamp" | "datetime(\'now\')" | "datetime(\'now\', \'localtime\')" => { | ||||||
| DefaultValue::now() | ||||||
| } | ||||||
| _ => DefaultValue::db_generated(default_string), | ||||||
| _ => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), | ||||||
| }, | ||||||
| ColumnTypeFamily::Json => DefaultValue::value(default_string), | ||||||
| ColumnTypeFamily::Binary => DefaultValue::db_generated(default_string), | ||||||
|
||||||
| ColumnTypeFamily::Binary => DefaultValue::db_generated(default_string), | |
| ColumnTypeFamily::Binary => DefaultValue::db_generated(wrap_in_parentheses(&default_string)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ken0x0a I think this comment is valid and the function should be applied for all types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition logic is incorrect. This checks if the first character is NOT '(' AND the last character IS ')', which is the opposite of what should be checked. This will wrap strings that end with ')' but don't start with '(', and will not wrap strings that need wrapping.
The condition should be:
!s.starts_with('(') || !s.ends_with(')')to check if either parenthesis is missing, or more likely:!(s.starts_with('(') && s.ends_with(')'))to check if the string is not already fully wrapped.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ken0x0a indeed, the condition should probably be
!(s.starts_with('(') && s.ends_with(')'))