@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
+
7
+ beforeAll(async () => {
8
+ ({ pg, db, teardown } = await getConnections());
9
+
10
+ // verify auth schema exists
11
+ const authSchemaExists = await pg.any(
12
+ `SELECT EXISTS (
13
+ SELECT FROM information_schema.schemata
14
+ WHERE schema_name = 'auth'
15
+ ) as exists`
16
+ );
17
+ expect(authSchemaExists[0].exists).toBe(true);
18
+
19
+ // grant access to auth schema for testing
20
+ await pg.any(
21
+ `GRANT USAGE ON SCHEMA auth TO public;
22
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
23
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
25
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
26
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
28
+ []
29
+ );
30
+
31
+ // assert refresh_tokens exists
32
+ const exists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'auth' AND table_name = 'refresh_tokens'
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: auth refresh_tokens table access', () => {
54
+
55
+ it('should verify refresh_tokens table exists', async () => {
56
+ db.setContext({ role: 'service_role' });
57
+
58
+ const exists = await db.any(
59
+ `SELECT EXISTS (
60
+ SELECT FROM information_schema.tables
61
+ WHERE table_schema = 'auth' AND table_name = 'refresh_tokens'
62
+ ) as exists`
63
+ );
64
+
65
+ expect(Array.isArray(exists)).toBe(true);
66
+ expect(exists[0].exists).toBe(true);
67
+ });
68
+
69
+ it('should verify service_role can read refresh_tokens', async () => {
70
+ db.setContext({ role: 'service_role' });
71
+
72
+ const tokens = await db.any(
73
+ `SELECT id, token, user_id, revoked, created_at, updated_at, instance_id, session_id
74
+ FROM auth.refresh_tokens
75
+ LIMIT 10`
76
+ );
77
+
78
+ expect(Array.isArray(tokens)).toBe(true);
79
+ });
80
+
81
+ it('should verify table has primary key on id', async () => {
82
+ db.setContext({ role: 'service_role' });
83
+
84
+ const pk = await db.any(
85
+ `SELECT constraint_name
86
+ FROM information_schema.table_constraints
87
+ WHERE table_schema = 'auth' AND table_name = 'refresh_tokens'
88
+ AND constraint_type = 'PRIMARY KEY'`
89
+ );
90
+
91
+ expect(Array.isArray(pk)).toBe(true);
92
+ if (pk.length > 0) {
93
+ expect(pk[0].constraint_name).toBeDefined();
94
+ }
95
+ });
96
+
97
+ it('should verify table has indexes on instance_id and token', async () => {
98
+ db.setContext({ role: 'service_role' });
99
+
100
+ const indexes = await db.any(
101
+ `SELECT indexname
102
+ FROM pg_indexes
103
+ WHERE schemaname = 'auth' AND tablename = 'refresh_tokens'
104
+ AND (indexname LIKE '%instance_id%' OR indexname LIKE '%token%')`
105
+ );
106
+
107
+ expect(Array.isArray(indexes)).toBe(true);
108
+ if (indexes.length > 0) {
109
+ const names = indexes.map((r: any) => r.indexname).join(' ');
110
+ expect(names.includes('instance_id') || names.includes('token')).toBe(true);
111
+ }
112
+ });
113
+
114
+ it('should verify anon access to refresh_tokens based on rls', async () => {
115
+ // check rls status
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 = 'auth' AND c.relname = 'refresh_tokens'`
122
+ );
123
+ expect(Array.isArray(rlsStatus)).toBe(true);
124
+ expect(rlsStatus.length).toBeGreaterThan(0);
125
+
126
+ db.clearContext();
127
+ const result = await db.any(`SELECT * FROM auth.refresh_tokens 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,154 @@
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 auth schema exists
13
+ const authSchemaExists = await pg.any(
14
+ `SELECT EXISTS (
15
+ SELECT FROM information_schema.schemata
16
+ WHERE schema_name = 'auth'
17
+ ) as exists`
18
+ );
19
+ expect(authSchemaExists[0].exists).toBe(true);
20
+
21
+ // grant access to auth schema for testing
22
+ await pg.any(
23
+ `GRANT USAGE ON SCHEMA auth TO public;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
25
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
26
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
28
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
29
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
30
+ []
31
+ );
32
+
33
+ // assert table exists
34
+ const exists = await pg.any(
35
+ `SELECT EXISTS (
36
+ SELECT FROM information_schema.tables
37
+ WHERE table_schema = 'auth' AND table_name = 'saml_providers'
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: auth saml_providers table access', () => {
56
+
57
+ it('should verify saml_providers 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 = 'auth' AND table_name = 'saml_providers'
65
+ ) as exists`
66
+ );
67
+
68
+ expect(Array.isArray(exists)).toBe(true);
69
+ expect(exists[0].exists).toBe(true);
70
+ });
71
+
72
+ it('should verify service_role can query saml_providers', async () => {
73
+ db.setContext({ role: 'service_role' });
74
+
75
+ // service_role should be able to query saml_providers
76
+ const providers = await db.any(
77
+ `SELECT id, sso_provider_id, entity_id, metadata_xml, metadata_url, attribute_mapping, created_at, updated_at
78
+ FROM auth.saml_providers
79
+ LIMIT 10`
80
+ );
81
+
82
+ expect(Array.isArray(providers)).toBe(true);
83
+ });
84
+
85
+ it('should verify table has foreign key to sso_providers', async () => {
86
+ db.setContext({ role: 'service_role' });
87
+
88
+ // check for foreign key constraints to sso_providers
89
+ const fks = await db.any(
90
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
91
+ FROM information_schema.table_constraints AS tc
92
+ JOIN information_schema.constraint_column_usage AS ccu
93
+ ON ccu.constraint_name = tc.constraint_name
94
+ WHERE tc.constraint_type = 'FOREIGN KEY'
95
+ AND tc.table_schema = 'auth'
96
+ AND tc.table_name = 'saml_providers'
97
+ AND ccu.table_name = 'sso_providers'`
98
+ );
99
+
100
+ expect(Array.isArray(fks)).toBe(true);
101
+ });
102
+
103
+ it('should verify unique constraint on entity_id', async () => {
104
+ db.setContext({ role: 'service_role' });
105
+ const uniques = await db.any(
106
+ `SELECT tc.constraint_name, ccu.column_name
107
+ FROM information_schema.table_constraints tc
108
+ JOIN information_schema.constraint_column_usage ccu
109
+ ON ccu.constraint_name = tc.constraint_name
110
+ WHERE tc.table_schema = 'auth'
111
+ AND tc.table_name = 'saml_providers'
112
+ AND tc.constraint_type = 'UNIQUE'`
113
+ );
114
+ expect(Array.isArray(uniques)).toBe(true);
115
+ if (uniques.length > 0) {
116
+ const cols = uniques.map((r: any) => r.column_name);
117
+ expect(cols.includes('entity_id')).toBe(true);
118
+ }
119
+ });
120
+
121
+ it('should verify index exists on sso_provider_id', async () => {
122
+ db.setContext({ role: 'service_role' });
123
+ const idx = await db.any(
124
+ `SELECT indexname, indexdef
125
+ FROM pg_indexes
126
+ WHERE schemaname = 'auth' AND tablename = 'saml_providers'`
127
+ );
128
+ expect(Array.isArray(idx)).toBe(true);
129
+ if (idx.length > 0) {
130
+ const defs = idx.map((r: any) => r.indexdef).join(' ');
131
+ expect(defs.toLowerCase().includes('sso_provider_id')).toBe(true);
132
+ }
133
+ });
134
+
135
+ it('should verify anon access to saml_providers based on rls', async () => {
136
+ db.setContext({ role: 'service_role' });
137
+ const rlsStatus = await db.any(
138
+ `SELECT c.relrowsecurity
139
+ FROM pg_class c
140
+ JOIN pg_namespace n ON n.oid = c.relnamespace
141
+ WHERE n.nspname = 'auth' AND c.relname = 'saml_providers'`
142
+ );
143
+ expect(Array.isArray(rlsStatus)).toBe(true);
144
+ expect(rlsStatus.length).toBeGreaterThan(0);
145
+
146
+ db.clearContext();
147
+ const result = await db.any(`SELECT * FROM auth.saml_providers LIMIT 1`);
148
+ expect(Array.isArray(result)).toBe(true);
149
+ if (rlsStatus[0].relrowsecurity === true) {
150
+ expect(result.length).toBe(0);
151
+ }
152
+ });
153
+ });
154
+
@@ -0,0 +1,145 @@
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 auth schema exists
13
+ const authSchemaExists = await pg.any(
14
+ `SELECT EXISTS (
15
+ SELECT FROM information_schema.schemata
16
+ WHERE schema_name = 'auth'
17
+ ) as exists`
18
+ );
19
+ expect(authSchemaExists[0].exists).toBe(true);
20
+
21
+ // grant access to auth schema for testing
22
+ await pg.any(
23
+ `GRANT USAGE ON SCHEMA auth TO public;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
25
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
26
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
28
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
29
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
30
+ []
31
+ );
32
+
33
+ // assert table exists
34
+ const exists = await pg.any(
35
+ `SELECT EXISTS (
36
+ SELECT FROM information_schema.tables
37
+ WHERE table_schema = 'auth' AND table_name = 'saml_relay_states'
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: auth saml_relay_states table access', () => {
56
+
57
+ it('should verify saml_relay_states 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 = 'auth' AND table_name = 'saml_relay_states'
65
+ ) as exists`
66
+ );
67
+
68
+ expect(Array.isArray(exists)).toBe(true);
69
+ expect(exists[0].exists).toBe(true);
70
+ });
71
+
72
+ it('should verify service_role can query saml_relay_states structure', 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 = 'auth' AND table_name = 'saml_relay_states'
80
+ ORDER BY ordinal_position`
81
+ );
82
+
83
+ expect(Array.isArray(columns)).toBe(true);
84
+ });
85
+
86
+ it('should verify table has created_at timestamp column', async () => {
87
+ db.setContext({ role: 'service_role' });
88
+
89
+ // check for timestamp columns like created_at or expires_at
90
+ const timestampCols = await db.any(
91
+ `SELECT column_name
92
+ FROM information_schema.columns
93
+ WHERE table_schema = 'auth' AND table_name = 'saml_relay_states'
94
+ AND (column_name LIKE '%created_at%' OR column_name LIKE '%expires_at%')`
95
+ );
96
+
97
+ expect(Array.isArray(timestampCols)).toBe(true);
98
+ });
99
+
100
+ it('should verify foreign key to sso_providers and indexes', async () => {
101
+ db.setContext({ role: 'service_role' });
102
+ const fks = await db.any(
103
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
104
+ FROM information_schema.table_constraints AS tc
105
+ JOIN information_schema.constraint_column_usage AS ccu
106
+ ON ccu.constraint_name = tc.constraint_name
107
+ WHERE tc.constraint_type = 'FOREIGN KEY'
108
+ AND tc.table_schema = 'auth'
109
+ AND tc.table_name = 'saml_relay_states'
110
+ AND ccu.table_name = 'sso_providers'`
111
+ );
112
+ expect(Array.isArray(fks)).toBe(true);
113
+ const idx = await db.any(
114
+ `SELECT indexname, indexdef
115
+ FROM pg_indexes
116
+ WHERE schemaname = 'auth' AND tablename = 'saml_relay_states'`
117
+ );
118
+ expect(Array.isArray(idx)).toBe(true);
119
+ if (idx.length > 0) {
120
+ const defs = idx.map((r: any) => r.indexdef).join(' ');
121
+ expect(defs.toLowerCase().includes('sso_provider_id')).toBe(true);
122
+ expect(defs.toLowerCase().includes('for_email')).toBe(true);
123
+ }
124
+ });
125
+
126
+ it('should verify anon access to saml_relay_states based on rls', async () => {
127
+ db.setContext({ role: 'service_role' });
128
+ const rlsStatus = await db.any(
129
+ `SELECT c.relrowsecurity
130
+ FROM pg_class c
131
+ JOIN pg_namespace n ON n.oid = c.relnamespace
132
+ WHERE n.nspname = 'auth' AND c.relname = 'saml_relay_states'`
133
+ );
134
+ expect(Array.isArray(rlsStatus)).toBe(true);
135
+ expect(rlsStatus.length).toBeGreaterThan(0);
136
+
137
+ db.clearContext();
138
+ const result = await db.any(`SELECT * FROM auth.saml_relay_states LIMIT 1`);
139
+ expect(Array.isArray(result)).toBe(true);
140
+ if (rlsStatus[0].relrowsecurity === true) {
141
+ expect(result.length).toBe(0);
142
+ }
143
+ });
144
+ });
145
+
@@ -0,0 +1,131 @@
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
+
11
+
12
+ ({ pg, db, teardown } = await getConnections());
13
+
14
+ // verify auth schema exists
15
+ const authSchemaExists = await pg.any(
16
+ `SELECT EXISTS (
17
+ SELECT FROM information_schema.schemata
18
+ WHERE schema_name = 'auth'
19
+ ) as exists`
20
+ );
21
+ expect(authSchemaExists[0].exists).toBe(true);
22
+
23
+ // grant access to auth schema for testing
24
+ await pg.any(
25
+ `GRANT USAGE ON SCHEMA auth TO public;
26
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
27
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
28
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
29
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
30
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
31
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
32
+ []
33
+ );
34
+
35
+ // check if auth.schema_migrations table exists (using pg in beforeAll only)
36
+ const exists = await pg.any(
37
+ `SELECT EXISTS (
38
+ SELECT FROM information_schema.tables
39
+ WHERE table_schema = 'auth' AND table_name = 'schema_migrations'
40
+ ) as exists`
41
+ );
42
+ tableExists = exists[0]?.exists === true;
43
+ });
44
+
45
+ afterAll(async () => {
46
+ await teardown();
47
+ });
48
+
49
+ beforeEach(async () => {
50
+ await db.beforeEach();
51
+ });
52
+
53
+ afterEach(async () => {
54
+ await db.afterEach();
55
+ });
56
+
57
+ describe('tutorial: auth schema_migrations table access', () => {
58
+
59
+ it('should verify schema_migrations table exists', async () => {
60
+ db.setContext({ role: 'service_role' });
61
+
62
+ // verify table exists in information schema
63
+ const exists = await db.any(
64
+ `SELECT EXISTS (
65
+ SELECT FROM information_schema.tables
66
+ WHERE table_schema = 'auth' AND table_name = 'schema_migrations'
67
+ ) as exists`
68
+ );
69
+
70
+ expect(Array.isArray(exists)).toBe(true);
71
+ if (exists[0]?.exists === false) {
72
+ expect(exists[0].exists).toBe(false);
73
+ return;
74
+ }
75
+ expect(exists[0].exists).toBe(true);
76
+ });
77
+
78
+ it('should verify service_role can read migration versions', async () => {
79
+ if (!tableExists) {
80
+ return;
81
+ }
82
+
83
+ db.setContext({ role: 'service_role' });
84
+
85
+ // service_role should be able to query migration versions
86
+ const migrations = await db.any(
87
+ `SELECT version FROM auth.schema_migrations ORDER BY version`
88
+ );
89
+
90
+ expect(Array.isArray(migrations)).toBe(true);
91
+ });
92
+
93
+ it('should verify table has primary key on version', async () => {
94
+ if (!tableExists) {
95
+ return;
96
+ }
97
+
98
+ db.setContext({ role: 'service_role' });
99
+
100
+ // check for primary key constraint
101
+ const pk = await db.any(
102
+ `SELECT constraint_name
103
+ FROM information_schema.table_constraints
104
+ WHERE table_schema = 'auth' AND table_name = 'schema_migrations'
105
+ AND constraint_type = 'PRIMARY KEY'`
106
+ );
107
+
108
+ expect(Array.isArray(pk)).toBe(true);
109
+ if (pk.length > 0) {
110
+ expect(pk[0].constraint_name).toBeDefined();
111
+ }
112
+ });
113
+
114
+ it('should prevent anon from accessing schema_migrations', async () => {
115
+ if (!tableExists) {
116
+ return;
117
+ }
118
+
119
+ // clear context to anon role
120
+ db.clearContext();
121
+
122
+ // anon should not be able to access schema_migrations (rls blocks)
123
+ const result = await db.any(
124
+ `SELECT * FROM auth.schema_migrations LIMIT 1`
125
+ );
126
+
127
+ // rls should block access, result should be empty
128
+ expect(result.length).toBe(0);
129
+ });
130
+ });
131
+
@@ -0,0 +1,140 @@
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
+
11
+
12
+ ({ pg, db, teardown } = await getConnections());
13
+
14
+ // verify auth schema exists
15
+ const authSchemaExists = await pg.any(
16
+ `SELECT EXISTS (
17
+ SELECT FROM information_schema.schemata
18
+ WHERE schema_name = 'auth'
19
+ ) as exists`
20
+ );
21
+ expect(authSchemaExists[0].exists).toBe(true);
22
+
23
+ // grant access to auth schema for testing
24
+ await pg.any(
25
+ `GRANT USAGE ON SCHEMA auth TO public;
26
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
27
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
28
+ GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
29
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
30
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
31
+ ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
32
+ []
33
+ );
34
+
35
+ // check if auth.sessions table exists (using pg in beforeAll only)
36
+ const exists = await pg.any(
37
+ `SELECT EXISTS (
38
+ SELECT FROM information_schema.tables
39
+ WHERE table_schema = 'auth' AND table_name = 'sessions'
40
+ ) as exists`
41
+ );
42
+ tableExists = exists[0]?.exists === true;
43
+ });
44
+
45
+ afterAll(async () => {
46
+ await teardown();
47
+ });
48
+
49
+ beforeEach(async () => {
50
+ await db.beforeEach();
51
+ });
52
+
53
+ afterEach(async () => {
54
+ await db.afterEach();
55
+ });
56
+
57
+ describe('tutorial: auth sessions table access', () => {
58
+
59
+ it('should verify sessions table exists', async () => {
60
+ db.setContext({ role: 'service_role' });
61
+
62
+ const exists = await db.any(
63
+ `SELECT EXISTS (
64
+ SELECT FROM information_schema.tables
65
+ WHERE table_schema = 'auth' AND table_name = 'sessions'
66
+ ) as exists`
67
+ );
68
+
69
+ expect(Array.isArray(exists)).toBe(true);
70
+ if (exists[0]?.exists === false) {
71
+ // table doesn't exist in this supabase setup, skip test
72
+ expect(exists[0].exists).toBe(false);
73
+ return;
74
+ }
75
+ expect(exists[0].exists).toBe(true);
76
+ });
77
+
78
+ it('should verify service_role can read sessions', async () => {
79
+ db.setContext({ role: 'service_role' });
80
+
81
+ const sessions = await db.any(
82
+ `SELECT id, user_id, created_at, updated_at
83
+ FROM auth.sessions
84
+ LIMIT 10`
85
+ );
86
+
87
+ expect(Array.isArray(sessions)).toBe(true);
88
+ });
89
+
90
+ it('should verify table has foreign key to auth.users', async () => {
91
+ db.setContext({ role: 'service_role' });
92
+
93
+ const fks = await db.any(
94
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
95
+ FROM information_schema.table_constraints AS tc
96
+ JOIN information_schema.constraint_column_usage AS ccu
97
+ ON ccu.constraint_name = tc.constraint_name
98
+ WHERE tc.constraint_type = 'FOREIGN KEY'
99
+ AND tc.table_schema = 'auth'
100
+ AND tc.table_name = 'sessions'
101
+ AND ccu.table_name = 'users'`
102
+ );
103
+
104
+ expect(Array.isArray(fks)).toBe(true);
105
+ });
106
+
107
+ it('should verify table has index on user_id', async () => {
108
+ db.setContext({ role: 'service_role' });
109
+
110
+ const indexes = await db.any(
111
+ `SELECT indexname
112
+ FROM pg_indexes
113
+ WHERE schemaname = 'auth' AND tablename = 'sessions'
114
+ AND indexname LIKE '%user_id%'`
115
+ );
116
+
117
+ expect(Array.isArray(indexes)).toBe(true);
118
+ });
119
+
120
+ it('should verify anon access to sessions based on rls', async () => {
121
+ // check rls status
122
+ db.setContext({ role: 'service_role' });
123
+ const rlsStatus = await db.any(
124
+ `SELECT c.relrowsecurity
125
+ FROM pg_class c
126
+ JOIN pg_namespace n ON n.oid = c.relnamespace
127
+ WHERE n.nspname = 'auth' AND c.relname = 'sessions'`
128
+ );
129
+ expect(Array.isArray(rlsStatus)).toBe(true);
130
+ expect(rlsStatus.length).toBeGreaterThan(0);
131
+
132
+ db.clearContext();
133
+ const result = await db.any(`SELECT * FROM auth.sessions LIMIT 1`);
134
+ expect(Array.isArray(result)).toBe(true);
135
+ if (rlsStatus[0].relrowsecurity === true) {
136
+ expect(result.length).toBe(0);
137
+ }
138
+ });
139
+ });
140
+