From f2bde408049bed98ecedc4c1d73abf9639f34d76 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 11 Jun 2020 22:08:13 +0100 Subject: [PATCH] Add doctor check to set IsArchived false if it is null (partial backport #11853) (#11859) Partial backport of #11853 Add doctor check to set IsArchived false if it is null. (Migration change unfortunately not possible to be backported.) Fix #11824 Signed-off-by: Andrew Thornton --- cmd/doctor.go | 16 ++++++++++++++++ models/consistency.go | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cmd/doctor.go b/cmd/doctor.go index 3456f26f7..45a50c826 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -574,6 +574,22 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) { } } + count, err = models.CountNullArchivedRepository() + if err != nil { + return nil, err + } + if count > 0 { + if ctx.Bool("fix") { + updatedCount, err := models.FixNullArchivedRepository() + if err != nil { + return nil, err + } + results = append(results, fmt.Sprintf("%d repositories with null is_archived updated", updatedCount)) + } else { + results = append(results, fmt.Sprintf("%d repositories with null is_archived", count)) + } + } + //ToDo: function to recalc all counters return results, nil diff --git a/models/consistency.go b/models/consistency.go index d6a158840..fbb99ca80 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -283,3 +283,15 @@ func DeleteOrphanedObjects(subject, refobject, joinCond string) error { Delete("`" + subject + "`") return err } + +// CountNullArchivedRepository counts the number of repositories with is_archived is null +func CountNullArchivedRepository() (int64, error) { + return x.Where(builder.IsNull{"is_archived"}).Count(new(Repository)) +} + +// FixNullArchivedRepository sets is_archived to false where it is null +func FixNullArchivedRepository() (int64, error) { + return x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{ + IsArchived: false, + }) +}