diff options
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/bundb.go | 29 | ||||
-rw-r--r-- | internal/db/bundb/migrations/20210816411877_struct_validation.go | 54 | ||||
-rw-r--r-- | internal/db/bundb/migrations/README.md | 21 | ||||
-rw-r--r-- | internal/db/bundb/migrations/main.go | 28 |
4 files changed, 132 insertions, 0 deletions
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index 248232fe3..ee26eeee1 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -37,11 +37,13 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/cache" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/dialect/sqlitedialect" + "github.com/uptrace/bun/migrate" _ "modernc.org/sqlite" ) @@ -73,6 +75,29 @@ type bunDBService struct { conn *DBConn } +func doMigration(ctx context.Context, db *bun.DB, log *logrus.Logger) error { + l := log.WithField("func", "doMigration") + + migrator := migrate.NewMigrator(db, migrations.Migrations) + + if err := migrator.Init(ctx); err != nil { + return err + } + + group, err := migrator.Migrate(ctx) + if err != nil { + return err + } + + if group.ID == 0 { + l.Info("there are no new migrations to run") + return nil + } + + l.Infof("MIGRATED DATABASE TO %s", group) + return nil +} + // NewBunDBService returns a bunDB derived from the provided config, which implements the go-fed DB interface. // Under the hood, it uses https://github.com/uptrace/bun to create and maintain a database connection. func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger) (db.DB, error) { @@ -121,6 +146,10 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger) conn.RegisterModel(t) } + if err := doMigration(ctx, conn.DB, log); err != nil { + return nil, fmt.Errorf("db migration error: %s", err) + } + ps := &bunDBService{ Account: &accountDB{ config: c, diff --git a/internal/db/bundb/migrations/20210816411877_struct_validation.go b/internal/db/bundb/migrations/20210816411877_struct_validation.go new file mode 100644 index 000000000..660d94517 --- /dev/null +++ b/internal/db/bundb/migrations/20210816411877_struct_validation.go @@ -0,0 +1,54 @@ +/* + GoToSocial + Copyright (C) 2021 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/superseriousbusiness/gotosocial/internal/gtsmodel" + "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 { + _, err := tx.NewCreateTable().Model(>smodel.Account{}).IfNotExists().Exec(ctx) + if err != nil { + return err + } + + return nil + }) + } + + down := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + _, err := tx.NewDropTable().Model(>smodel.Account{}).Exec(ctx) + if err != nil { + return err + } + + return nil + }) + } + + if err := Migrations.Register(up, down); err != nil { + panic(err) + } +} diff --git a/internal/db/bundb/migrations/README.md b/internal/db/bundb/migrations/README.md new file mode 100644 index 000000000..e293d43c7 --- /dev/null +++ b/internal/db/bundb/migrations/README.md @@ -0,0 +1,21 @@ +# Migrations + +## How do I write a migration file? + +[See here](https://bun.uptrace.dev/guide/migrations.html#migration-names) + +As a template, take one of the existing migration files and modify it. It will be automatically loaded and handled by Bun. + +## 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. + +```bash +echo "$(date --utc +%Y%m%H%M%S%N | head -c 14)_$(git rev-parse --abbrev-ref HEAD).go" +``` + +## Rules of thumb + +1. **DON'T DROP TABLES**!!!!!!!! diff --git a/internal/db/bundb/migrations/main.go b/internal/db/bundb/migrations/main.go new file mode 100644 index 000000000..7f4e76027 --- /dev/null +++ b/internal/db/bundb/migrations/main.go @@ -0,0 +1,28 @@ +/* + GoToSocial + Copyright (C) 2021 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 ( + "github.com/uptrace/bun/migrate" +) + +var ( + // Migrations provides migration logic for bun + Migrations = migrate.NewMigrations() +) |