Initial commit
This commit is contained in:
37
application/migration.go
Normal file
37
application/migration.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/migrate"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, db *bun.DB, logger *zap.Logger) error {
|
||||
migrations := migrate.NewMigrations()
|
||||
|
||||
if err := migrations.Discover(migrationFiles); err != nil {
|
||||
return fmt.Errorf("discover migrations: %w", err)
|
||||
}
|
||||
|
||||
migrator := migrate.NewMigrator(db, migrations)
|
||||
|
||||
if initErr := migrator.Init(ctx); initErr != nil {
|
||||
return fmt.Errorf("init migrations: %w", initErr)
|
||||
}
|
||||
|
||||
group, err := migrator.Migrate(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migrate: %w", err)
|
||||
}
|
||||
|
||||
logger.Sugar().Infof("migrated: %s", group.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user