From a3977968d5b6a8df2fa5ca4857b11e9f879242c9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 24 Sep 2024 15:07:04 -0700 Subject: [PATCH 1/4] Don't join repository when loading action table data --- models/activities/action.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/activities/action.go b/models/activities/action.go index 532667d495798..96110cb3a6be0 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -455,8 +455,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err if opts.Page < 10 { // TODO: why it's 10 but other values? It's an experience value. sess := db.GetEngine(ctx).Where(cond). - Select("`action`.*"). // this line will avoid select other joined table's columns - Join("INNER", "repository", "`repository`.id = `action`.repo_id") + Select("`action`.*") // this line will avoid select other joined table's columns opts.SetDefaultValues() sess = db.SetSessionPagination(sess, &opts) From dc99357f14ab866654618f0bd340e2349f01f0f5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 24 Sep 2024 15:26:33 -0700 Subject: [PATCH 2/4] Ignore corrupted data for action table when getting feed, users should fix the database themselves --- models/activities/action_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/models/activities/action_test.go b/models/activities/action_test.go index 36a38c7863971..e5dee33ae0224 100644 --- a/models/activities/action_test.go +++ b/models/activities/action_test.go @@ -228,6 +228,8 @@ func TestNotifyWatchers(t *testing.T) { } func TestGetFeedsCorrupted(t *testing.T) { + // Now we will not check for corrupted data in the feeds + // users should run doctor to fix their data assert.NoError(t, unittest.PrepareTestDatabase()) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) unittest.AssertExistsAndLoadBean(t, &activities_model.Action{ @@ -241,8 +243,8 @@ func TestGetFeedsCorrupted(t *testing.T) { IncludePrivate: true, }) assert.NoError(t, err) - assert.Len(t, actions, 0) - assert.Equal(t, int64(0), count) + assert.Len(t, actions, 1) + assert.Equal(t, int64(1), count) } func TestConsistencyUpdateAction(t *testing.T) { From ca8564157e0df4bafcbb4fc56744058c2461d975 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2024 17:53:49 -0700 Subject: [PATCH 3/4] improve the code --- models/activities/action.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/models/activities/action.go b/models/activities/action.go index 96110cb3a6be0..c28af6152a187 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -452,12 +452,10 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err actions := make([]*Action, 0, opts.PageSize) var count int64 + opts.SetDefaultValues() if opts.Page < 10 { // TODO: why it's 10 but other values? It's an experience value. - sess := db.GetEngine(ctx).Where(cond). - Select("`action`.*") // this line will avoid select other joined table's columns - - opts.SetDefaultValues() + sess := db.GetEngine(ctx).Where(cond) sess = db.SetSessionPagination(sess, &opts) count, err = sess.Desc("`action`.created_unix").FindAndCount(&actions) @@ -466,11 +464,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err } } else { // First, only query which IDs are necessary, and only then query all actions to speed up the overall query - sess := db.GetEngine(ctx).Where(cond). - Select("`action`.id"). - Join("INNER", "repository", "`repository`.id = `action`.repo_id") - - opts.SetDefaultValues() + sess := db.GetEngine(ctx).Where(cond).Select("`action`.id") sess = db.SetSessionPagination(sess, &opts) actionIDs := make([]int64, 0, opts.PageSize) From f045ca710d7e32398865748d9ef8151f709fa2c9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2024 21:20:34 -0700 Subject: [PATCH 4/4] Remove more join --- models/activities/action.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/activities/action.go b/models/activities/action.go index c28af6152a187..9b4ffd7725c4e 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -474,8 +474,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err count, err = db.GetEngine(ctx).Where(cond). Table("action"). - Cols("`action`.id"). - Join("INNER", "repository", "`repository`.id = `action`.repo_id").Count() + Cols("`action`.id").Count() if err != nil { return nil, 0, fmt.Errorf("Count: %w", err) }