Fix index creation (add if-not-exists) & query logging
This commit is contained in:
@@ -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 }}
|
||||
|
||||
@@ -22,7 +22,7 @@ type (
|
||||
const (
|
||||
// table creation
|
||||
genericCreateTable = `
|
||||
CREATE TABLE {{ .Name }} (
|
||||
CREATE TABLE {{template "if-not-exists-clause" .}} {{ .Name }} (
|
||||
{{ range $n, $c := .Columns -}}
|
||||
{{ if $n }}, {{ else }} {{ end }}{{ template "create-table-column" . }}
|
||||
{{ end -}}
|
||||
@@ -45,7 +45,7 @@ CREATE TABLE {{ .Name }} (
|
||||
genericRenameColumn = `ALTER TABLE {{ .Table }} RENAME COLUMN {{ .OldName }} TO {{ .NewName }}`
|
||||
|
||||
// index creation
|
||||
genericCreateIndex = `CREATE {{ if .Unique }}UNIQUE {{ end }}INDEX {{ template "index-name" . }} ON {{ .Table }} {{ template "index-fields" .Fields }}{{ template "index-condition" . }}`
|
||||
genericCreateIndex = `CREATE {{ if .Unique }}UNIQUE {{ end }}INDEX {{ template "if-not-exists-clause" . }} {{ template "index-name" . }} ON {{ .Table }} {{ template "index-fields" .Fields }}{{ template "index-condition" . }}`
|
||||
|
||||
genericIndexName = `{{ .Table }}_{{ .Name }}`
|
||||
genericIndexCondition = `{{- if .Condition }} WHERE ({{ .Condition }}){{ end }}`
|
||||
@@ -60,6 +60,9 @@ CREATE TABLE {{ .Name }} (
|
||||
{{- if .Desc }} DESC{{ end }}
|
||||
{{- end }})
|
||||
`
|
||||
|
||||
// table/index exist or not clause
|
||||
genericIfNotExistsClause = `IF NOT EXISTS`
|
||||
)
|
||||
|
||||
func NewGenerator(log *zap.Logger) *Generator {
|
||||
@@ -76,6 +79,7 @@ func NewGenerator(log *zap.Logger) *Generator {
|
||||
g.AddTemplate("index-condition", genericIndexCondition)
|
||||
g.AddTemplate("index-name", genericIndexName)
|
||||
g.AddTemplate("index-fields", genericIndexFields)
|
||||
g.AddTemplate("if-not-exists-clause", genericIfNotExistsClause)
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
@@ -5,10 +5,11 @@ import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/ngrok/sqlmw"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -62,7 +63,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
|
||||
|
||||
Reference in New Issue
Block a user