@pgpm/supabase 0.4.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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/Makefile +7 -0
  3. package/README.md +37 -0
  4. package/__tests__/_realtime/_realtime-extensions.test.ts +129 -0
  5. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +160 -0
  6. package/__tests__/_realtime/_realtime-tenants.test.ts +163 -0
  7. package/__tests__/auth/auth.audit-log-entries.test.ts +148 -0
  8. package/__tests__/auth/auth.flow-state.test.ts +114 -0
  9. package/__tests__/auth/auth.identities.test.ts +149 -0
  10. package/__tests__/auth/auth.instances.test.ts +134 -0
  11. package/__tests__/auth/auth.mfa-amr-claims.test.ts +142 -0
  12. package/__tests__/auth/auth.mfa-challenges.test.ts +117 -0
  13. package/__tests__/auth/auth.mfa-factors.test.ts +118 -0
  14. package/__tests__/auth/auth.oauth-authorizations.test.ts +123 -0
  15. package/__tests__/auth/auth.oauth-clients.test.ts +120 -0
  16. package/__tests__/auth/auth.oauth-consents.test.ts +122 -0
  17. package/__tests__/auth/auth.one-time-tokens.test.ts +194 -0
  18. package/__tests__/auth/auth.refresh-tokens.test.ts +134 -0
  19. package/__tests__/auth/auth.saml-providers.test.ts +154 -0
  20. package/__tests__/auth/auth.saml-relay-states.test.ts +145 -0
  21. package/__tests__/auth/auth.schema-migrations.test.ts +131 -0
  22. package/__tests__/auth/auth.sessions.test.ts +140 -0
  23. package/__tests__/auth/auth.sso-domains.test.ts +137 -0
  24. package/__tests__/auth/auth.sso-providers.test.ts +136 -0
  25. package/__tests__/auth/auth.users.test.ts +314 -0
  26. package/__tests__/conditional.test.ts +229 -0
  27. package/__tests__/functions.test.ts +252 -0
  28. package/__tests__/jsonb.test.ts +237 -0
  29. package/__tests__/net/net.http-request-queue.test.ts +159 -0
  30. package/__tests__/net/net.http-response.test.ts +163 -0
  31. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +168 -0
  32. package/__tests__/realtime/realtime.messages.test.ts +162 -0
  33. package/__tests__/realtime/realtime.schema-migrations.test.ts +139 -0
  34. package/__tests__/realtime/realtime.subscription.test.ts +159 -0
  35. package/__tests__/storage/storage.buckets-analytics.test.ts +134 -0
  36. package/__tests__/storage/storage.buckets.test.ts +146 -0
  37. package/__tests__/storage/storage.iceberg-namespaces.test.ts +132 -0
  38. package/__tests__/storage/storage.iceberg-tables.test.ts +142 -0
  39. package/__tests__/storage/storage.migrations.test.ts +133 -0
  40. package/__tests__/storage/storage.objects.test.ts +160 -0
  41. package/__tests__/storage/storage.prefixes.test.ts +138 -0
  42. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +133 -0
  43. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +128 -0
  44. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +125 -0
  45. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +213 -0
  46. package/__tests__/temporal.test.ts +225 -0
  47. package/__tests__/vault-secrets.test.ts +145 -0
  48. package/deploy/LICENSE +9 -0
  49. package/deploy/supabase.sql +2271 -0
  50. package/jest.config.js +15 -0
  51. package/launchql.plan +4 -0
  52. package/package.json +23 -0
  53. package/revert/supabase.sql +3 -0
  54. package/sql/supabase--0.0.1.sql +1368 -0
  55. package/src/index.ts +3 -0
  56. package/supabase.control +7 -0
  57. package/verify/supabase.sql +3 -0
@@ -0,0 +1,134 @@
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
+ // tests will assert table existence explicitly
33
+
34
+ const u = await pg.one(
35
+ `INSERT INTO auth.users (id, email)
36
+ VALUES (gen_random_uuid(), $1)
37
+ RETURNING id`,
38
+ ['storage-analytics-test@example.com']
39
+ );
40
+ testUserId = u.id;
41
+ });
42
+
43
+ afterAll(async () => {
44
+ await teardown();
45
+ });
46
+
47
+ beforeEach(async () => {
48
+ await db.beforeEach();
49
+ });
50
+
51
+ afterEach(async () => {
52
+ await db.afterEach();
53
+ });
54
+
55
+ describe('tutorial: storage buckets_analytics table access', () => {
56
+
57
+ it('should verify buckets_analytics table exists', async () => {
58
+ db.setContext({ role: 'service_role' });
59
+
60
+ // verify table exists in information schema
61
+ const exists = await db.any(
62
+ `SELECT EXISTS (
63
+ SELECT FROM information_schema.tables
64
+ WHERE table_schema = 'storage' AND table_name = 'buckets_analytics'
65
+ ) as exists`
66
+ );
67
+
68
+ expect(Array.isArray(exists)).toBe(true);
69
+ expect(typeof exists[0].exists).toBe('boolean');
70
+ });
71
+
72
+ it('should verify service_role can query buckets_analytics', async () => {
73
+ db.setContext({ role: 'service_role' });
74
+
75
+ // service_role should be able to query buckets_analytics
76
+ const analytics = await db.any(
77
+ `SELECT id
78
+ FROM storage.buckets_analytics
79
+ LIMIT 10`
80
+ );
81
+
82
+ expect(Array.isArray(analytics)).toBe(true);
83
+ });
84
+
85
+ it('should verify table structure via information_schema', async () => {
86
+ db.setContext({ role: 'service_role' });
87
+
88
+ // query table column structure
89
+ const columns = await db.any(
90
+ `SELECT column_name, data_type
91
+ FROM information_schema.columns
92
+ WHERE table_schema = 'storage' AND table_name = 'buckets_analytics'
93
+ ORDER BY ordinal_position`
94
+ );
95
+
96
+ expect(Array.isArray(columns)).toBe(true);
97
+ });
98
+
99
+ it('should verify authenticated access to buckets_analytics based on rls', async () => {
100
+ db.setContext({ role: 'service_role' });
101
+ const rlsStatus = await db.any(
102
+ `SELECT c.relrowsecurity
103
+ FROM pg_class c
104
+ JOIN pg_namespace n ON n.oid = c.relnamespace
105
+ WHERE n.nspname = 'storage' AND c.relname = 'buckets_analytics'`
106
+ );
107
+ expect(Array.isArray(rlsStatus)).toBe(true);
108
+
109
+ db.setContext({ role: 'authenticated', 'request.jwt.claim.sub': testUserId });
110
+ const result = await db.any(`SELECT * FROM storage.buckets_analytics LIMIT 1`);
111
+ expect(Array.isArray(result)).toBe(true);
112
+ if (rlsStatus[0]?.relrowsecurity === true) {
113
+ expect(result.length).toBe(0);
114
+ }
115
+ });
116
+
117
+ it('should verify anon access to buckets_analytics based on rls', async () => {
118
+ db.setContext({ role: 'service_role' });
119
+ const rlsStatus = await db.any(
120
+ `SELECT c.relrowsecurity
121
+ FROM pg_class c
122
+ JOIN pg_namespace n ON n.oid = c.relnamespace
123
+ WHERE n.nspname = 'storage' AND c.relname = 'buckets_analytics'`
124
+ );
125
+
126
+ db.clearContext();
127
+ const result = await db.any(`SELECT * FROM storage.buckets_analytics LIMIT 1`);
128
+ expect(Array.isArray(result)).toBe(true);
129
+ if (rlsStatus[0]?.relrowsecurity === true) {
130
+ expect(result.length).toBe(0);
131
+ }
132
+ });
133
+ });
134
+
@@ -0,0 +1,146 @@
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
+
9
+
10
+ ({ pg, db, teardown } = await getConnections());
11
+
12
+ // verify storage schema exists
13
+ const storageSchemaExists = await pg.any(
14
+ `SELECT EXISTS (
15
+ SELECT FROM information_schema.schemata
16
+ WHERE schema_name = 'storage'
17
+ ) as exists`
18
+ );
19
+ expect(storageSchemaExists[0].exists).toBe(true);
20
+
21
+ // grant access to storage schema for testing
22
+ await pg.any(
23
+ `GRANT USAGE ON SCHEMA storage TO public;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
25
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
26
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
28
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO authenticated;
29
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
30
+ []
31
+ );
32
+
33
+ // assert storage.buckets exists
34
+ const exists = await pg.any(
35
+ `SELECT EXISTS (
36
+ SELECT FROM information_schema.tables
37
+ WHERE table_schema = 'storage' AND table_name = 'buckets'
38
+ ) as exists`
39
+ );
40
+ expect(exists[0].exists).toBe(true);
41
+ });
42
+
43
+ afterAll(async () => {
44
+ await teardown();
45
+ });
46
+
47
+ beforeEach(async () => {
48
+ await db.beforeEach();
49
+ });
50
+
51
+ afterEach(async () => {
52
+ await db.afterEach();
53
+ });
54
+
55
+ describe('tutorial: storage buckets table access', () => {
56
+
57
+ it('should verify buckets table exists', async () => {
58
+ db.setContext({ role: 'service_role' });
59
+
60
+ const exists = await db.any(
61
+ `SELECT EXISTS (
62
+ SELECT FROM information_schema.tables
63
+ WHERE table_schema = 'storage' AND table_name = 'buckets'
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 read buckets', async () => {
72
+ db.setContext({ role: 'service_role' });
73
+
74
+ const buckets = await db.any(
75
+ `SELECT id, name, owner, created_at, updated_at
76
+ FROM storage.buckets
77
+ LIMIT 10`
78
+ );
79
+
80
+ expect(Array.isArray(buckets)).toBe(true);
81
+ });
82
+
83
+ it('should verify table has primary key on id', async () => {
84
+ db.setContext({ role: 'service_role' });
85
+
86
+ const pk = await db.any(
87
+ `SELECT constraint_name
88
+ FROM information_schema.table_constraints
89
+ WHERE table_schema = 'storage' AND table_name = 'buckets'
90
+ AND constraint_type = 'PRIMARY KEY'`
91
+ );
92
+
93
+ expect(Array.isArray(pk)).toBe(true);
94
+ if (pk.length > 0) {
95
+ expect(pk[0].constraint_name).toBeDefined();
96
+ }
97
+ });
98
+
99
+ it('should verify table has unique index on name', async () => {
100
+ db.setContext({ role: 'service_role' });
101
+
102
+ const indexes = await db.any(
103
+ `SELECT indexname, indexdef
104
+ FROM pg_indexes
105
+ WHERE schemaname = 'storage' AND tablename = 'buckets'`
106
+ );
107
+
108
+ expect(Array.isArray(indexes)).toBe(true);
109
+ if (indexes.length > 0) {
110
+ const defs = indexes.map((r: any) => String(r.indexdef).toLowerCase()).join(' ');
111
+ expect(defs.includes('unique')).toBe(true);
112
+ expect(defs.includes('(name)')).toBe(true);
113
+ }
114
+ });
115
+
116
+ it('should verify table has owner column for user linkage', async () => {
117
+ db.setContext({ role: 'service_role' });
118
+ const cols = await db.any(
119
+ `SELECT column_name
120
+ FROM information_schema.columns
121
+ WHERE table_schema = 'storage' AND table_name = 'buckets'`
122
+ );
123
+ expect(Array.isArray(cols)).toBe(true);
124
+ const names = cols.map((r: any) => r.column_name);
125
+ expect(names).toContain('owner');
126
+ });
127
+
128
+ it('should verify anon access to buckets based on rls', async () => {
129
+ db.setContext({ role: 'service_role' });
130
+ const rlsStatus = await db.any(
131
+ `SELECT c.relrowsecurity
132
+ FROM pg_class c
133
+ JOIN pg_namespace n ON n.oid = c.relnamespace
134
+ WHERE n.nspname = 'storage' AND c.relname = 'buckets'`
135
+ );
136
+ expect(Array.isArray(rlsStatus)).toBe(true);
137
+
138
+ db.clearContext();
139
+ const result = await db.any(`SELECT * FROM storage.buckets LIMIT 1`);
140
+ expect(Array.isArray(result)).toBe(true);
141
+ if (rlsStatus[0]?.relrowsecurity === true) {
142
+ expect(result.length).toBe(0);
143
+ }
144
+ });
145
+ });
146
+
@@ -0,0 +1,132 @@
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
+ const u = await pg.one(
33
+ `INSERT INTO auth.users (id, email)
34
+ VALUES (gen_random_uuid(), $1)
35
+ RETURNING id`,
36
+ ['storage-namespace-test@example.com']
37
+ );
38
+ testUserId = u.id;
39
+ });
40
+
41
+ afterAll(async () => {
42
+ await teardown();
43
+ });
44
+
45
+ beforeEach(async () => {
46
+ await db.beforeEach();
47
+ });
48
+
49
+ afterEach(async () => {
50
+ await db.afterEach();
51
+ });
52
+
53
+ describe('tutorial: storage iceberg_namespaces table access', () => {
54
+
55
+ it('should verify iceberg_namespaces table exists', async () => {
56
+ db.setContext({ role: 'service_role' });
57
+
58
+ // verify table exists in information schema
59
+ const exists = await db.any(
60
+ `SELECT EXISTS (
61
+ SELECT FROM information_schema.tables
62
+ WHERE table_schema = 'storage' AND table_name = 'iceberg_namespaces'
63
+ ) as exists`
64
+ );
65
+
66
+ expect(Array.isArray(exists)).toBe(true);
67
+ expect(exists[0].exists).toBe(true);
68
+ });
69
+
70
+ it('should verify service_role can query iceberg_namespaces', async () => {
71
+ db.setContext({ role: 'service_role' });
72
+
73
+ // service_role should be able to query iceberg_namespaces
74
+ const namespaces = await db.any(
75
+ `SELECT id, bucket_id, name, created_at, updated_at
76
+ FROM storage.iceberg_namespaces
77
+ LIMIT 10`
78
+ );
79
+
80
+ expect(Array.isArray(namespaces)).toBe(true);
81
+ });
82
+
83
+ it('should verify table has proper structure', async () => {
84
+ db.setContext({ role: 'service_role' });
85
+
86
+ // query table column structure
87
+ const columns = await db.any(
88
+ `SELECT column_name
89
+ FROM information_schema.columns
90
+ WHERE table_schema = 'storage' AND table_name = 'iceberg_namespaces'
91
+ ORDER BY ordinal_position`
92
+ );
93
+
94
+ expect(Array.isArray(columns)).toBe(true);
95
+ });
96
+
97
+ it('should verify authenticated access to iceberg_namespaces based on rls', async () => {
98
+ db.setContext({ role: 'service_role' });
99
+ const rlsStatus = await db.any(
100
+ `SELECT c.relrowsecurity
101
+ FROM pg_class c
102
+ JOIN pg_namespace n ON n.oid = c.relnamespace
103
+ WHERE n.nspname = 'storage' AND c.relname = 'iceberg_namespaces'`
104
+ );
105
+ expect(Array.isArray(rlsStatus)).toBe(true);
106
+
107
+ db.setContext({ role: 'authenticated', 'request.jwt.claim.sub': testUserId });
108
+ const result = await db.any(`SELECT * FROM storage.iceberg_namespaces LIMIT 1`);
109
+ expect(Array.isArray(result)).toBe(true);
110
+ if (rlsStatus[0]?.relrowsecurity === true) {
111
+ expect(result.length).toBe(0);
112
+ }
113
+ });
114
+
115
+ it('should verify anon access to iceberg_namespaces based on rls', async () => {
116
+ db.setContext({ role: 'service_role' });
117
+ const rlsStatus = await db.any(
118
+ `SELECT c.relrowsecurity
119
+ FROM pg_class c
120
+ JOIN pg_namespace n ON n.oid = c.relnamespace
121
+ WHERE n.nspname = 'storage' AND c.relname = 'iceberg_namespaces'`
122
+ );
123
+
124
+ db.clearContext();
125
+ const result = await db.any(`SELECT * FROM storage.iceberg_namespaces LIMIT 1`);
126
+ expect(Array.isArray(result)).toBe(true);
127
+ if (rlsStatus[0]?.relrowsecurity === true) {
128
+ expect(result.length).toBe(0);
129
+ }
130
+ });
131
+ });
132
+
@@ -0,0 +1,142 @@
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
+ const u = await pg.one(
33
+ `INSERT INTO auth.users (id, email)
34
+ VALUES (gen_random_uuid(), $1)
35
+ RETURNING id`,
36
+ ['storage-iceberg-test@example.com']
37
+ );
38
+ testUserId = u.id;
39
+ });
40
+
41
+ afterAll(async () => {
42
+ await teardown();
43
+ });
44
+
45
+ beforeEach(async () => {
46
+ await db.beforeEach();
47
+ });
48
+
49
+ afterEach(async () => {
50
+ await db.afterEach();
51
+ });
52
+
53
+ describe('tutorial: storage iceberg_tables table access', () => {
54
+
55
+ it('should verify iceberg_tables table exists', async () => {
56
+ db.setContext({ role: 'service_role' });
57
+
58
+ // verify table exists in information schema
59
+ const exists = await db.any(
60
+ `SELECT EXISTS (
61
+ SELECT FROM information_schema.tables
62
+ WHERE table_schema = 'storage' AND table_name = 'iceberg_tables'
63
+ ) as exists`
64
+ );
65
+
66
+ expect(Array.isArray(exists)).toBe(true);
67
+ expect(exists[0].exists).toBe(true);
68
+ });
69
+
70
+ it('should verify service_role can query iceberg_tables', async () => {
71
+ db.setContext({ role: 'service_role' });
72
+
73
+ // service_role should be able to query iceberg_tables
74
+ const tables = await db.any(
75
+ `SELECT id, namespace_id, bucket_id, name, location, created_at, updated_at
76
+ FROM storage.iceberg_tables
77
+ LIMIT 10`
78
+ );
79
+
80
+ expect(Array.isArray(tables)).toBe(true);
81
+ });
82
+
83
+ it('should verify table has foreign key to iceberg_namespaces', async () => {
84
+ db.setContext({ role: 'service_role' });
85
+
86
+ // check for foreign key constraints to iceberg_namespaces
87
+ const fks = await db.any(
88
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
89
+ FROM information_schema.table_constraints AS tc
90
+ JOIN information_schema.constraint_column_usage AS ccu
91
+ ON ccu.constraint_name = tc.constraint_name
92
+ WHERE tc.constraint_type = 'FOREIGN KEY'
93
+ AND tc.table_schema = 'storage'
94
+ AND tc.table_name = 'iceberg_tables'
95
+ AND ccu.table_name = 'iceberg_namespaces'`
96
+ );
97
+
98
+ expect(Array.isArray(fks)).toBe(true);
99
+ });
100
+
101
+ it('should verify authenticated access to iceberg_tables based on rls', async () => {
102
+ db.setContext({ role: 'service_role' });
103
+ const rlsStatus = await db.any(
104
+ `SELECT c.relrowsecurity
105
+ FROM pg_class c
106
+ JOIN pg_namespace n ON n.oid = c.relnamespace
107
+ WHERE n.nspname = 'storage' AND c.relname = 'iceberg_tables'`
108
+ );
109
+ expect(Array.isArray(rlsStatus)).toBe(true);
110
+
111
+ db.setContext({ role: 'authenticated', 'request.jwt.claim.sub': testUserId });
112
+ const result = await db.any(`SELECT * FROM storage.iceberg_tables LIMIT 1`);
113
+ expect(Array.isArray(result)).toBe(true);
114
+ if (rlsStatus[0]?.relrowsecurity === true) {
115
+ expect(result.length).toBe(0);
116
+ }
117
+ });
118
+
119
+ it('should verify anon access to iceberg_tables based on rls', async () => {
120
+ db.setContext({ role: 'service_role' });
121
+ const rlsStatus = await db.any(
122
+ `SELECT c.relrowsecurity
123
+ FROM pg_class c
124
+ JOIN pg_namespace n ON n.oid = c.relnamespace
125
+ WHERE n.nspname = 'storage' AND c.relname = 'iceberg_tables'`
126
+ );
127
+
128
+ // clear context to anon role
129
+ db.clearContext();
130
+
131
+ // anon should not be able to access iceberg_tables (rls blocks)
132
+ const result = await db.any(
133
+ `SELECT * FROM storage.iceberg_tables LIMIT 1`
134
+ );
135
+
136
+ expect(Array.isArray(result)).toBe(true);
137
+ if (rlsStatus[0]?.relrowsecurity === true) {
138
+ expect(result.length).toBe(0);
139
+ }
140
+ });
141
+ });
142
+
@@ -0,0 +1,133 @@
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
+ // verify storage schema exists
11
+ const storageSchemaExists = await pg.any(
12
+ `SELECT EXISTS (
13
+ SELECT FROM information_schema.schemata
14
+ WHERE schema_name = 'storage'
15
+ ) as exists`
16
+ );
17
+ expect(storageSchemaExists[0].exists).toBe(true);
18
+
19
+ // grant access to storage schema for testing
20
+ await pg.any(
21
+ `GRANT USAGE ON SCHEMA storage TO public;
22
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
23
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
25
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
26
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO authenticated;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
28
+ []
29
+ );
30
+
31
+ // assert storage.migrations exists
32
+ const exists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'storage' AND table_name = 'migrations'
36
+ ) as exists`
37
+ );
38
+ expect(exists[0].exists).toBe(true);
39
+ });
40
+
41
+ afterAll(async () => {
42
+ await teardown();
43
+ });
44
+
45
+ beforeEach(async () => {
46
+ await db.beforeEach();
47
+ });
48
+
49
+ afterEach(async () => {
50
+ await db.afterEach();
51
+ });
52
+
53
+ describe('tutorial: storage migrations table access', () => {
54
+
55
+ it('should verify migrations table exists', async () => {
56
+ db.setContext({ role: 'service_role' });
57
+
58
+ // verify table exists in information schema
59
+ const exists = await db.any(
60
+ `SELECT EXISTS (
61
+ SELECT FROM information_schema.tables
62
+ WHERE table_schema = 'storage' AND table_name = 'migrations'
63
+ ) as exists`
64
+ );
65
+
66
+ expect(Array.isArray(exists)).toBe(true);
67
+ expect(exists[0].exists).toBe(true);
68
+ });
69
+
70
+ it('should verify service_role can read migrations', async () => {
71
+ db.setContext({ role: 'service_role' });
72
+
73
+ // service_role should be able to query migrations
74
+ const migrations = await db.any(
75
+ `SELECT id, name, hash, executed_at
76
+ FROM storage.migrations
77
+ ORDER BY executed_at DESC
78
+ LIMIT 10`
79
+ );
80
+
81
+ expect(Array.isArray(migrations)).toBe(true);
82
+ });
83
+
84
+ it('should verify table has primary key on id', async () => {
85
+ db.setContext({ role: 'service_role' });
86
+
87
+ // check for primary key constraint
88
+ const pk = await db.any(
89
+ `SELECT constraint_name
90
+ FROM information_schema.table_constraints
91
+ WHERE table_schema = 'storage' AND table_name = 'migrations'
92
+ AND constraint_type = 'PRIMARY KEY'`
93
+ );
94
+
95
+ expect(Array.isArray(pk)).toBe(true);
96
+ if (pk.length > 0) {
97
+ expect(pk[0].constraint_name).toBeDefined();
98
+ }
99
+ });
100
+
101
+ it('should verify table has unique constraint on name', async () => {
102
+ db.setContext({ role: 'service_role' });
103
+
104
+ // check for unique constraints
105
+ const unique = await db.any(
106
+ `SELECT constraint_name
107
+ FROM information_schema.table_constraints
108
+ WHERE table_schema = 'storage' AND table_name = 'migrations'
109
+ AND constraint_type = 'UNIQUE'`
110
+ );
111
+
112
+ expect(Array.isArray(unique)).toBe(true);
113
+ });
114
+
115
+ it('should verify anon access to migrations based on rls', async () => {
116
+ db.setContext({ role: 'service_role' });
117
+ const rlsStatus = await db.any(
118
+ `SELECT c.relrowsecurity
119
+ FROM pg_class c
120
+ JOIN pg_namespace n ON n.oid = c.relnamespace
121
+ WHERE n.nspname = 'storage' AND c.relname = 'migrations'`
122
+ );
123
+ expect(Array.isArray(rlsStatus)).toBe(true);
124
+
125
+ db.clearContext();
126
+ const result = await db.any(`SELECT * FROM storage.migrations LIMIT 1`);
127
+ expect(Array.isArray(result)).toBe(true);
128
+ if (rlsStatus[0]?.relrowsecurity === true) {
129
+ expect(result.length).toBe(0);
130
+ }
131
+ });
132
+ });
133
+