- Clean up analysis and documentation files - Remove temporary migration and testing files - Update component structure for social features - Prepare wallet system integration - Optimize performance and remove unused code - Ready for timeline posts and private messaging implementation
19 KiB
🏗️ DATABASE IMPROVEMENT PROPOSAL
Date: January 30, 2025 Status: PROPOSAL - Awaiting Approval Author: Senior Database Architect
📊 EXECUTIVE SUMMARY
After thorough examination of your database schema using Supabase CLI, I've identified opportunities for improvement based on your requirements:
Your Requirements:
- ✅ Add "infrastructure" and "events" categories
- ✅ Profile privacy settings (public, semi-private, private)
- ✅ Project privacy settings (same 3 levels)
- ✅ Event deadlines/dates for time-sensitive projects
- ✅ Search respects privacy settings
- ✅ Scalability improvements
- ✅ Performance optimization
- ✅ Best practices compliance
Current State Assessment:
- Local Database: Clean 3-table structure (profiles, campaigns, organizations)
- Production Database: Has "projects" table with 19 columns
- Issue: Schema mismatch between local and production 🚨
- Good News: Well-indexed, proper constraints, good RLS policies
🔍 DETAILED ANALYSIS
Table 1: Schema Mismatch (CRITICAL)
| Table | Local | Production | Status |
|---|---|---|---|
| profiles | ✅ | ✅ | MATCH |
| campaigns | ✅ | ❌ | Local only |
| projects | ❌ | ✅ | Production only |
| organizations | ✅ | ✅ | MATCH |
| funding_pages | ✅ | ? | Unknown |
| transactions | ✅ | ? | Unknown |
This is the same problem we fixed earlier - local and production are out of sync!
Table 2: Current Categories Support
funding_pages table:
categories text[] -- Array of text categories
organizations table:
category text -- Single category
tags text[] -- Multiple tags
Production projects table:
category text -- Single category
tags text[] -- Multiple tags
Issue: No standardized category enum or validation!
Table 3: Privacy Settings (MISSING)
Current State:
- ❌ No privacy_level column in profiles
- ❌ No privacy_level column in projects/campaigns/funding_pages
- ❌ RLS policies are all-or-nothing (public or member-only)
- ❌ Search doesn't filter by privacy
Organizations have it:
is_public boolean -- But only 2 levels, not 3
Table 4: Event/Deadline Support (PARTIAL)
organization_proposals table (only place with deadlines):
voting_deadline timestamp with time zone
Missing from:
- ❌ campaigns
- ❌ funding_pages
- ❌ projects (production)
Table 5: Performance Analysis
Good:
- ✅ Primary keys on all tables
- ✅ Foreign key indexes exist
- ✅ Status columns indexed
- ✅ Created_at indexed for time-series queries
- ✅ Composite unique constraints where needed
Missing Indexes:
-- No index on categories arrays (GIN index needed for containment queries)
-- No index on privacy_level (once added)
-- No composite index on (status, created_at) for filtered pagination
🎯 PROPOSED IMPROVEMENTS
Phase 1: Schema Unification & Privacy (MUST DO FIRST)
1.1: Create Unified "projects" Table
Decision needed: Merge campaigns, funding_pages, and production projects into ONE table.
Benefits:
- Single source of truth
- Easier to maintain
- Better performance
- Simpler code
Proposed Schema:
CREATE TABLE projects (
-- Identity
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
organization_id UUID REFERENCES organizations(id) ON DELETE SET NULL,
-- Basic Info
title TEXT NOT NULL,
slug TEXT UNIQUE NOT NULL, -- For SEO-friendly URLs
description TEXT,
-- Media
cover_image_url TEXT,
banner_url TEXT,
media_urls TEXT[], -- Array of additional images/videos
-- Categories (IMPROVED)
project_type TEXT NOT NULL CHECK (project_type IN (
'fundraising', -- General fundraising
'event', -- Time-bound event
'infrastructure', -- NEW: Infrastructure projects
'grant', -- Grant applications
'bounty', -- Bug bounties, tasks
'scholarship', -- Educational funding
'emergency' -- Urgent needs
)),
categories TEXT[] NOT NULL DEFAULT '{}', -- Multiple categories
tags TEXT[] DEFAULT '{}',
-- Funding
goal_amount NUMERIC(20,8),
raised_amount NUMERIC(20,8) DEFAULT 0,
contributor_count INTEGER DEFAULT 0,
currency TEXT DEFAULT 'BTC' CHECK (currency IN ('BTC', 'sats', 'CHF', 'USD', 'EUR')),
-- Payment Addresses
bitcoin_address TEXT,
lightning_address TEXT,
website_url TEXT,
-- Event/Deadline Support (NEW)
event_date TIMESTAMPTZ, -- For events: when does it happen?
deadline TIMESTAMPTZ, -- For fundraising: when does funding close?
duration_days INTEGER, -- How long is the event/campaign?
-- Privacy (NEW - YOUR REQUEST)
privacy_level TEXT NOT NULL DEFAULT 'public' CHECK (privacy_level IN (
'public', -- Anyone can see
'semi_private', -- Only logged-in OrangeCat users
'private' -- Only owner/collaborators
)),
-- Status
status TEXT DEFAULT 'draft' CHECK (status IN (
'draft', -- Being created
'active', -- Published and accepting contributions
'paused', -- Temporarily stopped
'completed', -- Goal reached or event finished
'cancelled', -- Cancelled by owner
'expired' -- Deadline passed
)),
-- Metadata
metadata JSONB DEFAULT '{}',
settings JSONB DEFAULT '{}',
-- Timestamps
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
published_at TIMESTAMPTZ, -- When it went live
completed_at TIMESTAMPTZ -- When it reached goal/ended
);
1.2: Add Privacy to Profiles
ALTER TABLE profiles ADD COLUMN privacy_level TEXT NOT NULL DEFAULT 'public'
CHECK (privacy_level IN ('public', 'semi_private', 'private'));
ALTER TABLE profiles ADD COLUMN is_searchable BOOLEAN DEFAULT true;
ALTER TABLE profiles ADD COLUMN show_on_discover BOOLEAN DEFAULT true;
1.3: Update Organizations Privacy
-- Change from boolean to 3-level
ALTER TABLE organizations DROP COLUMN is_public;
ALTER TABLE organizations ADD COLUMN privacy_level TEXT NOT NULL DEFAULT 'public'
CHECK (privacy_level IN ('public', 'semi_private', 'private'));
Phase 2: Category System Standardization
2.1: Create Categories Enum Table (Best Practice)
CREATE TABLE project_categories (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
icon TEXT, -- Icon name or emoji
parent_id TEXT REFERENCES project_categories(id), -- For hierarchies
display_order INTEGER DEFAULT 0,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Seed with your categories
INSERT INTO project_categories (id, name, description, icon) VALUES
('infrastructure', 'Infrastructure', 'Physical and digital infrastructure projects', '🏗️'),
('events', 'Events', 'Conferences, meetups, and community events', '📅'),
('education', 'Education', 'Learning resources and courses', '📚'),
('development', 'Development', 'Software and product development', '💻'),
('community', 'Community', 'Community building and engagement', '🤝'),
('research', 'Research', 'Research and innovation projects', '🔬'),
('art', 'Art & Culture', 'Creative and cultural projects', '🎨'),
('health', 'Health & Wellness', 'Health and wellness initiatives', '🏥'),
('environment', 'Environment', 'Environmental and sustainability projects', '🌍'),
('emergency', 'Emergency', 'Urgent assistance and disaster relief', '🚨');
2.2: Add Category Validation
-- Add foreign key constraint to ensure valid categories
CREATE TABLE project_category_mappings (
project_id UUID REFERENCES projects(id) ON DELETE CASCADE,
category_id TEXT REFERENCES project_categories(id) ON DELETE CASCADE,
PRIMARY KEY (project_id, category_id)
);
-- Index for fast category filtering
CREATE INDEX idx_project_categories_project ON project_category_mappings(project_id);
CREATE INDEX idx_project_categories_category ON project_category_mappings(category_id);
Phase 3: Performance Optimization
3.1: Add Missing Indexes
-- Projects table indexes
CREATE INDEX idx_projects_user_id ON projects(user_id);
CREATE INDEX idx_projects_organization_id ON projects(organization_id) WHERE organization_id IS NOT NULL;
CREATE INDEX idx_projects_project_type ON projects(project_type);
CREATE INDEX idx_projects_privacy_level ON projects(privacy_level);
CREATE INDEX idx_projects_status ON projects(status) WHERE status IN ('active', 'draft');
-- Composite index for filtered pagination (HUGE performance boost)
CREATE INDEX idx_projects_status_created ON projects(status, created_at DESC)
WHERE status = 'active';
-- GIN index for array searches
CREATE INDEX idx_projects_categories_gin ON projects USING GIN(categories);
CREATE INDEX idx_projects_tags_gin ON projects USING GIN(tags);
-- Event/deadline queries
CREATE INDEX idx_projects_event_date ON projects(event_date)
WHERE event_date IS NOT NULL AND project_type = 'event';
CREATE INDEX idx_projects_deadline ON projects(deadline)
WHERE deadline IS NOT NULL;
-- Full-text search index (for search functionality)
CREATE INDEX idx_projects_search ON projects USING GIN(
to_tsvector('english',
COALESCE(title, '') || ' ' ||
COALESCE(description, '')
)
);
-- Profiles indexes
CREATE INDEX idx_profiles_privacy_level ON profiles(privacy_level);
CREATE INDEX idx_profiles_is_searchable ON profiles(is_searchable) WHERE is_searchable = true;
CREATE INDEX idx_profiles_search ON profiles USING GIN(
to_tsvector('english',
COALESCE(name, '') || ' ' ||
COALESCE(username, '') || ' ' ||
COALESCE(bio, '')
)
);
3.2: Materialized View for Popular Projects
CREATE MATERIALIZED VIEW popular_projects AS
SELECT
p.*,
COUNT(DISTINCT t.user_id) as unique_contributors,
SUM(t.amount) as total_raised_calculated,
GREATEST(
0,
50 * (COUNT(DISTINCT t.user_id)) +
20 * (EXTRACT(DAY FROM NOW() - p.created_at)) +
30 * (p.raised_amount / NULLIF(p.goal_amount, 0))
) as popularity_score
FROM projects p
LEFT JOIN transactions t ON t.project_id = p.id AND t.status = 'completed'
WHERE p.status = 'active' AND p.privacy_level = 'public'
GROUP BY p.id
ORDER BY popularity_score DESC;
-- Index on materialized view
CREATE INDEX idx_popular_projects_score ON popular_projects(popularity_score DESC);
-- Refresh strategy (run hourly via cron or trigger)
-- REFRESH MATERIALIZED VIEW CONCURRENTLY popular_projects;
Phase 4: Privacy-Aware RLS Policies
4.1: Projects Privacy RLS
-- Drop old policies
DROP POLICY IF EXISTS "Projects are viewable by everyone" ON projects;
-- New privacy-aware policy
CREATE POLICY "projects_select_by_privacy" ON projects FOR SELECT USING (
CASE privacy_level
WHEN 'public' THEN true
WHEN 'semi_private' THEN auth.uid() IS NOT NULL -- Any logged-in user
WHEN 'private' THEN
auth.uid() = user_id OR -- Owner
EXISTS ( -- Or collaborator
SELECT 1 FROM project_collaborators pc
WHERE pc.project_id = projects.id AND pc.user_id = auth.uid()
)
ELSE false
END
);
CREATE POLICY "projects_insert_own" ON projects FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "projects_update_own" ON projects FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "projects_delete_own" ON projects FOR DELETE
USING (auth.uid() = user_id);
4.2: Profiles Privacy RLS
DROP POLICY IF EXISTS "Public profiles are viewable by everyone" ON profiles;
DROP POLICY IF EXISTS "profiles_select_public" ON profiles;
CREATE POLICY "profiles_select_by_privacy" ON profiles FOR SELECT USING (
CASE privacy_level
WHEN 'public' THEN true
WHEN 'semi_private' THEN auth.uid() IS NOT NULL
WHEN 'private' THEN auth.uid() = id
ELSE false
END
);
-- Keep existing insert/update policies
Phase 5: Search Improvements
5.1: Privacy-Aware Search Function
CREATE OR REPLACE FUNCTION search_projects(
search_query TEXT,
filter_category TEXT DEFAULT NULL,
filter_type TEXT DEFAULT NULL,
requesting_user_id UUID DEFAULT auth.uid()
) RETURNS TABLE (
id UUID,
title TEXT,
description TEXT,
project_type TEXT,
privacy_level TEXT,
rank REAL
) AS $$
BEGIN
RETURN QUERY
SELECT
p.id,
p.title,
p.description,
p.project_type,
p.privacy_level,
ts_rank(
to_tsvector('english', p.title || ' ' || COALESCE(p.description, '')),
plainto_tsquery('english', search_query)
) as rank
FROM projects p
WHERE
-- Privacy check
(
(p.privacy_level = 'public') OR
(p.privacy_level = 'semi_private' AND requesting_user_id IS NOT NULL) OR
(p.privacy_level = 'private' AND p.user_id = requesting_user_id)
)
-- Status check
AND p.status = 'active'
-- Search match
AND to_tsvector('english', p.title || ' ' || COALESCE(p.description, ''))
@@ plainto_tsquery('english', search_query)
-- Optional filters
AND (filter_category IS NULL OR filter_category = ANY(p.categories))
AND (filter_type IS NULL OR p.project_type = filter_type)
ORDER BY rank DESC, p.created_at DESC
LIMIT 100;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
Phase 6: Event-Specific Features
6.1: Event Helper Functions
-- Check if event is upcoming
CREATE OR REPLACE FUNCTION is_upcoming_event(project projects)
RETURNS BOOLEAN AS $$
BEGIN
RETURN project.project_type = 'event'
AND project.event_date IS NOT NULL
AND project.event_date > NOW();
END;
$$ LANGUAGE plpgsql IMMUTABLE;
-- Auto-expire passed events (run via cron)
CREATE OR REPLACE FUNCTION expire_past_events()
RETURNS INTEGER AS $$
DECLARE
updated_count INTEGER;
BEGIN
UPDATE projects
SET status = 'expired', updated_at = NOW()
WHERE project_type = 'event'
AND event_date < NOW()
AND status = 'active'
RETURNING COUNT(*) INTO updated_count;
RETURN updated_count;
END;
$$ LANGUAGE plpgsql;
Phase 7: Data Migration Strategy
7.1: Safe Migration Plan
-- Step 1: Create new projects table with all features
-- (Already defined above)
-- Step 2: Migrate data from old tables
INSERT INTO projects (
id, user_id, title, description, goal_amount, raised_amount,
bitcoin_address, status, created_at, updated_at, project_type,
privacy_level
)
SELECT
id, user_id, title, description, goal_amount, raised_amount,
bitcoin_address, status, created_at, updated_at,
'fundraising' as project_type, -- Default type
'public' as privacy_level -- Default privacy
FROM campaigns
WHERE NOT EXISTS (SELECT 1 FROM projects WHERE projects.id = campaigns.id);
-- Similar for funding_pages if they exist
-- Step 3: Update foreign keys in other tables
ALTER TABLE transactions
ADD COLUMN project_id UUID REFERENCES projects(id);
UPDATE transactions t
SET project_id = t.funding_page_id
WHERE t.funding_page_id IS NOT NULL;
-- Step 4: Drop old tables (after verification)
-- DROP TABLE campaigns CASCADE;
-- DROP TABLE funding_pages CASCADE;
📈 PERFORMANCE IMPACT ANALYSIS
Before Improvements:
SELECT FROM projects WHERE status = 'active'
└─> Seq Scan (cost: high, time: ~500ms for 10k rows)
SELECT FROM projects WHERE 'events' = ANY(categories)
└─> Seq Scan (cost: very high, array scan per row)
Search query with privacy check
└─> Multiple full table scans, no optimization
After Improvements:
SELECT FROM projects WHERE status = 'active'
└─> Index Scan on idx_projects_status (cost: low, time: ~5ms)
SELECT FROM projects WHERE 'events' = ANY(categories)
└─> Bitmap Index Scan on idx_projects_categories_gin (cost: medium, time: ~10ms)
Search with privacy
└─> Index Scan on idx_projects_search + idx_projects_privacy_level (cost: low)
Expected Speedup: 50-100x faster for filtered queries
🎨 CATEGORY HIERARCHY EXAMPLE
Infrastructure 🏗️
├── Physical Infrastructure
│ ├── Bitcoin Mining Facilities
│ ├── Data Centers
│ └── Co-working Spaces
└── Digital Infrastructure
├── Lightning Nodes
├── Bitcoin Core Nodes
└── Development Tools
Events 📅
├── Conferences
├── Meetups
├── Hackathons
└── Workshops
(etc for other categories)
✅ RECOMMENDED ACTION PLAN
Immediate (Phase 1 - Days 1-2):
- ✅ Add privacy_level to profiles
- ✅ Add privacy_level to projects/organizations
- ✅ Update RLS policies for privacy
- ✅ Add event_date and deadline columns
Short-term (Phase 2-3 - Days 3-5):
- ✅ Create project_categories table
- ✅ Migrate existing categories
- ✅ Add all missing indexes
- ✅ Create materialized view
Medium-term (Phase 4-5 - Days 6-10):
- ✅ Unify campaigns/funding_pages into projects
- ✅ Migrate all data safely
- ✅ Update search functions
- ✅ Test privacy filtering
Long-term (Phase 6-7 - Weeks 2-4):
- ✅ Add full-text search with ranking
- ✅ Implement event auto-expiration
- ✅ Add analytics tables
- ✅ Performance monitoring
🚨 RISKS & MITIGATION
Risk 1: Schema mismatch between local/production
Severity: HIGH Mitigation:
- Run schema diff first
- Test migration on local copy of production
- Keep old tables until verified
Risk 2: Breaking existing code
Severity: MEDIUM Mitigation:
- Create database views for backwards compatibility
- Update TypeScript types incrementally
- Feature flag new privacy features
Risk 3: Performance degradation during migration
Severity: LOW Mitigation:
- Run migrations during low-traffic hours
- Use CONCURRENTLY for index creation
- Monitor query performance
💬 QUESTIONS FOR YOU
Before I proceed, I need to know:
-
Table unification: Should I merge campaigns + funding_pages + projects into ONE "projects" table? (Recommended: YES)
-
Privacy rollout: Should new projects default to 'public' or 'semi_private'?
-
Event categories: Do you want event subtypes (conference, meetup, hackathon, etc.)? Or just "events"?
-
Search: Should private profiles appear in search at all (with "Profile is private" message)? Or completely hidden?
-
Migration timing: Can you afford 10-30 minutes of downtime for the big migration? Or need zero-downtime?
-
Categories: Do you want hierarchical categories (Infrastructure > Physical > Mining)? Or flat list?
📊 ESTIMATED IMPACT
Database Size: +10% (new indexes and privacy columns) Query Performance: 50-100x faster for filtered searches Code Changes: ~30 files need updates Migration Time: 2-4 hours total work Downtime Required: 10-30 minutes (can be reduced to 0 with blue-green deployment)
Ready to proceed? Let me know which phases you want me to implement!