summaryrefslogtreecommitdiff
path: root/internal/db/bundb/migrations
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2024-03-22 14:03:46 +0100
committerLibravatar GitHub <noreply@github.com>2024-03-22 14:03:46 +0100
commit7f4a0a1aeb8a294ee967c63d7a48446df013ec44 (patch)
treeb9b3836fa0abe1d7a5758d07d6ebb6486a353d56 /internal/db/bundb/migrations
parent[bugfix] add all possible busy result codes to the sqlite errBusy catching ch... (diff)
downloadgotosocial-7f4a0a1aeb8a294ee967c63d7a48446df013ec44.tar.xz
[chore] Move local account settings to separate db table (#2770)
* [chore] Move local account settings to separate database model * don't use separate settings_id
Diffstat (limited to 'internal/db/bundb/migrations')
-rw-r--r--internal/db/bundb/migrations/20240318115336_account_settings.go122
1 files changed, 122 insertions, 0 deletions
diff --git a/internal/db/bundb/migrations/20240318115336_account_settings.go b/internal/db/bundb/migrations/20240318115336_account_settings.go
new file mode 100644
index 000000000..90d3ff420
--- /dev/null
+++ b/internal/db/bundb/migrations/20240318115336_account_settings.go
@@ -0,0 +1,122 @@
+// 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"
+
+ oldgtsmodel "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations/20230328203024_migration_fix"
+ newgtsmodel "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+
+ "github.com/uptrace/bun"
+)
+
+func init() {
+ up := func(ctx context.Context, db *bun.DB) error {
+ log.Info(ctx, "migrating account settings to new table, please wait...")
+ return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+ // Columns we'll be moving
+ // to AccountSettings.
+ var columns = []string{
+ "reason",
+ "privacy",
+ "sensitive",
+ "language",
+ "status_content_type",
+ "custom_css",
+ "enable_rss",
+ "hide_collections",
+ }
+
+ // Create the new account settings table.
+ if _, err := tx.
+ NewCreateTable().
+ Model(&newgtsmodel.AccountSettings{}).
+ IfNotExists().
+ Exec(ctx); err != nil {
+ return err
+ }
+
+ // Select each local account.
+ accounts := []*oldgtsmodel.Account{}
+ if err := tx.
+ NewSelect().
+ TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
+ Column("account.id").
+ Column(columns...).
+ Join(
+ "JOIN ? AS ? ON ? = ?",
+ bun.Ident("users"), bun.Ident("user"),
+ bun.Ident("user.account_id"), bun.Ident("account.id"),
+ ).
+ Scan(ctx, &accounts); err != nil {
+ return err
+ }
+
+ // Create a settings entry for each existing account, taking
+ // values from the old account model (with sensible defaults).
+ for _, account := range accounts {
+ settings := &newgtsmodel.AccountSettings{
+ AccountID: account.ID,
+ CreatedAt: account.CreatedAt,
+ Reason: account.Reason,
+ Privacy: newgtsmodel.Visibility(account.Privacy),
+ Sensitive: util.Ptr(util.PtrValueOr(account.Sensitive, false)),
+ Language: account.Language,
+ StatusContentType: account.StatusContentType,
+ CustomCSS: account.CustomCSS,
+ EnableRSS: util.Ptr(util.PtrValueOr(account.EnableRSS, false)),
+ HideCollections: util.Ptr(util.PtrValueOr(account.HideCollections, false)),
+ }
+
+ // Insert the settings model.
+ if _, err := tx.
+ NewInsert().
+ Model(settings).
+ Exec(ctx); err != nil {
+ return err
+ }
+ }
+
+ // Drop now unused columns from accounts table.
+ for _, column := range columns {
+ if _, err := tx.
+ NewDropColumn().
+ Table("accounts").
+ Column(column).
+ 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)
+ }
+}