From 6d2a307ad8af7d686f1c3a3706ff0f2df895658a Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Apr 2024 22:02:07 +0800 Subject: Rename migration package name for 1.22-rc1 (#30730) Ref: Propose to restart 1.22 release #30501 --- models/migrations/migrations.go | 15 +++++----- models/migrations/v1_22/v294.go | 53 ++++++++++++++++++++++++++++++++++++ models/migrations/v1_22/v294_test.go | 52 +++++++++++++++++++++++++++++++++++ models/migrations/v1_22/v295.go | 18 ++++++++++++ models/migrations/v1_22/v296.go | 16 +++++++++++ models/migrations/v1_22/v297.go | 17 ++++++++++++ models/migrations/v1_22/v298.go | 10 +++++++ models/migrations/v1_23/v294.go | 53 ------------------------------------ models/migrations/v1_23/v294_test.go | 52 ----------------------------------- models/migrations/v1_23/v295.go | 18 ------------ models/migrations/v1_23/v296.go | 16 ----------- models/migrations/v1_23/v297.go | 17 ------------ models/migrations/v1_23/v298.go | 10 ------- 13 files changed, 174 insertions(+), 173 deletions(-) create mode 100644 models/migrations/v1_22/v294.go create mode 100644 models/migrations/v1_22/v294_test.go create mode 100644 models/migrations/v1_22/v295.go create mode 100644 models/migrations/v1_22/v296.go create mode 100644 models/migrations/v1_22/v297.go create mode 100644 models/migrations/v1_22/v298.go delete mode 100644 models/migrations/v1_23/v294.go delete mode 100644 models/migrations/v1_23/v294_test.go delete mode 100644 models/migrations/v1_23/v295.go delete mode 100644 models/migrations/v1_23/v296.go delete mode 100644 models/migrations/v1_23/v297.go delete mode 100644 models/migrations/v1_23/v298.go (limited to 'models') diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 220d8c2331..4501585250 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -21,7 +21,6 @@ import ( "code.gitea.io/gitea/models/migrations/v1_20" "code.gitea.io/gitea/models/migrations/v1_21" "code.gitea.io/gitea/models/migrations/v1_22" - "code.gitea.io/gitea/models/migrations/v1_23" "code.gitea.io/gitea/models/migrations/v1_6" "code.gitea.io/gitea/models/migrations/v1_7" "code.gitea.io/gitea/models/migrations/v1_8" @@ -574,18 +573,20 @@ var migrations = []Migration{ // v293 -> v294 NewMigration("Ensure every project has exactly one default column", v1_22.CheckProjectColumnsConsistency), - // Gitea 1.22.0 ends at 294 + // Gitea 1.22.0-rc0 ends at 294 // v294 -> v295 - NewMigration("Add unique index for project issue table", v1_23.AddUniqueIndexForProjectIssue), + NewMigration("Add unique index for project issue table", v1_22.AddUniqueIndexForProjectIssue), // v295 -> v296 - NewMigration("Add commit status summary table", v1_23.AddCommitStatusSummary), + NewMigration("Add commit status summary table", v1_22.AddCommitStatusSummary), // v296 -> v297 - NewMigration("Add missing field of commit status summary table", v1_23.AddCommitStatusSummary2), + NewMigration("Add missing field of commit status summary table", v1_22.AddCommitStatusSummary2), // v297 -> v298 - NewMigration("Add everyone_access_mode for repo_unit", v1_23.AddRepoUnitEveryoneAccessMode), + NewMigration("Add everyone_access_mode for repo_unit", v1_22.AddRepoUnitEveryoneAccessMode), // v298 -> v299 - NewMigration("Drop wrongly created table o_auth2_application", v1_23.DropWronglyCreatedTable), + NewMigration("Drop wrongly created table o_auth2_application", v1_22.DropWronglyCreatedTable), + + // Gitea 1.22.0-rc1 ends at 299 } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_22/v294.go b/models/migrations/v1_22/v294.go new file mode 100644 index 0000000000..20e261fb1b --- /dev/null +++ b/models/migrations/v1_22/v294.go @@ -0,0 +1,53 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import ( + "fmt" + + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +// AddUniqueIndexForProjectIssue adds unique indexes for project issue table +func AddUniqueIndexForProjectIssue(x *xorm.Engine) error { + // remove possible duplicated records in table project_issue + type result struct { + IssueID int64 + ProjectID int64 + Cnt int + } + var results []result + if err := x.Select("issue_id, project_id, count(*) as cnt"). + Table("project_issue"). + GroupBy("issue_id, project_id"). + Having("count(*) > 1"). + Find(&results); err != nil { + return err + } + for _, r := range results { + if x.Dialect().URI().DBType == schemas.MSSQL { + if _, err := x.Exec(fmt.Sprintf("delete from project_issue where id in (SELECT top %d id FROM project_issue WHERE issue_id = ? and project_id = ?)", r.Cnt-1), r.IssueID, r.ProjectID); err != nil { + return err + } + } else { + var ids []int64 + if err := x.SQL("SELECT id FROM project_issue WHERE issue_id = ? and project_id = ? limit ?", r.IssueID, r.ProjectID, r.Cnt-1).Find(&ids); err != nil { + return err + } + if _, err := x.Table("project_issue").In("id", ids).Delete(); err != nil { + return err + } + } + } + + // add unique index for project_issue table + type ProjectIssue struct { //revive:disable-line:exported + ID int64 `xorm:"pk autoincr"` + IssueID int64 `xorm:"INDEX unique(s)"` + ProjectID int64 `xorm:"INDEX unique(s)"` + } + + return x.Sync(new(ProjectIssue)) +} diff --git a/models/migrations/v1_22/v294_test.go b/models/migrations/v1_22/v294_test.go new file mode 100644 index 0000000000..82a3bcd602 --- /dev/null +++ b/models/migrations/v1_22/v294_test.go @@ -0,0 +1,52 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import ( + "slices" + "testing" + + "code.gitea.io/gitea/models/migrations/base" + + "github.com/stretchr/testify/assert" + "xorm.io/xorm/schemas" +) + +func Test_AddUniqueIndexForProjectIssue(t *testing.T) { + type ProjectIssue struct { //revive:disable-line:exported + ID int64 `xorm:"pk autoincr"` + IssueID int64 `xorm:"INDEX"` + ProjectID int64 `xorm:"INDEX"` + } + + // Prepare and load the testing database + x, deferable := base.PrepareTestEnv(t, 0, new(ProjectIssue)) + defer deferable() + if x == nil || t.Failed() { + return + } + + cnt, err := x.Table("project_issue").Where("project_id=1 AND issue_id=1").Count() + assert.NoError(t, err) + assert.EqualValues(t, 2, cnt) + + assert.NoError(t, AddUniqueIndexForProjectIssue(x)) + + cnt, err = x.Table("project_issue").Where("project_id=1 AND issue_id=1").Count() + assert.NoError(t, err) + assert.EqualValues(t, 1, cnt) + + tables, err := x.DBMetas() + assert.NoError(t, err) + assert.EqualValues(t, 1, len(tables)) + found := false + for _, index := range tables[0].Indexes { + if index.Type == schemas.UniqueType { + found = true + slices.Equal(index.Cols, []string{"project_id", "issue_id"}) + break + } + } + assert.True(t, found) +} diff --git a/models/migrations/v1_22/v295.go b/models/migrations/v1_22/v295.go new file mode 100644 index 0000000000..17bdadb4ad --- /dev/null +++ b/models/migrations/v1_22/v295.go @@ -0,0 +1,18 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import "xorm.io/xorm" + +func AddCommitStatusSummary(x *xorm.Engine) error { + type CommitStatusSummary struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX UNIQUE(repo_id_sha)"` + SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_id_sha)"` + State string `xorm:"VARCHAR(7) NOT NULL"` + } + // there is no migrations because if there is no data on this table, it will fall back to get data + // from commit status + return x.Sync2(new(CommitStatusSummary)) +} diff --git a/models/migrations/v1_22/v296.go b/models/migrations/v1_22/v296.go new file mode 100644 index 0000000000..1ecacab95f --- /dev/null +++ b/models/migrations/v1_22/v296.go @@ -0,0 +1,16 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import "xorm.io/xorm" + +func AddCommitStatusSummary2(x *xorm.Engine) error { + type CommitStatusSummary struct { + ID int64 `xorm:"pk autoincr"` + TargetURL string `xorm:"TEXT"` + } + // there is no migrations because if there is no data on this table, it will fall back to get data + // from commit status + return x.Sync(new(CommitStatusSummary)) +} diff --git a/models/migrations/v1_22/v297.go b/models/migrations/v1_22/v297.go new file mode 100644 index 0000000000..7d4b506925 --- /dev/null +++ b/models/migrations/v1_22/v297.go @@ -0,0 +1,17 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import ( + "code.gitea.io/gitea/models/perm" + + "xorm.io/xorm" +) + +func AddRepoUnitEveryoneAccessMode(x *xorm.Engine) error { + type RepoUnit struct { //revive:disable-line:exported + EveryoneAccessMode perm.AccessMode `xorm:"NOT NULL DEFAULT 0"` + } + return x.Sync(&RepoUnit{}) +} diff --git a/models/migrations/v1_22/v298.go b/models/migrations/v1_22/v298.go new file mode 100644 index 0000000000..b9f3b95ade --- /dev/null +++ b/models/migrations/v1_22/v298.go @@ -0,0 +1,10 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import "xorm.io/xorm" + +func DropWronglyCreatedTable(x *xorm.Engine) error { + return x.DropTables("o_auth2_application") +} diff --git a/models/migrations/v1_23/v294.go b/models/migrations/v1_23/v294.go deleted file mode 100644 index f2a54f6d23..0000000000 --- a/models/migrations/v1_23/v294.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_23 //nolint - -import ( - "fmt" - - "xorm.io/xorm" - "xorm.io/xorm/schemas" -) - -// AddUniqueIndexForProjectIssue adds unique indexes for project issue table -func AddUniqueIndexForProjectIssue(x *xorm.Engine) error { - // remove possible duplicated records in table project_issue - type result struct { - IssueID int64 - ProjectID int64 - Cnt int - } - var results []result - if err := x.Select("issue_id, project_id, count(*) as cnt"). - Table("project_issue"). - GroupBy("issue_id, project_id"). - Having("count(*) > 1"). - Find(&results); err != nil { - return err - } - for _, r := range results { - if x.Dialect().URI().DBType == schemas.MSSQL { - if _, err := x.Exec(fmt.Sprintf("delete from project_issue where id in (SELECT top %d id FROM project_issue WHERE issue_id = ? and project_id = ?)", r.Cnt-1), r.IssueID, r.ProjectID); err != nil { - return err - } - } else { - var ids []int64 - if err := x.SQL("SELECT id FROM project_issue WHERE issue_id = ? and project_id = ? limit ?", r.IssueID, r.ProjectID, r.Cnt-1).Find(&ids); err != nil { - return err - } - if _, err := x.Table("project_issue").In("id", ids).Delete(); err != nil { - return err - } - } - } - - // add unique index for project_issue table - type ProjectIssue struct { //revive:disable-line:exported - ID int64 `xorm:"pk autoincr"` - IssueID int64 `xorm:"INDEX unique(s)"` - ProjectID int64 `xorm:"INDEX unique(s)"` - } - - return x.Sync(new(ProjectIssue)) -} diff --git a/models/migrations/v1_23/v294_test.go b/models/migrations/v1_23/v294_test.go deleted file mode 100644 index d9a44ad866..0000000000 --- a/models/migrations/v1_23/v294_test.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_23 //nolint - -import ( - "slices" - "testing" - - "code.gitea.io/gitea/models/migrations/base" - - "github.com/stretchr/testify/assert" - "xorm.io/xorm/schemas" -) - -func Test_AddUniqueIndexForProjectIssue(t *testing.T) { - type ProjectIssue struct { //revive:disable-line:exported - ID int64 `xorm:"pk autoincr"` - IssueID int64 `xorm:"INDEX"` - ProjectID int64 `xorm:"INDEX"` - } - - // Prepare and load the testing database - x, deferable := base.PrepareTestEnv(t, 0, new(ProjectIssue)) - defer deferable() - if x == nil || t.Failed() { - return - } - - cnt, err := x.Table("project_issue").Where("project_id=1 AND issue_id=1").Count() - assert.NoError(t, err) - assert.EqualValues(t, 2, cnt) - - assert.NoError(t, AddUniqueIndexForProjectIssue(x)) - - cnt, err = x.Table("project_issue").Where("project_id=1 AND issue_id=1").Count() - assert.NoError(t, err) - assert.EqualValues(t, 1, cnt) - - tables, err := x.DBMetas() - assert.NoError(t, err) - assert.EqualValues(t, 1, len(tables)) - found := false - for _, index := range tables[0].Indexes { - if index.Type == schemas.UniqueType { - found = true - slices.Equal(index.Cols, []string{"project_id", "issue_id"}) - break - } - } - assert.True(t, found) -} diff --git a/models/migrations/v1_23/v295.go b/models/migrations/v1_23/v295.go deleted file mode 100644 index 9a2003cfc1..0000000000 --- a/models/migrations/v1_23/v295.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_23 //nolint - -import "xorm.io/xorm" - -func AddCommitStatusSummary(x *xorm.Engine) error { - type CommitStatusSummary struct { - ID int64 `xorm:"pk autoincr"` - RepoID int64 `xorm:"INDEX UNIQUE(repo_id_sha)"` - SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_id_sha)"` - State string `xorm:"VARCHAR(7) NOT NULL"` - } - // there is no migrations because if there is no data on this table, it will fall back to get data - // from commit status - return x.Sync2(new(CommitStatusSummary)) -} diff --git a/models/migrations/v1_23/v296.go b/models/migrations/v1_23/v296.go deleted file mode 100644 index 495ae2ab23..0000000000 --- a/models/migrations/v1_23/v296.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_23 //nolint - -import "xorm.io/xorm" - -func AddCommitStatusSummary2(x *xorm.Engine) error { - type CommitStatusSummary struct { - ID int64 `xorm:"pk autoincr"` - TargetURL string `xorm:"TEXT"` - } - // there is no migrations because if there is no data on this table, it will fall back to get data - // from commit status - return x.Sync(new(CommitStatusSummary)) -} diff --git a/models/migrations/v1_23/v297.go b/models/migrations/v1_23/v297.go deleted file mode 100644 index e79f04cf9c..0000000000 --- a/models/migrations/v1_23/v297.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_23 //nolint - -import ( - "code.gitea.io/gitea/models/perm" - - "xorm.io/xorm" -) - -func AddRepoUnitEveryoneAccessMode(x *xorm.Engine) error { - type RepoUnit struct { //revive:disable-line:exported - EveryoneAccessMode perm.AccessMode `xorm:"NOT NULL DEFAULT 0"` - } - return x.Sync(&RepoUnit{}) -} diff --git a/models/migrations/v1_23/v298.go b/models/migrations/v1_23/v298.go deleted file mode 100644 index 8761a05d3d..0000000000 --- a/models/migrations/v1_23/v298.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_23 //nolint - -import "xorm.io/xorm" - -func DropWronglyCreatedTable(x *xorm.Engine) error { - return x.DropTables("o_auth2_application") -} -- cgit v1.2.3