feat: add in-built db migrator

- fix docker builds
- fix binary builds
This commit is contained in:
2023-03-01 11:19:57 +00:00
parent 4372b9dbf1
commit 4dfc1725cb
10 changed files with 123 additions and 66 deletions

View File

@@ -2,13 +2,20 @@ package postgres
import (
"context"
"os"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/tern/v2/migrate"
)
const (
schemaTable = "schema_version"
)
type PostgresPoolOpts struct {
DSN string
DSN string
MigrationsFolderPath string
}
// NewPostgresPool creates a reusbale connection pool across the cic-custodial component.
@@ -26,7 +33,22 @@ func NewPostgresPool(o PostgresPoolOpts) (*pgxpool.Pool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := dbPool.Ping(ctx); err != nil {
conn, err := dbPool.Acquire(ctx)
if err != nil {
return nil, err
}
defer conn.Release()
migrator, err := migrate.NewMigrator(ctx, conn.Conn(), schemaTable)
if err != nil {
return nil, err
}
if err := migrator.LoadMigrations(os.DirFS(o.MigrationsFolderPath)); err != nil {
return nil, err
}
if err := migrator.Migrate(ctx); err != nil {
return nil, err
}