GoToSocial/internal/db/bundb/migrations
Terin Stock 8e80f983b3
[bugfix] use Exec to rename media_attachments (#498)
The `database/sql` package in the Go stdlib has two methods that can run
queries against the database driver: `Query` and `Exec`. When the query
returns rows, such as "SELECT", the package expects the use of `Query`,
and returns `*sql.Rows`. When the query does not return rows, the
package expects the use of `Exec`, which returns `sql.Result`.

This changeset corrects the "media_attachments" migration to using
`ExecContext`, as "ALTER TABLE" does not return rows.

Signed-off-by: Terin Stock <terinjokes@gmail.com>
2022-04-28 08:43:41 +01: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
README.md Extend license notices to 2022 (#354) 2021-12-20 18:42:19 +01:00
main.go Extend license notices to 2022 (#354) 2021-12-20 18:42:19 +01: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.