From 02d338bf21905fb42bc8452b6d49f1328ded6f8a Mon Sep 17 00:00:00 2001 From: tobi Date: Mon, 3 Mar 2025 18:45:07 +0100 Subject: [PATCH] tweak migration a bit for SPEED --- ...0250224105654_token_app_client_refactor.go | 45 +++++-------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/internal/db/bundb/migrations/20250224105654_token_app_client_refactor.go b/internal/db/bundb/migrations/20250224105654_token_app_client_refactor.go index 02876bffa..4ceef0808 100644 --- a/internal/db/bundb/migrations/20250224105654_token_app_client_refactor.go +++ b/internal/db/bundb/migrations/20250224105654_token_app_client_refactor.go @@ -29,33 +29,7 @@ import ( func init() { up := func(ctx context.Context, db *bun.DB) error { - log.Info(ctx, "migrating applications to new model") - - // To make update queries faster, - // create an index on statuses - // that we'll drop when we're done. - log.Info(ctx, "creating temporary index statuses_created_with_application_id_idx, please wait...") - if _, err := db. - NewCreateIndex(). - Table("statuses"). - Index("statuses_created_with_application_id_idx"). - Column("created_with_application_id"). - Where("? = ?", bun.Ident("local"), true). - IfNotExists(). - Exec(ctx); err != nil { - return err - } - - defer func() { - log.Info(ctx, "cleaning up temporary index statuses_created_with_application_id_idx, please wait...") - if _, err := db. - NewDropIndex(). - Index("statuses_created_with_application_id_idx"). - IfExists(). - Exec(ctx); err != nil { - panic(err) - } - }() + log.Info(ctx, "migrating applications to new model, this may take a bit of time, please wait and do not interrupt!") return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { @@ -117,21 +91,24 @@ func init() { return err } - if len(oldApps) != 0 { + appsCount := len(oldApps) + if appsCount != 0 { // Convert all the old model applications into new ones. - newApps := make([]*newmodel.Application, 0, len(oldApps)) - for _, oldApp := range oldApps { - newAppID := id.NewULIDFromTime(oldApp.CreatedAt) - - log.Info(ctx, "migrating application %s (%s) to new model...", oldApp.Name, oldApp.ID) + newApps := make([]*newmodel.Application, 0, appsCount) + for i, oldApp := range oldApps { + log.Infof(ctx, "preparing to migrate application %s (%s) to new model...", oldApp.Name, oldApp.ID) // Update application ID on any // statuses that used this app. + newAppID := id.NewULIDFromTime(oldApp.CreatedAt) if _, err := tx. NewUpdate(). Table("statuses"). Set("? = ?", bun.Ident("created_with_application_id"), newAppID). + // Only local statuses have created_with_application_id set, + // and we have an index on "local", so use this to narrow down. + Where("? = ?", bun.Ident("local"), true). Where("? = ?", bun.Ident("created_with_application_id"), oldApp.ID). Exec(ctx); err != nil { return err @@ -147,6 +124,8 @@ func init() { ClientSecret: oldApp.ClientSecret, Scopes: oldApp.Scopes, }) + + log.Infof(ctx, "prepared %d of %d new model applications", i+1, appsCount) } // Whack all the new apps in