Skip to content

Commit b9e2812

Browse files
Add migration to set IsArchived false if it is null (#11853)
* Add migration to set IsArchived false if it is null Fix #11824 Signed-off-by: Andrew Thornton <[email protected]> * Add doctor Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent b682a2c commit b9e2812

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

cmd/doctor.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,22 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
574574
}
575575
}
576576

577+
count, err = models.CountNullArchivedRepository()
578+
if err != nil {
579+
return nil, err
580+
}
581+
if count > 0 {
582+
if ctx.Bool("fix") {
583+
updatedCount, err := models.FixNullArchivedRepository()
584+
if err != nil {
585+
return nil, err
586+
}
587+
results = append(results, fmt.Sprintf("%d repositories with null is_archived updated", updatedCount))
588+
} else {
589+
results = append(results, fmt.Sprintf("%d repositories with null is_archived", count))
590+
}
591+
}
592+
577593
//ToDo: function to recalc all counters
578594

579595
return results, nil

models/consistency.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,15 @@ func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
283283
Delete("`" + subject + "`")
284284
return err
285285
}
286+
287+
// CountNullArchivedRepository counts the number of repositories with is_archived is null
288+
func CountNullArchivedRepository() (int64, error) {
289+
return x.Where(builder.IsNull{"is_archived"}).Count(new(Repository))
290+
}
291+
292+
// FixNullArchivedRepository sets is_archived to false where it is null
293+
func FixNullArchivedRepository() (int64, error) {
294+
return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{
295+
IsArchived: false,
296+
})
297+
}

models/migrations/migrations.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ var migrations = []Migration{
214214
NewMigration("prepend refs/heads/ to issue refs", prependRefsHeadsToIssueRefs),
215215
// v140 -> v141
216216
NewMigration("Save detected language file size to database instead of percent", fixLanguageStatsToSaveSize),
217-
// v141 -> 142
217+
// v141 -> v142
218218
NewMigration("Add KeepActivityPrivate to User table", addKeepActivityPrivateUserColumn),
219+
// v142 -> v143
220+
NewMigration("Ensure Repository.IsArchived is not null", setIsArchivedToFalse),
219221
}
220222

221223
// GetCurrentDBVersion returns the current db version

models/migrations/v142.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"code.gitea.io/gitea/modules/log"
9+
"xorm.io/builder"
10+
"xorm.io/xorm"
11+
)
12+
13+
func setIsArchivedToFalse(x *xorm.Engine) error {
14+
type Repository struct {
15+
IsArchived bool `xorm:"INDEX"`
16+
}
17+
count, err := x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{
18+
IsArchived: false,
19+
})
20+
if err == nil {
21+
log.Debug("Updated %d repositories with is_archived IS NULL", count)
22+
}
23+
return err
24+
}

0 commit comments

Comments
 (0)