CREATE TABLE IF NOT EXISTS "sponsorship_tiers" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"maintainer_id" uuid NOT NULL,
"name" text NOT NULL,
"description" text NOT NULL DEFAULT '',
"monthly_cents" integer NOT NULL,
"one_time_allowed" boolean NOT NULL DEFAULT true,
"is_active" boolean NOT NULL DEFAULT true,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "sponsor_tiers_maintainer_fk" FOREIGN KEY ("maintainer_id") REFERENCES "users"("id") ON DELETE cascade
);
CREATE INDEX IF NOT EXISTS "sponsor_tiers_maintainer" ON "sponsorship_tiers" ("maintainer_id", "is_active");
CREATE TABLE IF NOT EXISTS "sponsorships" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"sponsor_id" uuid NOT NULL,
"maintainer_id" uuid NOT NULL,
"tier_id" uuid,
"amount_cents" integer NOT NULL,
"kind" text NOT NULL,
"note" text,
"is_public" boolean NOT NULL DEFAULT true,
"external_ref" text,
"cancelled_at" timestamp,
"created_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "sponsorships_sponsor_fk" FOREIGN KEY ("sponsor_id") REFERENCES "users"("id") ON DELETE cascade,
CONSTRAINT "sponsorships_maintainer_fk" FOREIGN KEY ("maintainer_id") REFERENCES "users"("id") ON DELETE cascade
);
CREATE INDEX IF NOT EXISTS "sponsorships_maintainer" ON "sponsorships" ("maintainer_id", "created_at");
CREATE INDEX IF NOT EXISTS "sponsorships_sponsor" ON "sponsorships" ("sponsor_id", "created_at");
|