Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 340 additions & 7 deletions backend/SQL
Original file line number Diff line number Diff line change
@@ -1,8 +1,341 @@
-- Table for user profiles
create table if not exists profiles (
id uuid references auth.users(id) on delete cascade,
name text not null,
role text check (role in ('Creator', 'Brand')) not null,
created_at timestamp with time zone default timezone('utc', now()),
primary key (id)
-- Define custom ENUM types (required by several tables below)
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'application_status') THEN
CREATE TYPE application_status AS ENUM ('applied', 'reviewing', 'accepted', 'rejected');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'invite_status') THEN
CREATE TYPE invite_status AS ENUM ('pending', 'accepted', 'declined', 'expired');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'payment_status') THEN
CREATE TYPE payment_status AS ENUM ('pending', 'processing', 'completed', 'failed', 'refunded');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'deal_status') THEN
CREATE TYPE deal_status AS ENUM ('draft', 'proposed', 'negotiating', 'active', 'completed', 'cancelled');
END IF;
END $$;


-- This file is no longer maintained here.
-- For the latest schema reference and documentation, see: docs/database/schema-reference.md

CREATE TABLE public.brands (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL UNIQUE,
company_name text NOT NULL,
company_tagline text,
company_description text,
company_logo_url text,
company_cover_image_url text,
industry text NOT NULL,
sub_industry text[] DEFAULT ARRAY[]::text[],
company_size text,
founded_year integer,
headquarters_location text,
company_type text,
website_url text NOT NULL,
contact_email text,
contact_phone text,
social_media_links jsonb,
target_audience_age_groups text[] DEFAULT ARRAY[]::text[],
target_audience_gender text[] DEFAULT ARRAY[]::text[],
target_audience_locations text[] DEFAULT ARRAY[]::text[],
target_audience_interests text[] DEFAULT ARRAY[]::text[],
target_audience_income_level text[] DEFAULT ARRAY[]::text[],
target_audience_description text,
brand_values text[] DEFAULT ARRAY[]::text[],
brand_personality text[] DEFAULT ARRAY[]::text[],
brand_voice text,
brand_colors jsonb,
marketing_goals text[] DEFAULT ARRAY[]::text[],
campaign_types_interested text[] DEFAULT ARRAY[]::text[],
preferred_content_types text[] DEFAULT ARRAY[]::text[],
preferred_platforms text[] DEFAULT ARRAY[]::text[],
campaign_frequency text,
monthly_marketing_budget numeric,
influencer_budget_percentage double precision,
budget_per_campaign_min numeric,
budget_per_campaign_max numeric,
typical_deal_size numeric,
payment_terms text,
offers_product_only_deals boolean DEFAULT false,
offers_affiliate_programs boolean DEFAULT false,
affiliate_commission_rate double precision,
preferred_creator_niches text[] DEFAULT ARRAY[]::text[],
preferred_creator_size text[] DEFAULT ARRAY[]::text[],
preferred_creator_locations text[] DEFAULT ARRAY[]::text[],
minimum_followers_required integer,
minimum_engagement_rate double precision,
content_dos text[] DEFAULT ARRAY[]::text[],
content_donts text[] DEFAULT ARRAY[]::text[],
brand_safety_requirements text[] DEFAULT ARRAY[]::text[],
competitor_brands text[] DEFAULT ARRAY[]::text[],
exclusivity_required boolean DEFAULT false,
exclusivity_duration_months integer,
past_campaigns_count integer DEFAULT 0,
successful_partnerships text[] DEFAULT ARRAY[]::text[],
case_studies text[] DEFAULT ARRAY[]::text[],
average_campaign_roi double precision,
products_services text[] DEFAULT ARRAY[]::text[],
product_price_range text,
product_categories text[] DEFAULT ARRAY[]::text[],
seasonal_products boolean DEFAULT false,
product_catalog_url text,
business_verified boolean DEFAULT false,
payment_verified boolean DEFAULT false,
tax_id_verified boolean DEFAULT false,
profile_completion_percentage integer DEFAULT 0,
is_active boolean DEFAULT true,
is_featured boolean DEFAULT false,
is_verified_brand boolean DEFAULT false,
subscription_tier text DEFAULT 'free'::text,
featured_until timestamp with time zone,
ai_profile_summary text,
search_keywords text[] DEFAULT ARRAY[]::text[],
matching_score_base double precision DEFAULT 50.0,
total_deals_posted integer DEFAULT 0,
total_deals_completed integer DEFAULT 0,
total_spent numeric DEFAULT 0,
average_deal_rating double precision,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
last_active_at timestamp with time zone DEFAULT now(),
CONSTRAINT brands_pkey PRIMARY KEY (id),
CONSTRAINT brands_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.campaign_applications (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
campaign_id uuid NOT NULL,
creator_id uuid NOT NULL,
profile_snapshot jsonb DEFAULT '{}'::jsonb,
message text,
proposed_amount numeric,
attachments jsonb DEFAULT '[]'::jsonb,
status USER-DEFINED DEFAULT 'applied'::application_status,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT campaign_applications_pkey PRIMARY KEY (id),
CONSTRAINT campaign_applications_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id),
CONSTRAINT campaign_applications_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.creators(id)
);
Comment on lines +105 to +119
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | πŸ”΄ Critical

Reorder table definitions to satisfy foreign key dependencies.

Several tables reference foreign keys to tables that are defined later in the script:

  • campaign_applications (line 98) references campaigns (defined at line 187) and creators (defined at line 211)
  • campaign_assets (line 113) references campaigns and deals (defined at line 292)
  • campaign_deliverables (line 127) references campaigns
  • campaign_invites (line 139) references campaigns, brands, and creators
  • campaign_payments (line 155) references deals
  • campaign_performance (line 169) references campaigns and deals

This ordering will cause foreign key constraint errors during execution.

Reorder the tables so that referenced tables are created before tables that reference them:

  1. profiles
  2. brands (references profiles)
  3. creators (references profiles)
  4. campaigns (references brands)
  5. campaign_applications (references campaigns, creators)
  6. campaign_deliverables (references campaigns)
  7. campaign_invites (references campaigns, brands, creators)
  8. deals (references campaigns, campaign_applications, brands, creators)
  9. campaign_assets (references campaigns, deals)
  10. campaign_payments (references deals)
  11. campaign_performance (references campaigns, deals)
  12. match_scores (references creators, brands)

Also applies to: 113-126, 127-138, 139-154, 155-168, 169-186

πŸ€– Prompt for AI Agents
In backend/SQL around lines 98-112 (and similarly for the blocks at 113-126,
127-138, 139-154, 155-168, 169-186), the CREATE TABLE statements are ordered so
tables with foreign keys reference tables that are defined later, causing FK
constraint errors; reorder the script so referenced tables are created before
their dependents following the sequence: profiles, brands, creators, campaigns,
campaign_applications, campaign_deliverables, campaign_invites, deals,
campaign_assets, campaign_payments, campaign_performance, match_scores; move
each CREATE TABLE block to that sequence and ensure any FK constraints reference
existing tables (or convert to ALTER TABLE ... ADD CONSTRAINT after target table
creation if reordering is infeasible).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL schema in SQL is not meant to be executed directly, and the file now only contains a pointer to the new documentation in schema-reference.md The actual schema reference is now in Markdown format for documentation purposes, not for execution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Saahi30, thank you for the clarification! I understand now that the SQL file is purely for documentation and reference purposes, not for execution. The warning comment in the file makes this clear.

Since the schema is not meant to be run directly and serves as a reference pointing to the proper documentation in schema-reference.md, the table ordering concern I raised is not applicable. I appreciate you taking the time to explain the file's purpose.


✏️ Learnings added
Learnt from: Saahi30
Repo: AOSSIE-Org/InPactAI PR: 145
File: backend/SQL:98-112
Timestamp: 2025-11-08T12:24:11.575Z
Learning: When a SQL file in the backend/SQL directory contains a warning comment indicating it is "for context only" or "not meant to be run," do not flag table ordering or foreign key dependency issues, as the file is for documentation purposes only, not execution.

Learnt from: Saahi30
Repo: AOSSIE-Org/InPactAI PR: 145
File: backend/SQL:98-112
Timestamp: 2025-11-08T12:24:11.575Z
Learning: In the InPactAI repository, the backend/SQL file is a documentation reference that points to schema-reference.md and is not meant to be executed directly. Table ordering and FK constraint issues should not be flagged for this file.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

CREATE TABLE public.campaign_assets (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
campaign_id uuid,
deal_id uuid,
uploaded_by uuid,
url text NOT NULL,
type text,
meta jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT campaign_assets_pkey PRIMARY KEY (id),
CONSTRAINT campaign_assets_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id),
CONSTRAINT campaign_assets_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id),
CONSTRAINT campaign_assets_uploaded_by_fkey FOREIGN KEY (uploaded_by) REFERENCES public.profiles(id)
);
CREATE TABLE public.campaign_deliverables (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
campaign_id uuid NOT NULL,
platform text,
content_type text,
quantity integer DEFAULT 1,
guidance text,
required boolean DEFAULT true,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT campaign_deliverables_pkey PRIMARY KEY (id),
CONSTRAINT campaign_deliverables_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id)
);
CREATE TABLE public.campaign_invites (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
campaign_id uuid NOT NULL,
brand_id uuid NOT NULL,
creator_id uuid NOT NULL,
message text,
proposed_amount numeric,
status USER-DEFINED DEFAULT 'pending'::invite_status,
sent_at timestamp with time zone,
responded_at timestamp with time zone,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT campaign_invites_pkey PRIMARY KEY (id),
CONSTRAINT campaign_invites_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id),
CONSTRAINT campaign_invites_brand_id_fkey FOREIGN KEY (brand_id) REFERENCES public.brands(id),
CONSTRAINT campaign_invites_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.creators(id)
);
CREATE TABLE public.campaign_payments (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
deal_id uuid NOT NULL,
amount numeric NOT NULL,
currency text DEFAULT 'INR'::text,
method text,
status USER-DEFINED DEFAULT 'pending'::payment_status,
external_payment_ref text,
metadata jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT now(),
paid_at timestamp with time zone,
CONSTRAINT campaign_payments_pkey PRIMARY KEY (id),
CONSTRAINT campaign_payments_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id)
);
CREATE TABLE public.campaign_performance (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
campaign_id uuid NOT NULL,
deal_id uuid,
report_source text,
recorded_at timestamp with time zone NOT NULL DEFAULT now(),
impressions bigint,
clicks bigint,
views bigint,
watch_time bigint,
engagements bigint,
conversions bigint,
revenue numeric,
raw jsonb DEFAULT '{}'::jsonb,
CONSTRAINT campaign_performance_pkey PRIMARY KEY (id),
CONSTRAINT campaign_performance_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id),
CONSTRAINT campaign_performance_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id)
);
CREATE TABLE public.campaigns (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
brand_id uuid NOT NULL,
title text NOT NULL,
slug text UNIQUE,
short_description text,
description text,
status text NOT NULL DEFAULT 'draft'::text,
platforms text[] DEFAULT ARRAY[]::text[],
deliverables jsonb DEFAULT '[]'::jsonb,
target_audience jsonb DEFAULT '{}'::jsonb,
budget_min numeric,
budget_max numeric,
preferred_creator_niches text[] DEFAULT ARRAY[]::text[],
preferred_creator_followers_range text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
published_at timestamp with time zone,
starts_at timestamp with time zone,
ends_at timestamp with time zone,
is_featured boolean DEFAULT false,
CONSTRAINT campaigns_pkey PRIMARY KEY (id),
CONSTRAINT campaigns_brand_id_fkey FOREIGN KEY (brand_id) REFERENCES public.brands(id)
);
CREATE TABLE public.creators (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
user_id uuid NOT NULL UNIQUE,
display_name text NOT NULL,
bio text,
tagline text,
profile_picture_url text,
cover_image_url text,
website_url text,
youtube_url text,
youtube_handle text,
youtube_subscribers integer,
instagram_url text,
instagram_handle text,
instagram_followers integer,
tiktok_url text,
tiktok_handle text,
tiktok_followers integer,
twitter_url text,
twitter_handle text,
twitter_followers integer,
twitch_url text,
twitch_handle text,
twitch_followers integer,
linkedin_url text,
facebook_url text,
primary_niche text NOT NULL,
secondary_niches text[] DEFAULT ARRAY[]::text[],
content_types text[] DEFAULT ARRAY[]::text[],
content_language text[] DEFAULT ARRAY[]::text[],
total_followers integer DEFAULT 0,
total_reach integer,
average_views integer,
engagement_rate double precision,
audience_age_primary text,
audience_age_secondary text[] DEFAULT ARRAY[]::text[],
audience_gender_split jsonb,
audience_locations jsonb,
audience_interests text[] DEFAULT ARRAY[]::text[],
average_engagement_per_post integer,
posting_frequency text,
best_performing_content_type text,
peak_posting_times jsonb,
years_of_experience integer,
content_creation_full_time boolean DEFAULT false,
team_size integer DEFAULT 1,
equipment_quality text,
editing_software text[] DEFAULT ARRAY[]::text[],
collaboration_types text[] DEFAULT ARRAY[]::text[],
preferred_brands_style text[] DEFAULT ARRAY[]::text[],
not_interested_in text[] DEFAULT ARRAY[]::text[],
rate_per_post numeric,
rate_per_video numeric,
rate_per_story numeric,
rate_per_reel numeric,
rate_negotiable boolean DEFAULT true,
accepts_product_only_deals boolean DEFAULT false,
minimum_deal_value numeric,
preferred_payment_terms text,
portfolio_links text[] DEFAULT ARRAY[]::text[],
past_brand_collaborations text[] DEFAULT ARRAY[]::text[],
case_study_links text[] DEFAULT ARRAY[]::text[],
media_kit_url text,
email_verified boolean DEFAULT false,
phone_verified boolean DEFAULT false,
identity_verified boolean DEFAULT false,
profile_completion_percentage integer DEFAULT 0,
is_active boolean DEFAULT true,
is_featured boolean DEFAULT false,
is_verified_creator boolean DEFAULT false,
featured_until timestamp with time zone,
ai_profile_summary text,
search_keywords text[] DEFAULT ARRAY[]::text[],
matching_score_base double precision DEFAULT 50.0,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
last_active_at timestamp with time zone DEFAULT now(),
social_platforms jsonb,
CONSTRAINT creators_pkey PRIMARY KEY (id),
CONSTRAINT creators_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.deals (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
campaign_id uuid,
application_id uuid,
brand_id uuid NOT NULL,
creator_id uuid NOT NULL,
agreed_amount numeric,
payment_schedule jsonb DEFAULT '[]'::jsonb,
terms jsonb DEFAULT '{}'::jsonb,
status USER-DEFINED DEFAULT 'draft'::deal_status,
starts_at timestamp with time zone,
ends_at timestamp with time zone,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT deals_pkey PRIMARY KEY (id),
CONSTRAINT deals_campaign_id_fkey FOREIGN KEY (campaign_id) REFERENCES public.campaigns(id),
CONSTRAINT deals_application_id_fkey FOREIGN KEY (application_id) REFERENCES public.campaign_applications(id),
CONSTRAINT deals_brand_id_fkey FOREIGN KEY (brand_id) REFERENCES public.brands(id),
CONSTRAINT deals_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.creators(id)
);
CREATE TABLE public.match_scores (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
creator_id uuid NOT NULL,
brand_id uuid NOT NULL,
score double precision NOT NULL,
reasons jsonb DEFAULT '[]'::jsonb,
computed_at timestamp with time zone DEFAULT now(),
CONSTRAINT match_scores_pkey PRIMARY KEY (id),
CONSTRAINT match_scores_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.creators(id),
CONSTRAINT match_scores_brand_id_fkey FOREIGN KEY (brand_id) REFERENCES public.brands(id)
);
CREATE TABLE public.profiles (
id uuid NOT NULL,
name text NOT NULL,
role text NOT NULL CHECK (role = ANY (ARRAY['Creator'::text, 'Brand'::text])),
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()),
onboarding_completed boolean DEFAULT false,
CONSTRAINT profiles_pkey PRIMARY KEY (id),
CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id)
);



Loading