This repository has been archived on 2026-11-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
artifactapi/internal/database/postgres.go
T
unkin-agent 6b53971d56
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
Import golib from github.com/IUCLabs
2026-11-20 00:05:06 +11:00

36 lines
757 B
Go

package database
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/IUCLabs/golib/pg"
"git.unkin.net/unkin/artifactapi/migrations"
)
// migrationLockName names the cluster-wide advisory lock the migration run
// contends for; golib derives the key as FNV-1a/64 of it. Replicas starting
// together queue on it instead of racing each other through the DDL.
const migrationLockName = "artifactapi-migrations"
type DB struct {
Pool *pgxpool.Pool
}
func New(dsn string) (*DB, error) {
ctx := context.Background()
pool, err := pg.NewMigrated(ctx, dsn, migrations.FS, pg.MigrateOptions{
LockName: migrationLockName,
})
if err != nil {
return nil, err
}
return &DB{Pool: pool}, nil
}
func (db *DB) Close() {
db.Pool.Close()
}