Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(db): recover the prompt_suggestions table definition (#711)

Gökhan Geyik59bdc5815ec5
diff --git a/supabase/migrations/00048_gsc_suggestion_support.sql b/supabase/migrations/00048_gsc_suggestion_support.sql
index 9af64ed4d..ec48165f3 100644
--- a/supabase/migrations/00048_gsc_suggestion_support.sql
+++ b/supabase/migrations/00048_gsc_suggestion_support.sql
@@ -9,6 +9,96 @@
-- repeat suggestion refreshes cost zero new DataForSEO calls. Service
-- role only (RLS enabled, no policies).
+-- ── Recovered table definition (#709) ────────────────────────────────────────
+--
+-- prompt_suggestions was created directly against the hosted database and
+-- never written back into a migration, so the ALTERs below had nothing to
+-- alter on a clean one: `supabase db reset` failed here with 42P01, and
+-- schema.sql inherited the same gap. Every documented fresh-install path was
+-- broken from the moment this migration shipped.
+--
+-- The definition belongs *here* rather than in a later numbered file, because
+-- the very next statement alters this table — a migration appended at the end
+-- would never be reached. Editing a shipped migration is normally off-limits;
+-- this one is safe because every clause is idempotent and any database that
+-- already ran 00048 will not run it again.
+--
+-- Deliberately the table as it was BEFORE the ALTERs below: no source_data
+-- column, and the two-value source check. Transcribing a live database's
+-- current shape instead would leave a fresh install failing one line later on
+-- "column source_data already exists".
+CREATE TABLE IF NOT EXISTS "public"."prompt_suggestions" (
+ "id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
+ "brand_id" "uuid" NOT NULL,
+ "suggested_text" "text" NOT NULL,
+ "topic_name" "text",
+ "topic_id" "uuid",
+ "reason" "text",
+ "est_volume" integer,
+ "source" "text" DEFAULT 'llm'::"text" NOT NULL,
+ "status" "text" DEFAULT 'new'::"text" NOT NULL,
+ "added_prompt_id" "uuid",
+ "generated_at" timestamp with time zone DEFAULT "now"() NOT NULL,
+ "expires_at" timestamp with time zone DEFAULT ("now"() + '48:00:00'::interval) NOT NULL,
+ "created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
+ "updated_at" timestamp with time zone DEFAULT "now"() NOT NULL,
+ CONSTRAINT "prompt_suggestions_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "prompt_suggestions_source_check" CHECK (
+ "source" = ANY (ARRAY['llm'::"text", 'heuristic'::"text"])
+ ),
+ CONSTRAINT "prompt_suggestions_status_check" CHECK (
+ "status" = ANY (ARRAY['new'::"text", 'added'::"text", 'dismissed'::"text"])
+ ),
+ CONSTRAINT "prompt_suggestions_brand_id_fkey" FOREIGN KEY ("brand_id")
+ REFERENCES "public"."brands"("id") ON DELETE CASCADE,
+ CONSTRAINT "prompt_suggestions_topic_id_fkey" FOREIGN KEY ("topic_id")
+ REFERENCES "public"."topics"("id") ON DELETE SET NULL,
+ CONSTRAINT "prompt_suggestions_added_prompt_id_fkey" FOREIGN KEY ("added_prompt_id")
+ REFERENCES "public"."prompts"("id") ON DELETE SET NULL
+);
+
+ALTER TABLE "public"."prompt_suggestions" OWNER TO "postgres";
+
+CREATE INDEX IF NOT EXISTS "idx_prompt_suggestions_brand_status"
+ ON "public"."prompt_suggestions" ("brand_id", "status", "generated_at" DESC);
+
+-- One live suggestion per brand per text, case-insensitively. Partial on
+-- 'new' so an accepted or dismissed suggestion never blocks a later one.
+CREATE UNIQUE INDEX IF NOT EXISTS "idx_prompt_suggestions_brand_text_active"
+ ON "public"."prompt_suggestions" ("brand_id", "lower"("suggested_text"))
+ WHERE ("status" = 'new'::"text");
+
+ALTER TABLE "public"."prompt_suggestions" ENABLE ROW LEVEL SECURITY;
+
+-- Writes go through the Express server's service-role client; these cover the
+-- web app's direct reads and the accept/dismiss updates it performs.
+CREATE POLICY "prompt_suggestions: member select" ON "public"."prompt_suggestions"
+ FOR SELECT USING (
+ "brand_id" IN (
+ SELECT "b"."id"
+ FROM "public"."brands" "b"
+ JOIN "public"."profiles" "p" ON "p"."organization_id" = "b"."organization_id"
+ WHERE "p"."id" = "auth"."uid"()
+ )
+ );
+
+CREATE POLICY "prompt_suggestions: admin/manager/analyst update" ON "public"."prompt_suggestions"
+ FOR UPDATE USING (
+ "brand_id" IN (
+ SELECT "b"."id"
+ FROM "public"."brands" "b"
+ JOIN "public"."profiles" "p" ON "p"."organization_id" = "b"."organization_id"
+ WHERE "p"."id" = "auth"."uid"()
+ AND "p"."role" = ANY (ARRAY['admin'::"public"."user_role", 'manager'::"public"."user_role", 'analyst'::"public"."user_role"])
+ )
+ );
+
+GRANT ALL ON TABLE "public"."prompt_suggestions" TO "anon";
+GRANT ALL ON TABLE "public"."prompt_suggestions" TO "authenticated";
+GRANT ALL ON TABLE "public"."prompt_suggestions" TO "service_role";
+
+-- ─────────────────────────────────────────────────────────────────────────────
+
ALTER TABLE public.prompt_suggestions ADD COLUMN source_data jsonb;
-- The live table's source check predates 'gsc' — extend it.
diff --git a/supabase/schema.sql b/supabase/schema.sql
index 4c7958539..b87e7ca61 100644
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@ -5021,6 +5021,96 @@ GRANT SELECT ON public.gsc_query_stats TO authenticated;
-- repeat suggestion refreshes cost zero new DataForSEO calls. Service
-- role only (RLS enabled, no policies).
+-- ── Recovered table definition (#709) ────────────────────────────────────────
+--
+-- prompt_suggestions was created directly against the hosted database and
+-- never written back into a migration, so the ALTERs below had nothing to
+-- alter on a clean one: `supabase db reset` failed here with 42P01, and
+-- schema.sql inherited the same gap. Every documented fresh-install path was
+-- broken from the moment this migration shipped.
+--
+-- The definition belongs *here* rather than in a later numbered file, because
+-- the very next statement alters this table — a migration appended at the end
+-- would never be reached. Editing a shipped migration is normally off-limits;
+-- this one is safe because every clause is idempotent and any database that
+-- already ran 00048 will not run it again.
+--
+-- Deliberately the table as it was BEFORE the ALTERs below: no source_data
+-- column, and the two-value source check. Transcribing a live database's
+-- current shape instead would leave a fresh install failing one line later on
+-- "column source_data already exists".
+CREATE TABLE IF NOT EXISTS "public"."prompt_suggestions" (
+ "id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
+ "brand_id" "uuid" NOT NULL,
+ "suggested_text" "text" NOT NULL,
+ "topic_name" "text",
+ "topic_id" "uuid",
+ "reason" "text",
+ "est_volume" integer,
+ "source" "text" DEFAULT 'llm'::"text" NOT NULL,
+ "status" "text" DEFAULT 'new'::"text" NOT NULL,
+ "added_prompt_id" "uuid",
+ "generated_at" timestamp with time zone DEFAULT "now"() NOT NULL,
+ "expires_at" timestamp with time zone DEFAULT ("now"() + '48:00:00'::interval) NOT NULL,
+ "created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
+ "updated_at" timestamp with time zone DEFAULT "now"() NOT NULL,
+ CONSTRAINT "prompt_suggestions_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "prompt_suggestions_source_check" CHECK (
+ "source" = ANY (ARRAY['llm'::"text", 'heuristic'::"text"])
+ ),
+ CONSTRAINT "prompt_suggestions_status_check" CHECK (
+ "status" = ANY (ARRAY['new'::"text", 'added'::"text", 'dismissed'::"text"])
+ ),
+ CONSTRAINT "prompt_suggestions_brand_id_fkey" FOREIGN KEY ("brand_id")
+ REFERENCES "public"."brands"("id") ON DELETE CASCADE,
+ CONSTRAINT "prompt_suggestions_topic_id_fkey" FOREIGN KEY ("topic_id")
+ REFERENCES "public"."topics"("id") ON DELETE SET NULL,
+ CONSTRAINT "prompt_suggestions_added_prompt_id_fkey" FOREIGN KEY ("added_prompt_id")
+ REFERENCES "public"."prompts"("id") ON DELETE SET NULL
+);
+
+ALTER TABLE "public"."prompt_suggestions" OWNER TO "postgres";
+
+CREATE INDEX IF NOT EXISTS "idx_prompt_suggestions_brand_status"
+ ON "public"."prompt_suggestions" ("brand_id", "status", "generated_at" DESC);
+
+-- One live suggestion per brand per text, case-insensitively. Partial on
+-- 'new' so an accepted or dismissed suggestion never blocks a later one.
+CREATE UNIQUE INDEX IF NOT EXISTS "idx_prompt_suggestions_brand_text_active"
+ ON "public"."prompt_suggestions" ("brand_id", "lower"("suggested_text"))
+ WHERE ("status" = 'new'::"text");
+
+ALTER TABLE "public"."prompt_suggestions" ENABLE ROW LEVEL SECURITY;
+
+-- Writes go through the Express server's service-role client; these cover the
+-- web app's direct reads and the accept/dismiss updates it performs.
+CREATE POLICY "prompt_suggestions: member select" ON "public"."prompt_suggestions"
+ FOR SELECT USING (
+ "brand_id" IN (
+ SELECT "b"."id"
+ FROM "public"."brands" "b"
+ JOIN "public"."profiles" "p" ON "p"."organization_id" = "b"."organization_id"
+ WHERE "p"."id" = "auth"."uid"()
+ )
+ );
+
+CREATE POLICY "prompt_suggestions: admin/manager/analyst update" ON "public"."prompt_suggestions"
+ FOR UPDATE USING (
+ "brand_id" IN (
+ SELECT "b"."id"
+ FROM "public"."brands" "b"
+ JOIN "public"."profiles" "p" ON "p"."organization_id" = "b"."organization_id"
+ WHERE "p"."id" = "auth"."uid"()
+ AND "p"."role" = ANY (ARRAY['admin'::"public"."user_role", 'manager'::"public"."user_role", 'analyst'::"public"."user_role"])
+ )
+ );
+
+GRANT ALL ON TABLE "public"."prompt_suggestions" TO "anon";
+GRANT ALL ON TABLE "public"."prompt_suggestions" TO "authenticated";
+GRANT ALL ON TABLE "public"."prompt_suggestions" TO "service_role";
+
+-- ─────────────────────────────────────────────────────────────────────────────
+
ALTER TABLE public.prompt_suggestions ADD COLUMN source_data jsonb;
-- The live table's source check predates 'gsc' — extend it.