@pgpm/supabase 0.5.0 → 1.0.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 (69) hide show
  1. package/LICENSE +2 -1
  2. package/Makefile +2 -3
  3. package/README.md +45 -32
  4. package/deploy/roles/anon.sql +15 -0
  5. package/deploy/schemas/supabase_integration/procedures/pre_request.sql +26 -0
  6. package/deploy/schemas/supabase_integration/schema.sql +9 -0
  7. package/package.json +27 -12
  8. package/pgpm-supabase.control +7 -0
  9. package/pgpm.plan +6 -3
  10. package/revert/roles/anon.sql +7 -0
  11. package/revert/schemas/supabase_integration/procedures/pre_request.sql +7 -0
  12. package/revert/schemas/supabase_integration/schema.sql +7 -0
  13. package/sql/pgpm-supabase--1.0.0.bundle.tar.gz +0 -0
  14. package/sql/pgpm-supabase--1.0.0.sql +32 -0
  15. package/verify/roles/anon.sql +12 -0
  16. package/verify/schemas/supabase_integration/procedures/pre_request.sql +19 -0
  17. package/verify/schemas/supabase_integration/schema.sql +18 -0
  18. package/__tests__/_realtime/_realtime-extensions.test.ts +0 -129
  19. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +0 -160
  20. package/__tests__/_realtime/_realtime-tenants.test.ts +0 -163
  21. package/__tests__/auth/auth.audit-log-entries.test.ts +0 -148
  22. package/__tests__/auth/auth.flow-state.test.ts +0 -114
  23. package/__tests__/auth/auth.identities.test.ts +0 -149
  24. package/__tests__/auth/auth.instances.test.ts +0 -134
  25. package/__tests__/auth/auth.mfa-amr-claims.test.ts +0 -142
  26. package/__tests__/auth/auth.mfa-challenges.test.ts +0 -117
  27. package/__tests__/auth/auth.mfa-factors.test.ts +0 -118
  28. package/__tests__/auth/auth.oauth-authorizations.test.ts +0 -123
  29. package/__tests__/auth/auth.oauth-clients.test.ts +0 -120
  30. package/__tests__/auth/auth.oauth-consents.test.ts +0 -122
  31. package/__tests__/auth/auth.one-time-tokens.test.ts +0 -194
  32. package/__tests__/auth/auth.refresh-tokens.test.ts +0 -134
  33. package/__tests__/auth/auth.saml-providers.test.ts +0 -154
  34. package/__tests__/auth/auth.saml-relay-states.test.ts +0 -145
  35. package/__tests__/auth/auth.schema-migrations.test.ts +0 -131
  36. package/__tests__/auth/auth.sessions.test.ts +0 -140
  37. package/__tests__/auth/auth.sso-domains.test.ts +0 -137
  38. package/__tests__/auth/auth.sso-providers.test.ts +0 -136
  39. package/__tests__/auth/auth.users.test.ts +0 -314
  40. package/__tests__/conditional.test.ts +0 -229
  41. package/__tests__/functions.test.ts +0 -252
  42. package/__tests__/jsonb.test.ts +0 -237
  43. package/__tests__/net/net.http-request-queue.test.ts +0 -159
  44. package/__tests__/net/net.http-response.test.ts +0 -163
  45. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +0 -168
  46. package/__tests__/realtime/realtime.messages.test.ts +0 -162
  47. package/__tests__/realtime/realtime.schema-migrations.test.ts +0 -139
  48. package/__tests__/realtime/realtime.subscription.test.ts +0 -159
  49. package/__tests__/storage/storage.buckets-analytics.test.ts +0 -134
  50. package/__tests__/storage/storage.buckets.test.ts +0 -146
  51. package/__tests__/storage/storage.iceberg-namespaces.test.ts +0 -132
  52. package/__tests__/storage/storage.iceberg-tables.test.ts +0 -142
  53. package/__tests__/storage/storage.migrations.test.ts +0 -133
  54. package/__tests__/storage/storage.objects.test.ts +0 -160
  55. package/__tests__/storage/storage.prefixes.test.ts +0 -138
  56. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +0 -133
  57. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +0 -128
  58. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +0 -125
  59. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +0 -213
  60. package/__tests__/temporal.test.ts +0 -225
  61. package/__tests__/vault-secrets.test.ts +0 -145
  62. package/deploy/LICENSE +0 -9
  63. package/deploy/supabase.sql +0 -2271
  64. package/jest.config.js +0 -15
  65. package/revert/supabase.sql +0 -3
  66. package/sql/supabase--0.0.1.sql +0 -1368
  67. package/src/index.ts +0 -3
  68. package/supabase.control +0 -7
  69. package/verify/supabase.sql +0 -3
package/LICENSE CHANGED
@@ -1,6 +1,7 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2025 Interweb, Inc. <developers@interweb.co>
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
7
  of this software and associated documentation files (the "Software"), to deal
package/Makefile CHANGED
@@ -1,7 +1,6 @@
1
- EXTENSION = supabase
2
- DATA = sql/supabase--0.0.1.sql
1
+ EXTENSION = pgpm-supabase
2
+ DATA = sql/pgpm-supabase--1.0.0.sql
3
3
 
4
4
  PG_CONFIG = pg_config
5
5
  PGXS := $(shell $(PG_CONFIG) --pgxs)
6
6
  include $(PGXS)
7
-
package/README.md CHANGED
@@ -1,37 +1,50 @@
1
1
  # @pgpm/supabase
2
2
 
3
- <p align="center" width="100%">
4
- <img height="250" src="https://raw.githubusercontent.com/launchql/supabase-test/refs/heads/main/docs/img/logos.svg" />
5
- </p>
6
-
7
- <p align="center" width="100%">
8
- <a href="https://github.com/launchql/supabase-test/actions/workflows/ci.yml">
9
- <img height="20" src="https://github.com/launchql/supabase-test/actions/workflows/ci.yml/badge.svg" />
10
- </a>
11
- <a href="https://github.com/launchql/supabase-test/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
12
- </p>
13
-
14
- Supabase-focused SQL, tests, and helpers for building robust applications with Row-Level Security.
15
-
16
- ## Features
17
-
18
- - 🔐 Supabase auth integration helpers
19
- - 🧪 Test utilities for RLS policies
20
- - 📦 Reusable SQL patterns and functions
21
- - ⚡ Optimized for Supabase local development
22
-
23
- ## Quick Start
24
-
25
- ```bash
26
- # Run tests
3
+ Bridge Supabase PostgREST requests to Constructive's transaction-local RLS claims.
4
+
5
+ ## Supabase setup
6
+
7
+ 1. Run `pgpm admin-users bootstrap --yes` against the Supabase database to create
8
+ Constructive's `anonymous` role (and the other base roles).
9
+ 2. Ensure Supabase's `anon` and `authenticated` roles already exist. Outside
10
+ Supabase, create `anon` with `CREATE ROLE anon NOINHERIT NOLOGIN;` before deployment.
11
+ 3. Install `@pgpm/supabase` and deploy `pgpm-supabase` with `pgpm deploy`.
12
+ 4. As the database administrator, configure PostgREST:
13
+
14
+ ```sql
15
+ ALTER ROLE authenticator SET pgrst.db_pre_request = 'supabase_integration.pre_request';
16
+ NOTIFY pgrst, 'reload config';
17
+ ```
18
+
19
+ Schema USAGE and hook EXECUTE are available to PUBLIC so PostgREST can also
20
+ invoke the hook as `service_role` without requiring that role at deploy time.
21
+ The hook is a no-op for roles other than `anon` and `authenticated`.
22
+
23
+ The security-invoker hook switches `anon` to `anonymous`. It does not grant
24
+ automatic inheritance: `anon` cannot use `anonymous` grants without switching.
25
+ On PostgreSQL 16+ the membership explicitly disables inheritance and allows SET;
26
+ on PostgreSQL 15 it relies on Supabase's `anon` being NOINHERIT.
27
+
28
+ For `authenticated`, the hook copies the verified `request.jwt.claims` JSON `sub`
29
+ to both `jwt.claims.user_id` and `jwt.claims.principal_id`, transaction-locally.
30
+ The subject must correspond to Constructive's `users.id`. Missing/empty claims
31
+ or subjects leave the claims unset; malformed JSON raises an error. Other roles
32
+ are unchanged. Only use this hook behind PostgREST's verified JWT boundary;
33
+ it does not verify JWTs or provision users. Each request must use a fresh transaction.
34
+
35
+ RLS policies must treat empty claim strings as absent, for example
36
+ `nullif(current_setting('jwt.claims.user_id', true), '')::uuid`. PostgreSQL can
37
+ return an empty string for a custom GUC after its transaction-local value is
38
+ rolled back; a direct UUID cast would then raise on a later unauthenticated request.
39
+
40
+ ## Development
41
+
42
+ ```sh
43
+ eval "$(pgpm env)"
27
44
  pnpm test
28
-
29
- # Watch mode for development
30
- pnpm test:watch
45
+ pgpm package
31
46
  ```
32
47
 
33
- ## Disclaimer
34
-
35
- AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
36
-
37
- No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
48
+ The tests create a missing `anon` role before deploying the module. Reverting
49
+ revokes the membership and drops the hook and schema, but does not drop roles
50
+ or change PostgREST configuration. Remove the pre-request setting before reverting.
@@ -0,0 +1,15 @@
1
+ -- Deploy roles/anon to pg
2
+
3
+ BEGIN;
4
+
5
+ DO $$
6
+ BEGIN
7
+ IF current_setting('server_version_num')::integer >= 160000 THEN
8
+ GRANT anonymous TO anon WITH INHERIT FALSE, SET TRUE;
9
+ ELSE
10
+ GRANT anonymous TO anon;
11
+ END IF;
12
+ END;
13
+ $$;
14
+
15
+ COMMIT;
@@ -0,0 +1,26 @@
1
+ -- Deploy schemas/supabase_integration/procedures/pre_request to pg
2
+
3
+ BEGIN;
4
+
5
+ CREATE FUNCTION supabase_integration.pre_request()
6
+ RETURNS void
7
+ LANGUAGE plpgsql
8
+ AS $$
9
+ DECLARE
10
+ subject text;
11
+ BEGIN
12
+ IF current_user = 'anon' THEN
13
+ PERFORM set_config('role', 'anonymous', true);
14
+ ELSIF current_user = 'authenticated' THEN
15
+ subject := nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'sub';
16
+ IF subject IS NOT NULL AND subject <> '' THEN
17
+ PERFORM set_config('jwt.claims.user_id', subject, true);
18
+ PERFORM set_config('jwt.claims.principal_id', subject, true);
19
+ END IF;
20
+ END IF;
21
+ END;
22
+ $$;
23
+
24
+ GRANT EXECUTE ON FUNCTION supabase_integration.pre_request() TO PUBLIC;
25
+
26
+ COMMIT;
@@ -0,0 +1,9 @@
1
+ -- Deploy schemas/supabase_integration/schema to pg
2
+
3
+ BEGIN;
4
+
5
+ CREATE SCHEMA supabase_integration;
6
+
7
+ GRANT USAGE ON SCHEMA supabase_integration TO PUBLIC;
8
+
9
+ COMMIT;
package/package.json CHANGED
@@ -1,23 +1,38 @@
1
1
  {
2
2
  "name": "@pgpm/supabase",
3
- "version": "0.5.0",
4
- "author": "Interweb <developers@interweb.co>",
5
- "license": "MIT",
3
+ "version": "1.0.0",
4
+ "description": "Bridge Supabase PostgREST requests to Constructive RLS claims",
5
+ "author": "Dan Lynch <pyramation@gmail.com>",
6
+ "contributors": [
7
+ "Constructive <developers@constructive.io>"
8
+ ],
9
+ "keywords": [
10
+ "postgresql",
11
+ "pgpm",
12
+ "supabase",
13
+ "authentication"
14
+ ],
6
15
  "publishConfig": {
7
16
  "access": "public"
8
17
  },
9
- "scripts": {
10
- "lint": "eslint . --fix",
11
- "test": "jest",
12
- "test:watch": "jest --watch"
18
+ "devDependencies": {
19
+ "pgpm": "^5",
20
+ "pgsql-test": "^5.20.13"
21
+ },
22
+ "dependencies": {
23
+ "@pgpm/verify": "0.47.7"
13
24
  },
14
25
  "repository": {
15
26
  "type": "git",
16
- "url": "https://github.com/launchql/supabase-test-suite"
27
+ "url": "https://github.com/constructive-io/pgpm-modules"
17
28
  },
18
- "homepage": "https://github.com/launchql/supabase-test-suite",
29
+ "homepage": "https://github.com/constructive-io/pgpm-modules",
19
30
  "bugs": {
20
- "url": "https://github.com/launchql/supabase-test-suite/issues"
31
+ "url": "https://github.com/constructive-io/pgpm-modules/issues"
21
32
  },
22
- "gitHead": "93ad24f779c0e569d9f42475bb0ddba17ad153c1"
23
- }
33
+ "scripts": {
34
+ "bundle": "pgpm package",
35
+ "test": "jest",
36
+ "test:watch": "jest --watch"
37
+ }
38
+ }
@@ -0,0 +1,7 @@
1
+ # pgpm-supabase extension
2
+ comment = 'Supabase PostgREST integration for Constructive'
3
+ default_version = '1.0.0'
4
+ module_pathname = '$libdir/pgpm-supabase'
5
+ requires = 'plpgsql,pgpm-verify'
6
+ relocatable = false
7
+ superuser = false
package/pgpm.plan CHANGED
@@ -1,4 +1,7 @@
1
1
  %syntax-version=1.0.0
2
- %project=supabase
3
- %uri=supabase
4
- supabase 2025-11-01T00:36:02Z launchql <launchql@5b0c196eeb62> # add supabase
2
+ %project=pgpm-supabase
3
+ %uri=pgpm-supabase
4
+
5
+ schemas/supabase_integration/schema [pgpm-verify:procedures/assert_schema pgpm-verify:procedures/assert_function] 2026-08-27T00:00:00Z Constructive <developers@constructive.io> # add schemas/supabase_integration/schema
6
+ roles/anon [schemas/supabase_integration/schema] 2026-08-27T00:00:00Z Constructive <developers@constructive.io> # add roles/anon
7
+ schemas/supabase_integration/procedures/pre_request [schemas/supabase_integration/schema roles/anon] 2026-08-27T00:00:00Z Constructive <developers@constructive.io> # add schemas/supabase_integration/procedures/pre_request
@@ -0,0 +1,7 @@
1
+ -- Revert roles/anon from pg
2
+
3
+ BEGIN;
4
+
5
+ REVOKE anonymous FROM anon;
6
+
7
+ COMMIT;
@@ -0,0 +1,7 @@
1
+ -- Revert schemas/supabase_integration/procedures/pre_request from pg
2
+
3
+ BEGIN;
4
+
5
+ DROP FUNCTION supabase_integration.pre_request();
6
+
7
+ COMMIT;
@@ -0,0 +1,7 @@
1
+ -- Revert schemas/supabase_integration/schema from pg
2
+
3
+ BEGIN;
4
+
5
+ DROP SCHEMA supabase_integration;
6
+
7
+ COMMIT;
@@ -0,0 +1,32 @@
1
+ \echo Use "CREATE EXTENSION pgpm-supabase" to load this file. \quit
2
+ CREATE SCHEMA supabase_integration;
3
+
4
+ GRANT USAGE ON SCHEMA supabase_integration TO PUBLIC;
5
+
6
+ DO $EOFCODE$
7
+ BEGIN
8
+ IF current_setting('server_version_num')::integer >= 160000 THEN
9
+ GRANT anonymous TO anon WITH INHERIT FALSE, SET TRUE;
10
+ ELSE
11
+ GRANT anonymous TO anon;
12
+ END IF;
13
+ END;
14
+ $EOFCODE$;
15
+
16
+ CREATE FUNCTION supabase_integration.pre_request() RETURNS void LANGUAGE plpgsql AS $EOFCODE$
17
+ DECLARE
18
+ subject text;
19
+ BEGIN
20
+ IF current_user = 'anon' THEN
21
+ PERFORM set_config('role', 'anonymous', true);
22
+ ELSIF current_user = 'authenticated' THEN
23
+ subject := nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'sub';
24
+ IF subject IS NOT NULL AND subject <> '' THEN
25
+ PERFORM set_config('jwt.claims.user_id', subject, true);
26
+ PERFORM set_config('jwt.claims.principal_id', subject, true);
27
+ END IF;
28
+ END IF;
29
+ END;
30
+ $EOFCODE$;
31
+
32
+ GRANT EXECUTE ON FUNCTION supabase_integration.pre_request() TO PUBLIC;
@@ -0,0 +1,12 @@
1
+ -- Verify roles/anon from pg
2
+
3
+ BEGIN;
4
+
5
+ DO $$
6
+ BEGIN
7
+ ASSERT pg_has_role('anon', 'anonymous', 'MEMBER');
8
+ ASSERT NOT pg_has_role('anon', 'anonymous', 'USAGE');
9
+ END;
10
+ $$;
11
+
12
+ ROLLBACK;
@@ -0,0 +1,19 @@
1
+ -- Verify schemas/supabase_integration/procedures/pre_request from pg
2
+
3
+ BEGIN;
4
+
5
+ SELECT assert_function('supabase_integration.pre_request()'::regprocedure);
6
+
7
+ DO $$
8
+ BEGIN
9
+ ASSERT EXISTS (
10
+ SELECT FROM pg_proc,
11
+ LATERAL aclexplode(coalesce(proacl, acldefault('f', proowner))) AS privilege
12
+ WHERE oid = 'supabase_integration.pre_request()'::regprocedure
13
+ AND privilege.grantee = 0 AND privilege.privilege_type = 'EXECUTE'
14
+ );
15
+ ASSERT NOT (SELECT prosecdef FROM pg_proc WHERE oid = 'supabase_integration.pre_request()'::regprocedure);
16
+ END;
17
+ $$;
18
+
19
+ ROLLBACK;
@@ -0,0 +1,18 @@
1
+ -- Verify schemas/supabase_integration/schema from pg
2
+
3
+ BEGIN;
4
+
5
+ SELECT assert_schema('supabase_integration'::regnamespace);
6
+
7
+ DO $$
8
+ BEGIN
9
+ ASSERT EXISTS (
10
+ SELECT FROM pg_namespace,
11
+ LATERAL aclexplode(nspacl) AS privilege
12
+ WHERE nspname = 'supabase_integration'
13
+ AND privilege.grantee = 0 AND privilege.privilege_type = 'USAGE'
14
+ );
15
+ END;
16
+ $$;
17
+
18
+ ROLLBACK;
@@ -1,129 +0,0 @@
1
- import { getConnections, PgTestClient } from 'supabase-test';
2
-
3
- let pg: PgTestClient;
4
- let db: PgTestClient;
5
- let teardown: () => Promise<void>;
6
-
7
- let tableExists = false;
8
-
9
- beforeAll(async () => {
10
- ({ pg, db, teardown } = await getConnections());
11
-
12
- // verify _realtime schema exists (optional schema)
13
- const realtimeSchemaExists = await pg.any(
14
- `SELECT EXISTS (
15
- SELECT FROM information_schema.schemata
16
- WHERE schema_name = '_realtime'
17
- ) as exists`
18
- );
19
-
20
- if (realtimeSchemaExists[0]?.exists === true) {
21
- // grant access to _realtime schema for testing
22
- await pg.any(
23
- `GRANT USAGE ON SCHEMA _realtime TO public;
24
- GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO service_role;
25
- GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO authenticated;
26
- GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO anon;
27
- ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO service_role;
28
- ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO authenticated;
29
- ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO anon;`,
30
- []
31
- );
32
-
33
- // check if _realtime.extensions table exists (using pg in beforeAll only)
34
- const exists = await pg.any(
35
- `SELECT EXISTS (
36
- SELECT FROM information_schema.tables
37
- WHERE table_schema = '_realtime' AND table_name = 'extensions'
38
- ) as exists`
39
- );
40
- tableExists = exists[0]?.exists === true;
41
- }
42
- });
43
-
44
- afterAll(async () => {
45
- await teardown();
46
- });
47
-
48
- beforeEach(async () => {
49
- await db.beforeEach();
50
- });
51
-
52
- afterEach(async () => {
53
- await db.afterEach();
54
- });
55
-
56
- describe('tutorial: _realtime extensions table access', () => {
57
-
58
- it('should verify extensions table exists in _realtime schema', async () => {
59
- db.setContext({ role: 'service_role' });
60
-
61
- // verify table exists in information schema
62
- const exists = await db.any(
63
- `SELECT EXISTS (
64
- SELECT FROM information_schema.tables
65
- WHERE table_schema = '_realtime' AND table_name = 'extensions'
66
- ) as exists`
67
- );
68
-
69
- expect(Array.isArray(exists)).toBe(true);
70
- if (exists[0]?.exists === false) {
71
- expect(exists[0].exists).toBe(false);
72
- return;
73
- }
74
- expect(exists[0].exists).toBe(true);
75
- });
76
-
77
- it('should verify service_role can query extensions table structure', async () => {
78
- if (!tableExists) {
79
- return;
80
- }
81
-
82
- db.setContext({ role: 'service_role' });
83
-
84
- // query table column structure
85
- const columns = await db.any(
86
- `SELECT column_name, data_type
87
- FROM information_schema.columns
88
- WHERE table_schema = '_realtime' AND table_name = 'extensions'
89
- ORDER BY ordinal_position`
90
- );
91
-
92
- expect(Array.isArray(columns)).toBe(true);
93
- });
94
-
95
- it('should prevent anon from accessing extensions table', async () => {
96
- if (!tableExists) {
97
- return;
98
- }
99
-
100
- // clear context to anon role
101
- db.clearContext();
102
-
103
- // anon should not be able to access _realtime.extensions (rls blocks)
104
- const result = await db.any(
105
- `SELECT * FROM _realtime.extensions LIMIT 1`
106
- );
107
-
108
- // rls should block access, result should be empty
109
- expect(result.length).toBe(0);
110
- });
111
-
112
- it('should verify table has proper grants via information_schema', async () => {
113
- if (!tableExists) {
114
- return;
115
- }
116
-
117
- db.setContext({ role: 'service_role' });
118
-
119
- // check table privileges/grants
120
- const grants = await db.any(
121
- `SELECT grantee, privilege_type
122
- FROM information_schema.table_privileges
123
- WHERE table_schema = '_realtime' AND table_name = 'extensions'`
124
- );
125
-
126
- expect(Array.isArray(grants)).toBe(true);
127
- });
128
- });
129
-
@@ -1,160 +0,0 @@
1
- import { getConnections, PgTestClient } from 'supabase-test';
2
-
3
- let pg: PgTestClient;
4
- let db: PgTestClient;
5
- let teardown: () => Promise<void>;
6
-
7
- let tableExists = false;
8
-
9
- beforeAll(async () => {
10
- ({ pg, db, teardown } = await getConnections());
11
-
12
- // verify _realtime schema exists (optional schema)
13
- const realtimeSchemaExists = await pg.any(
14
- `SELECT EXISTS (
15
- SELECT FROM information_schema.schemata
16
- WHERE schema_name = '_realtime'
17
- ) as exists`
18
- );
19
-
20
- if (realtimeSchemaExists[0]?.exists === true) {
21
- // grant access to _realtime schema for testing
22
- await pg.any(
23
- `GRANT USAGE ON SCHEMA _realtime TO public;
24
- GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO service_role;
25
- GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO authenticated;
26
- GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO anon;
27
- ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO service_role;
28
- ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO authenticated;
29
- ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO anon;`,
30
- []
31
- );
32
-
33
- // check if _realtime.schema_migrations table exists (using pg in beforeAll only)
34
- const exists = await pg.any(
35
- `SELECT EXISTS (
36
- SELECT FROM information_schema.tables
37
- WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
38
- ) as exists`
39
- );
40
- tableExists = exists[0]?.exists === true;
41
- }
42
- });
43
-
44
- afterAll(async () => {
45
- await teardown();
46
- });
47
-
48
- beforeEach(async () => {
49
- await db.beforeEach();
50
- });
51
-
52
- afterEach(async () => {
53
- await db.afterEach();
54
- });
55
-
56
- describe('tutorial: _realtime schema_migrations table access', () => {
57
-
58
- it('should verify schema_migrations table exists', async () => {
59
- db.setContext({ role: 'service_role' });
60
-
61
- // verify table exists in information schema
62
- const exists = await db.any(
63
- `SELECT EXISTS (
64
- SELECT FROM information_schema.tables
65
- WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
66
- ) as exists`
67
- );
68
-
69
- expect(Array.isArray(exists)).toBe(true);
70
- if (exists[0]?.exists === false) {
71
- expect(exists[0].exists).toBe(false);
72
- return;
73
- }
74
- expect(exists[0].exists).toBe(true);
75
- });
76
-
77
- it('should verify service_role can read migration records', async () => {
78
- if (!tableExists) {
79
- return;
80
- }
81
-
82
- db.setContext({ role: 'service_role' });
83
-
84
- // service_role should be able to query schema_migrations
85
- const migrations = await db.any(
86
- `SELECT * FROM _realtime.schema_migrations`
87
- );
88
-
89
- expect(Array.isArray(migrations)).toBe(true);
90
- });
91
-
92
- it('should verify table has version column', async () => {
93
- if (!tableExists) {
94
- return;
95
- }
96
-
97
- db.setContext({ role: 'service_role' });
98
-
99
- // check for version column in schema_migrations table
100
- const columns = await db.any(
101
- `SELECT column_name, data_type
102
- FROM information_schema.columns
103
- WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
104
- AND column_name = 'version'`
105
- );
106
-
107
- expect(Array.isArray(columns)).toBe(true);
108
- if (columns.length > 0) {
109
- expect(columns[0].column_name).toBe('version');
110
- }
111
- });
112
-
113
- it('should prevent authenticated users from accessing schema_migrations without proper permissions', async () => {
114
- if (!tableExists) {
115
- return;
116
- }
117
-
118
- // create a test user as admin using db with service_role context
119
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
120
- db.setContext({ role: 'service_role' });
121
- const user = await db.one(
122
- `INSERT INTO auth.users (id, email)
123
- VALUES (gen_random_uuid(), $1)
124
- RETURNING id`,
125
- ['realtime-migrations-test@example.com']
126
- );
127
-
128
- // set context to simulate authenticated user
129
- db.setContext({
130
- role: 'authenticated',
131
- 'request.jwt.claim.sub': user.id
132
- });
133
-
134
- // authenticated users should not be able to access schema_migrations (rls blocks)
135
- const result = await db.any(
136
- `SELECT * FROM _realtime.schema_migrations LIMIT 1`
137
- );
138
-
139
- // rls should block access, result should be empty
140
- expect(result.length).toBe(0);
141
- });
142
-
143
- it('should prevent anon from reading migrations', async () => {
144
- if (!tableExists) {
145
- return;
146
- }
147
-
148
- // clear context to anon role
149
- db.clearContext();
150
-
151
- // anon should not be able to access schema_migrations (rls blocks)
152
- const result = await db.any(
153
- `SELECT * FROM _realtime.schema_migrations LIMIT 1`
154
- );
155
-
156
- // rls should block access, result should be empty
157
- expect(result.length).toBe(0);
158
- });
159
- });
160
-