From 8d012f80b78adc940b0165b275623e98045bdc5b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 12 Apr 2024 16:00:01 +0800 Subject: [PATCH 1/3] Fix missed column of commit status summary table --- models/git/commit_status_summary.go | 32 ++++++++++++++++------------- models/migrations/migrations.go | 2 ++ models/migrations/v1_23/v296.go | 16 +++++++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 models/migrations/v1_23/v296.go diff --git a/models/git/commit_status_summary.go b/models/git/commit_status_summary.go index 01674e943d04f..7603e7aa65415 100644 --- a/models/git/commit_status_summary.go +++ b/models/git/commit_status_summary.go @@ -15,10 +15,11 @@ import ( // CommitStatusSummary holds the latest commit Status of a single Commit 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 api.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"` + 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 api.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"` + TargetURL string `xorm:"TEXT"` } func init() { @@ -44,9 +45,10 @@ func GetLatestCommitStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA commitStatuses := make([]*CommitStatus, 0, len(repoSHAs)) for _, summary := range summaries { commitStatuses = append(commitStatuses, &CommitStatus{ - RepoID: summary.RepoID, - SHA: summary.SHA, - State: summary.State, + RepoID: summary.RepoID, + SHA: summary.SHA, + State: summary.State, + TargetURL: summary.TargetURL, }) } return commitStatuses, nil @@ -61,22 +63,24 @@ func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) er // mysql will return 0 when update a record which state hasn't been changed which behaviour is different from other database, // so we need to use insert in on duplicate if setting.Database.Type.IsMySQL() { - _, err := db.GetEngine(ctx).Exec("INSERT INTO commit_status_summary (repo_id,sha,state) VALUES (?,?,?) ON DUPLICATE KEY UPDATE state=?", - repoID, sha, state.State, state.State) + _, err := db.GetEngine(ctx).Exec("INSERT INTO commit_status_summary (repo_id,sha,state,target_url) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE state=?", + repoID, sha, state.State, state.TargetURL, state.State) return err } if cnt, err := db.GetEngine(ctx).Where("repo_id=? AND sha=?", repoID, sha). - Cols("state"). + Cols("state, target_url"). Update(&CommitStatusSummary{ - State: state.State, + State: state.State, + TargetURL: state.TargetURL, }); err != nil { return err } else if cnt == 0 { _, err = db.GetEngine(ctx).Insert(&CommitStatusSummary{ - RepoID: repoID, - SHA: sha, - State: state.State, + RepoID: repoID, + SHA: sha, + State: state.State, + TargetURL: state.TargetURL, }) return err } diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 3ea8f2acbf260..7ed5d7bb0f351 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -580,6 +580,8 @@ var migrations = []Migration{ NewMigration("Add unique index for project issue table", v1_23.AddUniqueIndexForProjectIssue), // v295 -> v296 NewMigration("Add commit status summary table", v1_23.AddCommitStatusSummary), + // v296 -> v297 + NewMigration("Add missed field of commit status summary table", v1_23.AddCommitStatusSummary2), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_23/v296.go b/models/migrations/v1_23/v296.go new file mode 100644 index 0000000000000..2840e21ac5488 --- /dev/null +++ b/models/migrations/v1_23/v296.go @@ -0,0 +1,16 @@ +// 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.Sync2(new(CommitStatusSummary)) +} From 78e0961549ecc66540767a359fad9925893f570a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 12 Apr 2024 23:08:06 +0800 Subject: [PATCH 2/3] Update models/migrations/migrations.go Co-authored-by: silverwind --- 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 7ed5d7bb0f351..5326d48f901bc 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -581,7 +581,7 @@ var migrations = []Migration{ // v295 -> v296 NewMigration("Add commit status summary table", v1_23.AddCommitStatusSummary), // v296 -> v297 - NewMigration("Add missed field of commit status summary table", v1_23.AddCommitStatusSummary2), + NewMigration("Add missing field of commit status summary table", v1_23.AddCommitStatusSummary2), } // GetCurrentDBVersion returns the current db version From 5c5bf609054463abdb8b6fe654f70dd356a91089 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 13 Apr 2024 15:44:57 +0800 Subject: [PATCH 3/3] Update models/migrations/v1_23/v296.go Co-authored-by: delvh --- models/migrations/v1_23/v296.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v1_23/v296.go b/models/migrations/v1_23/v296.go index 2840e21ac5488..495ae2ab23d1a 100644 --- a/models/migrations/v1_23/v296.go +++ b/models/migrations/v1_23/v296.go @@ -12,5 +12,5 @@ func AddCommitStatusSummary2(x *xorm.Engine) error { } // 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)) + return x.Sync(new(CommitStatusSummary)) }