@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
@@ -1,128 +0,0 @@
1
- import { getConnections, PgTestClient } from 'supabase-test';
2
-
3
- let pg: PgTestClient;
4
- let db: PgTestClient;
5
- let teardown: () => Promise<void>;
6
- let testUserId: string | null = null;
7
-
8
- beforeAll(async () => {
9
- ({ pg, db, teardown } = await getConnections());
10
-
11
- // verify storage schema exists
12
- const storageSchemaExists = await pg.any(
13
- `SELECT EXISTS (
14
- SELECT FROM information_schema.schemata
15
- WHERE schema_name = 'storage'
16
- ) as exists`
17
- );
18
- expect(storageSchemaExists[0].exists).toBe(true);
19
-
20
- // grant access to storage schema for testing
21
- await pg.any(
22
- `GRANT USAGE ON SCHEMA storage TO public;
23
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
24
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
25
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
26
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
27
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO authenticated;
28
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
29
- []
30
- );
31
-
32
- // create a test user for authenticated context
33
- const u = await pg.one(
34
- `INSERT INTO auth.users (id, email)
35
- VALUES (gen_random_uuid(), $1)
36
- RETURNING id`,
37
- ['storage-upload-test@example.com']
38
- );
39
- testUserId = u.id;
40
- });
41
-
42
- afterAll(async () => {
43
- await teardown();
44
- });
45
-
46
- beforeEach(async () => {
47
- await db.beforeEach();
48
- });
49
-
50
- afterEach(async () => {
51
- await db.afterEach();
52
- });
53
-
54
- describe('tutorial: storage objects table access (renamed scenario)', () => {
55
-
56
- it('should verify objects table exists', async () => {
57
- db.setContext({ role: 'service_role' });
58
-
59
- // verify table exists in information schema
60
- const exists = await db.any(
61
- `SELECT EXISTS (
62
- SELECT FROM information_schema.tables
63
- WHERE table_schema = 'storage' AND table_name = 'objects'
64
- ) as exists`
65
- );
66
-
67
- expect(Array.isArray(exists)).toBe(true);
68
- expect(exists[0].exists).toBe(true);
69
- });
70
-
71
- it('should verify service_role can query objects', async () => {
72
- db.setContext({ role: 'service_role' });
73
-
74
- // service_role should be able to query objects
75
- const rows = await db.any(
76
- `SELECT id, bucket_id, name, owner, created_at, updated_at, last_accessed_at, metadata, level
77
- FROM storage.objects
78
- LIMIT 10`
79
- );
80
- expect(Array.isArray(rows)).toBe(true);
81
- });
82
-
83
- it('should verify objects table structure via information_schema', async () => {
84
- db.setContext({ role: 'service_role' });
85
- // query table column structure
86
- const columns = await db.any(
87
- `SELECT column_name, data_type
88
- FROM information_schema.columns
89
- WHERE table_schema = 'storage' AND table_name = 'objects'
90
- ORDER BY ordinal_position`
91
- );
92
- expect(Array.isArray(columns)).toBe(true);
93
- });
94
-
95
- it('should verify authenticated access to objects based on rls', async () => {
96
- db.setContext({ role: 'service_role' });
97
- const rlsStatus = await db.any(
98
- `SELECT c.relrowsecurity
99
- FROM pg_class c
100
- JOIN pg_namespace n ON n.oid = c.relnamespace
101
- WHERE n.nspname = 'storage' AND c.relname = 'objects'`
102
- );
103
- expect(Array.isArray(rlsStatus)).toBe(true);
104
- db.setContext({ role: 'authenticated', 'request.jwt.claim.sub': testUserId });
105
- const result = await db.any(`SELECT * FROM storage.objects LIMIT 1`);
106
- expect(Array.isArray(result)).toBe(true);
107
- if (rlsStatus[0]?.relrowsecurity === true) {
108
- expect(result.length).toBe(0);
109
- }
110
- });
111
-
112
- it('should verify anon access to objects based on rls', async () => {
113
- db.setContext({ role: 'service_role' });
114
- const rlsStatus = await db.any(
115
- `SELECT c.relrowsecurity
116
- FROM pg_class c
117
- JOIN pg_namespace n ON n.oid = c.relnamespace
118
- WHERE n.nspname = 'storage' AND c.relname = 'objects'`
119
- );
120
- db.clearContext();
121
- const result = await db.any(`SELECT * FROM storage.objects LIMIT 1`);
122
- expect(Array.isArray(result)).toBe(true);
123
- if (rlsStatus[0]?.relrowsecurity === true) {
124
- expect(result.length).toBe(0);
125
- }
126
- });
127
- });
128
-
@@ -1,125 +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
- beforeAll(async () => {
8
- ({ pg, db, teardown } = await getConnections());
9
-
10
- // grant access to supabase_functions schema for testing
11
- await pg.any(
12
- `GRANT USAGE ON SCHEMA supabase_functions TO public;
13
- GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO service_role;
14
- GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO authenticated;
15
- GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO anon;
16
- ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO service_role;
17
- ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO authenticated;
18
- ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO anon;`,
19
- []
20
- );
21
-
22
- // grant access to auth schema for auth.users inserts
23
- await pg.any(
24
- `GRANT USAGE ON SCHEMA auth TO public;
25
- GRANT INSERT ON TABLE auth.users TO service_role;`,
26
- []
27
- );
28
- });
29
-
30
- afterAll(async () => {
31
- await teardown();
32
- });
33
-
34
- beforeEach(async () => {
35
- await db.beforeEach();
36
- });
37
-
38
- afterEach(async () => {
39
- await db.afterEach();
40
- });
41
-
42
- describe('tutorial: supabase_functions hooks table access', () => {
43
-
44
- it('should verify hooks table exists', async () => {
45
- db.setContext({ role: 'service_role' });
46
-
47
- // verify table exists in information schema
48
- const exists = await db.any(
49
- `SELECT EXISTS (
50
- SELECT FROM information_schema.tables
51
- WHERE table_schema = 'supabase_functions' AND table_name = 'hooks'
52
- ) as exists`
53
- );
54
-
55
- expect(Array.isArray(exists)).toBe(true);
56
- expect(exists[0].exists).toBe(true);
57
- });
58
-
59
- it('should verify service_role can query hooks', async () => {
60
- db.setContext({ role: 'service_role' });
61
-
62
- // service_role should be able to query hooks
63
- const hooks = await db.any(
64
- `SELECT id, hook_table_id, hook_name, created_at
65
- FROM supabase_functions.hooks
66
- LIMIT 10`
67
- );
68
-
69
- expect(Array.isArray(hooks)).toBe(true);
70
- });
71
-
72
- it('should verify table structure via information_schema', async () => {
73
- db.setContext({ role: 'service_role' });
74
-
75
- // query table column structure
76
- const columns = await db.any(
77
- `SELECT column_name, data_type
78
- FROM information_schema.columns
79
- WHERE table_schema = 'supabase_functions' AND table_name = 'hooks'
80
- ORDER BY ordinal_position`
81
- );
82
-
83
- expect(Array.isArray(columns)).toBe(true);
84
- });
85
-
86
- it('should prevent authenticated users from accessing hooks without proper permissions', async () => {
87
- // create a test user as admin
88
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
89
- // use pg for auth.users insert since it requires superuser privileges
90
- const user = await pg.one(
91
- `INSERT INTO auth.users (id, email)
92
- VALUES (gen_random_uuid(), $1)
93
- RETURNING id`,
94
- ['functions-hooks-test@example.com']
95
- );
96
-
97
- // set context to simulate authenticated user
98
- db.setContext({
99
- role: 'authenticated',
100
- 'request.jwt.claim.sub': user.id
101
- });
102
-
103
- // authenticated users should not be able to access hooks (rls blocks)
104
- const result = await db.any(
105
- `SELECT * FROM supabase_functions.hooks LIMIT 1`
106
- );
107
-
108
- // rls should block access, result should be empty
109
- expect(result.length).toBe(0);
110
- });
111
-
112
- it('should prevent anon from accessing hooks', async () => {
113
- // clear context to anon role
114
- db.clearContext();
115
-
116
- // anon should not be able to access hooks (rls blocks)
117
- const result = await db.any(
118
- `SELECT * FROM supabase_functions.hooks LIMIT 1`
119
- );
120
-
121
- // rls should block access, result should be empty
122
- expect(result.length).toBe(0);
123
- });
124
- });
125
-
@@ -1,213 +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
- beforeAll(async () => {
8
- ({ pg, db, teardown } = await getConnections());
9
-
10
- // grant access to supabase_functions schema for testing
11
- await pg.any(
12
- `GRANT USAGE ON SCHEMA supabase_functions TO public;
13
- GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO service_role;
14
- GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO authenticated;
15
- GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO anon;
16
- ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO service_role;
17
- ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO authenticated;
18
- ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO anon;`,
19
- []
20
- );
21
- });
22
-
23
- afterAll(async () => {
24
- await teardown();
25
- });
26
-
27
- beforeEach(async () => {
28
- await db.beforeEach();
29
- });
30
-
31
- afterEach(async () => {
32
- await db.afterEach();
33
- });
34
-
35
- describe('tutorial: supabase_functions migrations table access', () => {
36
- it('should verify migrations table exists and has correct structure', async () => {
37
- db.setContext({ role: 'service_role' });
38
-
39
- // verify table exists in information schema
40
- const tableExists = await db.any(
41
- `SELECT EXISTS (
42
- SELECT FROM information_schema.tables
43
- WHERE table_schema = 'supabase_functions' AND table_name = 'migrations'
44
- ) as exists`
45
- );
46
- expect(tableExists[0].exists).toBe(true);
47
-
48
- // verify table has expected columns
49
- const columns = await db.any(
50
- `SELECT column_name, data_type, is_nullable
51
- FROM information_schema.columns
52
- WHERE table_schema = 'supabase_functions' AND table_name = 'migrations'
53
- ORDER BY ordinal_position`
54
- );
55
-
56
- expect(Array.isArray(columns)).toBe(true);
57
- expect(columns.length).toBeGreaterThan(0);
58
-
59
- // verify key columns exist (actual schema has version and inserted_at)
60
- const columnNames = columns.map((c: any) => c.column_name);
61
- expect(columnNames).toContain('version');
62
- expect(columnNames).toContain('inserted_at');
63
- });
64
-
65
- it('should verify service_role can read migrations table', async () => {
66
- db.setContext({ role: 'service_role' });
67
-
68
- // service_role should be able to query migrations
69
- const migrations = await db.any(
70
- `SELECT version, inserted_at
71
- FROM supabase_functions.migrations
72
- ORDER BY inserted_at DESC
73
- LIMIT 10`
74
- );
75
-
76
- expect(Array.isArray(migrations)).toBe(true);
77
-
78
- // verify result structure if any rows exist
79
- if (migrations.length > 0) {
80
- expect(migrations[0]).toHaveProperty('version');
81
- expect(migrations[0]).toHaveProperty('inserted_at');
82
- }
83
- });
84
-
85
- it('should verify table has primary key constraint', async () => {
86
- db.setContext({ role: 'service_role' });
87
-
88
- // verify primary key constraint exists
89
- const primaryKeys = await db.any(
90
- `SELECT constraint_name
91
- FROM information_schema.table_constraints
92
- WHERE table_schema = 'supabase_functions'
93
- AND table_name = 'migrations'
94
- AND constraint_type = 'PRIMARY KEY'`
95
- );
96
-
97
- expect(Array.isArray(primaryKeys)).toBe(true);
98
- expect(primaryKeys.length).toBeGreaterThan(0);
99
- expect(primaryKeys[0].constraint_name).toBeDefined();
100
- });
101
-
102
- it('should verify rls status on migrations table', async () => {
103
- db.setContext({ role: 'service_role' });
104
-
105
- // verify rls status (may or may not be enabled depending on setup)
106
- const rlsStatus = await db.any(
107
- `SELECT relname, relrowsecurity
108
- FROM pg_class c
109
- JOIN pg_namespace n ON n.oid = c.relnamespace
110
- WHERE n.nspname = 'supabase_functions'
111
- AND c.relname = 'migrations'`
112
- );
113
-
114
- expect(Array.isArray(rlsStatus)).toBe(true);
115
- expect(rlsStatus.length).toBeGreaterThan(0);
116
- expect(typeof rlsStatus[0].relrowsecurity).toBe('boolean');
117
- });
118
-
119
- it('should verify authenticated users access to migrations based on rls', async () => {
120
- // create a test user as admin
121
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
122
- // use pg for auth.users insert since it requires superuser privileges
123
- const user = await pg.one(
124
- `INSERT INTO auth.users (id, email)
125
- VALUES (gen_random_uuid(), $1)
126
- RETURNING id`,
127
- ['functions-migrations-test@example.com']
128
- );
129
-
130
- // set context to simulate authenticated user
131
- db.setContext({
132
- role: 'authenticated',
133
- 'request.jwt.claim.sub': user.id
134
- });
135
-
136
- // check rls status first
137
- db.setContext({ role: 'service_role' });
138
- const rlsStatus = await db.any(
139
- `SELECT relrowsecurity
140
- FROM pg_class c
141
- JOIN pg_namespace n ON n.oid = c.relnamespace
142
- WHERE n.nspname = 'supabase_functions'
143
- AND c.relname = 'migrations'`
144
- );
145
-
146
- // test as authenticated user
147
- db.setContext({
148
- role: 'authenticated',
149
- 'request.jwt.claim.sub': user.id
150
- });
151
- const result = await db.any(
152
- `SELECT * FROM supabase_functions.migrations LIMIT 1`
153
- );
154
-
155
- // if rls is enabled, result should be empty; if not, may have rows
156
- expect(Array.isArray(result)).toBe(true);
157
- if (rlsStatus[0]?.relrowsecurity === true) {
158
- expect(result.length).toBe(0);
159
- } else {
160
- // rls not enabled, access may be granted based on grants
161
- expect(Array.isArray(result)).toBe(true);
162
- }
163
- });
164
-
165
- it('should verify anon access to migrations based on rls', async () => {
166
- // check rls status first
167
- db.setContext({ role: 'service_role' });
168
- const rlsStatus = await db.any(
169
- `SELECT relrowsecurity
170
- FROM pg_class c
171
- JOIN pg_namespace n ON n.oid = c.relnamespace
172
- WHERE n.nspname = 'supabase_functions'
173
- AND c.relname = 'migrations'`
174
- );
175
-
176
- // clear context to anon role
177
- db.clearContext();
178
-
179
- // anon access depends on rls and grants
180
- const result = await db.any(
181
- `SELECT * FROM supabase_functions.migrations LIMIT 1`
182
- );
183
-
184
- // if rls is enabled, result should be empty; if not, may have rows
185
- expect(Array.isArray(result)).toBe(true);
186
- if (rlsStatus[0]?.relrowsecurity === true) {
187
- expect(result.length).toBe(0);
188
- } else {
189
- // rls not enabled, access may be granted based on grants
190
- expect(Array.isArray(result)).toBe(true);
191
- }
192
- });
193
-
194
- it('should verify migrations table has indexes', async () => {
195
- db.setContext({ role: 'service_role' });
196
-
197
- // verify indexes exist
198
- const indexes = await db.any(
199
- `SELECT indexname, indexdef
200
- FROM pg_indexes
201
- WHERE schemaname = 'supabase_functions'
202
- AND tablename = 'migrations'`
203
- );
204
-
205
- expect(Array.isArray(indexes)).toBe(true);
206
- // indexes may or may not exist, but structure should be valid
207
- if (indexes.length > 0) {
208
- expect(indexes[0].indexname).toBeDefined();
209
- expect(indexes[0].indexdef).toBeDefined();
210
- }
211
- });
212
- });
213
-
@@ -1,225 +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 temporalTableExists = false;
8
-
9
- beforeAll(async () => {
10
-
11
-
12
- ({ pg, db, teardown } = await getConnections());
13
-
14
- // verify storage schema exists
15
- const storageSchemaExists = await pg.any(
16
- `SELECT EXISTS (
17
- SELECT FROM information_schema.schemata
18
- WHERE schema_name = 'storage'
19
- ) as exists`
20
- );
21
- expect(storageSchemaExists[0].exists).toBe(true);
22
-
23
- // grant access to storage schema for testing
24
- await pg.any(
25
- `GRANT USAGE ON SCHEMA storage TO public;
26
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
27
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
28
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
29
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
30
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
31
- []
32
- );
33
-
34
- // check if storage.objects table exists (using pg in beforeAll only)
35
- const exists = await pg.any(
36
- `SELECT EXISTS (
37
- SELECT FROM information_schema.tables
38
- WHERE table_schema = 'storage' AND table_name = 'objects'
39
- ) as exists`
40
- );
41
- temporalTableExists = exists[0]?.exists === true;
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: rls with temporal columns on supabase tables', () => {
57
-
58
- it('should verify storage.objects has temporal columns', async () => {
59
- expect(temporalTableExists).toBe(true);
60
-
61
- db.setContext({ role: 'service_role' });
62
-
63
- const columns = await db.any(
64
- `SELECT column_name, data_type
65
- FROM information_schema.columns
66
- WHERE table_schema = 'storage'
67
- AND table_name = 'objects'
68
- AND column_name IN ('created_at', 'updated_at', 'last_accessed_at')`
69
- );
70
-
71
- expect(Array.isArray(columns)).toBe(true);
72
- // at least created_at should exist
73
- const hasCreatedAt = columns.some((c: any) => c.column_name === 'created_at');
74
- expect(hasCreatedAt).toBe(true);
75
- });
76
-
77
- it('should verify service_role can query temporal columns', async () => {
78
- expect(temporalTableExists).toBe(true);
79
-
80
- db.setContext({ role: 'service_role' });
81
-
82
- const objects = await db.any(
83
- `SELECT id, name, created_at, updated_at, last_accessed_at
84
- FROM storage.objects
85
- ORDER BY created_at DESC
86
- LIMIT 10`
87
- );
88
-
89
- expect(Array.isArray(objects)).toBe(true);
90
-
91
- if (objects.length > 0) {
92
- expect(objects[0].created_at).toBeDefined();
93
- }
94
- });
95
-
96
- it('should verify time-windowed queries work with rls', async () => {
97
- expect(temporalTableExists).toBe(true);
98
-
99
- db.setContext({ role: 'service_role' });
100
-
101
- // query objects from last 30 days
102
- const recentObjects = await db.any(
103
- `SELECT id, name, created_at
104
- FROM storage.objects
105
- WHERE created_at >= NOW() - INTERVAL '30 days'
106
- ORDER BY created_at DESC
107
- LIMIT 10`
108
- );
109
-
110
- expect(Array.isArray(recentObjects)).toBe(true);
111
- });
112
-
113
- it('should verify date range filtering works with rls', async () => {
114
- expect(temporalTableExists).toBe(true);
115
-
116
- db.setContext({ role: 'service_role' });
117
-
118
- // query objects created in last 7 days
119
- const weekOldObjects = await db.any(
120
- `SELECT id, name, created_at
121
- FROM storage.objects
122
- WHERE created_at >= NOW() - INTERVAL '7 days'
123
- AND created_at < NOW()
124
- ORDER BY created_at DESC
125
- LIMIT 10`
126
- );
127
-
128
- expect(Array.isArray(weekOldObjects)).toBe(true);
129
- });
130
-
131
- it('should verify temporal aggregations work with rls', async () => {
132
- expect(temporalTableExists).toBe(true);
133
-
134
- db.setContext({ role: 'service_role' });
135
-
136
- // aggregate objects by date
137
- const dailyCounts = await db.any(
138
- `SELECT DATE(created_at) as date, COUNT(*) as count
139
- FROM storage.objects
140
- WHERE created_at >= NOW() - INTERVAL '30 days'
141
- GROUP BY DATE(created_at)
142
- ORDER BY date DESC
143
- LIMIT 10`
144
- );
145
-
146
- expect(Array.isArray(dailyCounts)).toBe(true);
147
- });
148
-
149
- it('should verify last_accessed_at queries work with rls', async () => {
150
- expect(temporalTableExists).toBe(true);
151
-
152
- db.setContext({ role: 'service_role' });
153
-
154
- // query recently accessed objects
155
- const accessedObjects = await db.any(
156
- `SELECT id, name, last_accessed_at
157
- FROM storage.objects
158
- WHERE last_accessed_at >= NOW() - INTERVAL '7 days'
159
- ORDER BY last_accessed_at DESC
160
- LIMIT 10`
161
- );
162
-
163
- expect(Array.isArray(accessedObjects)).toBe(true);
164
- });
165
-
166
- it('should verify timezone-aware queries work with rls', async () => {
167
- expect(temporalTableExists).toBe(true);
168
-
169
- db.setContext({ role: 'service_role' });
170
-
171
- // query with timezone conversion
172
- const objects = await db.any(
173
- `SELECT id, name,
174
- created_at AT TIME ZONE 'UTC' as created_utc,
175
- created_at AT TIME ZONE 'America/New_York' as created_ny
176
- FROM storage.objects
177
- ORDER BY created_at DESC
178
- LIMIT 10`
179
- );
180
-
181
- expect(Array.isArray(objects)).toBe(true);
182
- });
183
-
184
- it('should verify anon cannot access temporal data', async () => {
185
- expect(temporalTableExists).toBe(true);
186
-
187
- db.setContext({ role: 'anon' });
188
-
189
- const result = await db.any(
190
- `SELECT id, created_at
191
- FROM storage.objects
192
- WHERE created_at >= NOW() - INTERVAL '30 days'
193
- LIMIT 1`
194
- );
195
-
196
- // rls should block access
197
- expect(result.length).toBe(0);
198
- });
199
-
200
- it('should verify temporal ordering works with rls', async () => {
201
- expect(temporalTableExists).toBe(true);
202
-
203
- db.setContext({ role: 'service_role' });
204
-
205
- // order by multiple temporal columns
206
- const objects = await db.any(
207
- `SELECT id, name, created_at, updated_at
208
- FROM storage.objects
209
- ORDER BY created_at DESC, updated_at DESC
210
- LIMIT 10`
211
- );
212
-
213
- expect(Array.isArray(objects)).toBe(true);
214
-
215
- // verify ordering (if multiple results)
216
- if (objects.length > 1) {
217
- for (let i = 0; i < objects.length - 1; i++) {
218
- const current = new Date(objects[i].created_at);
219
- const next = new Date(objects[i + 1].created_at);
220
- expect(current.getTime()).toBeGreaterThanOrEqual(next.getTime());
221
- }
222
- }
223
- });
224
- });
225
-