From 336db3d6ca223b73459639c7abbbfa00dc58b4ff Mon Sep 17 00:00:00 2001 From: kim Date: Mon, 25 Nov 2024 11:13:03 +0000 Subject: [PATCH] use int16 for enum types --- .../20241121121623_enum_strings_to_ints.go | 4 ++-- internal/gtsmodel/common.go | 24 +++++++++++++++++++ internal/gtsmodel/notification.go | 2 +- internal/gtsmodel/status.go | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 internal/gtsmodel/common.go diff --git a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go index 5c25d0daa..38f80876f 100644 --- a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go +++ b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go @@ -130,7 +130,7 @@ func init() { // convertEnums performs a transaction that converts // a table's column of our old-style enums (strings) to // more performant and space-saving integer types. -func convertEnums[OldType ~string, NewType ~int]( +func convertEnums[OldType ~string, NewType ~int16]( ctx context.Context, tx bun.Tx, table string, @@ -159,7 +159,7 @@ func convertEnums[OldType ~string, NewType ~int]( // Add new column to database. if _, err := tx.NewAddColumn(). Table(table). - ColumnExpr("? INTEGER NOT NULL DEFAULT ?", + ColumnExpr("? SMALLINT NOT NULL DEFAULT ?", bun.Ident(newColumn), *defaultValue). Exec(ctx); err != nil { diff --git a/internal/gtsmodel/common.go b/internal/gtsmodel/common.go new file mode 100644 index 000000000..e740bbb81 --- /dev/null +++ b/internal/gtsmodel/common.go @@ -0,0 +1,24 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package gtsmodel + +// enumType is the type we (at least, should) use +// for database enum types. it is the largest size +// supported by a PostgreSQL SMALLINT, since an +// SQLite SMALLINT is actually variable in size. +type enumType int16 diff --git a/internal/gtsmodel/notification.go b/internal/gtsmodel/notification.go index 88d5247be..49f1fe2bb 100644 --- a/internal/gtsmodel/notification.go +++ b/internal/gtsmodel/notification.go @@ -36,7 +36,7 @@ type Notification struct { // NotificationType describes the // reason/type of this notification. -type NotificationType int +type NotificationType enumType const ( // Notification Types diff --git a/internal/gtsmodel/status.go b/internal/gtsmodel/status.go index 245b06fcd..f8bd068ab 100644 --- a/internal/gtsmodel/status.go +++ b/internal/gtsmodel/status.go @@ -265,7 +265,7 @@ type StatusToEmoji struct { // Visibility represents the // visibility granularity of a status. -type Visibility int +type Visibility enumType const ( // VisibilityNone means nobody can see this.