From 25a47585572d0a75e3feb8bdd606dbdd362dd3c2 Mon Sep 17 00:00:00 2001 From: kim Date: Thu, 21 Nov 2024 17:32:14 +0000 Subject: [PATCH] fix instance count query using string literal instead of gtsmodel const type --- internal/db/bundb/instance.go | 2 +- .../20241121121623_enum_strings_to_ints.go | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/db/bundb/instance.go b/internal/db/bundb/instance.go index bbfd82ffb..613a2b13a 100644 --- a/internal/db/bundb/instance.go +++ b/internal/db/bundb/instance.go @@ -104,7 +104,7 @@ func (i *instanceDB) CountInstanceStatuses(ctx context.Context, domain string) ( q = q.Where("NOT ? = ?", bun.Ident("status.pending_approval"), true) // Ignore statuses that are direct messages. - q = q.Where("NOT ? = ?", bun.Ident("status.visibility"), "direct") + q = q.Where("NOT ? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityDirect) count, err := q.Count(ctx) if err != nil { 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 6fc024eac..4375e5268 100644 --- a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go +++ b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go @@ -165,15 +165,35 @@ func convertEnums[OldType ~string, NewType ~int]( return err } - // Update existing values via mapping. + // Get a count of all in table. + total, err := tx.NewSelect(). + Table(table). + Count(ctx) + if err != nil { + return err + } + + var updated int for old, new := range mapping { - if _, err := tx.NewUpdate(). + + // Update old to new values. + res, err := tx.NewUpdate(). Table(table). Where("? = ?", bun.Ident(column), old). Set("? = ?", bun.Ident(newColumn), new). - Exec(ctx); err != nil { + Exec(ctx) + if err != nil { return err } + + // Count number items updated. + n, _ := res.RowsAffected() + updated += int(n) + } + + // Check total updated. + if total != updated { + log.Warnf(ctx, "total=%d does not match updated=%d", total, updated) } // Drop the old column from table.