From f3908b4755c8d4d289d448343927f4715c754059 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 22 Dec 2023 23:26:07 +0800 Subject: [PATCH 1/4] Fix bug when sqlite update join --- models/issues/comment.go | 25 +++++++++++++++++++++++++ tests/integration/migrate_test.go | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/models/issues/comment.go b/models/issues/comment.go index ce5cf5902d776..67aa579deba0a 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/references" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/translation" @@ -1158,8 +1159,32 @@ func DeleteComment(ctx context.Context, comment *Comment) error { return DeleteReaction(ctx, &ReactionOptions{CommentID: comment.ID}) } +// updateCommentsMigrationsByTypeSqlite3 sqlite3 doesn't support update join SQL syntax +func updateCommentsMigrationsByTypeSqlite3(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error { + _, err := db.GetEngine(ctx).Table("comment"). + Where(builder.In("issue_id", + builder.Select("issue.id"). + From("issue"). + InnerJoin("repository", "issue.repo_id = repository.id"). + Where(builder.Eq{ + "repository.original_service_type": tp, + }), + )). + And("comment.original_author_id = ?", originalAuthorID). + Update(map[string]any{ + "poster_id": posterID, + "original_author": "", + "original_author_id": 0, + }) + return err +} + // UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id func UpdateCommentsMigrationsByType(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error { + if setting.Database.Type.IsSQLite3() { + return updateCommentsMigrationsByTypeSqlite3(ctx, tp, originalAuthorID, posterID) + } + _, err := db.GetEngine(ctx).Table("comment"). Join("INNER", "issue", "issue.id = comment.issue_id"). Join("INNER", "repository", "issue.repo_id = repository.id"). diff --git a/tests/integration/migrate_test.go b/tests/integration/migrate_test.go index f25329f66bcee..2f44de8a4f0c6 100644 --- a/tests/integration/migrate_test.go +++ b/tests/integration/migrate_test.go @@ -12,6 +12,8 @@ import ( "testing" auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -99,3 +101,10 @@ func TestMigrateGiteaForm(t *testing.T) { unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: migratedRepoName}) }) } + +func Test_UpdateCommentsMigrationsByType(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + err := issues_model.UpdateCommentsMigrationsByType(db.DefaultContext, structs.GithubService, "1", 1) + assert.NoError(t, err) +} From 8f08fc897a560221af871592900d72918a41759d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 30 Dec 2023 21:04:27 +0800 Subject: [PATCH 2/4] Upgrade xorm version which support update join for all supported database --- go.mod | 2 +- go.sum | 4 ++-- models/issues/comment.go | 25 ------------------------- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 54a56086701c3..22fb97fe5a378 100644 --- a/go.mod +++ b/go.mod @@ -121,7 +121,7 @@ require ( mvdan.cc/xurls/v2 v2.5.0 strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 xorm.io/builder v0.3.13 - xorm.io/xorm v1.3.4 + xorm.io/xorm v1.3.5-0.20231230105002-0398dee813a9 ) require ( diff --git a/go.sum b/go.sum index 348fc86da4142..0450921e4e3e7 100644 --- a/go.sum +++ b/go.sum @@ -1510,5 +1510,5 @@ strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251/go.mod h1: xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= xorm.io/builder v0.3.13 h1:a3jmiVVL19psGeXx8GIurTp7p0IIgqeDmwhcR6BAOAo= xorm.io/builder v0.3.13/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= -xorm.io/xorm v1.3.4 h1:vWFKzR3DhGUDl5b4srhUjhDwjxkZAc4C7BFszpu0swI= -xorm.io/xorm v1.3.4/go.mod h1:qFJGFoVYbbIdnz2vaL5OxSQ2raleMpyRRalnq3n9OJo= +xorm.io/xorm v1.3.5-0.20231230105002-0398dee813a9 h1:06dL49M+0CZCAoH33wQ7T30b2ar7OzPkQXtZC02t5yk= +xorm.io/xorm v1.3.5-0.20231230105002-0398dee813a9/go.mod h1:qFJGFoVYbbIdnz2vaL5OxSQ2raleMpyRRalnq3n9OJo= diff --git a/models/issues/comment.go b/models/issues/comment.go index 67aa579deba0a..ce5cf5902d776 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -22,7 +22,6 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/references" - "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/translation" @@ -1159,32 +1158,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error { return DeleteReaction(ctx, &ReactionOptions{CommentID: comment.ID}) } -// updateCommentsMigrationsByTypeSqlite3 sqlite3 doesn't support update join SQL syntax -func updateCommentsMigrationsByTypeSqlite3(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error { - _, err := db.GetEngine(ctx).Table("comment"). - Where(builder.In("issue_id", - builder.Select("issue.id"). - From("issue"). - InnerJoin("repository", "issue.repo_id = repository.id"). - Where(builder.Eq{ - "repository.original_service_type": tp, - }), - )). - And("comment.original_author_id = ?", originalAuthorID). - Update(map[string]any{ - "poster_id": posterID, - "original_author": "", - "original_author_id": 0, - }) - return err -} - // UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id func UpdateCommentsMigrationsByType(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error { - if setting.Database.Type.IsSQLite3() { - return updateCommentsMigrationsByTypeSqlite3(ctx, tp, originalAuthorID, posterID) - } - _, err := db.GetEngine(ctx).Table("comment"). Join("INNER", "issue", "issue.id = comment.issue_id"). Join("INNER", "repository", "issue.repo_id = repository.id"). From bf1bf7f172ce7687234a0904895b7fbb71e38d40 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 30 Dec 2023 21:09:09 +0800 Subject: [PATCH 3/4] Use join --- models/issues/comment.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index d92e49a444d74..7b068d49831ac 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -1161,14 +1161,9 @@ func DeleteComment(ctx context.Context, comment *Comment) error { // UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id func UpdateCommentsMigrationsByType(ctx context.Context, tp structs.GitServiceType, originalAuthorID string, posterID int64) error { _, err := db.GetEngine(ctx).Table("comment"). - Where(builder.In("issue_id", - builder.Select("issue.id"). - From("issue"). - InnerJoin("repository", "issue.repo_id = repository.id"). - Where(builder.Eq{ - "repository.original_service_type": tp, - }), - )). + Join("INNER", "issue", "issue.id = comment.issue_id"). + Join("INNER", "repository", "issue.repo_id = repository.id"). + Where("repository.original_service_type = ?", tp). And("comment.original_author_id = ?", originalAuthorID). Update(map[string]any{ "poster_id": posterID, From 5b7da32c320d7f914be4c9a41b24c7e2be2ae32c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 31 Dec 2023 12:22:27 +0800 Subject: [PATCH 4/4] Use xorm 1.3.6 instead of commit version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 22fb97fe5a378..c08010a916c71 100644 --- a/go.mod +++ b/go.mod @@ -121,7 +121,7 @@ require ( mvdan.cc/xurls/v2 v2.5.0 strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 xorm.io/builder v0.3.13 - xorm.io/xorm v1.3.5-0.20231230105002-0398dee813a9 + xorm.io/xorm v1.3.6 ) require ( diff --git a/go.sum b/go.sum index 0450921e4e3e7..69b2556dd3d38 100644 --- a/go.sum +++ b/go.sum @@ -1510,5 +1510,5 @@ strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251/go.mod h1: xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= xorm.io/builder v0.3.13 h1:a3jmiVVL19psGeXx8GIurTp7p0IIgqeDmwhcR6BAOAo= xorm.io/builder v0.3.13/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= -xorm.io/xorm v1.3.5-0.20231230105002-0398dee813a9 h1:06dL49M+0CZCAoH33wQ7T30b2ar7OzPkQXtZC02t5yk= -xorm.io/xorm v1.3.5-0.20231230105002-0398dee813a9/go.mod h1:qFJGFoVYbbIdnz2vaL5OxSQ2raleMpyRRalnq3n9OJo= +xorm.io/xorm v1.3.6 h1:hfpWHkDIWWqUi8FRF2H2M9O8lO3Ov47rwFcS9gPzPkU= +xorm.io/xorm v1.3.6/go.mod h1:qFJGFoVYbbIdnz2vaL5OxSQ2raleMpyRRalnq3n9OJo=