Skip to content

Commit 4d6087d

Browse files
committed
fix: enforce case-sensitive model name matching in MySQL
- Add case-sensitive collation (utf8mb4_bin) to model_name and ability.model columns - Update IsModelNameDuplicated to use BINARY comparison for MySQL - Add automatic migration to update existing MySQL databases - PostgreSQL and SQLite remain case-sensitive by default Fixes #1990
1 parent 5f5b942 commit 4d6087d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

model/main.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,45 @@ func InitLogDB() (err error) {
246246
return err
247247
}
248248

249+
// fixModelNameCollation updates the model_name column to use case-sensitive collation in MySQL
250+
func fixModelNameCollation() error {
251+
if !common.UsingMySQL {
252+
return nil // Only needed for MySQL
253+
}
254+
255+
// Fix models.model_name column
256+
var modelCollation string
257+
err := DB.Raw("SELECT COLLATION_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'models' AND COLUMN_NAME = 'model_name'").Scan(&modelCollation).Error
258+
if err != nil {
259+
// Table might not exist yet, ignore error
260+
common.SysLog(fmt.Sprintf("Could not check models.model_name collation: %v", err))
261+
} else if !strings.Contains(strings.ToLower(modelCollation), "_bin") {
262+
common.SysLog("Updating models.model_name column to use case-sensitive collation (utf8mb4_bin)")
263+
err = DB.Exec("ALTER TABLE models MODIFY COLUMN model_name VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL").Error
264+
if err != nil {
265+
return fmt.Errorf("failed to alter models.model_name column collation: %v", err)
266+
}
267+
common.SysLog("Successfully updated models.model_name column collation")
268+
}
269+
270+
// Fix abilities.model column
271+
var abilityCollation string
272+
err = DB.Raw("SELECT COLLATION_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'abilities' AND COLUMN_NAME = 'model'").Scan(&abilityCollation).Error
273+
if err != nil {
274+
// Table might not exist yet, ignore error
275+
common.SysLog(fmt.Sprintf("Could not check abilities.model collation: %v", err))
276+
} else if !strings.Contains(strings.ToLower(abilityCollation), "_bin") {
277+
common.SysLog("Updating abilities.model column to use case-sensitive collation (utf8mb4_bin)")
278+
err = DB.Exec("ALTER TABLE abilities MODIFY COLUMN model VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL").Error
279+
if err != nil {
280+
return fmt.Errorf("failed to alter abilities.model column collation: %v", err)
281+
}
282+
common.SysLog("Successfully updated abilities.model column collation")
283+
}
284+
285+
return nil
286+
}
287+
249288
func migrateDB() error {
250289
err := DB.AutoMigrate(
251290
&Channel{},
@@ -270,6 +309,13 @@ func migrateDB() error {
270309
if err != nil {
271310
return err
272311
}
312+
313+
// Fix model_name column collation for case-sensitive comparison in MySQL
314+
if err := fixModelNameCollation(); err != nil {
315+
common.SysLog(fmt.Sprintf("Warning: failed to update model_name collation: %v", err))
316+
// Don't return error, just log it as it's not critical for existing installations
317+
}
318+
273319
return nil
274320
}
275321

model/model_meta.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ func IsModelNameDuplicated(id int, name string) (bool, error) {
5454
return false, nil
5555
}
5656
var cnt int64
57-
err := DB.Model(&Model{}).Where("model_name = ? AND id <> ?", name, id).Count(&cnt).Error
57+
var err error
58+
// Use case-sensitive comparison for MySQL, PostgreSQL and SQLite are case-sensitive by default
59+
if common.UsingMySQL {
60+
err = DB.Model(&Model{}).Where("BINARY model_name = ? AND id <> ?", name, id).Count(&cnt).Error
61+
} else {
62+
err = DB.Model(&Model{}).Where("model_name = ? AND id <> ?", name, id).Count(&cnt).Error
63+
}
5864
return cnt > 0, err
5965
}
6066

0 commit comments

Comments
 (0)