Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: on update timestamp (#11065)

Danny Xu683d38c036b1
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.go
index fc21b6bfff..3b2cace003 100644
--- a/backend/plugin/schema/mysql/get_design_schema.go
+++ b/backend/plugin/schema/mysql/get_design_schema.go
@@ -924,7 +924,7 @@ func normalizeOnUpdate(s string) string {
re := regexp.MustCompile(`(current_timestamp|now|localtime|localtimestamp)(?:\((\d+)\))?`)
match := re.FindStringSubmatch(lowerS)
if len(match) > 0 {
- if len(match) > 1 && match[1] != "" {
+ if len(match) >= 3 && match[2] != "" {
// has precision
return fmt.Sprintf("CURRENT_TIMESTAMP(%s)", match[2])
}
diff --git a/backend/plugin/schema/mysql/get_design_schema_test.go b/backend/plugin/schema/mysql/get_design_schema_test.go
index a6bd6f9af8..9314c80040 100644
--- a/backend/plugin/schema/mysql/get_design_schema_test.go
+++ b/backend/plugin/schema/mysql/get_design_schema_test.go
@@ -67,3 +67,27 @@ func TestGetDesignSchema(t *testing.T) {
}
}
}
+
+func TestNormalizeOnUpdate(t *testing.T) {
+ tests := []struct {
+ s string
+ want string
+ }{
+ {
+ s: "current_timestamp(6)",
+ want: "CURRENT_TIMESTAMP(6)",
+ },
+ {
+ s: "current_timestamp",
+ want: "CURRENT_TIMESTAMP",
+ },
+ {
+ s: "hello",
+ want: "hello",
+ },
+ }
+ for _, tc := range tests {
+ got := normalizeOnUpdate(tc.s)
+ require.Equal(t, tc.want, got, tc.s)
+ }
+}
diff --git a/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml b/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml
index 173cd7ac7f..a88267903b 100644
--- a/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml
+++ b/backend/plugin/schema/mysql/testdata/get-design-schema/get_design_schema.yaml
@@ -708,7 +708,7 @@
result: |
CREATE TABLE `t` (
`c` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
- `d` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(),
+ `d` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`e` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`a` int NOT NULL,
`b` varchar(255) NOT NULL