From fd3cf7e09062d5e8871fda6017180a6b39c6f0ff Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Sun, 10 Jun 2018 12:03:51 +0200 Subject: [PATCH] remove stale watches --- models/migrations/migrations.go | 2 + models/migrations/v67.go | 93 +++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 models/migrations/v67.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 1300065ab..2537e5712 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -186,6 +186,8 @@ var migrations = []Migration{ NewMigration("add u2f", addU2FReg), // v66 -> v67 NewMigration("add login source id column for public_key table", addLoginSourceIDToPublicKeyTable), + // v67 -> v68 + NewMigration("remove stale watches", removeStaleWatches), } // Migrate database to current version diff --git a/models/migrations/v67.go b/models/migrations/v67.go new file mode 100644 index 000000000..0bb39b56d --- /dev/null +++ b/models/migrations/v67.go @@ -0,0 +1,93 @@ +// Copyright 2018 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/setting" + + "github.com/go-xorm/xorm" +) + +func removeStaleWatches(x *xorm.Engine) error { + type Watch struct { + ID int64 + UserID int64 + RepoID int64 + } + + type Repository struct { + ID int64 + IsPrivate bool + OwnerID int64 + } + + type Access struct { + UserID int64 + RepoID int64 + Mode int + } + + const ( + // AccessModeNone no access + AccessModeNone int = iota // 0 + // AccessModeRead read access + AccessModeRead // 1 + ) + + accessLevel := func(userID int64, repo *Repository) (int, error) { + mode := AccessModeNone + if !repo.IsPrivate { + mode = AccessModeRead + } + + if userID == 0 { + return mode, nil + } + + if userID == repo.OwnerID { + return 4, nil + } + + a := &Access{UserID: userID, RepoID: repo.ID} + if has, err := x.Get(a); !has || err != nil { + return mode, err + } + return a.Mode, nil + } + + repoCache := make(map[int64]*Repository) + return x.BufferSize(setting.IterateBufferSize).Iterate(new(Watch), + func(idx int, bean interface{}) error { + watch := bean.(*Watch) + + repo := repoCache[watch.RepoID] + if repo == nil { + repo = &Repository{ + ID: watch.RepoID, + } + if _, err := x.Get(repo); err != nil { + return err + } + } + + // Remove watches from now unaccessible + mode, err := accessLevel(watch.UserID, repo) + if err != nil { + return err + } + has := AccessModeRead <= mode + if has { + return nil + } + + sess := x.NewSession() + if _, err = sess.Delete(&Watch{0, watch.UserID, repo.ID}); err != nil { + return err + } + _, err = sess.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repo.ID) + + return sess.Commit() + }) +}