Skip to content

Commit

Permalink
sql/mysql: wrap default expression on inspection (#797)
Browse files Browse the repository at this point in the history
* try a fix

* add same changes for mysql as well

* skip CURRENT_TIMESTAMP cases

* change test cases for mariadb

* improve comment for clarity

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
  • Loading branch information
ofpiyush and a8m committed May 15, 2022
1 parent 91531de commit 7c40300
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions internal/integration/mysql_test.go
Expand Up @@ -612,7 +612,7 @@ table "users" {
}

func TestMySQL_CLI_MultiSchema(t *testing.T) {
h := `
h := `
schema "test" {
charset = "%s"
collation = "%s"
Expand Down Expand Up @@ -844,7 +844,7 @@ create table atlas_types_sanity
Default: &schema.RawExpr{
X: func() string {
if t.mariadb() {
return "current_timestamp()"
return "(current_timestamp())"
}
return "CURRENT_TIMESTAMP"
}(),
Expand All @@ -857,7 +857,7 @@ create table atlas_types_sanity
Default: &schema.RawExpr{
X: func() string {
if t.mariadb() {
return "current_timestamp(6)"
return "(current_timestamp(6))"
}
return "CURRENT_TIMESTAMP(6)"
}(),
Expand All @@ -870,7 +870,7 @@ create table atlas_types_sanity
Default: &schema.RawExpr{
X: func() string {
if t.mariadb() {
return "current_timestamp()"
return "(current_timestamp())"
}
return "CURRENT_TIMESTAMP"
}(),
Expand All @@ -892,7 +892,7 @@ create table atlas_types_sanity
Default: &schema.RawExpr{
X: func() string {
if t.mariadb() {
return "current_timestamp(6)"
return "(current_timestamp(6))"
}
return "CURRENT_TIMESTAMP(6)"
}(),
Expand Down
8 changes: 6 additions & 2 deletions sql/mysql/inspect.go
Expand Up @@ -542,7 +542,11 @@ var reCurrTimestamp = regexp.MustCompile(`(?i)^current_timestamp(?:\(\d?\))?$`)
func (i *inspect) myDefaultExpr(c *schema.Column, x string, attr *extraAttr) schema.Expr {
// In MySQL, the DEFAULT_GENERATED indicates the column has an expression default value.
if i.supportsExprDefault() && attr.defaultGenerated {
return &schema.RawExpr{X: x}
// Skip CURRENT_TIMESTAMP, because wrapping it with parens will translate it to now().
if _, ok := c.Type.Type.(*schema.TimeType); ok && reCurrTimestamp.MatchString(x) {
return &schema.RawExpr{X: x}
}
return &schema.RawExpr{X: sqlx.MayWrap(x)}
}
switch c.Type.Type.(type) {
case *schema.BinaryType:
Expand Down Expand Up @@ -626,7 +630,7 @@ func (i *inspect) marDefaultExpr(c *schema.Column, x string) schema.Expr {
if !i.supportsExprDefault() {
return &schema.Literal{V: quote(x)}
}
return &schema.RawExpr{X: x}
return &schema.RawExpr{X: sqlx.MayWrap(x)}
}

func (i *inspect) querySchema(ctx context.Context, query string, s *schema.Schema) (*sql.Rows, error) {
Expand Down

0 comments on commit 7c40300

Please sign in to comment.