summaryrefslogtreecommitdiff
path: root/internal/db/bundb/bundb.go
diff options
context:
space:
mode:
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,