gtsocial-umbx

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

20230515173919_lists.go (2402B)


      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 	gtsmodel "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 			// List table.
     31 			if _, err := tx.
     32 				NewCreateTable().
     33 				Model(&gtsmodel.List{}).
     34 				IfNotExists().
     35 				Exec(ctx); err != nil {
     36 				return err
     37 			}
     38 
     39 			// Add indexes to the List table.
     40 			for index, columns := range map[string][]string{
     41 				"lists_id_idx":         {"id"},
     42 				"lists_account_id_idx": {"account_id"},
     43 			} {
     44 				if _, err := tx.
     45 					NewCreateIndex().
     46 					Table("lists").
     47 					Index(index).
     48 					Column(columns...).
     49 					Exec(ctx); err != nil {
     50 					return err
     51 				}
     52 			}
     53 
     54 			// List entry table.
     55 			if _, err := tx.
     56 				NewCreateTable().
     57 				Model(&gtsmodel.ListEntry{}).
     58 				IfNotExists().
     59 				Exec(ctx); err != nil {
     60 				return err
     61 			}
     62 
     63 			// Add indexes to the List entry table.
     64 			for index, columns := range map[string][]string{
     65 				"list_entries_id_idx":        {"id"},
     66 				"list_entries_list_id_idx":   {"list_id"},
     67 				"list_entries_follow_id_idx": {"follow_id"},
     68 			} {
     69 				if _, err := tx.
     70 					NewCreateIndex().
     71 					Table("list_entries").
     72 					Index(index).
     73 					Column(columns...).
     74 					Exec(ctx); err != nil {
     75 					return err
     76 				}
     77 			}
     78 
     79 			return nil
     80 		})
     81 	}
     82 
     83 	down := func(ctx context.Context, db *bun.DB) error {
     84 		return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
     85 			return nil
     86 		})
     87 	}
     88 
     89 	if err := Migrations.Register(up, down); err != nil {
     90 		panic(err)
     91 	}
     92 }