database

package
v0.0.0-...-606ac71 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package database provides PostgreSQL database client and migration utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateGINIndexes

func CreateGINIndexes(ctx context.Context, driver *sql.Driver) error

CreateGINIndexes creates full-text search GIN indexes for PostgreSQL. These indexes enable efficient full-text search on alert_data, final_analysis, and timeline_events content fields.

func CreatePartialUniqueIndexes

func CreatePartialUniqueIndexes(ctx context.Context, driver *sql.Driver) error

CreatePartialUniqueIndexes creates PostgreSQL partial unique indexes that Ent/Atlas cannot express. These must match the constraints in 20260225235224_add_orchestrator_sub_agent_fields.up.sql.

Types

type Client

type Client struct {
	*ent.Client
	// contains filtered or unexported fields
}

Client wraps Ent client and provides access to the underlying database

func NewClient

func NewClient(ctx context.Context, cfg Config) (*Client, error)

NewClient creates a new database client with connection pooling and migrations

func NewClientFromEnt

func NewClientFromEnt(entClient *ent.Client, db *stdsql.DB) *Client

NewClientFromEnt wraps an existing Ent client (useful for testing)

func (*Client) DB

func (c *Client) DB() *stdsql.DB

DB returns the underlying database connection for health checks and direct queries

type Config

type Config struct {
	Host     string
	Port     int
	User     string
	Password string
	Database string
	SSLMode  string

	// Connection pool settings
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

Config holds database configuration

func LoadConfigFromEnv

func LoadConfigFromEnv() (Config, error)

LoadConfigFromEnv loads database configuration from environment variables with validation and production-ready defaults

func (Config) DSN

func (c Config) DSN() string

DSN returns a pgx-compatible connection string for this configuration. Used by NewClient for the connection pool and by NotifyListener for a dedicated LISTEN connection.

func (Config) Validate

func (c Config) Validate() error

Validate checks if the configuration is valid

type HealthStatus

type HealthStatus struct {
	Status          string `json:"status"`
	ResponseTime    int64  `json:"response_time_ms"`
	OpenConnections int    `json:"open_connections"`
	InUse           int    `json:"in_use"`
	Idle            int    `json:"idle"`
	WaitCount       int64  `json:"wait_count"`
	WaitDuration    int64  `json:"wait_duration_ms"`
	MaxOpenConns    int    `json:"max_open_conns"`
}

HealthStatus represents database health and connection pool statistics

func Health

func Health(ctx context.Context, db *sql.DB) (*HealthStatus, error)

Health checks database connectivity and returns connection pool statistics

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL