From 04b4431bc0981e17cb0c84e8a629eab25441b013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=B6=E7=88=B8?= <1@5.nu> Date: Mon, 11 Jun 2018 21:29:24 +0800 Subject: [PATCH] migrations: clean unlinked webhook and hook_tasks --- models/migrations/migrations.go | 2 ++ models/migrations/v19.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 models/migrations/v19.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 06747d381..5192419ba 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -64,6 +64,8 @@ var migrations = []Migration{ NewMigration("update repository sizes", updateRepositorySizes), // v16 -> v17:v0.10.31 NewMigration("remove invalid protect branch whitelist", removeInvalidProtectBranchWhitelist), + // v18 -> v19:v0.11.55 + NewMigration("clean unlinked webhook and hook_tasks", cleanUnlinkedWebhookAndHookTasks), } // Migrate database to current version diff --git a/models/migrations/v19.go b/models/migrations/v19.go new file mode 100644 index 000000000..839e181f3 --- /dev/null +++ b/models/migrations/v19.go @@ -0,0 +1,18 @@ +// Copyright 2018 The Gogs 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 ( + "github.com/go-xorm/xorm" +) + +func cleanUnlinkedWebhookAndHookTasks(x *xorm.Engine) error { + _, err := x.Exec(`DELETE FROM webhook WHERE repo_id NOT IN (SELECT id FROM repository);`) + if err != nil { + return err + } + _, err = x.Exec(`DELETE FROM hook_task WHERE repo_id NOT IN (SELECT id FROM repository);`) + return err +}