diff --git a/models/activities/action.go b/models/activities/action.go index 7ac050d79f..8fd7709e81 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -145,37 +145,49 @@ func (at ActionType) InActions(actions ...string) bool { // used in template render. type Action struct { ID int64 `xorm:"pk autoincr"` - UserID int64 `xorm:"INDEX"` // Receiver user id. + UserID int64 // Receiver user id. OpType ActionType ActUserID int64 // Action user id. ActUser *user_model.User `xorm:"-"` RepoID int64 Repo *repo_model.Repository `xorm:"-"` - CommentID int64 `xorm:"INDEX"` + CommentID int64 `xorm:"INDEX"` // indexed to support `DeleteIssueActions` Comment *issues_model.Comment `xorm:"-"` Issue *issues_model.Issue `xorm:"-"` // get the issue id from content RefName string IsPrivate bool `xorm:"NOT NULL DEFAULT false"` Content string `xorm:"TEXT"` - CreatedUnix timeutil.TimeStamp `xorm:"created"` + CreatedUnix timeutil.TimeStamp `xorm:"created INDEX"` // indexed to support `DeleteOldActions` } func init() { db.RegisterModel(new(Action)) } -// TableIndices implements xorm's TableIndices interface +// TableIndices implements xorm's TableIndices interface. It is used here to ensure indexes with specified column order +// are created, which can't be created through xorm tags on the struct. func (a *Action) TableIndices() []*schemas.Index { - repoIndex := schemas.NewIndex("r_u", schemas.IndexType) - repoIndex.AddColumn("repo_id", "user_id") - + // Index to support getUserHeatmapData, which searches for data that is visible-to (user_id) and performed-by + // (act_user_id) a user, but only includes visible repos (repo_id). actUserIndex := schemas.NewIndex("au_r_c_u", schemas.IndexType) actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id") - cudIndex := schemas.NewIndex("c_u", schemas.IndexType) - cudIndex.AddColumn("created_unix", "user_id") + // GetFeeds is a common access point to Action and requires that all action feeds be queried based upon one of + // user_id (opts.RequestedUser), repo_id (opts.RequestedTeam... kinda), and/or repo_id (opts.RequestedRepo), and + // then the results are ordered by created_unix and paginated. The most efficient indexes to support those queries + // are: + requestedUser := schemas.NewIndex("user_id_created_unix", schemas.IndexType) + requestedUser.AddColumn("user_id", "created_unix") + requestedRepo := schemas.NewIndex("repo_id_created_unix", schemas.IndexType) + requestedRepo.AddColumn("repo_id", "created_unix") - indices := []*schemas.Index{actUserIndex, repoIndex, cudIndex} + // To support `DeleteIssueActions` search for createissue / createpullrequest actions; this isn't a great search + // because `DeleteIssueActions` searches by `content` as well, but it should be sufficient performance-wise for + // infrequent deleting of issues. + repoOpType := schemas.NewIndex("repo_id_op_type", schemas.IndexType) + repoOpType.AddColumn("repo_id", "op_type") + + indices := []*schemas.Index{actUserIndex, requestedUser, requestedRepo, repoOpType} return indices } diff --git a/models/forgejo_migrations/index_utils.go b/models/forgejo_migrations/index_utils.go new file mode 100644 index 0000000000..dda08e4018 --- /dev/null +++ b/models/forgejo_migrations/index_utils.go @@ -0,0 +1,52 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package forgejo_migrations + +import ( + "fmt" + "strings" + + "forgejo.org/modules/setting" + + "xorm.io/xorm" +) + +func dropIndexIfExists(x *xorm.Engine, tableName, indexName string) error { + switch { + case setting.Database.Type.IsSQLite3(), setting.Database.Type.IsPostgreSQL(): + if _, err := x.Exec(fmt.Sprintf("DROP INDEX IF EXISTS %s", x.Quote(indexName))); err != nil { + return err + } + + case setting.Database.Type.IsMySQL(): + exists, err := indexExists(x, tableName, indexName) + if err != nil { + return err + } + + if exists { + if _, err := x.Exec(fmt.Sprintf("DROP INDEX %s ON %s", x.Quote(indexName), x.Quote(tableName))); err != nil { + return err + } + } + default: + return fmt.Errorf("unsupported db dialect type %v", x.Dialect().URI().DBType) + } + + return nil +} + +func indexExists(x *xorm.Engine, tableName, indexName string) (bool, error) { + switch { + case setting.Database.Type.IsSQLite3(): + return x.SQL("SELECT name FROM sqlite_master WHERE type = 'index' and name = ?", indexName).Exist() + case setting.Database.Type.IsPostgreSQL(): + return x.SQL("SELECT indexname FROM pg_indexes WHERE schemaname = ? AND tablename = ? AND indexname = ?", setting.Database.Schema, tableName, indexName).Exist() + case setting.Database.Type.IsMySQL(): + databaseName := strings.SplitN(setting.Database.Name, "?", 2)[0] + return x.SQL("SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME` = ?", databaseName, tableName, indexName).Exist() + } + + return false, fmt.Errorf("unsupported db dialect type %v", x.Dialect().URI().DBType) +} diff --git a/models/forgejo_migrations/index_utils_test.go b/models/forgejo_migrations/index_utils_test.go new file mode 100644 index 0000000000..a7ae3700e5 --- /dev/null +++ b/models/forgejo_migrations/index_utils_test.go @@ -0,0 +1,62 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package forgejo_migrations + +import ( + "testing" + + migration_tests "forgejo.org/models/gitea_migrations/test" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDropIndexIfExists(t *testing.T) { + type Table struct { + ID int64 `xorm:"pk"` + DoerID int64 `xorm:"INDEX INDEX(s)"` + OwnerID int64 `xorm:"INDEX"` + RepoID int64 `xorm:"INDEX(s)"` + } + + x, deferable := migration_tests.PrepareTestEnv(t, 0, new(Table)) + x.ShowSQL(true) + defer deferable() + if x == nil || t.Failed() { + return + } + + exists, err := indexExists(x, "table", "IDX_table_doer_id") + require.NoError(t, err) + assert.True(t, exists) + + exists, err = indexExists(x, "table", "IDX_table_owner_id") + require.NoError(t, err) + assert.True(t, exists) + + exists, err = indexExists(x, "table", "IDX_table_repo_id") + require.NoError(t, err) + assert.False(t, exists) + + exists, err = indexExists(x, "table", "IDX_table_s") + require.NoError(t, err) + assert.True(t, exists) + + require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_repo_id")) + + require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_doer_id")) + exists, err = indexExists(x, "table", "IDX_table_doer_id") + require.NoError(t, err) + assert.False(t, exists) + + require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_s")) + exists, err = indexExists(x, "table", "IDX_table_s") + require.NoError(t, err) + assert.False(t, exists) + + require.NoError(t, dropIndexIfExists(x, "table", "IDX_table_owner_id")) + exists, err = indexExists(x, "table", "IDX_table_owner_id") + require.NoError(t, err) + assert.False(t, exists) +} diff --git a/models/forgejo_migrations/v14b_action-reindexing.go b/models/forgejo_migrations/v14b_action-reindexing.go new file mode 100644 index 0000000000..6b5608a5d5 --- /dev/null +++ b/models/forgejo_migrations/v14b_action-reindexing.go @@ -0,0 +1,73 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package forgejo_migrations + +import ( + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +func init() { + registerMigration(&Migration{ + Description: "rework indexes on table action", + Upgrade: reworkActionIndexes, + }) +} + +type v14bAction struct { + ID int64 `xorm:"pk autoincr"` + UserID int64 // Receiver user id. + OpType int + ActUserID int64 // Action user id. + RepoID int64 + CommentID int64 `xorm:"INDEX"` // indexed to support `DeleteIssueActions` + CreatedUnix int64 `xorm:"created INDEX"` // indexed to support `DeleteOldActions` +} + +// TableName sets the name of this table +func (a *v14bAction) TableName() string { + return "action" +} + +// TableIndices implements xorm's TableIndices interface. It is used here to ensure indexes with specified column order +// are created, which can't be created through xorm tags on the struct. +func (a *v14bAction) TableIndices() []*schemas.Index { + // Index to support getUserHeatmapData, which searches for data that is visible-to (user_id) and performed-by + // (act_user_id) a user, but only includes visible repos (repo_id). + actUserIndex := schemas.NewIndex("au_r_c_u", schemas.IndexType) + actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id") + + // GetFeeds is a common access point to Action and requires that all action feeds be queried based upon one of + // user_id (opts.RequestedUser), repo_id (opts.RequestedTeam... kinda), and/or repo_id (opts.RequestedRepo), and + // then the results are ordered by created_unix and paginated. The most efficient indexes to support those queries + // are: + requestedUser := schemas.NewIndex("user_id_created_unix", schemas.IndexType) + requestedUser.AddColumn("user_id", "created_unix") + requestedRepo := schemas.NewIndex("repo_id_created_unix", schemas.IndexType) + requestedRepo.AddColumn("repo_id", "created_unix") + + // To support `DeleteIssueActions` search for createissue / createpullrequest actions; this isn't a great search + // because `DeleteIssueActions` searches by `content` as well, but it should be sufficient performance-wise for + // infrequent deleting of issues. + repoOpType := schemas.NewIndex("repo_id_op_type", schemas.IndexType) + repoOpType.AddColumn("repo_id", "op_type") + + indices := []*schemas.Index{actUserIndex, requestedUser, requestedRepo, repoOpType} + + return indices +} + +func reworkActionIndexes(x *xorm.Engine) error { + if err := dropIndexIfExists(x, "action", "IDX_action_c_u"); err != nil { + return err + } + if err := dropIndexIfExists(x, "action", "IDX_action_r_u"); err != nil { + return err + } + if err := dropIndexIfExists(x, "action", "IDX_action_user_id"); err != nil { + return err + } + + return x.Sync(new(v14bAction)) +}