gtsocial-umbx

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

20220905150505_custom_emoji_updates.go (3389B)


      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 	"database/sql"
     23 
     24 	gtsmodel "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations/20211113114307_init"
     25 	"github.com/uptrace/bun"
     26 	"github.com/uptrace/bun/dialect"
     27 )
     28 
     29 func init() {
     30 	up := func(ctx context.Context, db *bun.DB) error {
     31 		return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
     32 			// SQLite doesn't mind creating multiple constraints with the same name,
     33 			// but Postgres balks at it, so remove the constraint before we go editing
     34 			// the emoji tables.
     35 			if tx.Dialect().Name() == dialect.PG {
     36 				if _, err := tx.ExecContext(ctx, "ALTER TABLE ? DROP CONSTRAINT ?", bun.Ident("emojis"), bun.Safe("shortcodedomain")); err != nil {
     37 					return err
     38 				}
     39 			}
     40 
     41 			// create the new emojis table
     42 			if _, err := tx.
     43 				NewCreateTable().
     44 				Model(&gtsmodel.Emoji{}).
     45 				ModelTableExpr("new_emojis").
     46 				Exec(ctx); err != nil {
     47 				return err
     48 			}
     49 
     50 			// move all old emojis to the new table
     51 			currentEmojis := []*gtsmodel.Emoji{}
     52 			if err := tx.
     53 				NewSelect().
     54 				Model(&currentEmojis).
     55 				Scan(ctx); err != nil && err != sql.ErrNoRows {
     56 				return err
     57 			}
     58 
     59 			for _, currentEmoji := range currentEmojis {
     60 				if _, err := tx.
     61 					NewInsert().
     62 					Model(currentEmoji).
     63 					ModelTableExpr("new_emojis").
     64 					Exec(ctx); err != nil {
     65 					return err
     66 				}
     67 			}
     68 
     69 			// we have all the data we need from the old table, so we can safely drop it now
     70 			if _, err := tx.NewDropTable().Model(&gtsmodel.Emoji{}).Exec(ctx); err != nil {
     71 				return err
     72 			}
     73 
     74 			// rename the new table to the same name as the old table was
     75 			if _, err := tx.ExecContext(ctx, "ALTER TABLE ? RENAME TO ?", bun.Ident("new_emojis"), bun.Ident("emojis")); err != nil {
     76 				return err
     77 			}
     78 
     79 			// add indexes to the new table
     80 			if _, err := tx.
     81 				NewCreateIndex().
     82 				Model(&gtsmodel.Emoji{}).
     83 				Index("emojis_id_idx").
     84 				Column("id").
     85 				Exec(ctx); err != nil {
     86 				return err
     87 			}
     88 
     89 			if _, err := tx.
     90 				NewCreateIndex().
     91 				Model(&gtsmodel.Emoji{}).
     92 				Index("emojis_uri_idx").
     93 				Column("uri").
     94 				Exec(ctx); err != nil {
     95 				return err
     96 			}
     97 
     98 			if _, err := tx.
     99 				NewCreateIndex().
    100 				Model(&gtsmodel.Emoji{}).
    101 				Index("emojis_available_custom_idx").
    102 				Column("visible_in_picker", "disabled", "shortcode").
    103 				Exec(ctx); err != nil {
    104 				return err
    105 			}
    106 
    107 			return nil
    108 		})
    109 	}
    110 
    111 	down := func(ctx context.Context, db *bun.DB) error {
    112 		return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
    113 			return nil
    114 		})
    115 	}
    116 
    117 	if err := Migrations.Register(up, down); err != nil {
    118 		panic(err)
    119 	}
    120 }