-
Notifications
You must be signed in to change notification settings - Fork 140
fix(backend): Correct SQL schema, fix enums, and update local setup #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c46146d
7b2cad6
1954310
2a85114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,5 @@ GROQ_API_KEY= | |
| SUPABASE_URL= | ||
| SUPABASE_KEY= | ||
| GEMINI_API_KEY= | ||
| YOUTUBE_API_KEY= | ||
| YOUTUBE_API_KEY= | ||
| SUPABASE_JWT_SECRET= | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| -- Enable UUID extension | ||
| CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
|
|
||
| -- Create Enum Types | ||
| DO $$ BEGIN | ||
| CREATE TYPE application_status AS ENUM ('pending', 'accepted', 'rejected'); | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$; | ||
|
|
||
| DO $$ BEGIN | ||
| CREATE TYPE invite_status AS ENUM ('pending', 'accepted', 'declined'); | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$; | ||
|
|
||
| DO $$ BEGIN | ||
| CREATE TYPE payment_status AS ENUM ('pending', 'completed', 'failed'); | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$; | ||
|
|
||
| DO $$ BEGIN | ||
| CREATE TYPE deal_status AS ENUM ('open', 'closed', 'in_progress'); | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$; | ||
|
|
||
| -- Create Users Table | ||
| CREATE TABLE IF NOT EXISTS public.users ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| username TEXT UNIQUE NOT NULL, | ||
| email TEXT UNIQUE NOT NULL, | ||
| role TEXT NOT NULL, -- 'creator' or 'brand' | ||
| profile_image TEXT, | ||
| bio TEXT, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), | ||
| is_online BOOLEAN DEFAULT FALSE, | ||
| last_seen TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create Audience Insights Table | ||
| CREATE TABLE IF NOT EXISTS public.audience_insights ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| audience_age_group JSONB, | ||
| audience_location JSONB, | ||
| engagement_rate FLOAT, | ||
| average_views INTEGER, | ||
| time_of_attention INTEGER, -- in seconds | ||
| price_expectation DECIMAL(10, 2), | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create Sponsorships Table | ||
| CREATE TABLE IF NOT EXISTS public.sponsorships ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| brand_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| title TEXT NOT NULL, | ||
| description TEXT NOT NULL, | ||
| required_audience JSONB, -- {"age": ["18-24"], "location": ["USA", "UK"]} | ||
| budget DECIMAL(10, 2), | ||
| engagement_minimum FLOAT, | ||
| status deal_status DEFAULT 'open', | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create User Posts Table | ||
| CREATE TABLE IF NOT EXISTS public.user_posts ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| title TEXT NOT NULL, | ||
| content TEXT NOT NULL, | ||
| post_url TEXT, | ||
| category TEXT, | ||
| engagement_metrics JSONB, -- {"likes": 500, "comments": 100, "shares": 50} | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create Sponsorship Applications Table | ||
| CREATE TABLE IF NOT EXISTS public.sponsorship_applications ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| creator_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| sponsorship_id UUID NOT NULL REFERENCES public.sponsorships(id) ON DELETE CASCADE, | ||
| post_id UUID REFERENCES public.user_posts(id) ON DELETE SET NULL, | ||
| proposal TEXT NOT NULL, | ||
| status application_status DEFAULT 'pending', | ||
| applied_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create Collaborations Table | ||
| CREATE TABLE IF NOT EXISTS public.collaborations ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| creator_1_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| creator_2_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| collaboration_details TEXT NOT NULL, | ||
| status invite_status DEFAULT 'pending', | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Create Sponsorship Payments Table | ||
| CREATE TABLE IF NOT EXISTS public.sponsorship_payments ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| creator_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| brand_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | ||
| sponsorship_id UUID NOT NULL REFERENCES public.sponsorships(id) ON DELETE CASCADE, | ||
| amount DECIMAL(10, 2) NOT NULL, | ||
| status payment_status DEFAULT 'pending', | ||
| transaction_date TIMESTAMP WITH TIME ZONE DEFAULT NOW() | ||
| ); | ||
|
|
||
| -- Grant Permissions | ||
| GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role; | ||
| GRANT ALL ON ALL TABLES IN SCHEMA public TO service_role; | ||
| GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO service_role; | ||
| GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO authenticated; | ||
| GRANT SELECT ON ALL TABLES IN SCHEMA public TO anon; | ||
|
Comment on lines
+112
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -name "schema.sql" -type fRepository: AOSSIE-Org/InPactAI Length of output: 83 🏁 Script executed: wc -l ./Backend/schema.sqlRepository: AOSSIE-Org/InPactAI Length of output: 87 🏁 Script executed: cat -n ./Backend/schema.sqlRepository: AOSSIE-Org/InPactAI Length of output: 5177 🏁 Script executed: rg -i "supabase" --type md --type json --type yaml --type txt --type sqlRepository: AOSSIE-Org/InPactAI Length of output: 3185 🏁 Script executed: find ./Backend -name "*.sql" -o -name "*migration*" -type fRepository: AOSSIE-Org/InPactAI Length of output: 83 🌐 Web query:
💡 Result: Short summary — ALTER DEFAULT PRIVILEGES (PostgreSQL)
References: 🌐 Web query:
💡 Result: Below are concise Supabase Row-Level Security (RLS) best practices for anon/authenticated roles and general security — with links to authoritative docs and guidance. High-level rules
Policy design and role usage
Performance & complexity
Role hardening & key management
Operational & auditing
Examples & helpers
Primary references
If you want, I can:
🌐 Web query:
💡 Result: Syntax (Postgres): Examples (Supabase default roles):
Notes for Supabase:
References: Add ALTER DEFAULT PRIVILEGES to ensure future tables inherit permissions, and enable RLS with policies for Supabase safety Two legitimate concerns with the current grant setup:
Recommended additions (at minimum): GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL TABLES IN SCHEMA public TO service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO authenticated;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO anon;
+
+-- Ensure future tables/sequences get consistent privileges
+ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO anon;
+ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO authenticated;
+ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO service_role;
+ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role;Additionally, add 🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Schema-qualify ENUM types + avoid silently masking incompatible existing types
Right now
CREATE TYPE application_status ...etc will create in whateversearch_pathresolves first, andduplicate_objecthides cases where the type exists but doesn’t match expected labels (you’ll only find out later at insert/cast time). Consider pinning topublicand/or asserting shape.(Repeat for
invite_status,payment_status,deal_status.)📝 Committable suggestion
🤖 Prompt for AI Agents