more updates

This commit is contained in:
tsmethurst 2021-08-31 19:27:02 +02:00
commit 3d8aa7a4d2
18 changed files with 1369 additions and 133 deletions

View file

@ -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,