30 lines
784 B
Go
30 lines
784 B
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"downloader/internal/app/persistence/migrations"
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect/pgdialect"
|
|
"github.com/uptrace/bun/driver/pgdriver"
|
|
"github.com/uptrace/bun/extra/bundebug"
|
|
"github.com/uptrace/bun/migrate"
|
|
)
|
|
|
|
func NewPostgresDB() *bun.DB {
|
|
dsn := "postgres://downloader:downloadersecret@localhost:5432/downloader?sslmode=disable"
|
|
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
|
|
|
db := bun.NewDB(sqldb, pgdialect.New())
|
|
db.AddQueryHook(bundebug.NewQueryHook(
|
|
bundebug.WithVerbose(true),
|
|
bundebug.FromEnv("BUNDEBUG")))
|
|
|
|
migrator := migrate.NewMigrator(db, migrations.Migrations)
|
|
bgCtx := context.Background()
|
|
migrator.Init(bgCtx)
|
|
migrator.Migrate(bgCtx)
|
|
|
|
return db
|
|
}
|