Микросервис обучения портала: тесты, курсы, видео-уроки, доступы, public-ссылки для кандидатов с email-валидацией. В этой итерации: - Skeleton (config, migrate, main, health) по паттерну tasks/candidates - Migration 001_init: 10 таблиц (tests/questions/answers/attempts/ attempt_answers + courses/lessons/lesson_progress + access_grants + public_tokens) с подробными комментариями why - Tests: полный CRUD + вопросы/ответы; non-owner'у is_correct и explanation скрываются в выдаче - Заглушки 501 для attempts / courses / lessons / video-stream / access / public-tokens — следующие итерации - k8s: namespace, configmap, secrets, postgres, deployment с HPA, service с portal-discovery annotations - Dockerfile, Makefile, .gitignore См. README.md для полного списка отложенного и инструкций запуска. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
// Package migrate — runner sql-миграций, идентичный candidates/tasks/booking.
|
|
package migrate
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func Run(ctx context.Context, pool *pgxpool.Pool, migrationsDir string) error {
|
|
_, err := pool.Exec(ctx, `
|
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version VARCHAR(255) PRIMARY KEY,
|
|
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
)
|
|
`)
|
|
if err != nil {
|
|
return fmt.Errorf("create migrations table: %w", err)
|
|
}
|
|
|
|
files, err := filepath.Glob(filepath.Join(migrationsDir, "*.up.sql"))
|
|
if err != nil {
|
|
return fmt.Errorf("glob migrations: %w", err)
|
|
}
|
|
sort.Strings(files)
|
|
|
|
for _, f := range files {
|
|
version := strings.TrimSuffix(filepath.Base(f), ".up.sql")
|
|
var exists bool
|
|
if err := pool.QueryRow(ctx,
|
|
`SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version = $1)`,
|
|
version).Scan(&exists); err != nil {
|
|
return fmt.Errorf("check migration %s: %w", version, err)
|
|
}
|
|
if exists {
|
|
continue
|
|
}
|
|
sql, err := os.ReadFile(f)
|
|
if err != nil {
|
|
return fmt.Errorf("read migration %s: %w", version, err)
|
|
}
|
|
if _, err := pool.Exec(ctx, string(sql)); err != nil {
|
|
return fmt.Errorf("apply migration %s: %w", version, err)
|
|
}
|
|
if _, err := pool.Exec(ctx,
|
|
`INSERT INTO schema_migrations (version) VALUES ($1)`, version); err != nil {
|
|
return fmt.Errorf("record migration %s: %w", version, err)
|
|
}
|
|
slog.Info("applied migration", "version", version)
|
|
}
|
|
return nil
|
|
}
|