-
-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy path20260903104500.go
More file actions
73 lines (62 loc) · 2.38 KB
/
Copy path20260903104500.go
File metadata and controls
73 lines (62 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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 License 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 License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package migration
import (
"fmt"
"code.vikunja.io/api/pkg/db"
"src.techknowlogick.com/xormigrate"
"xorm.io/builder"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
type projects20260903104500 struct {
ParentProjectID *int64 `xorm:"bigint INDEX null"`
}
func (projects20260903104500) TableName() string {
return "projects"
}
func rootProjectsParentToNull20260903104500(tx *xorm.Engine) error {
_, err := tx.
Where(builder.Eq{"parent_project_id": 0}).
Cols("parent_project_id").
Nullable("parent_project_id").
Update(&projects20260903104500{})
if err != nil {
return fmt.Errorf("could not set the parent of top-level projects to null: %w", err)
}
// MySQL has no partial indexes; it keeps using IDX_projects_parent_project_id.
if db.Type() == schemas.MYSQL {
return nil
}
// The recursive access CTE joins parent_project_id against a project id, which implies
// NOT NULL, so the planner can walk this index over the real children only instead of
// the full one where the root rows dominate.
_, err = tx.Exec("CREATE INDEX IF NOT EXISTS IDX_projects_parent_project_id_children ON projects (parent_project_id) WHERE parent_project_id IS NOT NULL")
if err != nil {
return fmt.Errorf("could not create the partial index on projects.parent_project_id: %w", err)
}
return nil
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20260903104500",
Description: "Store the parent of top-level projects as null and index only real children",
Migrate: rootProjectsParentToNull20260903104500,
Rollback: func(_ *xorm.Engine) error {
return nil
},
})
}