Fix gorm newlines issue (#108)

Even if you made a custom formatter for gorm, it would blithely println
anything returned, including nils, resulting in lots of new lines when
we were ignoring output. This also creates a custom logger that doesn't
do anything when newlines are found.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-06-08 21:12:00 -04:00
1 parent 6804c98b0b
commit 5333482ff8
3 files changed
+20

No files matched your search

+1
View File
@@ -402,6 +402,7 @@ func (b *Server) CreateDevDatabase(dialect string) error {
b.Database = dbase
gorm.LogFormatter = db.GetGormLogFormatter(b.Logger)
b.Database.SetLogger(db.GetGormLogger(b.Logger))
b.Database.LogMode(true)
rw := db.New(b.Database)
+18
View File
@@ -182,3 +182,21 @@ func GetGormLogFormatter(log hclog.Logger) func(values ...interface{}) (messages
return nil
}
}
type gormLogger struct {
logger hclog.Logger
}
func (g gormLogger) Print(values ...interface{}) {
formatted := gorm.LogFormatter(values...)
if formatted == nil {
return
}
// Our formatter should elide anything we don't want so this should never
// happen, panic if so so we catch/fix
panic("unhandled error case")
}
func GetGormLogger(log hclog.Logger) gormLogger {
return gormLogger{logger: log}
}
+1
View File
@@ -684,6 +684,7 @@ func TestDb_SearchWhere(t *testing.T) {
Level: hclog.Trace,
})
gorm.LogFormatter = GetGormLogFormatter(log)
db.SetLogger(GetGormLogger(log))
db.LogMode(true)
defer func() {
assert.True(strings.Contains(buf.String(), "syntax error at or near"))