Skip to content

Commit 7654132

Browse files
committed
fix(migrations): tolerate missing postgres role in rls grant migration
Wrap the grant-select-to-postgres block in an existence check so self-hosted deployments using a non-default DB superuser no longer crash-loop on first start with `ERROR: role "postgres" does not exist`. RLS is still enabled unconditionally on every auth table; only the grants are gated on the role actually existing. This matches the intent of the upstream Supabase migration without breaking custom Postgres setups. Documented in appflowy/APPFLOWY_BRANCH_MIGRATION_HISTORY.md so the patch is re-applied on every master merge. Refs AppFlowy-Cloud#1615
1 parent 5deab02 commit 7654132

2 files changed

Lines changed: 35 additions & 17 deletions

File tree

appflowy/APPFLOWY_BRANCH_MIGRATION_HISTORY.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ mindmap
6666

6767
---
6868

69+
### In-Place Patches to Upstream Migrations
70+
71+
In addition to the cherry-picked feature commits, the following upstream files
72+
have been modified in place. Re-apply these on every merge from master:
73+
74+
| File | Patch | Why |
75+
|------|-------|-----|
76+
| `migrations/20240612123726_enable_rls_update_grants.up.sql` | Wraps the `grant select … to postgres with grant option` block in `if exists (select 1 from pg_roles where rolname = 'postgres') then … end if;` | Upstream hardcodes the `postgres` role. Self-hosted deployments using a non-default DB superuser hit `ERROR: role "postgres" does not exist (SQLSTATE 42704)` and crash-loop on first start. RLS is still enabled unconditionally; only the grants are gated. See AppFlowy-Cloud issue #1615. |
77+
78+
**On every master merge:** check `git diff master..HEAD -- migrations/20240612123726_enable_rls_update_grants.up.sql` is non-empty and the conditional is still present. If a merge undoes the patch (e.g., upstream rewrites the file), re-apply the gate before tagging a release.
79+
80+
---
81+
6982
## Verification: Master Does NOT Have These
7083

7184
**All 11 commits are necessary** - Verified against current master (commit `4e8275f`):

migrations/20240612123726_enable_rls_update_grants.up.sql

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@ do $$ begin
1616
alter table {{ index .Options "Namespace" }}.flow_state enable row level security;
1717
alter table {{ index .Options "Namespace" }}.identities enable row level security;
1818
alter table {{ index .Options "Namespace" }}.one_time_tokens enable row level security;
19-
-- allow postgres role to select from auth tables and allow it to grant select to other roles
20-
grant select on {{ index .Options "Namespace" }}.schema_migrations to postgres with grant option;
21-
grant select on {{ index .Options "Namespace" }}.instances to postgres with grant option;
22-
grant select on {{ index .Options "Namespace" }}.users to postgres with grant option;
23-
grant select on {{ index .Options "Namespace" }}.audit_log_entries to postgres with grant option;
24-
grant select on {{ index .Options "Namespace" }}.saml_relay_states to postgres with grant option;
25-
grant select on {{ index .Options "Namespace" }}.refresh_tokens to postgres with grant option;
26-
grant select on {{ index .Options "Namespace" }}.mfa_factors to postgres with grant option;
27-
grant select on {{ index .Options "Namespace" }}.sessions to postgres with grant option;
28-
grant select on {{ index .Options "Namespace" }}.sso_providers to postgres with grant option;
29-
grant select on {{ index .Options "Namespace" }}.sso_domains to postgres with grant option;
30-
grant select on {{ index .Options "Namespace" }}.mfa_challenges to postgres with grant option;
31-
grant select on {{ index .Options "Namespace" }}.mfa_amr_claims to postgres with grant option;
32-
grant select on {{ index .Options "Namespace" }}.saml_providers to postgres with grant option;
33-
grant select on {{ index .Options "Namespace" }}.flow_state to postgres with grant option;
34-
grant select on {{ index .Options "Namespace" }}.identities to postgres with grant option;
35-
grant select on {{ index .Options "Namespace" }}.one_time_tokens to postgres with grant option;
19+
-- allow postgres role to select from auth tables and allow it to grant select to other roles.
20+
-- AppFlowy patch: skip these grants when the postgres role does not exist, so self-hosted
21+
-- deployments that use a non-default DB superuser can still apply this migration.
22+
-- See AppFlowy-Cloud issue #1615.
23+
if exists (select 1 from pg_roles where rolname = 'postgres') then
24+
grant select on {{ index .Options "Namespace" }}.schema_migrations to postgres with grant option;
25+
grant select on {{ index .Options "Namespace" }}.instances to postgres with grant option;
26+
grant select on {{ index .Options "Namespace" }}.users to postgres with grant option;
27+
grant select on {{ index .Options "Namespace" }}.audit_log_entries to postgres with grant option;
28+
grant select on {{ index .Options "Namespace" }}.saml_relay_states to postgres with grant option;
29+
grant select on {{ index .Options "Namespace" }}.refresh_tokens to postgres with grant option;
30+
grant select on {{ index .Options "Namespace" }}.mfa_factors to postgres with grant option;
31+
grant select on {{ index .Options "Namespace" }}.sessions to postgres with grant option;
32+
grant select on {{ index .Options "Namespace" }}.sso_providers to postgres with grant option;
33+
grant select on {{ index .Options "Namespace" }}.sso_domains to postgres with grant option;
34+
grant select on {{ index .Options "Namespace" }}.mfa_challenges to postgres with grant option;
35+
grant select on {{ index .Options "Namespace" }}.mfa_amr_claims to postgres with grant option;
36+
grant select on {{ index .Options "Namespace" }}.saml_providers to postgres with grant option;
37+
grant select on {{ index .Options "Namespace" }}.flow_state to postgres with grant option;
38+
grant select on {{ index .Options "Namespace" }}.identities to postgres with grant option;
39+
grant select on {{ index .Options "Namespace" }}.one_time_tokens to postgres with grant option;
40+
end if;
3641
end $$;

0 commit comments

Comments
 (0)