From d7f76104aa6b9e6f556c8fda384a3403d61a8603 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Tue, 25 May 2021 18:27:36 +0200 Subject: [PATCH] Fix index creation (add if-not-exists) & query logging --- store/mysql/upgrade.go | 1 + store/rdbms/ddl/gen.go | 3 ++- store/rdbms/instrumentation/debug.go | 11 ++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/store/mysql/upgrade.go b/store/mysql/upgrade.go index 39d3ac529..8a1c6c49d 100644 --- a/store/mysql/upgrade.go +++ b/store/mysql/upgrade.go @@ -39,6 +39,7 @@ func NewUpgrader(log *zap.Logger, store *Store) *upgrader { `{{ if not .Condition }}CREATE {{ if .Unique }}UNIQUE {{ end }}INDEX {{ template "index-name" . }} ON {{ .Table }} {{ template "index-fields" .Fields }}{{ else }}SELECT 1 -- dummy sql, just to prevent "empty query" errors...{{ end }}`, ) + u.ddl.AddTemplate("if-not-exists-clause", "") u.ddl.AddTemplate("index-fields", ` ({{ range $n, $f := . -}} {{ if $n }}, {{ end }} diff --git a/store/rdbms/ddl/gen.go b/store/rdbms/ddl/gen.go index 7b1b62700..cf34d889a 100644 --- a/store/rdbms/ddl/gen.go +++ b/store/rdbms/ddl/gen.go @@ -32,6 +32,7 @@ const ( {{- end }} ) {{- template "create-table-suffix" . -}} ` + genericCreateTableSuffix = `` genericCreateTableColumn = ` @@ -62,7 +63,7 @@ const ( {{- end }}) ` // table/index exist or not clause - genericIfNotExistsClause = `` + genericIfNotExistsClause = `IF NOT EXISTS` ) func NewGenerator(log *zap.Logger) *Generator { diff --git a/store/rdbms/instrumentation/debug.go b/store/rdbms/instrumentation/debug.go index 15f493e96..f4b2228ad 100644 --- a/store/rdbms/instrumentation/debug.go +++ b/store/rdbms/instrumentation/debug.go @@ -61,7 +61,16 @@ func (debug) argToZapFields(args []driver.NamedValue) []zap.Field { name += fmt.Sprintf("%d", args[i].Ordinal) } - out[i] = zap.Any(name, args[i].Value) + // Catch time to ensure proper format + switch args[i].Value.(type) { + case time.Time: + out[i] = zap.String(name, args[i].Value.(time.Time).Format(time.RFC3339)) + case *time.Time: + out[i] = zap.String(name, (args[i].Value.(*time.Time)).Format(time.RFC3339)) + default: + out[i] = zap.Any(name, args[i].Value) + } + } return out