-
Notifications
You must be signed in to change notification settings - Fork 2
feat: implement PostgreSQL database foundation with complete schema design #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,150 @@ | ||||||
| package database | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "fmt" | ||||||
| "log/slog" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/jackc/pgx/v5/pgxpool" | ||||||
| "github.com/voidrunnerhq/voidrunner/internal/config" | ||||||
| ) | ||||||
|
|
||||||
| // Connection represents a database connection pool | ||||||
| type Connection struct { | ||||||
| Pool *pgxpool.Pool | ||||||
| logger *slog.Logger | ||||||
| } | ||||||
|
|
||||||
| // NewConnection creates a new database connection pool | ||||||
| func NewConnection(cfg *config.DatabaseConfig, logger *slog.Logger) (*Connection, error) { | ||||||
| if cfg == nil { | ||||||
| return nil, fmt.Errorf("database configuration is required") | ||||||
| } | ||||||
|
|
||||||
| if logger == nil { | ||||||
| logger = slog.Default() | ||||||
| } | ||||||
|
|
||||||
| connStr := fmt.Sprintf( | ||||||
| "postgres://%s:%s@%s:%s/%s?sslmode=%s", | ||||||
| cfg.User, | ||||||
| cfg.Password, | ||||||
| cfg.Host, | ||||||
| cfg.Port, | ||||||
| cfg.Database, | ||||||
| cfg.SSLMode, | ||||||
| ) | ||||||
|
|
||||||
| poolConfig, err := pgxpool.ParseConfig(connStr) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to parse database connection string: %w", err) | ||||||
| } | ||||||
|
|
||||||
| // Configure connection pool settings for optimal performance | ||||||
| poolConfig.MaxConns = 25 // Maximum number of connections | ||||||
| poolConfig.MinConns = 5 // Minimum number of connections | ||||||
| poolConfig.MaxConnLifetime = time.Hour * 1 // Maximum connection lifetime | ||||||
| poolConfig.MaxConnIdleTime = time.Minute * 30 // Maximum connection idle time | ||||||
| poolConfig.HealthCheckPeriod = time.Minute * 5 // Health check frequency | ||||||
|
|
||||||
| // Connection timeout settings | ||||||
| poolConfig.ConnConfig.ConnectTimeout = time.Second * 10 | ||||||
| poolConfig.ConnConfig.RuntimeParams["statement_timeout"] = "30s" | ||||||
| poolConfig.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = "60s" | ||||||
|
|
||||||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||||||
| defer cancel() | ||||||
|
|
||||||
| pool, err := pgxpool.NewWithConfig(ctx, poolConfig) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to create database pool: %w", err) | ||||||
| } | ||||||
|
|
||||||
| // Test the connection | ||||||
| if err := pool.Ping(ctx); err != nil { | ||||||
| pool.Close() | ||||||
| return nil, fmt.Errorf("failed to ping database: %w", err) | ||||||
| } | ||||||
|
|
||||||
| logger.Info("database connection pool created successfully", | ||||||
| "host", cfg.Host, | ||||||
| "port", cfg.Port, | ||||||
| "database", cfg.Database, | ||||||
| "max_conns", poolConfig.MaxConns, | ||||||
| "min_conns", poolConfig.MinConns, | ||||||
| ) | ||||||
|
|
||||||
| return &Connection{ | ||||||
| Pool: pool, | ||||||
| logger: logger, | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
| // Close closes the database connection pool | ||||||
| func (c *Connection) Close() { | ||||||
| if c.Pool != nil { | ||||||
| c.logger.Info("closing database connection pool") | ||||||
| c.Pool.Close() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Ping checks if the database connection is alive | ||||||
| func (c *Connection) Ping(ctx context.Context) error { | ||||||
| return c.Pool.Ping(ctx) | ||||||
| } | ||||||
|
|
||||||
| // Stats returns connection pool statistics | ||||||
| func (c *Connection) Stats() *pgxpool.Stat { | ||||||
| return c.Pool.Stat() | ||||||
| } | ||||||
|
|
||||||
| // LogStats logs connection pool statistics | ||||||
| func (c *Connection) LogStats() { | ||||||
| stats := c.Stats() | ||||||
| c.logger.Info("database connection pool stats", | ||||||
| "total_conns", stats.TotalConns(), | ||||||
| "idle_conns", stats.IdleConns(), | ||||||
| "acquired_conns", stats.AcquiredConns(), | ||||||
| "constructing_conns", stats.ConstructingConns(), | ||||||
| "acquire_count", stats.AcquireCount(), | ||||||
| "acquire_duration", stats.AcquireDuration(), | ||||||
| "acquired_conns_duration", stats.AcquiredConns(), | ||||||
|
||||||
| "acquired_conns_duration", stats.AcquiredConns(), | |
| "acquired_conns_duration", stats.AcquireDuration(), |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
lib/pqmodule appears unused since you rely on pgx for database connections. Removing it will reduce your dependency footprint.