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 special case for now() string #2360

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,31 @@ where
},
},
},
{
Name: "preserve now()",
SetUpScript: []string{
"create table t1 (i int default (cast(now() as signed)));",
"create table t2 (i int default (cast(current_timestamp(6) as signed)));",
},
Assertions: []ScriptTestAssertion{
{
Query: "show create table t1",
Expected: []sql.Row{
{"t1", "CREATE TABLE `t1` (\n" +
" `i` int DEFAULT (convert(NOW(), signed))\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
},
},
{
Query: "show create table t2",
Expected: []sql.Row{
{"t2", "CREATE TABLE `t2` (\n" +
" `i` int DEFAULT (convert(NOW(6), signed))\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
19 changes: 14 additions & 5 deletions sql/columndefault.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package sql

import (
"fmt"
"strings"
)

// ColumnDefaultValue is an expression representing the default value of a column. May represent both a default literal
Expand Down Expand Up @@ -151,11 +150,21 @@ func (e *ColumnDefaultValue) String() string {
}

// There's a special case for NOW()
if str == "NOW()" || str == "NOW(0)" {
switch str {
case "NOW()", "NOW(0)":
return "CURRENT_TIMESTAMP"
}
if strings.Contains(str, "NOW(") {
return strings.Replace(str, "NOW(", "CURRENT_TIMESTAMP(", 1)
case "NOW(1)":
return "CURRENT_TIMESTAMP(1)"
case "NOW(2)":
return "CURRENT_TIMESTAMP(2)"
case "NOW(3)":
return "CURRENT_TIMESTAMP(3)"
case "NOW(4)":
return "CURRENT_TIMESTAMP(4)"
case "NOW(5)":
return "CURRENT_TIMESTAMP(5)"
case "NOW(6)":
return "CURRENT_TIMESTAMP(6)"
}
return fmt.Sprintf("(%s)", str)
}
Expand Down
Loading