mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 08:42:26 -05:00
# Description This is quite a complex database migration that updates the `statuses.thread_id` column to be notnull, in order that statuses always be threaded, which will be useful in various pieces of upcoming work. This is unfortunately a migration that acts over the entire statuses table, and is quite complex in order to ensure that all existing statuses get correctly threaded together, and where possible fix any issues of statuses in the same thread having incorrect thread_ids. TODO: - ~~update testrig models to all be threaded~~ - ~~update code to ensure thread_id is always set~~ - ~~run on **a copy** of an sqlite production database~~ - ~~run on **a copy** of a postgres production database~~ ## Checklist - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [x] I/we have performed a self-review of added code. - [x] I/we have written code that is legible and maintainable by others. - [x] I/we have commented the added code, particularly in hard-to-understand areas. - [ ] I/we have made any necessary changes to documentation. - [x] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [x] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4160 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
// 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"
|
|
"strings"
|
|
|
|
gtsmodel "code.superseriousbusiness.org/gotosocial/internal/db/bundb/migrations/20231016113235_mute_status_thread"
|
|
"code.superseriousbusiness.org/gotosocial/internal/log"
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect"
|
|
)
|
|
|
|
func init() {
|
|
up := func(ctx context.Context, db *bun.DB) error {
|
|
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
// Create thread table.
|
|
if _, err := tx.
|
|
NewCreateTable().
|
|
Model(>smodel.Thread{}).
|
|
IfNotExists().
|
|
Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create thread intermediate table.
|
|
if _, err := tx.
|
|
NewCreateTable().
|
|
Model(>smodel.ThreadToStatus{}).
|
|
IfNotExists().
|
|
Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Drop old pkey constraint from
|
|
// deprecated status mute table.
|
|
//
|
|
// This is only necessary with postgres.
|
|
if tx.Dialect().Name() == dialect.PG {
|
|
if _, err := tx.ExecContext(
|
|
ctx,
|
|
"ALTER TABLE ? DROP CONSTRAINT IF EXISTS ?",
|
|
bun.Ident("status_mutes"),
|
|
bun.Safe("status_mutes_pkey"),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Drop old index.
|
|
if _, err := tx.
|
|
NewDropIndex().
|
|
Index("status_mutes_account_id_target_account_id_status_id_idx").
|
|
IfExists().
|
|
Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Drop deprecated status mute table.
|
|
if _, err := tx.
|
|
NewDropTable().
|
|
Table("status_mutes").
|
|
IfExists().
|
|
Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create new thread mute table.
|
|
if _, err := tx.
|
|
NewCreateTable().
|
|
Model(>smodel.ThreadMute{}).
|
|
IfNotExists().
|
|
Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Info(ctx, "creating a new index on the statuses table, please wait and don't interrupt it (this may take a few minutes)")
|
|
|
|
// Update statuses to add thread ID column.
|
|
_, err := tx.ExecContext(
|
|
ctx,
|
|
"ALTER TABLE ? ADD COLUMN ? CHAR(26)",
|
|
bun.Ident("statuses"),
|
|
bun.Ident("thread_id"),
|
|
)
|
|
if err != nil && !(strings.Contains(err.Error(), "already exists") ||
|
|
strings.Contains(err.Error(), "duplicate column name") ||
|
|
strings.Contains(err.Error(), "SQLSTATE 42701")) {
|
|
return err
|
|
}
|
|
|
|
// Index new + existing tables properly.
|
|
for table, indexes := range map[string]map[string][]string{
|
|
"threads": {
|
|
"threads_id_idx": {"id"},
|
|
},
|
|
"thread_mutes": {
|
|
"thread_mutes_id_idx": {"id"},
|
|
// Eg., check if target thread is muted by account.
|
|
"thread_mutes_thread_id_account_id_idx": {"thread_id", "account_id"},
|
|
},
|
|
"statuses": {
|
|
// Eg., select all statuses in a thread.
|
|
"statuses_thread_id_idx": {"thread_id"},
|
|
},
|
|
} {
|
|
for index, columns := range indexes {
|
|
if _, err := tx.
|
|
NewCreateIndex().
|
|
Table(table).
|
|
Index(index).
|
|
Column(columns...).
|
|
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)
|
|
}
|
|
}
|