Files
mtusi2026lab3/cmd/server/dbcheck.go
T

42 lines
935 B
Go

package main
import (
"context"
"errors"
"fmt"
"time"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
)
func checkDatabase() error {
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
pool, err := store.OpenPostgres(ctx, envOrDefault(
"DATABASE_PASSWORD_FILE", "/run/secrets/postgres_password",
))
if err != nil {
return err
}
defer pool.Close()
queryCtx, queryCancel := context.WithTimeout(ctx, 3*time.Second)
defer queryCancel()
var applied bool
err = pool.QueryRow(queryCtx,
"SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version = $1)",
int64(1),
).Scan(&applied)
if err != nil {
return errors.New("cannot read schema_migrations; check database and migrations")
}
if !applied {
return errors.New("migration 000001 is not applied")
}
fmt.Println("PASS: PostgreSQL connection, migration 000001; pool max_conns=10")
return nil
}