From b9e281265ee2c510199cd0241ae02bff2bd8ecc1 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 11 Jun 2020 21:18:11 +0100 Subject: [PATCH] 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 * Add doctor Signed-off-by: Andrew Thornton Co-authored-by: techknowlogick --- cmd/doctor.go | 16 ++++++++++++++++ models/consistency.go | 12 ++++++++++++ models/migrations/migrations.go | 4 +++- models/migrations/v142.go | 24 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 models/migrations/v142.go diff --git a/cmd/doctor.go b/cmd/doctor.go index 3456f26f70..45a50c8266 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 d6a158840b..fbb99ca80c 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, + }) +} diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 432bcffb1b..6cc80249a3 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -214,8 +214,10 @@ var migrations = []Migration{ NewMigration("prepend refs/heads/ to issue refs", prependRefsHeadsToIssueRefs), // v140 -> v141 NewMigration("Save detected language file size to database instead of percent", fixLanguageStatsToSaveSize), - // v141 -> 142 + // v141 -> v142 NewMigration("Add KeepActivityPrivate to User table", addKeepActivityPrivateUserColumn), + // v142 -> v143 + NewMigration("Ensure Repository.IsArchived is not null", setIsArchivedToFalse), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v142.go b/models/migrations/v142.go new file mode 100644 index 0000000000..1abdd34961 --- /dev/null +++ b/models/migrations/v142.go @@ -0,0 +1,24 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "code.gitea.io/gitea/modules/log" + "xorm.io/builder" + "xorm.io/xorm" +) + +func setIsArchivedToFalse(x *xorm.Engine) error { + type Repository struct { + IsArchived bool `xorm:"INDEX"` + } + count, err := x.Where(builder.IsNull{"is_archived"}).Cols("is_archived").Update(&Repository{ + IsArchived: false, + }) + if err == nil { + log.Debug("Updated %d repositories with is_archived IS NULL", count) + } + return err +}