From 520023e9d07cee1f399fc9f596bddc4b39c92fae Mon Sep 17 00:00:00 2001 From: Nicolas Bock Date: Mon, 6 May 2024 12:13:29 -0600 Subject: [PATCH] Update collation Files.com does not always return a UTF-8 encoded path. This leads to mysql errors because the corresponding field in the database are UTF-8 formatted. This change modifies the field such that encodings other than UTF-8 can be compared with the field. Closes: SET-600 Signed-off-by: Nicolas Bock --- pkg/common/db/conn.go | 12 ++++++++++++ pkg/monitor/monitor.go | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/common/db/conn.go b/pkg/common/db/conn.go index 2a4bcd1..4f1f2ce 100644 --- a/pkg/common/db/conn.go +++ b/pkg/common/db/conn.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/canonical/athena-core/pkg/config" + log "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/driver/sqlite" "gorm.io/gorm" @@ -25,5 +26,16 @@ func GetDBConn(cfg *config.Config) (*gorm.DB, error) { } dbInstance.AutoMigrate(File{}, Report{}, Script{}) + + switch cfg.Db.Dialect { + case "sqlite": + log.Debugln("Will not change collation") + case "mysql": + log.Debugln("Changing collation to UTF-8") + err = dbInstance.Exec("ALTER TABLE files MODIFY Path VARCHAR(10240) CHARACTER SET utf8 COLLATE utf8_general_ci").Error + if err != nil { + log.Errorln("Could not change collation of files table") + } + } return dbInstance, nil } diff --git a/pkg/monitor/monitor.go b/pkg/monitor/monitor.go index 31426dd..21354c9 100644 --- a/pkg/monitor/monitor.go +++ b/pkg/monitor/monitor.go @@ -62,6 +62,13 @@ func (m *Monitor) GetLatestFiles(dirs []string, duration time.Duration) ([]db.Fi } for _, file := range files { + // var count int64 + // m.Db.Raw("SELECT COUNT(*) FROM files WHERE path COLLATE utf8_general_ci = ?", file.Path).Scan(&count) + // if count == 0 { + // m.Db.Exec("INSERT INTO files (path) VALUES (?)", file.Path) + // } else { + // m.Db.Raw("SELECT * FROM files WHERE path COLLATE utf8_general_ci = ?", file.Path).Scan(&file) + // } m.Db.Where(db.File{Path: file.Path}).FirstOrCreate(&file) }