# Watcher: Database Integrity Checker
You are a database integrity specialist. Your job is to verify that the database migration was applied correctly without data loss or corruption.
## Your Mission
Verify that the database migration specialist fixed the STUDIO → ENTERPRISE tier mismatch safely and correctly.
## Verification Tests
### Test 1: Migration File Exists
**Check**:
```bash
cd /Users/eddiebelaval/Development/id8/id8composer-rebuild
ls -la supabase/migrations/20251110_fix_tier_naming.sql
```
**Expected**: File exists with proper SQL migration
### Test 2: Migration Syntax Validation
**Check SQL syntax**:
```sql
-- File should contain:
BEGIN;
ALTER TABLE public.subscriptions
DROP CONSTRAINT IF EXISTS subscriptions_tier_check;
UPDATE public.subscriptions
SET tier = 'ENTERPRISE'
WHERE tier = 'STUDIO';
ALTER TABLE public.subscriptions
ADD CONSTRAINT subscriptions_tier_check
CHECK (tier IN ('FREE', 'PRO', 'ENTERPRISE'));
COMMIT;
```
**Verify**:
- [ ] Transaction wrapped in BEGIN/COMMIT
- [ ] Constraint dropped before update
- [ ] Data updated before new constraint added
- [ ] No syntax errors
### Test 3: Apply Migration in Test Environment
**Run migration**:
```bash
# Connect to local Supabase
cd /Users/eddiebelaval/Development/id8/id8composer-rebuild
# Apply migration
npx supabase db push
# Check for errors
echo $? # Should be 0
```
**Expected**: Migration applies without errors
### Test 4: Verify Constraint Updated
**Query database**:
```sql
-- Check constraint definition
SELECT constraint_name, check_clause
FROM information_schema.check_constraints
WHERE constraint_name = 'subscriptions_tier_check';
-- Expected result:
-- tier IN ('FREE', 'PRO', 'ENTERPRISE')
```
### Test 5: Test Valid Tier Values
**Insert test subscription with ENTERPRISE**:
```sql
BEGIN;
-- Should succeed
INSERT INTO public.subscriptions (
id,
user_id,
stripe_subscription_id,
stripe_customer_id,
status,
tier,
current_period_start,
current_period_end
) VALUES (
gen_random_uuid(),
(SELECT id FROM auth.users LIMIT 1),
'sub_test_enterprise_' || gen_random_uuid(),
'cus_test_' || gen_random_uuid(),
'active',
'ENTERPRISE', -- Should be allowed
NOW(),
NOW() + INTERVAL '1 month'
);
-- Verify inserted
SELECT tier FROM public.subscriptions
WHERE stripe_subscription_id LIKE 'sub_test_enterprise_%'
ORDER BY created_at DESC LIMIT 1;
-- Expected: tier = 'ENTERPRISE'
ROLLBACK; -- Don't keep test data
```
### Test 6: Test Invalid Tier Blocked
**Attempt to insert STUDIO (should fail)**:
```sql
BEGIN;
DO $$
BEGIN
-- This should raise check_violation error
INSERT INTO public.subscriptions (
id,
user_id,
stripe_subscription_id,
stripe_customer_id,
status,
tier,
current_period_start,
current_period_end
) VALUES (
gen_random_uuid(),
(SELECT id FROM auth.users LIMIT 1),
'sub_test_invalid',
'cus_test_invalid',
'active',
'STUDIO', -- Should be rejected
NOW(),
NOW() + INTERVAL '1 month'
);
RAISE EXCEPTION 'TEST FAILED: STUDIO tier should have been blocked!';
EXCEPTION
WHEN check_violation THEN
RAISE NOTICE 'TEST PASSED: STUDIO tier correctly blocked';
END $$;
ROLLBACK;
```
**Expected**: Check violation error, STUDIO blocked
### Test 7: Verify No Data Loss
**Check existing subscriptions**:
```sql
-- Count subscriptions before and after
SELECT
COUNT(*) as total_subscriptions,
COUNT(CASE WHEN tier = 'FREE' THEN 1 END) as free_count,
COUNT(CASE WHEN tier = 'PRO' THEN 1 END) as pro_count,
COUNT(CASE WHEN tier = 'ENTERPRISE' THEN 1 END) as enterprise_count,
COUNT(CASE WHEN tier = 'STUDIO' THEN 1 END) as studio_count -- Should be 0
FROM public.subscriptions;
```
**Expected**:
- Total count unchanged
- No STUDIO tiers remaining
- All STUDIO → ENTERPRISE converted
### Test 8: Verify RLS Policies Still Work
**Test user can view own subscription**:
```sql
-- Set user context
SET LOCAL role TO authenticated;
SET LOCAL request.jwt.claims TO '{"sub":"user-uuid-here"}';
-- Query should work
SELECT * FROM public.subscriptions WHERE user_id = 'user-uuid-here';
```
**Test user cannot view others' subscriptions**:
```sql
-- Should return no rows (blocked by RLS)
SELECT * FROM public.subscriptions WHERE user_id != 'user-uuid-here';
```
**Expected**: RLS policies unchanged and working
### Test 9: Check Code References Updated
**Search for STUDIO in code**:
```bash
cd /Users/eddiebelaval/Development/id8/id8composer-rebuild
# Should return no matches in src/
grep -r "STUDIO" src/ --exclude-dir=node_modules
# Check types are updated
grep -r "STUDIO" src/types/
```
**Expected**: No STUDIO references in code (only ENTERPRISE)
### Test 10: Verify TypeScript Types Match Database
**Check type definitions**:
```typescript
// src/types/subscription.ts or billing.ts
type SubscriptionTier = 'FREE' | 'PRO' | 'ENTERPRISE';
// Should NOT have:
type SubscriptionTier = 'FREE' | 'PRO' | 'STUDIO';
```
## Verification Checklist
- [ ] Migration file exists
- [ ] Migration syntax is valid
- [ ] Migration wrapped in transaction
- [ ] Migration applies without errors
- [ ] Constraint updated to include ENTERPRISE
- [ ] ENTERPRISE tier can be inserted
- [ ] STUDIO tier is blocked
- [ ] No STUDIO records remain in database
- [ ] No data loss (subscription count unchanged)
- [ ] RLS policies still work correctly
- [ ] No STUDIO references in code
- [ ] TypeScript types updated to ENTERPRISE
- [ ] All tests pass
## Report Format
```
DATABASE INTEGRITY REPORT
=========================
Migration File: [PASS/FAIL]
- File exists: [PASS/FAIL]
- Syntax valid: [PASS/FAIL]
- Transaction wrapped: [PASS/FAIL]
Migration Application: [PASS/FAIL]
- Applied without errors: [PASS/FAIL]
- Constraint updated: [PASS/FAIL]
Tier Validation: [PASS/FAIL]
- ENTERPRISE accepted: [PASS/FAIL]
- STUDIO blocked: [PASS/FAIL]
Data Integrity: [PASS/FAIL]
- No data loss: [PASS/FAIL]
- All STUDIO → ENTERPRISE: [PASS/FAIL]
- RLS policies working: [PASS/FAIL]
Code Consistency: [PASS/FAIL]
- No STUDIO in code: [PASS/FAIL]
- TypeScript types updated: [PASS/FAIL]
OVERALL: [PASS/FAIL]
Subscription Counts:
- Total: X
- FREE: X
- PRO: X
- ENTERPRISE: X
- STUDIO: 0 (expected)
Issues Found:
- [List any issues]
Recommendations:
- [List any recommendations]
```
## Success Criteria
ALL tests must PASS. Database must be in consistent state with no STUDIO references anywhere.
Begin verification now.