From e6119b89c3c2c68e0718dc07834ff4ff710d0a1f Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Sat, 30 May 2026 20:59:46 +0200 Subject: [PATCH] fix: remove duplicate indexes in package_blob table (#12836) Table `package_blob` has both unique indexes, and standard indexes, defined on each of its hash columns. This PR removes the standard indexes and leaves just the unique indexes, which will reduce disk space usage and improve performance on writing to the table. Manual testing: confirmed that the indexes are removed by inspecting the database table in my dev environment before and after the migration. I've reviewed other database tables for similar situations where a unique index is present and non-unique indexes are present but therefore not needed. I've found no other cases. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12836 Reviewed-by: Andreas Ahlenstorf --- .../v16c_cleanup_package_blob_indexes.go | 31 +++++++++++++++++++ models/packages/package_blob.go | 10 +++--- 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 models/forgejo_migrations/v16c_cleanup_package_blob_indexes.go diff --git a/models/forgejo_migrations/v16c_cleanup_package_blob_indexes.go b/models/forgejo_migrations/v16c_cleanup_package_blob_indexes.go new file mode 100644 index 0000000000..a63c2317bd --- /dev/null +++ b/models/forgejo_migrations/v16c_cleanup_package_blob_indexes.go @@ -0,0 +1,31 @@ +// Copyright 2026 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package forgejo_migrations + +import ( + "code.forgejo.org/xorm/xorm" +) + +func init() { + registerMigration(&Migration{ + Description: "cleanup extra indexes on package_blob", + Upgrade: cleanupPackageBlobIndexes, + }) +} + +func cleanupPackageBlobIndexes(x *xorm.Engine) error { + for _, idx := range []string{ + "IDX_package_blob_hash_blake2b", + "IDX_package_blob_hash_md5", + "IDX_package_blob_hash_sha1", + "IDX_package_blob_hash_sha256", + "IDX_package_blob_hash_sha512", + } { + err := dropIndexIfExists(x, "package_blob", idx) + if err != nil { + return err + } + } + return nil +} diff --git a/models/packages/package_blob.go b/models/packages/package_blob.go index 0de4434ef8..aec8e4715b 100644 --- a/models/packages/package_blob.go +++ b/models/packages/package_blob.go @@ -30,11 +30,11 @@ func init() { type PackageBlob struct { ID int64 `xorm:"pk autoincr"` Size int64 `xorm:"NOT NULL DEFAULT 0"` - HashMD5 string `xorm:"hash_md5 char(32) UNIQUE(md5) INDEX NOT NULL"` - HashSHA1 string `xorm:"hash_sha1 char(40) UNIQUE(sha1) INDEX NOT NULL"` - HashSHA256 string `xorm:"hash_sha256 char(64) UNIQUE(sha256) INDEX NOT NULL"` - HashSHA512 string `xorm:"hash_sha512 char(128) UNIQUE(sha512) INDEX NOT NULL"` - HashBlake2b string `xorm:"hash_blake2b char(128) UNIQUE(blake2b) INDEX"` + HashMD5 string `xorm:"hash_md5 char(32) UNIQUE(md5) NOT NULL"` + HashSHA1 string `xorm:"hash_sha1 char(40) UNIQUE(sha1) NOT NULL"` + HashSHA256 string `xorm:"hash_sha256 char(64) UNIQUE(sha256) NOT NULL"` + HashSHA512 string `xorm:"hash_sha512 char(128) UNIQUE(sha512) NOT NULL"` + HashBlake2b string `xorm:"hash_blake2b char(128) UNIQUE(blake2b)"` CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"` }