@@ -270,6 +270,18 @@ impl<'a> SqlSchemaDescriber<'a> {
270270 }
271271}
272272
273+ /// Wraps the given string in parentheses if it is not already.
274+ /// The result of `PRAGMA table_info` removes parentheses from default values,
275+ /// so we need to add parentheses when generating prisma schema.
276+ /// See <https://github.com/prisma/prisma/issues/29064>
277+ fn wrap_in_parentheses ( s : & str ) -> Cow < ' _ , str > {
278+ if s[ 0 ..1 ] != * "(" && s[ s. len ( ) - 1 ..] == * ")" {
279+ format ! ( "({})" , s) . into ( )
280+ } else {
281+ s. into ( )
282+ }
283+ }
284+
273285async fn push_columns (
274286 table_name : & str ,
275287 container_id : Either < TableId , ViewId > ,
@@ -306,26 +318,26 @@ async fn push_columns(
306318 Some ( match & tpe. family {
307319 ColumnTypeFamily :: Int => match SqlSchemaDescriber :: parse_int ( & default_string) {
308320 Some ( int_value) => DefaultValue :: value ( int_value) ,
309- None => DefaultValue :: db_generated ( default_string) ,
321+ None => DefaultValue :: db_generated ( wrap_in_parentheses ( & default_string) ) ,
310322 } ,
311323 ColumnTypeFamily :: BigInt => match SqlSchemaDescriber :: parse_big_int ( & default_string) {
312324 Some ( int_value) => DefaultValue :: value ( int_value) ,
313- None => DefaultValue :: db_generated ( default_string) ,
325+ None => DefaultValue :: db_generated ( wrap_in_parentheses ( & default_string) ) ,
314326 } ,
315327 ColumnTypeFamily :: Float => match SqlSchemaDescriber :: parse_float ( & default_string) {
316328 Some ( float_value) => DefaultValue :: value ( float_value) ,
317- None => DefaultValue :: db_generated ( default_string) ,
329+ None => DefaultValue :: db_generated ( wrap_in_parentheses ( & default_string) ) ,
318330 } ,
319331 ColumnTypeFamily :: Decimal => match SqlSchemaDescriber :: parse_float ( & default_string) {
320332 Some ( float_value) => DefaultValue :: value ( float_value) ,
321- None => DefaultValue :: db_generated ( default_string) ,
333+ None => DefaultValue :: db_generated ( wrap_in_parentheses ( & default_string) ) ,
322334 } ,
323335 ColumnTypeFamily :: Boolean => match SqlSchemaDescriber :: parse_int ( & default_string) {
324336 Some ( PrismaValue :: Int ( 1 ) ) => DefaultValue :: value ( true ) ,
325337 Some ( PrismaValue :: Int ( 0 ) ) => DefaultValue :: value ( false ) ,
326338 _ => match SqlSchemaDescriber :: parse_bool ( & default_string) {
327339 Some ( bool_value) => DefaultValue :: value ( bool_value) ,
328- None => DefaultValue :: db_generated ( default_string) ,
340+ None => DefaultValue :: db_generated ( wrap_in_parentheses ( & default_string) ) ,
329341 } ,
330342 } ,
331343 ColumnTypeFamily :: String => {
@@ -335,7 +347,7 @@ async fn push_columns(
335347 "current_timestamp" | "datetime(\' now\' )" | "datetime(\' now\' , \' localtime\' )" => {
336348 DefaultValue :: now ( )
337349 }
338- _ => DefaultValue :: db_generated ( default_string) ,
350+ _ => DefaultValue :: db_generated ( wrap_in_parentheses ( & default_string) ) ,
339351 } ,
340352 ColumnTypeFamily :: Json => DefaultValue :: value ( default_string) ,
341353 ColumnTypeFamily :: Binary => DefaultValue :: db_generated ( default_string) ,
0 commit comments