GoToSocial/internal/db/bundb/migrations
Blackle Morisanchetto daec9ab10e
[feature] Sort follow requests, followers, and following by updated_at (#774)
* Sort follow requests, followers, and following by updated_at

* Add migration to regenerate indexes for follows and follow requests
2022-08-31 11:27:39 +02:00
..
20211113114307_init Extend license notices to 2022 (#354) 2021-12-20 18:42:19 +01:00
20220214175650_media_cleanup [feature] Clean up/uncache remote media (#407) 2022-03-07 11:08:26 +01:00
20220315160814_admin_account_actions [feature] Admin account actions (#432) 2022-03-19 12:01:40 +01:00
20211113114307_init.go Extend license notices to 2022 (#354) 2021-12-20 18:42:19 +01:00
20220214175650_media_cleanup.go [bugfix] use Exec to rename media_attachments (#498) 2022-04-28 08:43:41 +01:00
20220305130328_database_optimizations.go [performance] Database optimizations (#419) 2022-03-07 11:33:18 +01:00
20220315160814_admin_account_actions.go [feature] Admin account actions (#432) 2022-03-19 12:01:40 +01:00
20220502113806_add_missing_indexes.go add extra indexes as a migration (#527) 2022-05-02 13:55:48 +01:00
20220506110822_add_account_raw_note.go postgres locale: fix accounts.note_raw migration (#651) 2022-06-16 11:22:51 +02:00
20220511165212_add_account_raw_note_fix.go [bugfix] for the second accounts.note_raw migration (#653) 2022-06-16 19:39:57 +02:00
20220518123621_additional_indexing.go [performance] Add further indexes to mitigate laggy queries (#586) 2022-05-18 15:58:26 +01:00
20220612091800_duplicated_media_cleanup.go [chore] use our own logging implementation (#716) 2022-07-19 10:47:55 +02:00
20220710153020_fix_slow_web_profile_queries.go [performance] Add new index to fix slow web profile queries (#706) 2022-07-11 12:52:12 +01:00
20220804120132_account_default_post_format.go [feature] Let accounts set default status format, and use this when processing new statuses (#739) 2022-08-06 12:09:21 +02:00
20220827085121_assign_missing_in_reply_to_uris.go [bugfix] Status visibility + `excludeReplies` fixes (#769) 2022-08-27 11:35:31 +02:00
20220830014732_regenerate_indexes_for_follows.go [feature] Sort follow requests, followers, and following by updated_at (#774) 2022-08-31 11:27:39 +02:00
README.md Extend license notices to 2022 (#354) 2021-12-20 18:42:19 +01:00
main.go [chore] use our own logging implementation (#716) 2022-07-19 10:47:55 +02:00

README.md

Migrations

How do I write a migration file?

See here

As a template, take one of the existing migration files and modify it, or use the below code snippet:

/*
   GoToSocial
   Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package migrations

import (
    "context"

    "github.com/uptrace/bun"
)

func init() {
    up := func(ctx context.Context, db *bun.DB) error {
        return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
            // your logic here
            return nil
        })
    }

    down := func(ctx context.Context, db *bun.DB) error {
        return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
            // your logic here
            return nil
        })
    }

    if err := Migrations.Register(up, down); err != nil {
        panic(err)
    }
}

File format

Bun requires a very specific format: 14 digits, then letters or underscores.

You can use the following bash command on your branch to generate a suitable migration filename.

echo "$(date --utc +%Y%m%d%H%M%S | head -c 14)_$(git rev-parse --abbrev-ref HEAD).go"

Rules of thumb

  1. DON'T DROP TABLES!!!!!!!!
  2. Don't make something NOT NULL if it's likely to already contain null fields.