fix: use MySQL-compatible CREATE INDEX in migration 20260224215050

MySQL does not support CREATE INDEX IF NOT EXISTS syntax. Switch on
database type to use IF NOT EXISTS for Postgres/SQLite and plain
CREATE INDEX with duplicate key error suppression for MySQL.

Fixes #2431
This commit is contained in:
kolaente 2026-03-23 17:14:49 +01:00 committed by kolaente
parent c1418c1619
commit 867c52745f
1 changed files with 16 additions and 2 deletions

View File

@ -17,10 +17,14 @@
package migration
import (
"strings"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
func init() {
@ -38,8 +42,18 @@ func init() {
}
}
if _, err = tx.Exec("CREATE INDEX IF NOT EXISTS IDX_webhooks_user_id ON webhooks (user_id)"); err != nil {
return err
var indexQuery string
switch db.Type() {
case schemas.POSTGRES, schemas.SQLITE:
indexQuery = "CREATE INDEX IF NOT EXISTS IDX_webhooks_user_id ON webhooks (user_id)"
case schemas.MYSQL:
indexQuery = "CREATE INDEX IDX_webhooks_user_id ON webhooks (user_id)"
}
if _, err = tx.Exec(indexQuery); err != nil {
// For MySQL, ignore duplicate key name error (Error 1061)
if !strings.Contains(err.Error(), "Error 1061") && !strings.Contains(err.Error(), "Duplicate key name") {
return err
}
}
// Make project_id nullable so user-level webhooks can have NULL project_id.