mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-02-23 23:17:43 +01:00
[bugfix] Drop status indices AFTER updating visibility (#3795)
* [bugfix] Drop status indices AFTER updating visibility * rename to status vis indices just to indicate they're only used in the statuses hook func --------- Co-authored-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
1ee56d66ce
commit
879ca2d2f8
@ -32,20 +32,8 @@ func init() {
|
||||
up := func(ctx context.Context, db *bun.DB) error {
|
||||
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
|
||||
// Tables with visibility types.
|
||||
var visTables = []struct {
|
||||
Table string
|
||||
Column string
|
||||
Default *new_gtsmodel.Visibility
|
||||
}{
|
||||
{Table: "statuses", Column: "visibility"},
|
||||
{Table: "sin_bin_statuses", Column: "visibility"},
|
||||
{Table: "account_settings", Column: "privacy", Default: util.Ptr(new_gtsmodel.VisibilityDefault)},
|
||||
{Table: "account_settings", Column: "web_visibility", Default: util.Ptr(new_gtsmodel.VisibilityDefault)},
|
||||
}
|
||||
|
||||
// Visibility type indices.
|
||||
var visIndices = []struct {
|
||||
// Status visibility type indices.
|
||||
var statusVisIndices = []struct {
|
||||
name string
|
||||
cols []string
|
||||
order string
|
||||
@ -67,10 +55,20 @@ func init() {
|
||||
},
|
||||
}
|
||||
|
||||
// Before making changes to the visibility col
|
||||
// we must drop all indices that rely on it.
|
||||
log.Info(ctx, "dropping old visibility indexes...")
|
||||
for _, index := range visIndices {
|
||||
// Tables with visibility types.
|
||||
var visTables = []struct {
|
||||
Table string
|
||||
Column string
|
||||
Default *new_gtsmodel.Visibility
|
||||
IndexCleanupCallback func(ctx context.Context, tx bun.Tx) error
|
||||
}{
|
||||
{
|
||||
Table: "statuses",
|
||||
Column: "visibility",
|
||||
IndexCleanupCallback: func(ctx context.Context, tx bun.Tx) error {
|
||||
// After new column has been created and
|
||||
// populated, drop indices relying on old column.
|
||||
for _, index := range statusVisIndices {
|
||||
log.Infof(ctx, "dropping old index %s...", index.name)
|
||||
if _, err := tx.NewDropIndex().
|
||||
Index(index.name).
|
||||
@ -78,6 +76,22 @@ func init() {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Table: "sin_bin_statuses",
|
||||
Column: "visibility",
|
||||
},
|
||||
{
|
||||
Table: "account_settings",
|
||||
Column: "privacy",
|
||||
Default: util.Ptr(new_gtsmodel.VisibilityDefault)},
|
||||
{
|
||||
Table: "account_settings",
|
||||
Column: "web_visibility",
|
||||
Default: util.Ptr(new_gtsmodel.VisibilityDefault)},
|
||||
}
|
||||
|
||||
// Get the mapping of old enum string values to new integer values.
|
||||
visibilityMapping := visibilityEnumMapping[old_gtsmodel.Visibility]()
|
||||
@ -85,14 +99,14 @@ func init() {
|
||||
// Convert all visibility tables.
|
||||
for _, table := range visTables {
|
||||
if err := convertEnums(ctx, tx, table.Table, table.Column,
|
||||
visibilityMapping, table.Default); err != nil {
|
||||
visibilityMapping, table.Default, table.IndexCleanupCallback); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Recreate the visibility indices.
|
||||
log.Info(ctx, "creating new visibility indexes...")
|
||||
for _, index := range visIndices {
|
||||
for _, index := range statusVisIndices {
|
||||
log.Infof(ctx, "creating new index %s...", index.name)
|
||||
q := tx.NewCreateIndex().
|
||||
Table("statuses").
|
||||
@ -111,7 +125,7 @@ func init() {
|
||||
|
||||
// Migrate over old notifications table column over to new column type.
|
||||
if err := convertEnums(ctx, tx, "notifications", "notification_type", //nolint:revive
|
||||
notificationMapping, nil); err != nil {
|
||||
notificationMapping, nil, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -45,6 +45,7 @@ func convertEnums[OldType ~string, NewType ~int16](
|
||||
column string,
|
||||
mapping map[OldType]NewType,
|
||||
defaultValue *NewType,
|
||||
indexCleanupCallback func(context.Context, bun.Tx) error,
|
||||
) error {
|
||||
if len(mapping) == 0 {
|
||||
return errors.New("empty mapping")
|
||||
@ -110,6 +111,13 @@ func convertEnums[OldType ~string, NewType ~int16](
|
||||
log.Warnf(ctx, "total=%d does not match updated=%d", total, updated)
|
||||
}
|
||||
|
||||
// Run index cleanup callback if set.
|
||||
if indexCleanupCallback != nil {
|
||||
if err := indexCleanupCallback(ctx, tx); err != nil {
|
||||
return gtserror.Newf("error running index cleanup callback: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Drop the old column from table.
|
||||
if _, err := tx.NewDropColumn().
|
||||
Table(table).
|
||||
|
Loading…
x
Reference in New Issue
Block a user