@rebasepro/server-postgres 0.13.1-canary.gef9608c → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/dist/PostgresBackendDriver.d.ts +48 -1
  2. package/dist/PostgresBootstrapper.d.ts +26 -0
  3. package/dist/auth/services.d.ts +21 -0
  4. package/dist/{src-CU6WZGYV.js → auth-users-columns-BfQHf9JE.js} +1111 -92
  5. package/dist/auth-users-columns-BfQHf9JE.js.map +1 -0
  6. package/dist/{backup-service-CD8o_1Sl.js → backup-service-BH0Dzo_h.js} +2 -3
  7. package/dist/{backup-service-CD8o_1Sl.js.map → backup-service-BH0Dzo_h.js.map} +1 -1
  8. package/dist/cli-helpers.d.ts +56 -0
  9. package/dist/cli-output.d.ts +34 -0
  10. package/dist/data-transformer.d.ts +7 -2
  11. package/dist/data_driver-ULAyJEi9.js +193 -0
  12. package/dist/data_driver-ULAyJEi9.js.map +1 -0
  13. package/dist/ensure-collection-policies-8vuu-n4r.js +124 -0
  14. package/dist/ensure-collection-policies-8vuu-n4r.js.map +1 -0
  15. package/dist/{ensure-collection-tables-BLIIACla.js → ensure-collection-tables-CbvaGuVn.js} +162 -16
  16. package/dist/ensure-collection-tables-CbvaGuVn.js.map +1 -0
  17. package/dist/index.es.js +1720 -946
  18. package/dist/index.es.js.map +1 -1
  19. package/dist/rls-bootstrap-sql-69hYT8nr.js +244 -0
  20. package/dist/rls-bootstrap-sql-69hYT8nr.js.map +1 -0
  21. package/dist/rls-enforcement-BJ_3wxwg.js +425 -0
  22. package/dist/rls-enforcement-BJ_3wxwg.js.map +1 -0
  23. package/dist/schema/auth-schema.d.ts +102 -0
  24. package/dist/schema/auth-users-columns.d.ts +97 -0
  25. package/dist/schema/doctor-policy-checks.d.ts +28 -0
  26. package/dist/schema/doctor.d.ts +41 -25
  27. package/dist/schema/ensure-collection-policies.d.ts +33 -9
  28. package/dist/schema/ensure-collection-tables.d.ts +60 -6
  29. package/dist/schema/generate-drizzle-schema-logic.d.ts +9 -1
  30. package/dist/schema/generate-postgres-ddl-logic.d.ts +48 -0
  31. package/dist/schema/introspect-db-inference.d.ts +8 -1
  32. package/dist/schema/introspect-db-logic.d.ts +49 -0
  33. package/dist/schema/introspect-db-project.d.ts +21 -0
  34. package/dist/schema/rls-bootstrap-sql.d.ts +135 -0
  35. package/dist/schema/search-column.d.ts +248 -0
  36. package/dist/security/policy-drift.d.ts +34 -0
  37. package/dist/security/rls-enforcement.d.ts +61 -5
  38. package/dist/services/FetchService.d.ts +24 -0
  39. package/dist/services/PersistService.d.ts +21 -17
  40. package/dist/services/RelationService.d.ts +9 -57
  41. package/dist/services/RelationWriteService.d.ts +82 -0
  42. package/dist/services/collection-helpers.d.ts +42 -0
  43. package/dist/services/dataService.d.ts +3 -0
  44. package/dist/services/junction-writes.d.ts +82 -0
  45. package/dist/services/realtimeService.d.ts +139 -2
  46. package/dist/services/write-denial.d.ts +36 -0
  47. package/dist/{src-DoU9yPqq.js → src-DCdn3Val.js} +124 -3
  48. package/dist/src-DCdn3Val.js.map +1 -0
  49. package/dist/utils/drizzle-conditions.d.ts +124 -2
  50. package/dist/{websocket-B2LsrINK.js → websocket-C8ZqVBiV.js} +75 -18
  51. package/dist/websocket-C8ZqVBiV.js.map +1 -0
  52. package/package.json +8 -7
  53. package/src/PostgresBackendDriver.ts +172 -6
  54. package/src/PostgresBootstrapper.ts +136 -11
  55. package/src/auth/ensure-tables.ts +212 -91
  56. package/src/auth/services.ts +82 -5
  57. package/src/backup/backup-cli.ts +59 -57
  58. package/src/cli-errors.ts +6 -6
  59. package/src/cli-helpers.ts +124 -11
  60. package/src/cli-output.ts +43 -0
  61. package/src/cli.ts +299 -168
  62. package/src/collections/buildRegistry.ts +3 -1
  63. package/src/data-transformer.ts +129 -25
  64. package/src/history/ensure-history-table.ts +9 -2
  65. package/src/schema/auth-schema.ts +17 -1
  66. package/src/schema/auth-users-columns.ts +131 -0
  67. package/src/schema/doctor-cli.ts +14 -65
  68. package/src/schema/doctor-policy-checks.ts +105 -0
  69. package/src/schema/doctor.ts +149 -72
  70. package/src/schema/ensure-collection-policies.ts +99 -6
  71. package/src/schema/ensure-collection-tables.ts +366 -30
  72. package/src/schema/generate-drizzle-schema-logic.ts +146 -66
  73. package/src/schema/generate-drizzle-schema.ts +11 -10
  74. package/src/schema/generate-postgres-ddl-logic.ts +277 -10
  75. package/src/schema/generate-postgres-ddl.ts +38 -14
  76. package/src/schema/generated-schema-staleness.ts +14 -7
  77. package/src/schema/introspect-db-inference.ts +9 -2
  78. package/src/schema/introspect-db-logic.ts +251 -75
  79. package/src/schema/introspect-db-project.ts +78 -0
  80. package/src/schema/introspect-db.ts +42 -25
  81. package/src/schema/introspect-runtime.ts +14 -2
  82. package/src/schema/rls-bootstrap-sql.ts +288 -0
  83. package/src/schema/search-column.ts +643 -0
  84. package/src/security/anonymous-grants.test.ts +4 -2
  85. package/src/security/policy-drift.test.ts +104 -3
  86. package/src/security/policy-drift.ts +129 -7
  87. package/src/security/rls-enforcement.ts +150 -7
  88. package/src/services/BranchService.ts +5 -0
  89. package/src/services/FetchService.ts +243 -22
  90. package/src/services/PersistService.ts +68 -42
  91. package/src/services/RelationService.ts +37 -696
  92. package/src/services/RelationWriteService.ts +653 -0
  93. package/src/services/cdc/trigger-cdc.ts +5 -1
  94. package/src/services/channel-history.ts +14 -0
  95. package/src/services/channel-presence.ts +13 -0
  96. package/src/services/collection-helpers.ts +89 -4
  97. package/src/services/dataService.ts +3 -0
  98. package/src/services/junction-writes.ts +295 -0
  99. package/src/services/pg-notify-listener.ts +1 -1
  100. package/src/services/realtimeService.ts +347 -86
  101. package/src/services/write-denial.ts +55 -0
  102. package/src/utils/drizzle-conditions.ts +433 -35
  103. package/src/utils/pg-error-utils.ts +8 -3
  104. package/src/websocket.ts +113 -16
  105. package/dist/ensure-collection-policies-Bck0ky4u.js +0 -57
  106. package/dist/ensure-collection-policies-Bck0ky4u.js.map +0 -1
  107. package/dist/ensure-collection-tables-BLIIACla.js.map +0 -1
  108. package/dist/policy-CeA1JcxP.js +0 -105
  109. package/dist/policy-CeA1JcxP.js.map +0 -1
  110. package/dist/schema/auth-bootstrap-sql.d.ts +0 -24
  111. package/dist/src-CU6WZGYV.js.map +0 -1
  112. package/dist/src-DoU9yPqq.js.map +0 -1
  113. package/dist/websocket-B2LsrINK.js.map +0 -1
  114. package/src/schema/auth-bootstrap-sql.ts +0 -47
@@ -0,0 +1,244 @@
1
+ import { createRequire as __createRequire } from "module";
2
+ import "process";
3
+ __createRequire(import.meta.url);
4
+ import { c as __exportAll } from "./connection-BuZ97wsr.js";
5
+ import { n as REBASE_SCHEMA, t as LEGACY_RLS_SCHEMA } from "./src-DCdn3Val.js";
6
+ //#region src/schema/rls-bootstrap-sql.ts
7
+ var rls_bootstrap_sql_exports = /* @__PURE__ */ __exportAll({
8
+ DROP_LEGACY_AUTH_SCHEMA_SQL: () => DROP_LEGACY_AUTH_SCHEMA_SQL,
9
+ LEGACY_RLS_DEPENDENTS_SQL: () => LEGACY_RLS_DEPENDENTS_SQL,
10
+ LEGACY_RLS_FUNCTION_DEPENDENTS_SQL: () => LEGACY_RLS_FUNCTION_DEPENDENTS_SQL,
11
+ RLS_BOOTSTRAP_STATEMENTS: () => RLS_BOOTSTRAP_STATEMENTS,
12
+ dropLegacyAuthSchema: () => dropLegacyAuthSchema
13
+ });
14
+ /**
15
+ * Canonical SQL bootstrap for the RLS helper functions.
16
+ *
17
+ * Generated RLS policies reference `rebase.uid()` / `rebase.roles()` /
18
+ * `rebase.jwt()`, so any SQL stream that can contain policies must be
19
+ * self-contained: it has to (re)create these helpers first. This matters for
20
+ * the migration directory in particular — Atlas replays migrations against a
21
+ * clean dev database where no out-of-band bootstrap has ever run, so a
22
+ * migration carrying policies without this preamble fails with
23
+ * "function rebase.uid() does not exist".
24
+ *
25
+ * Idempotent (`IF NOT EXISTS` / `OR REPLACE`) so it can be prepended to every
26
+ * policies block and re-applied freely. The runtime boot path
27
+ * (`auth/ensure-tables.ts`) creates the same functions under an advisory lock
28
+ * for HMR-safety; keep the definitions in sync.
29
+ *
30
+ * ## Creating the `rebase` schema here is now safe, and required
31
+ *
32
+ * It deliberately did not, once. These functions lived in a schema called
33
+ * `auth`, and the note here read: creating `rebase` would leak it into Atlas's
34
+ * replayed migration state, and — absent from the desired `schema.sql` — Atlas
35
+ * would then plan `DROP SCHEMA "rebase" CASCADE`, taking the auth tables with
36
+ * it. That reasoning still holds; what changed is the second half of it. The
37
+ * DDL generator now emits `CREATE SCHEMA IF NOT EXISTS "rebase"`
38
+ * unconditionally, so the schema is always in the desired state and the diff is
39
+ * empty. (It used to appear only when some collection happened to declare
40
+ * `schema: "rebase"` — true for the scaffold's users collection, and not a
41
+ * property anything guaranteed.) `db push` additionally excludes the whole
42
+ * schema from the declarative apply.
43
+ *
44
+ * See `@rebasepro/types`' `rls-functions` for why the functions moved out of
45
+ * `auth` at all: the short version is that the name was Supabase's, and
46
+ * `CREATE OR REPLACE FUNCTION auth.uid() RETURNS text` cannot be applied over
47
+ * Supabase's `RETURNS uuid` — Postgres refuses, and the refusal used to be
48
+ * swallowed.
49
+ */
50
+ /**
51
+ * The bootstrap as individual statements.
52
+ *
53
+ * Kept as an array because the two consumers need different shapes and only one
54
+ * of them can take a multi-command string: the migration preamble is written to
55
+ * a file and replayed by Atlas, but the boot path runs through drizzle, whose
56
+ * node-postgres handle speaks the extended query protocol and rejects more than
57
+ * one command per call. Splitting a joined string back apart on `$$;` would be
58
+ * a parser for a problem that does not need one.
59
+ */
60
+ var RLS_BOOTSTRAP_STATEMENTS = [
61
+ `CREATE SCHEMA IF NOT EXISTS ${REBASE_SCHEMA}`,
62
+ `CREATE OR REPLACE FUNCTION ${REBASE_SCHEMA}.uid() RETURNS text AS $$
63
+ SELECT COALESCE(
64
+ NULLIF(current_setting('app.uid', true), ''),
65
+ NULLIF(current_setting('app.user_id', true), '')
66
+ );
67
+ $$ LANGUAGE sql STABLE`,
68
+ `CREATE OR REPLACE FUNCTION ${REBASE_SCHEMA}.jwt() RETURNS jsonb AS $$
69
+ SELECT COALESCE(
70
+ NULLIF(current_setting('app.jwt', true), ''),
71
+ '{}'
72
+ )::jsonb;
73
+ $$ LANGUAGE sql STABLE`,
74
+ `CREATE OR REPLACE FUNCTION ${REBASE_SCHEMA}.roles() RETURNS text AS $$
75
+ SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');
76
+ $$ LANGUAGE sql STABLE`
77
+ ];
78
+ "" + RLS_BOOTSTRAP_STATEMENTS.map((s) => `${s};`).join("\n\n");
79
+ /**
80
+ * Removes the pre-1.0 `auth` schema, but only when Rebase is what put it there.
81
+ *
82
+ * ## Why this is safe against a Supabase database
83
+ *
84
+ * Two independent guards, and both have to pass:
85
+ *
86
+ * 1. **Each function is identified before it is dropped.** Ours returns `text`
87
+ * and reads the `app.uid` GUC; Supabase's returns `uuid` and reads
88
+ * `request.jwt.claims`. Nothing is dropped on a signature we did not write,
89
+ * so a Supabase database — where our `CREATE OR REPLACE` could never have
90
+ * succeeded in the first place, Postgres refusing to change a return type —
91
+ * matches nothing and this is a no-op.
92
+ * 2. **`DROP SCHEMA … RESTRICT`**, never CASCADE. If anything else at all still
93
+ * lives in `auth` (Supabase's `users` table, its other helpers), the drop
94
+ * fails and the schema stays. CASCADE here would be unrecoverable.
95
+ *
96
+ * ## Why it cannot run too early
97
+ *
98
+ * Postgres records a dependency from every RLS policy to the functions its body
99
+ * calls, so `DROP FUNCTION auth.uid()` fails for as long as a single policy
100
+ * still references it. That is the interlock, and it is load-bearing: the drop
101
+ * can only succeed once every policy has been recompiled to \`rebase.uid()\`.
102
+ * Callers therefore run this *after* applying policies, and treat a failure as
103
+ * "not yet — try again next boot" rather than as an error.
104
+ */
105
+ var DROP_LEGACY_AUTH_SCHEMA_SQL = `
106
+ DO $rebase_drop_legacy$
107
+ DECLARE
108
+ dropped_any boolean := false;
109
+ BEGIN
110
+ -- Each function is matched on its own result type and body, so a schema
111
+ -- that merely shares the name keeps everything it has.
112
+ IF EXISTS (
113
+ SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
114
+ WHERE n.nspname = 'auth' AND p.proname = 'uid'
115
+ AND pg_get_function_result(p.oid) = 'text'
116
+ AND p.prosrc LIKE '%app.uid%'
117
+ ) THEN
118
+ DROP FUNCTION auth.uid();
119
+ dropped_any := true;
120
+ END IF;
121
+
122
+ IF EXISTS (
123
+ SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
124
+ WHERE n.nspname = 'auth' AND p.proname = 'jwt'
125
+ AND pg_get_function_result(p.oid) = 'jsonb'
126
+ AND p.prosrc LIKE '%app.jwt%'
127
+ ) THEN
128
+ DROP FUNCTION auth.jwt();
129
+ dropped_any := true;
130
+ END IF;
131
+
132
+ IF EXISTS (
133
+ SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
134
+ WHERE n.nspname = 'auth' AND p.proname = 'roles'
135
+ AND pg_get_function_result(p.oid) = 'text'
136
+ AND p.prosrc LIKE '%app.user_roles%'
137
+ ) THEN
138
+ DROP FUNCTION auth.roles();
139
+ dropped_any := true;
140
+ END IF;
141
+
142
+ -- RESTRICT: only an empty schema goes. Anything else in there — including a
143
+ -- Supabase installation left untouched above — keeps it.
144
+ IF dropped_any THEN
145
+ BEGIN
146
+ EXECUTE 'DROP SCHEMA auth RESTRICT';
147
+ EXCEPTION WHEN OTHERS THEN
148
+ NULL;
149
+ END;
150
+ END IF;
151
+ END
152
+ $rebase_drop_legacy$;
153
+ `;
154
+ /**
155
+ * Policies whose body still calls `auth.uid()` / `auth.roles()` / `auth.jwt()`.
156
+ *
157
+ * Postgres will not drop a function a policy depends on, so this is exactly the
158
+ * set standing between a database and losing the legacy schema. Rebase's own
159
+ * policies leave the list on the next push or boot, when they are recompiled —
160
+ * anything still here afterwards is hand-written, will never be recompiled by
161
+ * anybody, and is the reason the drop keeps being skipped. Silence there would
162
+ * leave an operator staring at a schema the release notes said would go.
163
+ */
164
+ var LEGACY_RLS_DEPENDENTS_SQL = `
165
+ SELECT n.nspname AS schema, c.relname AS "table", p.polname AS policy
166
+ FROM pg_policy p
167
+ JOIN pg_class c ON c.oid = p.polrelid
168
+ JOIN pg_namespace n ON n.oid = c.relnamespace
169
+ WHERE pg_get_expr(p.polqual, p.polrelid) ~* '\\m${LEGACY_RLS_SCHEMA}\\.(uid|jwt|roles)\\s*\\('
170
+ OR pg_get_expr(p.polwithcheck, p.polrelid) ~* '\\m${LEGACY_RLS_SCHEMA}\\.(uid|jwt|roles)\\s*\\('
171
+ ORDER BY 1, 2, 3
172
+ `;
173
+ /**
174
+ * Functions whose body calls `auth.uid()` / `auth.roles()` / `auth.jwt()`.
175
+ *
176
+ * This is the half `DROP FUNCTION ... RESTRICT` cannot see, and the reason it
177
+ * needs its own query. Postgres records a dependency for a *policy* that calls a
178
+ * function, which is why the drop is safe against the policies above — but a
179
+ * `LANGUAGE sql` function whose body is a **string literal** is not parsed when
180
+ * it is created, so nothing is recorded and `RESTRICT` has nothing to refuse on.
181
+ * The drop succeeds and the caller is left pointing at a function that no longer
182
+ * exists, which fails at *query* time rather than at boot.
183
+ *
184
+ * A downstream project building on these helpers is not hypothetical: the Rebase
185
+ * control plane defines `auth.is_org_member(uuid)` and `auth.is_org_admin(uuid)`
186
+ * in this very schema, each calling `auth.uid()` in a string body, and eleven of
187
+ * its row-level-security policies go through them. Every one of those would have
188
+ * started failing the first time a recompile left no policy referencing
189
+ * `auth.uid()` directly — the drop's own precondition.
190
+ *
191
+ * Matching on the body text is the only option available, and it is deliberately
192
+ * broad: a false positive costs a schema that stays one release longer and says
193
+ * why, while a false negative costs somebody their policies.
194
+ */
195
+ var LEGACY_RLS_FUNCTION_DEPENDENTS_SQL = `
196
+ SELECT n.nspname AS schema, p.proname AS function
197
+ FROM pg_proc p
198
+ JOIN pg_namespace n ON n.oid = p.pronamespace
199
+ WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
200
+ AND NOT (n.nspname = '${LEGACY_RLS_SCHEMA}' AND p.proname IN ('uid', 'jwt', 'roles'))
201
+ AND p.prosrc ~* '\\m${LEGACY_RLS_SCHEMA}\\.(uid|jwt|roles)\\s*\\('
202
+ ORDER BY 1, 2
203
+ `;
204
+ /**
205
+ * Retire the pre-1.0 `auth` schema, reporting what is holding it back.
206
+ *
207
+ * The shared implementation behind the CLI's post-push step and the runtime's
208
+ * post-policy step. Both used to just fire {@link DROP_LEGACY_AUTH_SCHEMA_SQL}
209
+ * and swallow whatever came back, which is right for the ordinary case — a
210
+ * table not recompiled *yet* — and wrong for the one that never resolves: a
211
+ * hand-written policy nothing will ever rewrite. Then the schema stays forever
212
+ * and nothing ever says why.
213
+ */
214
+ async function dropLegacyAuthSchema(run, report) {
215
+ let blockers;
216
+ try {
217
+ blockers = await run(LEGACY_RLS_DEPENDENTS_SQL);
218
+ } catch {
219
+ return;
220
+ }
221
+ if (blockers.length > 0) {
222
+ report.warn(`The pre-1.0 \`${LEGACY_RLS_SCHEMA}\` schema cannot be removed yet: ${blockers.length} ${blockers.length === 1 ? "policy still calls" : "policies still call"} \`${LEGACY_RLS_SCHEMA}.uid()\` and friends, and Postgres will not drop a function a policy depends on. Rebase's own policies are rewritten automatically — anything listed here is hand-written SQL that has to be updated to \`${REBASE_SCHEMA}.uid()\` by hand, after which the schema goes on its own:\n` + blockers.map((b) => ` • ${b.schema}.${b.table} → "${b.policy}"`).join("\n"));
223
+ return;
224
+ }
225
+ let borrowers;
226
+ try {
227
+ borrowers = await run(LEGACY_RLS_FUNCTION_DEPENDENTS_SQL);
228
+ } catch {
229
+ return;
230
+ }
231
+ if (borrowers.length > 0) {
232
+ report.warn(`The pre-1.0 \`${LEGACY_RLS_SCHEMA}\` schema cannot be removed yet: ${borrowers.length} ${borrowers.length === 1 ? "function calls" : "functions call"} \`${LEGACY_RLS_SCHEMA}.uid()\` and friends from its own body. Postgres records no dependency for that — a SQL body written as a string literal is never parsed — so dropping the helpers would leave these pointing at nothing, and they would fail when a query reached them rather than now. Repoint them at \`${REBASE_SCHEMA}.uid()\`, after which the schema goes on its own:\n` + borrowers.map((b) => ` • ${b.schema}.${b.function}()`).join("\n"));
233
+ return;
234
+ }
235
+ try {
236
+ await run(DROP_LEGACY_AUTH_SCHEMA_SQL);
237
+ } catch (err) {
238
+ report.info(`Left the \`${LEGACY_RLS_SCHEMA}\` schema in place: ` + (err instanceof Error ? err.message : String(err)));
239
+ }
240
+ }
241
+ //#endregion
242
+ export { rls_bootstrap_sql_exports as n, RLS_BOOTSTRAP_STATEMENTS as t };
243
+
244
+ //# sourceMappingURL=rls-bootstrap-sql-69hYT8nr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rls-bootstrap-sql-69hYT8nr.js","names":[],"sources":["../src/schema/rls-bootstrap-sql.ts"],"sourcesContent":["import { LEGACY_RLS_SCHEMA, REBASE_SCHEMA } from \"@rebasepro/types\";\n\n/**\n * Canonical SQL bootstrap for the RLS helper functions.\n *\n * Generated RLS policies reference `rebase.uid()` / `rebase.roles()` /\n * `rebase.jwt()`, so any SQL stream that can contain policies must be\n * self-contained: it has to (re)create these helpers first. This matters for\n * the migration directory in particular — Atlas replays migrations against a\n * clean dev database where no out-of-band bootstrap has ever run, so a\n * migration carrying policies without this preamble fails with\n * \"function rebase.uid() does not exist\".\n *\n * Idempotent (`IF NOT EXISTS` / `OR REPLACE`) so it can be prepended to every\n * policies block and re-applied freely. The runtime boot path\n * (`auth/ensure-tables.ts`) creates the same functions under an advisory lock\n * for HMR-safety; keep the definitions in sync.\n *\n * ## Creating the `rebase` schema here is now safe, and required\n *\n * It deliberately did not, once. These functions lived in a schema called\n * `auth`, and the note here read: creating `rebase` would leak it into Atlas's\n * replayed migration state, and — absent from the desired `schema.sql` — Atlas\n * would then plan `DROP SCHEMA \"rebase\" CASCADE`, taking the auth tables with\n * it. That reasoning still holds; what changed is the second half of it. The\n * DDL generator now emits `CREATE SCHEMA IF NOT EXISTS \"rebase\"`\n * unconditionally, so the schema is always in the desired state and the diff is\n * empty. (It used to appear only when some collection happened to declare\n * `schema: \"rebase\"` — true for the scaffold's users collection, and not a\n * property anything guaranteed.) `db push` additionally excludes the whole\n * schema from the declarative apply.\n *\n * See `@rebasepro/types`' `rls-functions` for why the functions moved out of\n * `auth` at all: the short version is that the name was Supabase's, and\n * `CREATE OR REPLACE FUNCTION auth.uid() RETURNS text` cannot be applied over\n * Supabase's `RETURNS uuid` — Postgres refuses, and the refusal used to be\n * swallowed.\n */\n/**\n * The bootstrap as individual statements.\n *\n * Kept as an array because the two consumers need different shapes and only one\n * of them can take a multi-command string: the migration preamble is written to\n * a file and replayed by Atlas, but the boot path runs through drizzle, whose\n * node-postgres handle speaks the extended query protocol and rejects more than\n * one command per call. Splitting a joined string back apart on `$$;` would be\n * a parser for a problem that does not need one.\n */\nexport const RLS_BOOTSTRAP_STATEMENTS: readonly string[] = [\n `CREATE SCHEMA IF NOT EXISTS ${REBASE_SCHEMA}`,\n\n // Falls back to the pre-rename `app.user_id` so a database that has taken\n // the new schema but is still served by an older backend keeps resolving\n // the principal. Drop the COALESCE once no such deployment remains.\n `CREATE OR REPLACE FUNCTION ${REBASE_SCHEMA}.uid() RETURNS text AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.uid', true), ''),\n NULLIF(current_setting('app.user_id', true), '')\n );\n$$ LANGUAGE sql STABLE`,\n\n `CREATE OR REPLACE FUNCTION ${REBASE_SCHEMA}.jwt() RETURNS jsonb AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.jwt', true), ''),\n '{}'\n )::jsonb;\n$$ LANGUAGE sql STABLE`,\n\n `CREATE OR REPLACE FUNCTION ${REBASE_SCHEMA}.roles() RETURNS text AS $$\n SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');\n$$ LANGUAGE sql STABLE`\n];\n\n/** The same statements as one script, for migration files and raw clients. */\nexport const RLS_BOOTSTRAP_SQL =\n \"-- Rebase schema + RLS helper functions (required by the policies below)\\n\" +\n RLS_BOOTSTRAP_STATEMENTS.map(s => `${s};`).join(\"\\n\\n\") + \"\\n\";\n\n/**\n * Removes the pre-1.0 `auth` schema, but only when Rebase is what put it there.\n *\n * ## Why this is safe against a Supabase database\n *\n * Two independent guards, and both have to pass:\n *\n * 1. **Each function is identified before it is dropped.** Ours returns `text`\n * and reads the `app.uid` GUC; Supabase's returns `uuid` and reads\n * `request.jwt.claims`. Nothing is dropped on a signature we did not write,\n * so a Supabase database — where our `CREATE OR REPLACE` could never have\n * succeeded in the first place, Postgres refusing to change a return type —\n * matches nothing and this is a no-op.\n * 2. **`DROP SCHEMA … RESTRICT`**, never CASCADE. If anything else at all still\n * lives in `auth` (Supabase's `users` table, its other helpers), the drop\n * fails and the schema stays. CASCADE here would be unrecoverable.\n *\n * ## Why it cannot run too early\n *\n * Postgres records a dependency from every RLS policy to the functions its body\n * calls, so `DROP FUNCTION auth.uid()` fails for as long as a single policy\n * still references it. That is the interlock, and it is load-bearing: the drop\n * can only succeed once every policy has been recompiled to \\`rebase.uid()\\`.\n * Callers therefore run this *after* applying policies, and treat a failure as\n * \"not yet — try again next boot\" rather than as an error.\n */\nexport const DROP_LEGACY_AUTH_SCHEMA_SQL = `\nDO $rebase_drop_legacy$\nDECLARE\n dropped_any boolean := false;\nBEGIN\n -- Each function is matched on its own result type and body, so a schema\n -- that merely shares the name keeps everything it has.\n IF EXISTS (\n SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'uid'\n AND pg_get_function_result(p.oid) = 'text'\n AND p.prosrc LIKE '%app.uid%'\n ) THEN\n DROP FUNCTION auth.uid();\n dropped_any := true;\n END IF;\n\n IF EXISTS (\n SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'jwt'\n AND pg_get_function_result(p.oid) = 'jsonb'\n AND p.prosrc LIKE '%app.jwt%'\n ) THEN\n DROP FUNCTION auth.jwt();\n dropped_any := true;\n END IF;\n\n IF EXISTS (\n SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'roles'\n AND pg_get_function_result(p.oid) = 'text'\n AND p.prosrc LIKE '%app.user_roles%'\n ) THEN\n DROP FUNCTION auth.roles();\n dropped_any := true;\n END IF;\n\n -- RESTRICT: only an empty schema goes. Anything else in there — including a\n -- Supabase installation left untouched above — keeps it.\n IF dropped_any THEN\n BEGIN\n EXECUTE 'DROP SCHEMA auth RESTRICT';\n EXCEPTION WHEN OTHERS THEN\n NULL;\n END;\n END IF;\nEND\n$rebase_drop_legacy$;\n`;\n\n/** Somebody's policy that still calls a pre-1.0 helper. */\nexport interface LegacyRlsDependent {\n schema: string;\n table: string;\n policy: string;\n}\n\n/**\n * Policies whose body still calls `auth.uid()` / `auth.roles()` / `auth.jwt()`.\n *\n * Postgres will not drop a function a policy depends on, so this is exactly the\n * set standing between a database and losing the legacy schema. Rebase's own\n * policies leave the list on the next push or boot, when they are recompiled —\n * anything still here afterwards is hand-written, will never be recompiled by\n * anybody, and is the reason the drop keeps being skipped. Silence there would\n * leave an operator staring at a schema the release notes said would go.\n */\nexport const LEGACY_RLS_DEPENDENTS_SQL = `\n SELECT n.nspname AS schema, c.relname AS \"table\", p.polname AS policy\n FROM pg_policy p\n JOIN pg_class c ON c.oid = p.polrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n WHERE pg_get_expr(p.polqual, p.polrelid) ~* '\\\\m${LEGACY_RLS_SCHEMA}\\\\.(uid|jwt|roles)\\\\s*\\\\('\n OR pg_get_expr(p.polwithcheck, p.polrelid) ~* '\\\\m${LEGACY_RLS_SCHEMA}\\\\.(uid|jwt|roles)\\\\s*\\\\('\n ORDER BY 1, 2, 3\n`;\n\n/** Somebody's *function* that still calls a pre-1.0 helper from its own body. */\nexport interface LegacyRlsFunctionDependent {\n schema: string;\n function: string;\n}\n\n/**\n * Functions whose body calls `auth.uid()` / `auth.roles()` / `auth.jwt()`.\n *\n * This is the half `DROP FUNCTION ... RESTRICT` cannot see, and the reason it\n * needs its own query. Postgres records a dependency for a *policy* that calls a\n * function, which is why the drop is safe against the policies above — but a\n * `LANGUAGE sql` function whose body is a **string literal** is not parsed when\n * it is created, so nothing is recorded and `RESTRICT` has nothing to refuse on.\n * The drop succeeds and the caller is left pointing at a function that no longer\n * exists, which fails at *query* time rather than at boot.\n *\n * A downstream project building on these helpers is not hypothetical: the Rebase\n * control plane defines `auth.is_org_member(uuid)` and `auth.is_org_admin(uuid)`\n * in this very schema, each calling `auth.uid()` in a string body, and eleven of\n * its row-level-security policies go through them. Every one of those would have\n * started failing the first time a recompile left no policy referencing\n * `auth.uid()` directly — the drop's own precondition.\n *\n * Matching on the body text is the only option available, and it is deliberately\n * broad: a false positive costs a schema that stays one release longer and says\n * why, while a false negative costs somebody their policies.\n */\nexport const LEGACY_RLS_FUNCTION_DEPENDENTS_SQL = `\n SELECT n.nspname AS schema, p.proname AS function\n FROM pg_proc p\n JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')\n AND NOT (n.nspname = '${LEGACY_RLS_SCHEMA}' AND p.proname IN ('uid', 'jwt', 'roles'))\n AND p.prosrc ~* '\\\\m${LEGACY_RLS_SCHEMA}\\\\.(uid|jwt|roles)\\\\s*\\\\('\n ORDER BY 1, 2\n`;\n\n/**\n * Retire the pre-1.0 `auth` schema, reporting what is holding it back.\n *\n * The shared implementation behind the CLI's post-push step and the runtime's\n * post-policy step. Both used to just fire {@link DROP_LEGACY_AUTH_SCHEMA_SQL}\n * and swallow whatever came back, which is right for the ordinary case — a\n * table not recompiled *yet* — and wrong for the one that never resolves: a\n * hand-written policy nothing will ever rewrite. Then the schema stays forever\n * and nothing ever says why.\n */\nexport async function dropLegacyAuthSchema(\n run: (sql: string) => Promise<Record<string, unknown>[]>,\n report: { info: (m: string) => void; warn: (m: string) => void }\n): Promise<void> {\n let blockers: LegacyRlsDependent[];\n try {\n blockers = (await run(LEGACY_RLS_DEPENDENTS_SQL)) as unknown as LegacyRlsDependent[];\n } catch {\n return; // No catalogue access; nothing here is worth failing a boot for.\n }\n\n if (blockers.length > 0) {\n report.warn(\n `The pre-1.0 \\`${LEGACY_RLS_SCHEMA}\\` schema cannot be removed yet: ${blockers.length} ` +\n `${blockers.length === 1 ? \"policy still calls\" : \"policies still call\"} ` +\n `\\`${LEGACY_RLS_SCHEMA}.uid()\\` and friends, and Postgres will not drop a function a policy ` +\n `depends on. Rebase's own policies are ` +\n `rewritten automatically — anything listed here is hand-written SQL that has to be updated to ` +\n `\\`${REBASE_SCHEMA}.uid()\\` by hand, after which the schema goes on its own:\\n` +\n blockers.map(b => ` • ${b.schema}.${b.table} → \"${b.policy}\"`).join(\"\\n\")\n );\n return;\n }\n\n // The half Postgres will not refuse on. See LEGACY_RLS_FUNCTION_DEPENDENTS_SQL:\n // a string-literal SQL body records no dependency, so `DROP FUNCTION` drops\n // out from under a caller that is still using it and nothing complains until\n // a query runs.\n let borrowers: LegacyRlsFunctionDependent[];\n try {\n borrowers = (await run(LEGACY_RLS_FUNCTION_DEPENDENTS_SQL)) as unknown as LegacyRlsFunctionDependent[];\n } catch {\n return;\n }\n\n if (borrowers.length > 0) {\n report.warn(\n `The pre-1.0 \\`${LEGACY_RLS_SCHEMA}\\` schema cannot be removed yet: ${borrowers.length} ` +\n `${borrowers.length === 1 ? \"function calls\" : \"functions call\"} ` +\n `\\`${LEGACY_RLS_SCHEMA}.uid()\\` and friends from its own body. Postgres records no dependency ` +\n `for that — a SQL body written as a string literal is never parsed — so dropping the helpers ` +\n `would leave these pointing at nothing, and they would fail when a query reached them rather ` +\n `than now. Repoint them at \\`${REBASE_SCHEMA}.uid()\\`, after which the schema goes on its own:\\n` +\n borrowers.map(b => ` • ${b.schema}.${b.function}()`).join(\"\\n\")\n );\n return;\n }\n\n try {\n await run(DROP_LEGACY_AUTH_SCHEMA_SQL);\n } catch (err) {\n // Something else depends on it, or the connection does not own it.\n // Not fatal: the schema is inert either way now that no policy calls it.\n report.info(\n `Left the \\`${LEGACY_RLS_SCHEMA}\\` schema in place: ` +\n (err instanceof Error ? err.message : String(err))\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,IAAa,2BAA8C;CACvD,+BAA+B;CAK/B,8BAA8B,cAAc;;;;;;CAO5C,8BAA8B,cAAc;;;;;;CAO5C,8BAA8B,cAAc;;;AAGhD;AAII,KACA,yBAAyB,KAAI,MAAK,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4B1D,IAAa,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmE3C,IAAa,4BAA4B;;;;;sDAKa,kBAAkB;2DACb,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;AAgC7E,IAAa,qCAAqC;;;;;8BAKpB,kBAAkB;4BACpB,kBAAkB;;;;;;;;;;;;;AAc9C,eAAsB,qBAClB,KACA,QACa;CACb,IAAI;CACJ,IAAI;EACA,WAAY,MAAM,IAAI,yBAAyB;CACnD,QAAQ;EACJ;CACJ;CAEA,IAAI,SAAS,SAAS,GAAG;EACrB,OAAO,KACH,iBAAiB,kBAAkB,mCAAmC,SAAS,OAAO,GACnF,SAAS,WAAW,IAAI,uBAAuB,sBAAsB,KACnE,kBAAkB,4MAGlB,cAAc,+DACnB,SAAS,KAAI,MAAK,OAAO,EAAE,OAAO,GAAG,EAAE,MAAM,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,KAAK,IAAI,CAC7E;EACA;CACJ;CAMA,IAAI;CACJ,IAAI;EACA,YAAa,MAAM,IAAI,kCAAkC;CAC7D,QAAQ;EACJ;CACJ;CAEA,IAAI,UAAU,SAAS,GAAG;EACtB,OAAO,KACH,iBAAiB,kBAAkB,mCAAmC,UAAU,OAAO,GACpF,UAAU,WAAW,IAAI,mBAAmB,iBAAiB,KAC3D,kBAAkB,6RAGQ,cAAc,uDAC7C,UAAU,KAAI,MAAK,OAAO,EAAE,OAAO,GAAG,EAAE,SAAS,GAAG,CAAC,CAAC,KAAK,IAAI,CACnE;EACA;CACJ;CAEA,IAAI;EACA,MAAM,IAAI,2BAA2B;CACzC,SAAS,KAAK;EAGV,OAAO,KACH,cAAc,kBAAkB,yBAC/B,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,EACpD;CACJ;AACJ"}