This repo had no version control prior to this commit. The import is a
straight snapshot of the working tree at 2026-05-03; the deployed
binary on fihelvop01 was being rebuilt from this source via `make
build` + scp into place, with no upstream review path.
The snapshot already includes one in-flight fix made on 2026-05-03 to
internal/service/persona.go:GetSelfModel — the handler queried
`source` and `strength` columns plus an `is_active = true` filter on
persona.persona_commitments, none of which exist on that table (its
shape is session-bound commitments with `status`, `commitment_meta`,
etc.). The query returned a 500 every time SynapseHub bootstrapped a
persona's self-model, dropping the IdentityConstraints / Commitments /
ConscienceStandards layer from the assembled prompt. The patched
query reads existing columns only (commitment_text, commitment_type),
filters on `status='active'`, and synthesises Source="learned" /
Strength=1.0 to keep the SelfModel response shape stable for callers.
Verified live: `GET /api/v1/personas/70f7cfd9-.../self-model` now
returns 200 with `{identityConstraints:[],commitments:[],
conscienceStandards:[]}` instead of 500.
Future changes go through PRs against this repo — no more bin-only
deploys.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/gosec/gsc-ops-api/internal/config"
|
|
)
|
|
|
|
// DB wraps the database connection pool
|
|
type DB struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
// New creates a new database connection pool using the legacy config
|
|
func New(cfg *config.Config) (*DB, error) {
|
|
return NewFromDSN(cfg.DatabaseDSN(), cfg.Database.MaxConns, cfg.Database.MinConns)
|
|
}
|
|
|
|
// NewNamed creates a new database connection pool for a named database
|
|
func NewNamed(cfg *config.Config, name string) (*DB, error) {
|
|
db, ok := cfg.Databases[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("database %q not configured", name)
|
|
}
|
|
return NewFromDSN(cfg.NamedDatabaseDSN(name), db.MaxConns, db.MinConns)
|
|
}
|
|
|
|
// NewFromDSN creates a new database connection pool from a DSN string
|
|
func NewFromDSN(dsn string, maxConns, minConns int) (*DB, error) {
|
|
poolConfig, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse database config: %w", err)
|
|
}
|
|
|
|
poolConfig.MaxConns = int32(maxConns)
|
|
poolConfig.MinConns = int32(minConns)
|
|
poolConfig.MaxConnLifetime = 1 * time.Hour
|
|
poolConfig.MaxConnIdleTime = 30 * time.Minute
|
|
poolConfig.HealthCheckPeriod = 1 * time.Minute
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create connection pool: %w", err)
|
|
}
|
|
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("failed to ping database: %w", err)
|
|
}
|
|
|
|
return &DB{pool: pool}, nil
|
|
}
|
|
|
|
func (db *DB) Close() {
|
|
if db.pool != nil {
|
|
db.pool.Close()
|
|
}
|
|
}
|
|
|
|
func (db *DB) Pool() *pgxpool.Pool {
|
|
return db.pool
|
|
}
|
|
|
|
func (db *DB) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
|
|
return db.pool.Query(ctx, sql, args...)
|
|
}
|
|
|
|
func (db *DB) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
|
return db.pool.QueryRow(ctx, sql, args...)
|
|
}
|
|
|
|
func (db *DB) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error) {
|
|
return db.pool.Exec(ctx, sql, args...)
|
|
}
|
|
|
|
func (db *DB) Health(ctx context.Context) error {
|
|
return db.pool.Ping(ctx)
|
|
}
|
|
|
|
func (db *DB) Stats() *pgxpool.Stat {
|
|
return db.pool.Stat()
|
|
}
|