postgresai 0.15.0-dev.7 → 0.15.0-rc.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.
- package/README.md +3 -1
- package/bin/postgres-ai.ts +93 -57
- package/bun.lock +4 -4
- package/dist/bin/postgres-ai.js +855 -222
- package/instances.demo.yml +14 -0
- package/lib/checkup-api.ts +25 -6
- package/lib/checkup.ts +225 -0
- package/lib/init.ts +195 -3
- package/lib/metrics-loader.ts +3 -1
- package/lib/supabase.ts +8 -1
- package/package.json +4 -4
- package/scripts/embed-checkup-dictionary.ts +9 -0
- package/scripts/embed-metrics.ts +2 -0
- package/test/PERMISSION_CHECK_TEST_SUMMARY.md +139 -0
- package/test/checkup.test.ts +1288 -2
- package/test/config-consistency.test.ts +321 -5
- package/test/init.integration.test.ts +27 -28
- package/test/init.test.ts +469 -4
- package/test/permission-check-sql.test.ts +116 -0
- package/test/schema-validation.test.ts +81 -0
- package/test/test-utils.ts +51 -2
- package/test/upgrade.test.ts +422 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Permission Check Test Summary
|
|
2
|
+
|
|
3
|
+
## Changes Made
|
|
4
|
+
|
|
5
|
+
Changed all references from `public.pg_statistic` to `postgres_ai.pg_statistic` in:
|
|
6
|
+
- `cli/lib/init.ts` - Permission check SQL query
|
|
7
|
+
- `cli/test/init.test.ts` - All test expectations (28 occurrences)
|
|
8
|
+
|
|
9
|
+
## Key Fix: Safe Schema Checking
|
|
10
|
+
|
|
11
|
+
**Before (883fa95):**
|
|
12
|
+
```sql
|
|
13
|
+
exists (
|
|
14
|
+
select from pg_views
|
|
15
|
+
where schemaname = 'public' and viewname = 'pg_statistic'
|
|
16
|
+
) as granted
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**After (6db79f6) - INCORRECT, caused crashes:**
|
|
20
|
+
```sql
|
|
21
|
+
to_regclass('postgres_ai.pg_statistic') is not null as granted
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Current (this fix):**
|
|
25
|
+
```sql
|
|
26
|
+
case
|
|
27
|
+
when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
|
|
28
|
+
else to_regclass('postgres_ai.pg_statistic') is not null
|
|
29
|
+
end as granted
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Why this fix matters
|
|
33
|
+
|
|
34
|
+
**Issue with bare `to_regclass()`:**
|
|
35
|
+
- Returns NULL when the schema doesn't exist ✓
|
|
36
|
+
- Returns NULL when the view doesn't exist ✓
|
|
37
|
+
- **Throws error** when the schema exists but user lacks USAGE privilege ✗
|
|
38
|
+
|
|
39
|
+
**Fix:**
|
|
40
|
+
- Check `has_schema_privilege()` first to avoid the permission error
|
|
41
|
+
- Returns NULL safely in all cases where we can't check the view
|
|
42
|
+
- Prevents crashes when postgres_ai schema exists but user lacks USAGE
|
|
43
|
+
|
|
44
|
+
## Test Results
|
|
45
|
+
|
|
46
|
+
### Unit Tests ✅
|
|
47
|
+
```
|
|
48
|
+
✓ 95 tests passed across 3 files
|
|
49
|
+
- 84 tests in init.test.ts (including 9 checkCurrentUserPermissions tests)
|
|
50
|
+
- 2 tests in config-consistency.test.ts
|
|
51
|
+
- 9 tests in permission-check-sql.test.ts
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Expected Behavior by Scenario
|
|
55
|
+
|
|
56
|
+
| Scenario | User Permissions | postgres_ai Schema | Expected Result |
|
|
57
|
+
|----------|-----------------|-------------------|-----------------|
|
|
58
|
+
| 1. Superuser | superuser + postgres_ai.pg_statistic | ✓ Exists | ✅ PASS (clean) |
|
|
59
|
+
| 2. pg_monitor, no schema access | pg_monitor only | ✗ No USAGE | ✅ PASS (warning) |
|
|
60
|
+
| 3. No pg_monitor | minimal permissions | ✗ Doesn't exist | ✅ PASS (error + fix SQL) |
|
|
61
|
+
| 8. After prepare-db | pg_monitor + postgres_ai grants | ✓ Exists + SELECT | ✅ PASS (clean) |
|
|
62
|
+
|
|
63
|
+
### SQL Behavior Verification
|
|
64
|
+
|
|
65
|
+
**Scenario 2 & 3: Schema doesn't exist or no USAGE**
|
|
66
|
+
```sql
|
|
67
|
+
-- Check privilege first, then to_regclass (no crash)
|
|
68
|
+
case
|
|
69
|
+
when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
|
|
70
|
+
else to_regclass('postgres_ai.pg_statistic') is not null
|
|
71
|
+
end → NULL
|
|
72
|
+
|
|
73
|
+
-- SELECT check is skipped (returns NULL, not treated as missing optional)
|
|
74
|
+
case
|
|
75
|
+
when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
|
|
76
|
+
when to_regclass('postgres_ai.pg_statistic') is null then null
|
|
77
|
+
else has_table_privilege(current_user, 'postgres_ai.pg_statistic', 'select')
|
|
78
|
+
end → NULL
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Scenario 1 & 8: Schema exists with proper grants**
|
|
82
|
+
```sql
|
|
83
|
+
-- User has USAGE, to_regclass returns OID (view is visible)
|
|
84
|
+
case
|
|
85
|
+
when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
|
|
86
|
+
else to_regclass('postgres_ai.pg_statistic') is not null
|
|
87
|
+
end → TRUE
|
|
88
|
+
|
|
89
|
+
-- SELECT check is performed
|
|
90
|
+
has_table_privilege(current_user, 'postgres_ai.pg_statistic', 'select') → TRUE/FALSE
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Integration Test Limitations
|
|
94
|
+
|
|
95
|
+
Integration tests cannot run due to locale configuration issues with `initdb`:
|
|
96
|
+
```
|
|
97
|
+
error: initdb: error: invalid locale settings; check LANG and LC_* environment variables
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
However, unit tests provide comprehensive coverage of the permission check logic, including:
|
|
101
|
+
- All permission scenarios (granted, denied, skipped)
|
|
102
|
+
- Multiple missing permissions
|
|
103
|
+
- Error propagation
|
|
104
|
+
- Fix command generation
|
|
105
|
+
- Message formatting
|
|
106
|
+
|
|
107
|
+
## Schema Consistency
|
|
108
|
+
|
|
109
|
+
The change ensures consistency across the codebase:
|
|
110
|
+
- ✅ `cli/lib/init.ts` - now checks postgres_ai.pg_statistic
|
|
111
|
+
- ✅ `cli/lib/supabase.ts` - already checks postgres_ai.pg_statistic
|
|
112
|
+
- ✅ `cli/sql/03.permissions.sql` - creates postgres_ai.pg_statistic
|
|
113
|
+
- ✅ `config/target-db/init.sql` - creates postgres_ai.pg_statistic
|
|
114
|
+
- ✅ `config/pgwatch-prometheus/metrics.yml` - references postgres_ai.pg_statistic
|
|
115
|
+
|
|
116
|
+
## Commits
|
|
117
|
+
|
|
118
|
+
1. **955cff2** - `fix: change public.pg_statistic to postgres_ai.pg_statistic`
|
|
119
|
+
- Updated permission check queries
|
|
120
|
+
- Updated all test expectations
|
|
121
|
+
|
|
122
|
+
2. **6db79f6** - `fix: use to_regclass() for safe postgres_ai.pg_statistic check`
|
|
123
|
+
- Replaced pg_views query with to_regclass()
|
|
124
|
+
- ⚠️ This introduced a bug: crashes when schema exists but user lacks USAGE
|
|
125
|
+
|
|
126
|
+
3. **[current]** - `fix: wrap to_regclass() with has_schema_privilege() check`
|
|
127
|
+
- Fixed crash when postgres_ai schema exists but user lacks USAGE privilege
|
|
128
|
+
- Added privilege check before calling to_regclass() in all locations
|
|
129
|
+
- Updated in: init.ts (3 places) and supabase.ts (1 place)
|
|
130
|
+
|
|
131
|
+
## Verification Command
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# Run all permission-related tests
|
|
135
|
+
bun test test/init.test.ts test/config-consistency.test.ts test/permission-check-sql.test.ts
|
|
136
|
+
|
|
137
|
+
# Verify no public.pg_statistic references remain (except in comments)
|
|
138
|
+
git grep -n 'public\.pg_statistic' cli/
|
|
139
|
+
```
|