feat: establish Go service and database foundation

This commit is contained in:
2026-08-20 19:33:38 +03:30
parent 552cae5f9d
commit 961980d18c
11 changed files with 341 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
package database
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func Open(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
poolConfig, err := pgxpool.ParseConfig(databaseURL)
if err != nil {
return nil, fmt.Errorf("parsing database configuration: %w", err)
}
poolConfig.MaxConns = 12
poolConfig.MinConns = 1
poolConfig.MaxConnLifetime = 30 * time.Minute
poolConfig.MaxConnIdleTime = 5 * time.Minute
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
if err != nil {
return nil, fmt.Errorf("opening database: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, fmt.Errorf("pinging database: %w", err)
}
return pool, nil
}

View File

@@ -0,0 +1,23 @@
package database
import (
"context"
"fmt"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
)
// Migrate applies the idempotent local MVP schema. Production migration tooling
// can execute the same reviewed SQL from migrations/.
func Migrate(ctx context.Context, pool *pgxpool.Pool, migration string) error {
for statement := range strings.SplitSeq(migration, "-- statement-break") {
if strings.TrimSpace(statement) == "" {
continue
}
if _, err := pool.Exec(ctx, statement); err != nil {
return fmt.Errorf("applying migration: %w", err)
}
}
return nil
}