diff options
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/migrations/20231215115920_add_status_poll_index.go | 66 | ||||
-rw-r--r-- | internal/db/bundb/poll.go | 14 | ||||
-rw-r--r-- | internal/db/bundb/poll_test.go | 8 | ||||
-rw-r--r-- | internal/db/bundb/status.go | 11 | ||||
-rw-r--r-- | internal/db/poll.go | 3 | ||||
-rw-r--r-- | internal/db/status.go | 9 |
6 files changed, 83 insertions, 28 deletions
diff --git a/internal/db/bundb/migrations/20231215115920_add_status_poll_index.go b/internal/db/bundb/migrations/20231215115920_add_status_poll_index.go new file mode 100644 index 000000000..54b585d60 --- /dev/null +++ b/internal/db/bundb/migrations/20231215115920_add_status_poll_index.go @@ -0,0 +1,66 @@ +// 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 <http://www.gnu.org/licenses/>. + +package migrations + +import ( + "context" + + "github.com/uptrace/bun" +) + +func init() { + up := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + type spec struct { + index string + table string + columns []string + } + + for _, spec := range []spec{ + { + index: "statuses_poll_id_idx", + table: "statuses", + columns: []string{"poll_id"}, + }, + } { + if _, err := tx. + NewCreateIndex(). + Table(spec.table). + Index(spec.index). + Column(spec.columns...). + IfNotExists(). + Exec(ctx); err != nil { + return err + } + } + + return nil + }) + } + + down := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + return nil + }) + } + + if err := Migrations.Register(up, down); err != nil { + panic(err) + } +} diff --git a/internal/db/bundb/poll.go b/internal/db/bundb/poll.go index 830fb88ec..3e77fb6c5 100644 --- a/internal/db/bundb/poll.go +++ b/internal/db/bundb/poll.go @@ -50,20 +50,6 @@ func (p *pollDB) GetPollByID(ctx context.Context, id string) (*gtsmodel.Poll, er ) } -func (p *pollDB) GetPollByStatusID(ctx context.Context, statusID string) (*gtsmodel.Poll, error) { - return p.getPoll( - ctx, - "StatusID", - func(poll *gtsmodel.Poll) error { - return p.db.NewSelect(). - Model(poll). - Where("? = ?", bun.Ident("poll.status_id"), statusID). - Scan(ctx) - }, - statusID, - ) -} - func (p *pollDB) getPoll(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Poll) error, keyParts ...any) (*gtsmodel.Poll, error) { // Fetch poll from database cache with loader callback poll, err := p.state.Caches.GTS.Poll().Load(lookup, func() (*gtsmodel.Poll, error) { diff --git a/internal/db/bundb/poll_test.go b/internal/db/bundb/poll_test.go index 479557c55..6bdbdb983 100644 --- a/internal/db/bundb/poll_test.go +++ b/internal/db/bundb/poll_test.go @@ -67,10 +67,6 @@ func (suite *PollTestSuite) TestGetPollBy() { "id": func() (*gtsmodel.Poll, error) { return suite.db.GetPollByID(ctx, poll.ID) }, - - "status_id": func() (*gtsmodel.Poll, error) { - return suite.db.GetPollByStatusID(ctx, poll.StatusID) - }, } { // Clear database caches. @@ -287,10 +283,6 @@ func (suite *PollTestSuite) TestDeletePoll() { // Ensure that afterwards we cannot fetch poll. _, err = suite.db.GetPollByID(ctx, poll.ID) suite.ErrorIs(err, db.ErrNoEntries) - - // Or again by the status it's attached to. - _, err = suite.db.GetPollByStatusID(ctx, poll.StatusID) - suite.ErrorIs(err, db.ErrNoEntries) } } diff --git a/internal/db/bundb/status.go b/internal/db/bundb/status.go index dd161e1ec..da252c7f7 100644 --- a/internal/db/bundb/status.go +++ b/internal/db/bundb/status.go @@ -87,6 +87,17 @@ func (s *statusDB) GetStatusByURL(ctx context.Context, url string) (*gtsmodel.St ) } +func (s *statusDB) GetStatusByPollID(ctx context.Context, pollID string) (*gtsmodel.Status, error) { + return s.getStatus( + ctx, + "PollID", + func(status *gtsmodel.Status) error { + return s.db.NewSelect().Model(status).Where("? = ?", bun.Ident("status.poll_id"), pollID).Scan(ctx) + }, + pollID, + ) +} + func (s *statusDB) GetStatusBoost(ctx context.Context, boostOfID string, byAccountID string) (*gtsmodel.Status, error) { return s.getStatus( ctx, diff --git a/internal/db/poll.go b/internal/db/poll.go index b59d27c73..ac0229855 100644 --- a/internal/db/poll.go +++ b/internal/db/poll.go @@ -27,9 +27,6 @@ type Poll interface { // GetPollByID fetches the Poll with given ID from the database. GetPollByID(ctx context.Context, id string) (*gtsmodel.Poll, error) - // GetPollByStatusID fetches the Poll with given status ID column value from the database. - GetPollByStatusID(ctx context.Context, statusID string) (*gtsmodel.Poll, error) - // GetOpenPolls fetches all local Polls in the database with an unset `closed_at` column. GetOpenPolls(ctx context.Context) ([]*gtsmodel.Poll, error) diff --git a/internal/db/status.go b/internal/db/status.go index 1ebf503a8..8034d39e7 100644 --- a/internal/db/status.go +++ b/internal/db/status.go @@ -25,15 +25,18 @@ import ( // Status contains functions for getting statuses, creating statuses, and checking various other fields on statuses. type Status interface { - // GetStatusByID returns one status from the database, with no rel fields populated, only their linking ID / URIs + // GetStatusByID fetches the status from the database with matching id column. GetStatusByID(ctx context.Context, id string) (*gtsmodel.Status, error) - // GetStatusByURI returns one status from the database, with no rel fields populated, only their linking ID / URIs + // GetStatusByURI fetches the status from the database with matching uri column. GetStatusByURI(ctx context.Context, uri string) (*gtsmodel.Status, error) - // GetStatusByURL returns one status from the database, with no rel fields populated, only their linking ID / URIs + // GetStatusByURL fetches the status from the database with matching url column. GetStatusByURL(ctx context.Context, uri string) (*gtsmodel.Status, error) + // GetStatusByPollID fetches the status from the database with matching poll_id column. + GetStatusByPollID(ctx context.Context, pollID string) (*gtsmodel.Status, error) + // GetStatusBoost fetches the status whose boost_of_id column refers to boostOfID, authored by given account ID. GetStatusBoost(ctx context.Context, boostOfID string, byAccountID string) (*gtsmodel.Status, error) |