From 45933937f8a0995e171d4c01f9860c41d71f6cbb Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 4 Jul 2023 08:15:44 +0000 Subject: [PATCH 01/15] fix logic --- models/organization/team_list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/organization/team_list.go b/models/organization/team_list.go index efb3104ad5971..87f3db1e0adfd 100644 --- a/models/organization/team_list.go +++ b/models/organization/team_list.go @@ -29,8 +29,8 @@ func (t TeamList) LoadUnits(ctx context.Context) error { func (t TeamList) UnitMaxAccess(tp unit.Type) perm.AccessMode { maxAccess := perm.AccessModeNone for _, team := range t { - if team.IsOwnerTeam() { - return perm.AccessModeOwner + if team.AccessMode >= perm.AccessModeAdmin { + return team.AccessMode } for _, teamUnit := range team.Units { if teamUnit.Type != tp { From 6ea41739dd671b8a7c8d4132d41d4301e20d1976 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 5 Jul 2023 00:09:28 +0000 Subject: [PATCH 02/15] add migration --- models/migrations/migrations.go | 2 + models/migrations/v1_21/v265.go | 113 ++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 models/migrations/v1_21/v265.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index a15b6e4eec8ca..13b140b851cbe 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -511,6 +511,8 @@ var migrations = []Migration{ NewMigration("Add git_size and lfs_size columns to repository table", v1_21.AddGitSizeAndLFSSizeToRepositoryTable), // v264 -> v265 NewMigration("Add branch table", v1_21.AddBranchTable), + // v265 -> v266 + NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_21/v265.go b/models/migrations/v1_21/v265.go new file mode 100644 index 0000000000000..25073bd50c15e --- /dev/null +++ b/models/migrations/v1_21/v265.go @@ -0,0 +1,113 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_21 //nolint + +import ( + "code.gitea.io/gitea/modules/container" + "xorm.io/xorm" +) + +func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error { + type UnitType int + type AccessMode int + + type Team struct { + ID int64 `xorm:"pk autoincr"` + OrgID int64 `xorm:"INDEX"` + AccessMode AccessMode `xorm:"'authorize'"` + } + + type TeamUnit struct { + ID int64 `xorm:"pk autoincr"` + OrgID int64 `xorm:"INDEX"` + TeamID int64 `xorm:"UNIQUE(s)"` + Type UnitType `xorm:"UNIQUE(s)"` + AccessMode AccessMode + } + + const ( + // AccessModeRead read access + AccessModeRead = 1 + // AccessModeAdmin admin access + AccessModeAdmin = 3 + + // Unit Type + TypeInvalid UnitType = iota // 0 invalid + TypeCode // 1 code + TypeIssues // 2 issues + TypePullRequests // 3 PRs + TypeReleases // 4 Releases + TypeWiki // 5 Wiki + TypeExternalWiki // 6 ExternalWiki + TypeExternalTracker // 7 ExternalTracker + TypeProjects // 8 Kanban board + TypePackages // 9 Packages + TypeActions // 10 Actions + ) + + var AllRepoUnitTypes = []UnitType{ + TypeCode, + TypeIssues, + TypePullRequests, + TypeReleases, + TypeWiki, + TypeExternalWiki, + TypeExternalTracker, + TypeProjects, + TypePackages, + TypeActions, + } + + sess := x.NewSession() + defer sess.Close() + + if err := sess.Begin(); err != nil { + return err + } + + // find all admin teams + teams := make([]*Team, 0) + err := sess.Where("team.authorize == ?", AccessModeAdmin).Find(&teams) + if err != nil { + return err + } + + for _, team := range teams { + // find all existing records + teamunits := make([]*TeamUnit, 0, len(AllRepoUnitTypes)) + err := sess.Where("`team_unit`.team_id == ?", team.ID).Find(&teamunits) + if err != nil { + return err + } + existingUnitTypes := make(container.Set[UnitType], 0) + for _, tu := range teamunits { + if tu.Type > 0 { + existingUnitTypes.Add(tu.Type) + } + } + + // insert or update records + for _, u := range AllRepoUnitTypes { + newTeamUnit := &TeamUnit{ + OrgID: team.OrgID, + TeamID: team.ID, + Type: u, + } + // external unit should be read + if u == TypeExternalWiki || u == TypeExternalTracker { + newTeamUnit.AccessMode = AccessModeRead + } else { + newTeamUnit.AccessMode = AccessModeAdmin + } + + if existingUnitTypes.Contains(u) { + sess.Cols("access_mode").Update(newTeamUnit) + } else { + sess.Insert(newTeamUnit) + } + } + } + + return sess.Commit() +} From c36b931beb831d67f47e36e241e0989264251710 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 5 Jul 2023 01:50:01 +0000 Subject: [PATCH 03/15] fix lint --- models/migrations/v1_21/v265.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v1_21/v265.go b/models/migrations/v1_21/v265.go index 25073bd50c15e..6ff3f7d2fd445 100644 --- a/models/migrations/v1_21/v265.go +++ b/models/migrations/v1_21/v265.go @@ -46,7 +46,7 @@ func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error { TypeActions // 10 Actions ) - var AllRepoUnitTypes = []UnitType{ + AllRepoUnitTypes := []UnitType{ TypeCode, TypeIssues, TypePullRequests, From 82dc31fefb87f784eb96138db62195599ecd66c9 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 5 Jul 2023 07:27:20 +0000 Subject: [PATCH 04/15] fix lint --- models/migrations/v1_21/v265.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/migrations/v1_21/v265.go b/models/migrations/v1_21/v265.go index 6ff3f7d2fd445..ee4bed40b1059 100644 --- a/models/migrations/v1_21/v265.go +++ b/models/migrations/v1_21/v265.go @@ -5,6 +5,7 @@ package v1_21 //nolint import ( "code.gitea.io/gitea/modules/container" + "xorm.io/xorm" ) From 74129a346f9ce84a3f7f44fbb66e1bd8d8c41f53 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 5 Jul 2023 07:38:18 +0000 Subject: [PATCH 05/15] fix sql --- models/migrations/v1_21/v265.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/migrations/v1_21/v265.go b/models/migrations/v1_21/v265.go index ee4bed40b1059..14e03083a94de 100644 --- a/models/migrations/v1_21/v265.go +++ b/models/migrations/v1_21/v265.go @@ -69,7 +69,7 @@ func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error { // find all admin teams teams := make([]*Team, 0) - err := sess.Where("team.authorize == ?", AccessModeAdmin).Find(&teams) + err := sess.Where("team.authorize = ?", AccessModeAdmin).Find(&teams) if err != nil { return err } @@ -77,7 +77,7 @@ func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error { for _, team := range teams { // find all existing records teamunits := make([]*TeamUnit, 0, len(AllRepoUnitTypes)) - err := sess.Where("`team_unit`.team_id == ?", team.ID).Find(&teamunits) + err := sess.Where("`team_unit`.team_id = ?", team.ID).Find(&teamunits) if err != nil { return err } From a1e5db3df7cca41d62051bdc79b274fc092c8cec Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 24 Jul 2023 00:36:45 +0000 Subject: [PATCH 06/15] rename to v267 --- models/migrations/v1_21/{v265.go => v267.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/v1_21/{v265.go => v267.go} (100%) diff --git a/models/migrations/v1_21/v265.go b/models/migrations/v1_21/v267.go similarity index 100% rename from models/migrations/v1_21/v265.go rename to models/migrations/v1_21/v267.go From 169ae50a1433a98f341bb6727e2cb81fba5aca52 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 24 Jul 2023 07:05:21 +0000 Subject: [PATCH 07/15] rename to v268 --- models/migrations/v1_21/{v267.go => v268.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/v1_21/{v267.go => v268.go} (100%) diff --git a/models/migrations/v1_21/v267.go b/models/migrations/v1_21/v268.go similarity index 100% rename from models/migrations/v1_21/v267.go rename to models/migrations/v1_21/v268.go From 4de3b01aa7a7bd7e4faf0242b84c28cd55df4606 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 25 Jul 2023 02:09:06 +0000 Subject: [PATCH 08/15] fix lint --- models/migrations/migrations.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index bb27be88e0754..4c765cc0a0ae3 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -517,8 +517,8 @@ var migrations = []Migration{ NewMigration("Reduce commit status", v1_21.ReduceCommitStatus), // v267 -> v268 NewMigration("Add action_tasks_version table", v1_21.CreateActionTasksVersionTable), - // v268 -> v269 - NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords), + // v268 -> v269 + NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords), } // GetCurrentDBVersion returns the current db version From b4b72e7dbf68388cdbe5999d31b031e25f8a15cb Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 27 Jul 2023 08:57:39 +0000 Subject: [PATCH 09/15] rename to v270 --- models/migrations/v1_21/{v268.go => v270.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/v1_21/{v268.go => v270.go} (100%) diff --git a/models/migrations/v1_21/v268.go b/models/migrations/v1_21/v270.go similarity index 100% rename from models/migrations/v1_21/v268.go rename to models/migrations/v1_21/v270.go From 0bd77f1269755f27dcfff04626ada673fe607dad Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 9 Aug 2023 04:32:45 +0000 Subject: [PATCH 10/15] rename to v271 --- models/migrations/migrations.go | 4 ++-- models/migrations/v1_21/{v270.go => v271.go} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename models/migrations/v1_21/{v270.go => v271.go} (100%) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 09572eac5040e..1a0c2dd0eaa29 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -521,8 +521,8 @@ var migrations = []Migration{ NewMigration("Update Action Ref", v1_21.UpdateActionsRefIndex), // v269 -> v270 NewMigration("Drop deleted branch table", v1_21.DropDeletedBranchTable), - // v270 -> v271 - NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords), + // v271 -> v272 + NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_21/v270.go b/models/migrations/v1_21/v271.go similarity index 100% rename from models/migrations/v1_21/v270.go rename to models/migrations/v1_21/v271.go From c5d335aa0f12dc05ab297c5f4d807dd95e3adca9 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 15 Nov 2023 07:49:40 +0000 Subject: [PATCH 11/15] rename to v283 --- models/migrations/{v1_21/v271.go => v1_22/v283.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/{v1_21/v271.go => v1_22/v283.go} (100%) diff --git a/models/migrations/v1_21/v271.go b/models/migrations/v1_22/v283.go similarity index 100% rename from models/migrations/v1_21/v271.go rename to models/migrations/v1_22/v283.go From c60d25ec0de9e36a2f928fa76eea1c96358cca54 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 16 Nov 2023 14:14:40 +0900 Subject: [PATCH 12/15] Update models/migrations/v1_22/v283.go Co-authored-by: Lunny Xiao --- models/migrations/v1_22/v283.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v1_22/v283.go b/models/migrations/v1_22/v283.go index 14e03083a94de..1fa0dd81db39a 100644 --- a/models/migrations/v1_22/v283.go +++ b/models/migrations/v1_22/v283.go @@ -1,7 +1,7 @@ // Copyright 2023 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package v1_21 //nolint +package v1_22 //nolint import ( "code.gitea.io/gitea/modules/container" From 9a9c144aa80d772c42e3884471bcd434de85831c Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 16 Nov 2023 05:15:34 +0000 Subject: [PATCH 13/15] fix --- models/migrations/migrations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 9f7005d3994c7..b7044cc23a147 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -551,7 +551,7 @@ var migrations = []Migration{ // v282 -> v283 NewMigration("Add Index to pull_auto_merge.doer_id", v1_22.AddIndexToPullAutoMergeDoerID), // v283 -> v284 - NewMigration("Fix missing admin team unit records", v1_21.FixMissingAdminTeamUnitRecords), + NewMigration("Fix missing admin team unit records", v1_22.FixMissingAdminTeamUnitRecords), } // GetCurrentDBVersion returns the current db version From 44f066621f288ec74d0c3fcec934f78a31a7ab49 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 18 Jan 2024 02:31:02 +0000 Subject: [PATCH 14/15] rename migration --- models/migrations/v1_22/{v283.go => v285.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/v1_22/{v283.go => v285.go} (100%) diff --git a/models/migrations/v1_22/v283.go b/models/migrations/v1_22/v285.go similarity index 100% rename from models/migrations/v1_22/v283.go rename to models/migrations/v1_22/v285.go From eb8a311f4d9085653843a8e8a080e35b4b6ca08b Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 18 Jan 2024 07:25:18 +0000 Subject: [PATCH 15/15] avoid a too big transaction --- models/migrations/v1_22/v285.go | 83 +++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/models/migrations/v1_22/v285.go b/models/migrations/v1_22/v285.go index 1fa0dd81db39a..e15e331df6b8d 100644 --- a/models/migrations/v1_22/v285.go +++ b/models/migrations/v1_22/v285.go @@ -5,6 +5,7 @@ package v1_22 //nolint import ( "code.gitea.io/gitea/modules/container" + "code.gitea.io/gitea/modules/setting" "xorm.io/xorm" ) @@ -67,45 +68,67 @@ func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error { return err } - // find all admin teams - teams := make([]*Team, 0) - err := sess.Where("team.authorize = ?", AccessModeAdmin).Find(&teams) - if err != nil { - return err + limit := setting.Database.IterateBufferSize + if limit <= 0 { + limit = 50 } - for _, team := range teams { - // find all existing records - teamunits := make([]*TeamUnit, 0, len(AllRepoUnitTypes)) - err := sess.Where("`team_unit`.team_id = ?", team.ID).Find(&teamunits) + start := 0 + + for { + var teams []*Team + err := sess.Where("team.authorize = ?", AccessModeAdmin).Limit(limit, start).Find(&teams) if err != nil { return err } - existingUnitTypes := make(container.Set[UnitType], 0) - for _, tu := range teamunits { - if tu.Type > 0 { - existingUnitTypes.Add(tu.Type) - } - } - // insert or update records - for _, u := range AllRepoUnitTypes { - newTeamUnit := &TeamUnit{ - OrgID: team.OrgID, - TeamID: team.ID, - Type: u, + if len(teams) == 0 { + break + } + start += len(teams) + + for _, team := range teams { + // find all existing records + teamunits := make([]*TeamUnit, 0, len(AllRepoUnitTypes)) + err := sess.Where("`team_unit`.team_id = ?", team.ID).Find(&teamunits) + if err != nil { + return err } - // external unit should be read - if u == TypeExternalWiki || u == TypeExternalTracker { - newTeamUnit.AccessMode = AccessModeRead - } else { - newTeamUnit.AccessMode = AccessModeAdmin + existingUnitTypes := make(container.Set[UnitType], 0) + for _, tu := range teamunits { + if tu.Type > 0 { + existingUnitTypes.Add(tu.Type) + } } - if existingUnitTypes.Contains(u) { - sess.Cols("access_mode").Update(newTeamUnit) - } else { - sess.Insert(newTeamUnit) + // insert or update records + for _, u := range AllRepoUnitTypes { + newTeamUnit := &TeamUnit{ + OrgID: team.OrgID, + TeamID: team.ID, + Type: u, + } + // external unit should be read + if u == TypeExternalWiki || u == TypeExternalTracker { + newTeamUnit.AccessMode = AccessModeRead + } else { + newTeamUnit.AccessMode = AccessModeAdmin + } + + if existingUnitTypes.Contains(u) { + sess.Cols("access_mode").Update(newTeamUnit) + } else { + sess.Insert(newTeamUnit) + } + } + } + + if start%1000 == 0 { // avoid a too big transaction + if err := sess.Commit(); err != nil { + return err + } + if err := sess.Begin(); err != nil { + return err } } }