go-urlsh/internal/db/migrations.go

29 lines
582 B
Go
Raw Permalink Normal View History

2024-05-04 17:06:01 +02:00
package db
import (
"context"
"code.lila.network/adoralaura/go-urlsh/migrations"
2024-09-30 13:19:01 +02:00
"github.com/rs/zerolog"
"github.com/uptrace/bun"
2024-05-04 17:06:01 +02:00
"github.com/uptrace/bun/migrate"
)
2024-09-30 13:19:01 +02:00
func doMigrations(db *bun.DB, logger *zerolog.Logger) error {
2024-05-04 17:06:01 +02:00
ctx := context.Background()
migrator := migrate.NewMigrator(db, migrations.Migrations)
2024-05-04 17:06:01 +02:00
migrator.Init(ctx)
group, err := migrator.Migrate(ctx)
if err != nil {
return err
}
if group.IsZero() {
2024-09-30 13:19:01 +02:00
logger.Info().Msg("No new database migrations to run")
2024-05-04 17:06:01 +02:00
return nil
}
2024-09-30 13:19:01 +02:00
logger.Info().Msgf("Database got migrated to %s", group)
2024-05-04 17:06:01 +02:00
return nil
}