Documentation
¶
Overview ¶
Package postgres provides a PostgreSQL-backed implementation of the types.DB interface from github.com/slackmgr/types.
It uses pgx v5 with connection pooling (pgxpool) and stores all entities as JSONB with indexed columns for efficient querying.
Usage ¶
Create a client using New with functional options, call Client.Connect to establish the connection pool, and then Client.Init to create the database schema:
client := postgres.New(
postgres.WithHost("localhost"),
postgres.WithPort(5432),
postgres.WithUser("postgres"),
postgres.WithPassword("secret"),
postgres.WithDatabase("slack_manager"),
)
if err := client.Connect(ctx); err != nil {
log.Fatal(err)
}
defer client.Close(ctx)
if err := client.Init(ctx, false); err != nil {
log.Fatal(err)
}
Database Tables ¶
Four tables are created automatically by Client.Init (table names are configurable via the corresponding With* options):
- issues — Slack issues with correlation IDs, open/closed state, and TTL
- alerts — Alert records with TTL
- move_mappings — Issue move tracking between channels
- channel_processing_state — Per-channel processing timestamps
Each table includes a version column and an attrs JSONB column that holds the full serialised entity.
Connection Pool ¶
The underlying pgxpool can be tuned with the pool-specific options: WithPoolMaxConnections, WithPoolMinConnections, WithPoolMinIdleConnections, WithPoolMaxConnectionLifetime, WithPoolMaxConnectionIdleTime, WithPoolHealthCheckPeriod, and WithPoolMaxConnectionLifetimeJitter.
TTL and Cleanup ¶
Closed issues and alerts are assigned an expiry timestamp on write. Default TTLs are 180 days for issues and 30 days for alerts; both are configurable via WithIssuesTimeToLive and WithAlertsTimeToLive.
A background goroutine periodically deletes expired rows. Its interval defaults to 1 hour and can be changed with WithTTLCleanupInterval or disabled entirely with WithTTLCleanupDisabled.
Schema Validation ¶
When Client.Init is called with skipSchemaValidation set to false, it queries information_schema.columns and verifies that every expected column exists with the correct data type and nullability. Pass true to skip this check in environments where the schema is managed externally.
SSL ¶
SSL behaviour is controlled by WithSSLMode using the SSLMode constants (SSLModeDisable, SSLModeAllow, SSLModePrefer, SSLModeRequire, SSLModeVerifyCA, SSLModeVerifyFull). The default is SSLModePrefer.
Index ¶
- Constants
- type Client
- func (c *Client) Close(_ context.Context) error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) DeleteMoveMapping(ctx context.Context, channelID, correlationID string) error
- func (c *Client) DropAllData(ctx context.Context) error
- func (c *Client) FindActiveChannels(ctx context.Context) ([]string, error)
- func (c *Client) FindChannelProcessingState(ctx context.Context, channelID string) (*types.ChannelProcessingState, error)
- func (c *Client) FindIssueBySlackPostID(ctx context.Context, channelID, postID string) (string, json.RawMessage, error)
- func (c *Client) FindMoveMapping(ctx context.Context, channelID, correlationID string) (json.RawMessage, error)
- func (c *Client) FindOpenIssueByCorrelationID(ctx context.Context, channelID, correlationID string) (string, json.RawMessage, error)
- func (c *Client) Init(ctx context.Context, skipSchemaValidation bool) error
- func (c *Client) LoadOpenIssuesInChannel(ctx context.Context, channelID string) (map[string]json.RawMessage, error)
- func (c *Client) MoveIssue(ctx context.Context, issue types.Issue, _, targetChannelID string) error
- func (c *Client) SaveAlert(ctx context.Context, alert *types.Alert) error
- func (c *Client) SaveChannelProcessingState(ctx context.Context, state *types.ChannelProcessingState) error
- func (c *Client) SaveIssue(ctx context.Context, issue types.Issue) error
- func (c *Client) SaveIssues(ctx context.Context, issues ...types.Issue) error
- func (c *Client) SaveMoveMapping(ctx context.Context, moveMapping types.MoveMapping) error
- type Option
- func WithAlertsTable(name string) Option
- func WithAlertsTimeToLive(d time.Duration) Option
- func WithChannelProcessingStateTable(name string) Option
- func WithDatabase(database string) Option
- func WithHost(host string) Option
- func WithIssuesTable(name string) Option
- func WithIssuesTimeToLive(d time.Duration) Option
- func WithMoveMappingsTable(name string) Option
- func WithPassword(password string) Option
- func WithPoolHealthCheckPeriod(d time.Duration) Option
- func WithPoolMaxConnectionIdleTime(d time.Duration) Option
- func WithPoolMaxConnectionLifetime(d time.Duration) Option
- func WithPoolMaxConnectionLifetimeJitter(d time.Duration) Option
- func WithPoolMaxConnections(n int32) Option
- func WithPoolMinConnections(n int32) Option
- func WithPoolMinIdleConnections(n int32) Option
- func WithPort(port int) Option
- func WithSSLMode(mode SSLMode) Option
- func WithTTLCleanupDisabled() Option
- func WithTTLCleanupInterval(d time.Duration) Option
- func WithUser(user string) Option
- type SSLMode
Constants ¶
const ( IssueModelVersion = 2 AlertModelVersion = 2 MoveMappingModelVersion = 2 ChannelProcessingStateModelVersion = 2 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) DeleteMoveMapping ¶
func (*Client) FindActiveChannels ¶
func (*Client) FindChannelProcessingState ¶
func (*Client) FindIssueBySlackPostID ¶
func (*Client) FindMoveMapping ¶
func (*Client) FindOpenIssueByCorrelationID ¶
func (*Client) LoadOpenIssuesInChannel ¶
func (*Client) SaveChannelProcessingState ¶
func (*Client) SaveIssues ¶
func (*Client) SaveMoveMapping ¶
type Option ¶
type Option func(*options)
Option is a functional option for configuring a Client.
func WithAlertsTable ¶
func WithAlertsTimeToLive ¶
WithAlertsTimeToLive sets the TTL applied to alert records. The default is 30 days. The duration must be greater than zero.
func WithDatabase ¶
func WithIssuesTable ¶
func WithIssuesTimeToLive ¶
WithIssuesTimeToLive sets the TTL applied to closed issue records. The default is 180 days. The duration must be greater than zero.
func WithMoveMappingsTable ¶
func WithPassword ¶
func WithPoolMaxConnections ¶
func WithPoolMinConnections ¶
func WithSSLMode ¶
func WithTTLCleanupDisabled ¶
func WithTTLCleanupDisabled() Option
WithTTLCleanupDisabled disables the background TTL cleanup goroutine. When disabled, expired rows are excluded from reads but never physically deleted. Useful in tests or environments that handle cleanup externally.
func WithTTLCleanupInterval ¶
WithTTLCleanupInterval sets how often the background goroutine runs to physically delete expired rows. Defaults to 1 hour. The duration must be greater than zero.
type SSLMode ¶
type SSLMode string
SSLMode represents PostgreSQL SSL connection modes.
const ( SSLModeDisable SSLMode = "disable" // No SSL SSLModeAllow SSLMode = "allow" // Try non-SSL first, then SSL SSLModePrefer SSLMode = "prefer" // Try SSL first, then non-SSL (default) SSLModeRequire SSLMode = "require" // Only SSL (no certificate verification) SSLModeVerifyCA SSLMode = "verify-ca" // SSL with CA verification SSLModeVerifyFull SSLMode = "verify-full" // SSL with CA and hostname verification )