Skip to content

Commit 4ee5d61

Browse files
Update supabase schemas and migrations
1 parent 80a89ce commit 4ee5d61

File tree

11 files changed

+2102
-430
lines changed

11 files changed

+2102
-430
lines changed

supabase/.gitignore

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
# testing
10+
/coverage
11+
12+
# demo files (temporary for testing)
13+
.env.local
14+
15+
# next.js
16+
/.next/
17+
/out/
18+
19+
.env
20+
21+
.mcp.json
22+
23+
.temp
24+
25+
# production
26+
/build
27+
28+
# misc
29+
.DS_Store
30+
*.pem
31+
32+
# debug
33+
npm-debug.log*
34+
yarn-debug.log*
35+
yarn-error.log*
36+
37+
# local env files
38+
.env*.local
39+
.env.local
40+
41+
# vercel
42+
.vercel
43+
44+
# typescript
45+
*.tsbuildinfo
46+
next-env.d.ts
47+
48+
49+
.env
50+
51+
scripts
52+
.vscode
53+
mcp.json

supabase/README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# CodingIT.dev Supabase Database Schema
2+
3+
This directory contains the complete database schema for CodingIT.dev, organized into logical files without duplicates.
4+
5+
## Schema Organization
6+
7+
### Main Schema Files (`/schemas/`)
8+
9+
1. **`extensions.sql`** - PostgreSQL extensions and system setup
10+
- Core extensions (uuid-ossp, vector, pg_trgm)
11+
- Supabase-specific extensions (pgjwt, supabase_vault)
12+
- Foreign data wrapper statistics table
13+
- **Load first** before any other schema files
14+
15+
2. **`sequences.sql`** - Database sequences and auto-increment management
16+
- All sequences used across schemas
17+
- Sequence utility functions and monitoring
18+
- **Load second** after extensions
19+
20+
3. **`auth.sql`** - Complete Supabase authentication schema
21+
- User management tables (users, sessions, identities)
22+
- Multi-factor authentication (MFA) support
23+
- SSO and SAML provider configurations
24+
- Row Level Security policies for auth tables
25+
- **Load third** after sequences
26+
27+
4. **`storage.sql`** - Complete Supabase storage schema
28+
- Bucket and object management
29+
- Multipart upload support
30+
- File organization and metadata
31+
- Storage policies and permissions
32+
- **Load fourth** after auth
33+
34+
5. **`public.sql`** - Main application schema
35+
- All application tables (projects, messages, fragments, etc.)
36+
- User profiles and preferences
37+
- Teams and organization management
38+
- Chat and messaging system
39+
- Usage tracking and analytics
40+
- Complete business logic functions
41+
- Comprehensive RLS policies
42+
- **Load last** after all other schemas
43+
44+
### Migration Files (`/migrations/`)
45+
46+
1. **`20250929060300_create_tasks_table.sql`** - Historical tasks table creation
47+
2. **`20250929163414_create_save_message_function.sql`** - Message handling function
48+
49+
## Loading Order
50+
51+
For a fresh database setup, load files in this order:
52+
53+
```sql
54+
-- 1. Load extensions first
55+
\i schemas/extensions.sql
56+
57+
-- 2. Load sequences
58+
\i schemas/sequences.sql
59+
60+
-- 3. Load auth schema
61+
\i schemas/auth.sql
62+
63+
-- 4. Load storage schema
64+
\i schemas/storage.sql
65+
66+
-- 5. Load public schema (includes all business logic)
67+
\i schemas/public.sql
68+
69+
-- 6. Apply any additional migrations
70+
\i migrations/20250929060300_create_tasks_table.sql
71+
\i migrations/20250929163414_create_save_message_function.sql
72+
```
73+
74+
## Key Features
75+
76+
### Security
77+
- Row Level Security (RLS) enabled on all user-facing tables
78+
- Comprehensive security policies
79+
- Multi-factor authentication support
80+
- API key management with permissions
81+
82+
### Performance
83+
- Optimized indexes for all major query patterns
84+
- Vector indexes for AI embeddings
85+
- Full-text search capabilities
86+
- Efficient foreign key relationships
87+
88+
### Scalability
89+
- Team-based multi-tenancy
90+
- Usage limits and tracking
91+
- Analytics and monitoring
92+
- Horizontal scaling considerations
93+
94+
### Business Logic
95+
- Complete user management workflow
96+
- Project and fragment organization
97+
- Chat message caching and search
98+
- File upload and storage management
99+
- Subscription and billing support
100+
101+
## Schema Statistics
102+
103+
- **Total Tables**: 40+ across all schemas
104+
- **Total Functions**: 15+ business logic functions
105+
- **Total Indexes**: 50+ performance-optimized indexes
106+
- **Total Policies**: 25+ RLS security policies
107+
108+
## Maintenance
109+
110+
### Duplicate Removal
111+
All duplicate schema files have been removed:
112+
- ✅ Removed: `auth.sql`, `public.sql`, `storage.sql`, `projects.sql`
113+
- ✅ Consolidated: Functions moved from `complete_database_functions.sql` to `public.sql`
114+
- ✅ Renamed: `*_complete.sql` files renamed to clean names
115+
116+
### Schema Validation
117+
All schemas include:
118+
- Proper constraints and checks
119+
- Comprehensive indexes
120+
- Security policies
121+
- Performance optimizations
122+
- Documentation and comments
123+
124+
## Development Notes
125+
126+
- Use `IF NOT EXISTS` clauses for safe re-running
127+
- All tables include proper audit timestamps
128+
- Foreign key constraints maintain referential integrity
129+
- Enum types used for better type safety
130+
- Vector support for AI/ML features included
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- Migration: Create tasks table for tracking user tasks and workflows
2+
-- Note: This migration is superseded by the full schema in public.sql
3+
-- Kept for historical reference and migration ordering
4+
5+
CREATE TABLE IF NOT EXISTS public.tasks (
6+
id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
7+
user_id uuid NOT NULL,
8+
status text NOT NULL DEFAULT 'pending'::text CHECK (status = ANY (ARRAY['pending'::text, 'processing'::text, 'completed'::text, 'error'::text])),
9+
progress integer NOT NULL DEFAULT 0 CHECK (progress >= 0 AND progress <= 100),
10+
prompt text NOT NULL,
11+
repo_url text,
12+
selected_agent text DEFAULT 'claude'::text,
13+
selected_model text,
14+
sandbox_url text,
15+
branch_name text,
16+
logs jsonb DEFAULT '[]'::jsonb,
17+
created_at timestamp with time zone NOT NULL DEFAULT now(),
18+
updated_at timestamp with time zone NOT NULL DEFAULT now(),
19+
CONSTRAINT tasks_pkey PRIMARY KEY (id),
20+
CONSTRAINT tasks_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE,
21+
CONSTRAINT tasks_prompt_check CHECK (char_length(prompt) >= 1)
22+
);
23+
24+
-- Create indexes for performance
25+
CREATE INDEX IF NOT EXISTS idx_tasks_user_id ON public.tasks (user_id);
26+
CREATE INDEX IF NOT EXISTS idx_tasks_status ON public.tasks (status);
27+
CREATE INDEX IF NOT EXISTS idx_tasks_created_at ON public.tasks (created_at DESC);
28+
29+
-- Enable RLS
30+
ALTER TABLE public.tasks ENABLE ROW LEVEL SECURITY;
31+
32+
-- Create RLS policy
33+
CREATE POLICY "Users can manage their own tasks" ON public.tasks
34+
FOR ALL USING (auth.uid() = user_id);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Function to save message and update project timestamp
2+
CREATE OR REPLACE FUNCTION public.save_message_and_update_project(
3+
project_id_param UUID,
4+
role_param TEXT,
5+
content_param JSONB,
6+
object_data_param JSONB DEFAULT NULL,
7+
result_data_param JSONB DEFAULT NULL,
8+
sequence_number_param INTEGER
9+
)
10+
RETURNS VOID
11+
LANGUAGE plpgsql
12+
SECURITY DEFINER
13+
AS $$
14+
BEGIN
15+
-- Insert or update the message (upsert to handle duplicates)
16+
INSERT INTO public.messages (
17+
project_id,
18+
role,
19+
content,
20+
object_data,
21+
result_data,
22+
sequence_number
23+
) VALUES (
24+
project_id_param,
25+
role_param,
26+
content_param,
27+
object_data_param,
28+
result_data_param,
29+
sequence_number_param
30+
)
31+
ON CONFLICT (project_id, sequence_number)
32+
DO UPDATE SET
33+
role = EXCLUDED.role,
34+
content = EXCLUDED.content,
35+
object_data = EXCLUDED.object_data,
36+
result_data = EXCLUDED.result_data;
37+
38+
-- Update the project's updated_at timestamp
39+
UPDATE public.projects
40+
SET updated_at = NOW()
41+
WHERE id = project_id_param;
42+
END;
43+
$$;
44+
45+
-- Grant execute permission to authenticated users
46+
GRANT EXECUTE ON FUNCTION public.save_message_and_update_project TO authenticated;

supabase/migrations/tasks.sql

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)