Browse Source
* [chore] Move local account settings to separate database model * don't use separate settings_idpull/2780/head
36 changed files with 526 additions and 192 deletions
@ -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) |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@
|
||||
// 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 gtsmodel |
||||
|
||||
import "time" |
||||
|
||||
// AccountSettings models settings / preferences for a local, non-instance account.
|
||||
type AccountSettings struct { |
||||
AccountID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // AccountID that owns this settings.
|
||||
CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created.
|
||||
UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item was last updated.
|
||||
Reason string `bun:",nullzero"` // What reason was given for signing up when this account was created?
|
||||
Privacy Visibility `bun:",nullzero"` // Default post privacy for this account
|
||||
Sensitive *bool `bun:",nullzero,notnull,default:false"` // Set posts from this account to sensitive by default?
|
||||
Language string `bun:",nullzero,notnull,default:'en'"` // What language does this account post in?
|
||||
StatusContentType string `bun:",nullzero"` // What is the default format for statuses posted by this account (only for local accounts).
|
||||
CustomCSS string `bun:",nullzero"` // Custom CSS that should be displayed for this Account's profile and statuses.
|
||||
EnableRSS *bool `bun:",nullzero,notnull,default:false"` // enable RSS feed subscription for this account's public posts at [URL]/feed
|
||||
HideCollections *bool `bun:",nullzero,notnull,default:false"` // Hide this account's followers/following collections.
|
||||
} |
||||
Loading…
Reference in new issue