Skip to content

Commit

Permalink
Update collation
Browse files Browse the repository at this point in the history
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 <nicolas.bock@canonical.com>
  • Loading branch information
nicolasbock committed May 6, 2024
1 parent 979326c commit 520023e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/common/db/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
7 changes: 7 additions & 0 deletions pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 520023e

Please sign in to comment.