ci / contract (сгенерированный код совпадает с openapi.yaml) (push) Successful in 48s
ci / api (push) Successful in 51s
ci / recommender (push) Canceled after 2s
ci / ts (packages + web) (push) Canceled after 0s
ci / docker (образы собираются — их сервер собирает сам при обновлении) (apps/web/Dockerfile, web, ) (push) Canceled after 0s
ci / docker (образы собираются — их сервер собирает сам при обновлении) (services/api/Dockerfile, api, api) (push) Canceled after 0s
ci / docker (образы собираются — их сервер собирает сам при обновлении) (services/api/Dockerfile, storage-agent, storage-agent) (push) Canceled after 0s
ci / docker (образы собираются — их сервер собирает сам при обновлении) (services/recommender/Dockerfile, recommender, ) (push) Canceled after 0s
ci / mobile (Metro-бандл Android) (push) Canceled after 0s
Security and robustness: - storage-agent rejects an empty INTERNAL_TOKEN (config notEmpty + middleware) - api takes the client IP only from X-Real-IP (chi RealIP trusted True-Client-IP) - /readyz reports ok/fail and logs the cause; checks run in parallel with a timeout - storage-agent shuts down gracefully; shared httpx.NewServer/Serve with IdleTimeout - goose migrations take a Postgres advisory lock - recommender readyz: per-check timeout, redis socket timeouts Clients: - Android release allows cleartext HTTP for LAN servers (expo-build-properties) - checkServer times out after 10s; connect button is disabled while checking Deploy and CI: - install.sh hands cache/uploads/updates/models to the non-root service users - update.sh refuses downgrades unless PIPU_ALLOW_DOWNGRADE=1 - recommender/analyzer get only the env they need; required secrets fail fast - nginx security headers and CSP; strip True-Client-IP - dev compose binds ports to 127.0.0.1; postgres/redis images pinned - version.mjs fails on a malformed explicit tag instead of releasing 0.0.0-dev - contract check catches untracked generated files - final releases fail without a release keystore; changelog skips rc tags Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
// Package db — подключение к PostgreSQL и применение миграций.
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/pressly/goose/v3"
|
|
"github.com/pressly/goose/v3/lock"
|
|
|
|
"pipu/api/migrations"
|
|
)
|
|
|
|
// Connect открывает пул соединений и проверяет доступность БД.
|
|
func Connect(ctx context.Context, url string) (*pgxpool.Pool, error) {
|
|
pool, err := pgxpool.New(ctx, url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("db: open pool: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("db: ping: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|
|
|
|
// Migrate применяет встроенные миграции goose. Advisory-lock в PostgreSQL не даёт
|
|
// нескольким экземплярам api применять миграции одновременно.
|
|
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
|
sqlDB := stdlib.OpenDBFromPool(pool)
|
|
defer func(d *sql.DB) { _ = d.Close() }(sqlDB)
|
|
|
|
locker, err := lock.NewPostgresSessionLocker()
|
|
if err != nil {
|
|
return fmt.Errorf("db: migrations lock: %w", err)
|
|
}
|
|
provider, err := goose.NewProvider(goose.DialectPostgres, sqlDB, migrations.FS, goose.WithSessionLocker(locker))
|
|
if err != nil {
|
|
return fmt.Errorf("db: migrations: %w", err)
|
|
}
|
|
if _, err := provider.Up(ctx); err != nil {
|
|
return fmt.Errorf("db: migrate up: %w", err)
|
|
}
|
|
return nil
|
|
}
|