feat(policy): add immutable versioned product policy store

This commit is contained in:
shira committed 2026-10-06 01:52:39 +03:00
1 parent 0e2a3b02ab
commit 7a5105acc7
30 files changed
+3893 -37

No files matched your search

@@ -19,13 +19,20 @@ const (
concurrentMigrators = 4
)
// identityMigrationVersion creates identityTableNames.
const identityMigrationVersion = 2
const (
// identityMigrationVersion creates identityTableNames.
identityMigrationVersion = 2
// policyMigrationVersion creates policyTableNames and policyFunctionNames.
policyMigrationVersion = 3
)
var (
// Tables created by 00001 and by 00002.
// Tables created by 00001, 00002, and 00003.
outboxTables = []string{"outbox_events"}
identityTableNames = []string{"users", "email_tokens", "sessions"}
policyTableNames = []string{"policy_versions"}
// Trigger functions created by 00003.
policyFunctionNames = []string{"policy_versions_refuse_change()", "policy_versions_stamp_creation()"}
)
func TestPostgresMigrations(t *testing.T) {
@@ -52,8 +59,10 @@ func TestPostgresMigrations(t *testing.T) {
}
assertTablesExist(ctx, t, pool, true, outboxTables...)
assertTablesExist(ctx, t, pool, true, identityTableNames...)
assertTablesExist(ctx, t, pool, true, policyTableNames...)
assertFunctionsExist(ctx, t, pool, true, policyFunctionNames...)
identityRolledBack := false
identityRolledBack, policyRolledBack := false, false
for rolledBack := 0; ; rolledBack++ {
result, err := migrator.DownOne(ctx)
if errors.Is(err, postgresadapter.ErrNoMigrationToRollBack) {
@@ -65,6 +74,14 @@ func TestPostgresMigrations(t *testing.T) {
if err != nil {
t.Fatalf("DownOne() after %d rollbacks error = %v", rolledBack, err)
}
if result.Version == policyMigrationVersion {
// The policy migration rolls back its table, triggers, and
// function and leaves the identity tables.
assertTablesExist(ctx, t, pool, false, policyTableNames...)
assertFunctionsExist(ctx, t, pool, false, policyFunctionNames...)
assertTablesExist(ctx, t, pool, true, identityTableNames...)
policyRolledBack = true
}
if result.Version == identityMigrationVersion {
// The identity migration rolls back on its own and leaves the
// outbox migration before it.
@@ -73,11 +90,12 @@ func TestPostgresMigrations(t *testing.T) {
identityRolledBack = true
}
}
if !identityRolledBack {
t.Fatalf("migration %d was not rolled back", identityMigrationVersion)
if !identityRolledBack || !policyRolledBack {
t.Fatalf("migrations %d and %d were not both rolled back", identityMigrationVersion, policyMigrationVersion)
}
assertTablesExist(ctx, t, pool, false, outboxTables...)
assertTablesExist(ctx, t, pool, false, identityTableNames...)
assertTablesExist(ctx, t, pool, false, policyTableNames...)
var leftovers int
err = pool.QueryRow(ctx, `
SELECT count(*) FROM pg_catalog.pg_class AS c
@@ -89,6 +107,17 @@ func TestPostgresMigrations(t *testing.T) {
if leftovers != 0 {
t.Fatalf("%d relations remain after rolling back every migration", leftovers)
}
var leftoverFunctions int
err = pool.QueryRow(ctx, `
SELECT count(*) FROM pg_catalog.pg_proc AS p
JOIN pg_catalog.pg_namespace AS n ON n.oid = p.pronamespace
WHERE n.nspname = 'public'`).Scan(&leftoverFunctions)
if err != nil {
t.Fatalf("count leftover functions: %v", err)
}
if leftoverFunctions != 0 {
t.Fatalf("%d functions remain after rolling back every migration", leftoverFunctions)
}
applied, err = migrator.Up(ctx)
if err != nil {
@@ -99,6 +128,8 @@ func TestPostgresMigrations(t *testing.T) {
}
assertTablesExist(ctx, t, pool, true, outboxTables...)
assertTablesExist(ctx, t, pool, true, identityTableNames...)
assertTablesExist(ctx, t, pool, true, policyTableNames...)
assertFunctionsExist(ctx, t, pool, true, policyFunctionNames...)
assertAllApplied(ctx, t, migrator, total)
})
@@ -164,3 +195,16 @@ func assertTablesExist(ctx context.Context, t *testing.T, pool *pgxpool.Pool, wa
}
}
}
func assertFunctionsExist(ctx context.Context, t *testing.T, pool *pgxpool.Pool, want bool, signatures ...string) {
t.Helper()
for _, signature := range signatures {
var exists bool
if err := pool.QueryRow(ctx, `SELECT to_regprocedure($1) IS NOT NULL`, "public."+signature).Scan(&exists); err != nil {
t.Fatalf("check function %s: %v", signature, err)
}
if exists != want {
t.Fatalf("function %s exists = %t, want %t", signature, exists, want)
}
}
}