-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
47 lines (34 loc) · 972 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package queue
import (
"context"
"embed"
"log/slog"
"net/url"
"github.com/amacneil/dbmate/v2/pkg/dbmate"
_ "github.com/amacneil/dbmate/v2/pkg/driver/postgres"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/tracelog"
pgxslog "github.com/mcosta74/pgx-slog"
)
//go:embed internal/db/migration
var migrations embed.FS
func Migrate(connStr string) error {
u, err := url.Parse(connStr)
if err != nil {
return err
}
dbmateDB := dbmate.New(u)
dbmateDB.FS = migrations
dbmateDB.MigrationsDir = []string{"internal/db/migration"}
dbmateDB.AutoDumpSchema = false
return dbmateDB.CreateAndMigrate()
}
func NewDBPool(ctx context.Context, connStr string) (*pgxpool.Pool, error) {
pgxConfig, err := pgxpool.ParseConfig(connStr)
if err != nil {
return nil, err
}
pgxConfig.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgxslog.NewLogger(slog.Default()), LogLevel: tracelog.LogLevelWarn}
return pgxpool.NewWithConfig(ctx, pgxConfig)
}