From 7bb4afa04ad238096997e7dab61c08e76d75008a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 01:28:43 +0100 Subject: [PATCH] fix(migrations): run migrations in the system scope and repair skipped data Row-level security hid every row from the data changes in migrations 00009 and 00012: existing members did not receive the Viewer role and the built-in Admin role lacked tokens.manage. Built-in roles were also not writable in the system scope. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/migrations/migrations.go | 10 ++++-- internal/migrations/migrations_test.go | 35 +++++++++++++++++++ .../sql/00016_repair_scoped_data.sql | 24 +++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 internal/migrations/migrations_test.go create mode 100644 internal/migrations/sql/00016_repair_scoped_data.sql diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index 26587b3..82646e6 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -9,7 +9,8 @@ import ( "fmt" "io/fs" - _ "github.com/jackc/pgx/v5/stdlib" // registers the "pgx" database/sql driver + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/stdlib" "github.com/pressly/goose/v3" "github.com/pressly/goose/v3/lock" ) @@ -21,11 +22,16 @@ var files embed.FS // a rolling deploy serialize instead of racing. const lockID = 0x7570746d +// newProvider connects with app.scope=system as a session default: tables +// are under row-level security, and data changes in migrations would +// otherwise silently affect no rows. func newProvider(dsn string) (*goose.Provider, *sql.DB, error) { - db, err := sql.Open("pgx", dsn) + cfg, err := pgx.ParseConfig(dsn) if err != nil { return nil, nil, err } + cfg.RuntimeParams["app.scope"] = "system" + db := stdlib.OpenDB(*cfg) sub, err := fs.Sub(files, "sql") if err != nil { return nil, nil, err diff --git a/internal/migrations/migrations_test.go b/internal/migrations/migrations_test.go new file mode 100644 index 0000000..1a36e75 --- /dev/null +++ b/internal/migrations/migrations_test.go @@ -0,0 +1,35 @@ +package migrations + +import ( + "context" + "os" + "slices" + "testing" + + "github.com/jackc/pgx/v5" +) + +// TestDataMigrationsApply catches data changes that row-level security +// silently filtered out, as happened to 00009 and 00012. +func TestDataMigrationsApply(t *testing.T) { + dsn := os.Getenv("UPTIME_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("UPTIME_TEST_DATABASE_URL not set") + } + ctx := context.Background() + if _, err := Up(ctx, dsn); err != nil { + t.Fatalf("Up() failed: %v", err) + } + conn, err := pgx.Connect(ctx, dsn) + if err != nil { + t.Fatal(err) + } + defer func() { _ = conn.Close(ctx) }() + var perms []string + if err := conn.QueryRow(ctx, `SELECT permissions FROM roles WHERE key = 'admin'`).Scan(&perms); err != nil { + t.Fatal(err) + } + if !slices.Contains(perms, "tokens.manage") { + t.Errorf("admin role permissions = %v, want tokens.manage", perms) + } +} diff --git a/internal/migrations/sql/00016_repair_scoped_data.sql b/internal/migrations/sql/00016_repair_scoped_data.sql new file mode 100644 index 0000000..4ab1eb7 --- /dev/null +++ b/internal/migrations/sql/00016_repair_scoped_data.sql @@ -0,0 +1,24 @@ +-- +goose Up +-- Migrations 00009 and 00012 changed data before migration connections ran +-- with the system scope, so row-level security hid every row from them. +-- Built-in roles were also not writable even in the system scope. + +DROP POLICY roles_write ON roles; +CREATE POLICY roles_write ON roles FOR ALL + USING (app_scope_system() OR (org_id IS NOT NULL AND org_id = app_org_id())) + WITH CHECK (app_scope_system() OR (org_id IS NOT NULL AND org_id = app_org_id())); + +UPDATE roles SET permissions = array_append(permissions, 'tokens.manage') +WHERE key = 'admin' AND NOT 'tokens.manage' = ANY (permissions); + +INSERT INTO role_assignments (org_id, user_id, role_id) +SELECT m.org_id, m.user_id, '00000000-0000-0000-0000-00000000a003' +FROM memberships m +WHERE NOT m.is_owner + AND NOT EXISTS (SELECT 1 FROM role_assignments a WHERE a.org_id = m.org_id AND a.user_id = m.user_id); + +-- +goose Down +DROP POLICY roles_write ON roles; +CREATE POLICY roles_write ON roles FOR ALL + USING (org_id IS NOT NULL AND (app_scope_system() OR org_id = app_org_id())) + WITH CHECK (org_id IS NOT NULL AND (app_scope_system() OR org_id = app_org_id()));