Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[performance] improved enum migrations #3782

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {
// we must drop all indices that rely on it.
log.Info(ctx, "dropping old visibility indexes...")
for _, index := range visIndices {
log.Info(ctx, "dropping old index %s...", index.name)
log.Infof(ctx, "dropping old index %s...", index.name)
if _, err := tx.NewDropIndex().
Index(index.name).
Exec(ctx); err != nil {
Expand All @@ -93,7 +93,7 @@ func init() {
// Recreate the visibility indices.
log.Info(ctx, "creating new visibility indexes...")
for _, index := range visIndices {
log.Info(ctx, "creating new index %s...", index.name)
log.Infof(ctx, "creating new index %s...", index.name)
q := tx.NewCreateIndex().
Table("statuses").
Index(index.name).
Expand Down
37 changes: 21 additions & 16 deletions internal/db/bundb/migrations/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,31 @@ func convertEnums[OldType ~string, NewType ~int16](
return gtserror.Newf("error selecting total count: %w", err)
}

var updated int
var args []any
var qbuf byteutil.Buffer

// Prepare a singular UPDATE statement using
// SET $newColumn = (CASE $column WHEN $old THEN $new ... END)
qbuf.B = append(qbuf.B, "UPDATE ? SET ? = (CASE ? "...)
args = append(args, bun.Ident(table))
args = append(args, bun.Ident(newColumn))
args = append(args, bun.Ident(column))
for old, new := range mapping {
qbuf.B = append(qbuf.B, "WHEN ? THEN ? "...)
args = append(args, old, new)
}
qbuf.B = append(qbuf.B, "ELSE ? END)"...)
args = append(args, *defaultValue)

// Update old to new values.
res, err := tx.NewUpdate().
Table(table).
Where("? = ?", bun.Ident(column), old).
Set("? = ?", bun.Ident(newColumn), new).
Exec(ctx)
if err != nil {
return gtserror.Newf("error updating old column values: %w", err)
}

// Count number items updated.
n, _ := res.RowsAffected()
updated += int(n)
// Execute the prepared raw query with arguments.
res, err := tx.NewRaw(qbuf.String(), args...).Exec(ctx)
if err != nil {
return gtserror.Newf("error updating old column values: %w", err)
}

// Check total updated.
if total != updated {
// Count number items updated.
updated, _ := res.RowsAffected()
if total != int(updated) {
log.Warnf(ctx, "total=%d does not match updated=%d", total, updated)
}

Expand Down