GoToSocial/internal/db/bundb/migrations
kim 098dbe6ff4
[chore] use our own logging implementation (#716)
* first commit

Signed-off-by: kim <grufwub@gmail.com>

* replace logging with our own log library

Signed-off-by: kim <grufwub@gmail.com>

* fix imports

Signed-off-by: kim <grufwub@gmail.com>

* fix log imports

Signed-off-by: kim <grufwub@gmail.com>

* add license text

Signed-off-by: kim <grufwub@gmail.com>

* fix package import cycle between config and log package

Signed-off-by: kim <grufwub@gmail.com>

* fix empty kv.Fields{} being passed to WithFields()

Signed-off-by: kim <grufwub@gmail.com>

* fix uses of log.WithFields() with whitespace issues and empty slices

Signed-off-by: kim <grufwub@gmail.com>

* *linter related grumbling*

Signed-off-by: kim <grufwub@gmail.com>

* gofmt the codebase! also fix more log.WithFields() formatting issues

Signed-off-by: kim <grufwub@gmail.com>

* update testrig code to match new changes

Signed-off-by: kim <grufwub@gmail.com>

* fix error wrapping in non fmt.Errorf function

Signed-off-by: kim <grufwub@gmail.com>

* add benchmarking of log.Caller() vs non-cached

Signed-off-by: kim <grufwub@gmail.com>

* fix syslog tests, add standard build tags to test runner to ensure consistency

Signed-off-by: kim <grufwub@gmail.com>

* make syslog tests more robust

Signed-off-by: kim <grufwub@gmail.com>

* fix caller depth arithmatic (is that how you spell it?)

Signed-off-by: kim <grufwub@gmail.com>

* update to use unkeyed fields in kv.Field{} instances

Signed-off-by: kim <grufwub@gmail.com>

* update go-kv library

Signed-off-by: kim <grufwub@gmail.com>

* update libraries list

Signed-off-by: kim <grufwub@gmail.com>

* fuck you linter get nerfed

Signed-off-by: kim <grufwub@gmail.com>

Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
2022-07-19 10:47:55 +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
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.