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
encapi/internal/database/postgres.go
T
unkin-agent 2c54e52514
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
Import golib from github.com/IUCLabs
2026-11-20 00:05:05 +11:00

39 lines
1.1 KiB
Go

// Package database is the Postgres persistence layer for encapi. It stores
// three entities — statuses (Puppet environments), roles (class assignment
// targets with inheritable default params), and nodes (host-to-role
// assignments) — and enforces referential integrity between them.
package database
import (
"context"
"log/slog"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/IUCLabs/golib/pg"
"git.unkin.net/unkin/encapi/migrations"
)
// DB wraps a pgx connection pool.
type DB struct {
Pool *pgxpool.Pool
}
// New connects to Postgres, verifies the connection, and brings the schema up
// to date from the embedded migrations before returning, so the server never
// serves against a half-migrated database. log may be nil.
func New(ctx context.Context, dsn string, log *slog.Logger) (*DB, error) {
pool, err := pg.NewMigrated(ctx, dsn, migrations.FS, pg.MigrateOptions{
LockName: migrations.LockName,
Logger: log,
})
if err != nil {
return nil, err
}
return &DB{Pool: pool}, nil
}
// Close releases the pool.
func (db *DB) Close() { db.Pool.Close() }