gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

20220502113806_add_missing_indexes.go (3028B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package migrations
     19 
     20 import (
     21 	"context"
     22 
     23 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     24 	"github.com/uptrace/bun"
     25 )
     26 
     27 func init() {
     28 	up := func(ctx context.Context, db *bun.DB) error {
     29 		return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
     30 			// new account indexes
     31 			if _, err := tx.
     32 				NewCreateIndex().
     33 				Model(&gtsmodel.Account{}).
     34 				Index("accounts_url_idx").
     35 				Column("url").
     36 				Exec(ctx); err != nil {
     37 				return err
     38 			}
     39 
     40 			if _, err := tx.
     41 				NewCreateIndex().
     42 				Model(&gtsmodel.Account{}).
     43 				Index("accounts_public_key_uri_idx").
     44 				Column("public_key_uri").
     45 				Exec(ctx); err != nil {
     46 				return err
     47 			}
     48 
     49 			if _, err := tx.
     50 				NewCreateIndex().
     51 				Model(&gtsmodel.Account{}).
     52 				Index("accounts_suspended_at_idx").
     53 				Column("suspended_at").
     54 				Exec(ctx); err != nil {
     55 				return err
     56 			}
     57 
     58 			// new user indexes
     59 			if _, err := tx.
     60 				NewCreateIndex().
     61 				Model(&gtsmodel.User{}).
     62 				Index("users_account_id_idx").
     63 				Column("account_id").
     64 				Exec(ctx); err != nil {
     65 				return err
     66 			}
     67 
     68 			// new tags indexes
     69 			if _, err := tx.
     70 				NewCreateIndex().
     71 				Model(&gtsmodel.Tag{}).
     72 				Index("tags_name_idx").
     73 				Column("name").
     74 				Exec(ctx); err != nil {
     75 				return err
     76 			}
     77 
     78 			// new applications indexes
     79 			if _, err := tx.
     80 				NewCreateIndex().
     81 				Model(&gtsmodel.Application{}).
     82 				Index("applications_client_id_idx").
     83 				Column("client_id").
     84 				Exec(ctx); err != nil {
     85 				return err
     86 			}
     87 
     88 			// new status_faves indexes
     89 			if _, err := tx.
     90 				NewCreateIndex().
     91 				Model(&gtsmodel.StatusFave{}).
     92 				Index("status_faves_account_id_idx").
     93 				Column("account_id").
     94 				Exec(ctx); err != nil {
     95 				return err
     96 			}
     97 
     98 			// new status_mutes indexes
     99 			if _, err := tx.
    100 				NewCreateIndex().
    101 				Model(&gtsmodel.StatusMute{}).
    102 				Index("status_mutes_account_id_target_account_id_status_id_idx").
    103 				Column("account_id", "target_account_id", "status_id").
    104 				Exec(ctx); err != nil {
    105 				return err
    106 			}
    107 
    108 			return nil
    109 		})
    110 	}
    111 
    112 	down := func(ctx context.Context, db *bun.DB) error {
    113 		return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
    114 			return nil
    115 		})
    116 	}
    117 
    118 	if err := Migrations.Register(up, down); err != nil {
    119 		panic(err)
    120 	}
    121 }