3
0

Fix index creation (add if-not-exists) & query logging

This commit is contained in:
Denis Arh
2021-05-25 18:27:36 +02:00
parent 2dbf536faa
commit d7f76104aa
3 changed files with 13 additions and 2 deletions

View File

@@ -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 }}

View File

@@ -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 {

View File

@@ -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