summaryrefslogtreecommitdiff
path: root/internal/db/bundb/bundb.go
diff options
context:
space:
mode:
authorLibravatar tsmethurst <tobi.smethurst@klarrio.com>2021-08-31 19:27:02 +0200
committerLibravatar tsmethurst <tobi.smethurst@klarrio.com>2021-08-31 19:27:02 +0200
commit3d8aa7a4d2f3f3f986241da21a953fa194d25fd8 (patch)
tree395b059a4885d8dc8aec9c85f532abe4475d62d9 /internal/db/bundb/bundb.go
parentchange muchos things (diff)
downloadgotosocial-3d8aa7a4d2f3f3f986241da21a953fa194d25fd8.tar.xz
more updates
Diffstat (limited to 'internal/db/bundb/bundb.go')
-rw-r--r--internal/db/bundb/bundb.go29
1 files changed, 29 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,