feat(archie): add container runtime, migrate command, secret files, and proxy egress
This commit is contained in:
13 files changed
+803
-61
No files matched your search
@@ -9,6 +9,9 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
// Registers the "pgx" database/sql driver that sqlx and goose use.
|
||||
@@ -121,9 +124,69 @@ func grantRuntime(ctx context.Context, db *sqlx.DB, runtimeURL string, schemas [
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, "GRANT SELECT ON TABLE public.goose_db_version TO "+role); err != nil {
|
||||
return fmt.Errorf("grant runtime role on goose_db_version: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// VerifySchema checks that every embedded migration has been applied to db.
|
||||
// Serving processes without DDL credentials call this at boot and refuse to
|
||||
// serve if migrations are pending.
|
||||
func VerifySchema(ctx context.Context, db *sqlx.DB) error {
|
||||
embedded, err := embeddedMigrationVersions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var applied []int64
|
||||
if err := db.SelectContext(ctx, &applied, "SELECT version_id FROM public.goose_db_version WHERE is_applied"); err != nil {
|
||||
return fmt.Errorf("read schema version (has 'archie migrate' run?): %w", err)
|
||||
}
|
||||
missing := missingMigrations(embedded, applied)
|
||||
if len(missing) > 0 {
|
||||
return fmt.Errorf("database schema is behind by %d migration(s) (next missing: %d); run 'archie migrate'", len(missing), missing[0])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func embeddedMigrationVersions() ([]int64, error) {
|
||||
entries, err := fs.ReadDir(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read embedded migrations: %w", err)
|
||||
}
|
||||
var versions []int64
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
|
||||
continue
|
||||
}
|
||||
prefix, _, ok := strings.Cut(entry.Name(), "_")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("migration %q has no version prefix", entry.Name())
|
||||
}
|
||||
v, err := strconv.ParseInt(prefix, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse migration %q version: %w", entry.Name(), err)
|
||||
}
|
||||
versions = append(versions, v)
|
||||
}
|
||||
slices.Sort(versions)
|
||||
return versions, nil
|
||||
}
|
||||
|
||||
func missingMigrations(embedded, applied []int64) []int64 {
|
||||
seen := make(map[int64]struct{}, len(applied))
|
||||
for _, v := range applied {
|
||||
seen[v] = struct{}{}
|
||||
}
|
||||
var missing []int64
|
||||
for _, v := range embedded {
|
||||
if _, ok := seen[v]; !ok {
|
||||
missing = append(missing, v)
|
||||
}
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
// ServeLockKey is the advisory-lock key the serving processes hold in its shared
|
||||
// form and a restore takes exclusively: several processes may serve at once, and
|
||||
// none may serve while a restore runs. The other archie binaries lock the same
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package pg
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmbeddedMigrationVersions(t *testing.T) {
|
||||
versions, err := embeddedMigrationVersions()
|
||||
if err != nil {
|
||||
t.Fatalf("embeddedMigrationVersions: %v", err)
|
||||
}
|
||||
if len(versions) == 0 {
|
||||
t.Fatal("expected at least one embedded migration")
|
||||
}
|
||||
if !slices.IsSorted(versions) {
|
||||
t.Fatalf("versions not sorted: %v", versions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMissingMigrations guards schema verification for DML-only serving pods.
|
||||
func TestMissingMigrations(t *testing.T) {
|
||||
embedded := []int64{1, 2, 3}
|
||||
tests := []struct {
|
||||
name string
|
||||
applied []int64
|
||||
want []int64
|
||||
}{
|
||||
{name: "up to date", applied: []int64{1, 2, 3}, want: nil},
|
||||
{name: "ahead with extra history", applied: []int64{1, 2, 3, 4}, want: nil},
|
||||
{name: "pending tail migration", applied: []int64{1, 2}, want: []int64{3}},
|
||||
{name: "missing middle migration", applied: []int64{1, 3}, want: []int64{2}},
|
||||
{name: "empty database", applied: nil, want: []int64{1, 2, 3}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := missingMigrations(embedded, tc.applied)
|
||||
if !slices.Equal(got, tc.want) {
|
||||
t.Fatalf("missingMigrations(%v, %v) = %v, want %v", embedded, tc.applied, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user