Files
knowledge-governance/internal/platform/postgres/v1_migration_seed_test.go
T

63 lines
2.1 KiB
Go

package postgres
import (
"context"
"os"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
)
// TestV1Migration31SeedsRealSizedConnectionIDs reproduces the exact live-stack
// failure the first deployment surfaced: source_connections rows carry 36-char
// UUID ids, so a seeded revision id of 'scr-'||c.id overflows VARCHAR(36).
// Migration 31 must seed cleanly for real-sized ids.
func TestV1Migration31SeedsRealSizedConnectionIDs(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
// Roll back to just before migration 31 and re-enter it with live-shaped ids.
for {
var version int
if err := pool.QueryRow(ctx, "SELECT MAX(version) FROM schema_migrations").Scan(&version); err != nil {
t.Fatal(err)
}
if version <= 30 {
break
}
if err := RollbackLatestMigration(ctx, pool); err != nil {
t.Fatal(err)
}
}
const uuidID = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d" // exactly 36 chars
if _, err := pool.Exec(ctx, `INSERT INTO source_connections(id,source_type,name,enabled) VALUES($1,'alist','真实尺寸UUID源',true)`, uuidID); err != nil {
t.Fatal(err)
}
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatalf("migration 31 with 36-char connection id: %v", err)
}
var revisionID, currentRevisionID string
if err := pool.QueryRow(ctx, `SELECT id FROM source_connection_revisions WHERE connection_id=$1 AND revision_number=1 AND state='active'`, uuidID).Scan(&revisionID); err != nil {
t.Fatal(err)
}
if len(revisionID) > 36 {
t.Fatalf("seeded revision id %q exceeds 36 characters", revisionID)
}
if err := pool.QueryRow(ctx, `SELECT current_revision_id FROM source_connections WHERE id=$1`, uuidID).Scan(&currentRevisionID); err != nil {
t.Fatal(err)
}
if currentRevisionID != revisionID {
t.Fatalf("current_revision_id=%q want %q", currentRevisionID, revisionID)
}
}