fix: validate database schema before startup
This commit is contained in:
@@ -10,6 +10,21 @@ jobs:
|
||||
name: Run Tests
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16
|
||||
env:
|
||||
POSTGRES_USER: tp
|
||||
POSTGRES_PASSWORD: tp
|
||||
POSTGRES_DB: tp_test
|
||||
ports:
|
||||
- 5432:5432
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 5s
|
||||
--health-timeout 5s
|
||||
--health-retries 10
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v7
|
||||
@@ -23,10 +38,25 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Install psql
|
||||
run: |
|
||||
if ! command -v psql >/dev/null; then
|
||||
sudo apt-get update && sudo apt-get install -y postgresql-client
|
||||
fi
|
||||
|
||||
- name: Apply database migrations
|
||||
run: |
|
||||
for f in internal/db/sqlc/migrations/*.up.sql; do
|
||||
echo "applying $f"
|
||||
PGPASSWORD=tp psql -h 127.0.0.1 -U tp -d tp_test -v ON_ERROR_STOP=1 -f "$f"
|
||||
done
|
||||
|
||||
- name: Run go vet
|
||||
run: go vet ./...
|
||||
|
||||
- name: Run tests
|
||||
env:
|
||||
TP_TEST_DATABASE_URL: postgres://tp:tp@127.0.0.1:5432/tp_test
|
||||
run: go test -v -p 4 ./...
|
||||
|
||||
build-and-push:
|
||||
|
||||
@@ -10,6 +10,20 @@ jobs:
|
||||
sonarqube:
|
||||
name: SonarQube Trigger
|
||||
runs-on: ubuntu-latest
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16
|
||||
env:
|
||||
POSTGRES_USER: tp
|
||||
POSTGRES_PASSWORD: tp
|
||||
POSTGRES_DB: tp_test
|
||||
ports:
|
||||
- 5432:5432
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 5s
|
||||
--health-timeout 5s
|
||||
--health-retries 10
|
||||
steps:
|
||||
- name: Checking out
|
||||
uses: actions/checkout@v7
|
||||
@@ -25,10 +39,25 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: go mod tidy
|
||||
|
||||
- name: Install psql
|
||||
run: |
|
||||
if ! command -v psql >/dev/null; then
|
||||
sudo apt-get update && sudo apt-get install -y postgresql-client
|
||||
fi
|
||||
|
||||
- name: Apply database migrations
|
||||
run: |
|
||||
for f in internal/db/sqlc/migrations/*.up.sql; do
|
||||
echo "applying $f"
|
||||
PGPASSWORD=tp psql -h 127.0.0.1 -U tp -d tp_test -v ON_ERROR_STOP=1 -f "$f"
|
||||
done
|
||||
|
||||
- name: Run go vet
|
||||
run: go vet ./... 2>&1 | tee vet-results.txt
|
||||
|
||||
- name: Run tests with coverage
|
||||
env:
|
||||
TP_TEST_DATABASE_URL: postgres://tp:tp@127.0.0.1:5432/tp_test
|
||||
run: |
|
||||
go test ./... -v -p 4 -coverprofile=coverage
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
# Database migrations
|
||||
|
||||
Apply every forward migration in filename order before starting the controller. Back up the database first. Migration `000003` stops if existing Google account links conflict; resolve those rows before retrying.
|
||||
|
||||
With `psql` installed and a PostgreSQL database selected by `DATABASE_URL`:
|
||||
|
||||
```sh
|
||||
set -eu
|
||||
for migration in internal/db/sqlc/migrations/*.up.sql; do
|
||||
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 --single-transaction -f "$migration"
|
||||
done
|
||||
```
|
||||
|
||||
The controller checks for required tables and indexes at startup. It does not apply migrations automatically.
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"git.fossy.my.id/bagas/tunnel-please-controller/internal/config"
|
||||
"git.fossy.my.id/bagas/tunnel-please-controller/internal/db/sqlc/repository"
|
||||
"git.fossy.my.id/bagas/tunnel-please-controller/internal/server"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/lestrrat-go/httprc/v3"
|
||||
"github.com/lestrrat-go/jwx/v3/jwk"
|
||||
@@ -92,5 +93,36 @@ func startDatabase(ctx context.Context, dbURL string) (*repository.Queries, *pgx
|
||||
pool.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
if err = checkSchema(ctx, pool); err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
return repository.New(pool), pool, nil
|
||||
}
|
||||
|
||||
var requiredTables = []string{"user", "account", "refresh_token"}
|
||||
var requiredIndexes = []string{"account_provider_account_unique", "refresh_token_hash_unique"}
|
||||
|
||||
func checkSchema(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
for _, table := range requiredTables {
|
||||
identifier := "public." + pgx.Identifier{table}.Sanitize()
|
||||
var exists bool
|
||||
if err := pool.QueryRow(ctx, "SELECT to_regclass($1) IS NOT NULL", identifier).Scan(&exists); err != nil {
|
||||
return fmt.Errorf("schema check: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("schema check: table %s is missing; apply the migrations in internal/db/sqlc/migrations with your migration tool before starting", identifier)
|
||||
}
|
||||
}
|
||||
for _, index := range requiredIndexes {
|
||||
identifier := "public." + pgx.Identifier{index}.Sanitize()
|
||||
var exists bool
|
||||
if err := pool.QueryRow(ctx, "SELECT to_regclass($1) IS NOT NULL", identifier).Scan(&exists); err != nil {
|
||||
return fmt.Errorf("schema check: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("schema check: index %s is missing; apply the migrations in internal/db/sqlc/migrations with your migration tool before starting", identifier)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,10 +2,19 @@ package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const testDatabaseURLEnv = "TP_TEST_DATABASE_URL"
|
||||
const schemaCheckDatabaseName = "tp_test_bootstrap"
|
||||
|
||||
type fakeConfig struct {
|
||||
databaseURL string
|
||||
}
|
||||
@@ -41,3 +50,95 @@ func TestRunReturnsDatabaseError(t *testing.T) {
|
||||
t.Fatal("expected Run to fail when the database cannot be configured")
|
||||
}
|
||||
}
|
||||
|
||||
func schemaCheckPool(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
|
||||
adminURL := os.Getenv(testDatabaseURLEnv)
|
||||
if adminURL == "" {
|
||||
t.Skipf("%s not set; skipping schema check test", testDatabaseURLEnv)
|
||||
}
|
||||
if !strings.Contains(adminURL, "/tp_test") {
|
||||
t.Fatalf("%s must point to the disposable tp_test database", testDatabaseURLEnv)
|
||||
}
|
||||
target, _, _ := strings.Cut(adminURL, "?")
|
||||
target = strings.Replace(target, "/tp_test", "/"+schemaCheckDatabaseName, 1)
|
||||
|
||||
pool, err := pgxpool.New(context.Background(), adminURL)
|
||||
if err != nil {
|
||||
t.Fatalf("connect admin database: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
t.Cleanup(func() {
|
||||
if _, err := pool.Exec(context.Background(), "DROP DATABASE IF EXISTS "+schemaCheckDatabaseName+" WITH (FORCE)"); err != nil {
|
||||
t.Logf("drop %s: %v", schemaCheckDatabaseName, err)
|
||||
}
|
||||
})
|
||||
if _, err := pool.Exec(context.Background(), "DROP DATABASE IF EXISTS "+schemaCheckDatabaseName+" WITH (FORCE)"); err != nil {
|
||||
t.Fatalf("drop stale %s: %v", schemaCheckDatabaseName, err)
|
||||
}
|
||||
if _, err := pool.Exec(context.Background(), "CREATE DATABASE "+schemaCheckDatabaseName); err != nil {
|
||||
t.Fatalf("create %s: %v", schemaCheckDatabaseName, err)
|
||||
}
|
||||
|
||||
targetPool, err := pgxpool.New(context.Background(), target)
|
||||
if err != nil {
|
||||
t.Fatalf("connect %s: %v", schemaCheckDatabaseName, err)
|
||||
}
|
||||
t.Cleanup(targetPool.Close)
|
||||
return targetPool
|
||||
}
|
||||
|
||||
func applyMigrations(t *testing.T, pool *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
|
||||
entries, err := filepath.Glob(filepath.Join("..", "..", "internal", "db", "sqlc", "migrations", "*.up.sql"))
|
||||
if err != nil {
|
||||
t.Fatalf("glob migrations: %v", err)
|
||||
}
|
||||
sort.Strings(entries)
|
||||
for _, entry := range entries {
|
||||
sql, err := os.ReadFile(entry)
|
||||
if err != nil {
|
||||
t.Fatalf("read migration %s: %v", entry, err)
|
||||
}
|
||||
if _, err := pool.Exec(context.Background(), string(sql)); err != nil {
|
||||
t.Fatalf("apply migration %s: %v", entry, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartDatabaseMissingSchema(t *testing.T) {
|
||||
schemaCheckPool(t)
|
||||
|
||||
target, _, _ := strings.Cut(os.Getenv(testDatabaseURLEnv), "?")
|
||||
target = strings.Replace(target, "/tp_test", "/"+schemaCheckDatabaseName, 1)
|
||||
repo, dbPool, err := startDatabase(context.Background(), target)
|
||||
if err == nil {
|
||||
dbPool.Close()
|
||||
t.Fatal("expected error when schema is missing")
|
||||
}
|
||||
if repo != nil || dbPool != nil {
|
||||
t.Fatal("expected nil repo and pool on schema failure")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "internal/db/sqlc/migrations") {
|
||||
t.Fatalf("expected actionable migration guidance, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartDatabaseWithAppliedMigrations(t *testing.T) {
|
||||
pool := schemaCheckPool(t)
|
||||
applyMigrations(t, pool)
|
||||
|
||||
target, _, _ := strings.Cut(os.Getenv(testDatabaseURLEnv), "?")
|
||||
target = strings.Replace(target, "/tp_test", "/"+schemaCheckDatabaseName, 1)
|
||||
|
||||
repo, dbPool, err := startDatabase(context.Background(), target)
|
||||
if err != nil {
|
||||
t.Fatalf("expected success after migrations, got: %v", err)
|
||||
}
|
||||
defer dbPool.Close()
|
||||
if repo == nil || dbPool == nil {
|
||||
t.Fatal("expected non-nil repo and pool after migrations")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user