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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 01:28:43 +01:00
1 parent 5f42e3e650
commit 7bb4afa04a
3 files changed
+67 -2

No files matched your search

@@ -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()));