From a70c472aa3ebe4a50fa1c241f848e0a3d2b270af Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 28 Oct 2024 13:25:38 +0100 Subject: [PATCH] fix: create missing indexes on postgres --- pkg/events/events.go | 3 +- pkg/migration/20241028131622.go | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 pkg/migration/20241028131622.go diff --git a/pkg/events/events.go b/pkg/events/events.go index 42021047a..e622076f0 100644 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -20,9 +20,10 @@ import ( "context" "encoding/json" "fmt" - "github.com/getsentry/sentry-go" "time" + "github.com/getsentry/sentry-go" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" vmetrics "code.vikunja.io/api/pkg/metrics" diff --git a/pkg/migration/20241028131622.go b/pkg/migration/20241028131622.go new file mode 100644 index 000000000..459df212c --- /dev/null +++ b/pkg/migration/20241028131622.go @@ -0,0 +1,66 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package migration + +import ( + "code.vikunja.io/api/pkg/db" + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20241028131622", + Description: "Add potentially missing indexes", + Migrate: func(tx *xorm.Engine) error { + + var queries []string + + switch db.Type() { + case schemas.POSTGRES: + queries = []string{ + "CREATE INDEX IF NOT EXISTS IDX_projects_owner_id ON projects (owner_id)", + "CREATE INDEX IF NOT EXISTS IDX_projects_parent_project_id ON projects (parent_project_id)", + } + case schemas.MYSQL: + queries = []string{ + "CREATE INDEX IDX_projects_owner_id ON projects (owner_id)", + "CREATE INDEX IDX_projects_parent_project_id ON projects (parent_project_id)", + } + case schemas.SQLITE: + queries = []string{ + "CREATE INDEX IF NOT EXISTS IDX_projects_owner_id ON projects (owner_id)", + "CREATE INDEX IF NOT EXISTS IDX_projects_parent_project_id ON projects (parent_project_id)", + } + } + + for _, query := range queries { + _, err := tx.Exec(query) + if err != nil && err.Error() != "Error 1061: Duplicate key name 'IDX_projects_owner_id'" && + err.Error() != "Error 1061: Duplicate key name 'IDX_projects_parent_project_id'" { + return err + } + } + + return nil + }, + Rollback: func(tx *xorm.Engine) error { + return nil + }, + }) +}