create-nextblock 0.14.5 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/package.json +1 -1
  2. package/templates/nextblock-template/AGENTS.md +9 -0
  3. package/templates/nextblock-template/CLAUDE.md +1 -0
  4. package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +285 -1
  5. package/templates/nextblock-template/app/api/mcp/route.ts +415 -0
  6. package/templates/nextblock-template/app/cms/CmsClientLayout.tsx +5 -1
  7. package/templates/nextblock-template/app/cms/media/import-external-image.ts +97 -11
  8. package/templates/nextblock-template/app/cms/settings/cortex-ai/McpServerSettingsCard.tsx +584 -0
  9. package/templates/nextblock-template/app/cms/settings/cortex-ai/StoredCortexAiSettingsClient.tsx +6 -0
  10. package/templates/nextblock-template/app/cms/settings/cortex-ai/actions.ts +4 -20
  11. package/templates/nextblock-template/app/cms/settings/cortex-ai/mcp-actions.ts +205 -0
  12. package/templates/nextblock-template/app/cms/settings/cortex-ai/page.tsx +64 -1
  13. package/templates/nextblock-template/app/cms/settings/cortex-ai/require-admin.ts +34 -0
  14. package/templates/nextblock-template/app/cms/settings/site-scripts/actions.ts +346 -0
  15. package/templates/nextblock-template/app/cms/settings/site-scripts/components/SiteScriptManager.tsx +492 -0
  16. package/templates/nextblock-template/app/cms/settings/site-scripts/page.tsx +51 -0
  17. package/templates/nextblock-template/app/layout.tsx +33 -0
  18. package/templates/nextblock-template/components/BlockRenderer.tsx +9 -3
  19. package/templates/nextblock-template/components/SiteScripts.tsx +56 -0
  20. package/templates/nextblock-template/components/blocks/renderers/TextBlockRenderer.tsx +1 -9
  21. package/templates/nextblock-template/docs/04-DATABASE-AND-AUTH.md +31 -1
  22. package/templates/nextblock-template/docs/08-NEXTBLOCK-CORTEX-AI-ARCHITECTURE.md +272 -0
  23. package/templates/nextblock-template/lib/blocks/inlineScriptNonce.ts +20 -0
  24. package/templates/nextblock-template/lib/setup/migrations-bundle.ts +5 -0
  25. package/templates/nextblock-template/lib/site-scripts/revisions.ts +71 -0
  26. package/templates/nextblock-template/lib/site-scripts/types.ts +46 -0
  27. package/templates/nextblock-template/package.json +1 -1
  28. package/templates/nextblock-template/tsconfig.tsbuildinfo +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-nextblock",
3
- "version": "0.14.5",
3
+ "version": "0.15.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -0,0 +1,9 @@
1
+ <!-- BEGIN:nextjs-agent-rules -->
2
+
3
+ # This is NOT the Next.js you know
4
+
5
+ This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices.
6
+
7
+ This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean.
8
+
9
+ <!-- END:nextjs-agent-rules -->
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -6513,6 +6513,287 @@ SELECT
6513
6513
  ON CONFLICT (product_id, version) DO NOTHING;
6514
6514
 
6515
6515
 
6516
+ -- >>> FROM: 00000000000017_cortex_ai_mcp_server.sql <<<
6517
+ -- Cortex AI MCP (Model Context Protocol) server access.
6518
+ --
6519
+ -- Adds the bearer-token store that gates /api/mcp, the endpoint that exposes the
6520
+ -- Cortex AI tool registry to external MCP clients (Claude Code, Claude Desktop,
6521
+ -- Cursor, VS Code). Two pieces:
6522
+ --
6523
+ -- 1. public.mcp_access_tokens — one row per issued token. We store ONLY the
6524
+ -- SHA-256 hash of the token, never the token itself: the plaintext is shown
6525
+ -- to the admin exactly once at mint time and is unrecoverable afterwards, so
6526
+ -- a database leak cannot be replayed against the MCP endpoint. \`token_prefix\`
6527
+ -- is the non-secret leading fragment kept purely so the UI can tell two tokens
6528
+ -- apart in a list.
6529
+ --
6530
+ -- 2. cortex_ai_mcp_settings — a non-secret JSON site_settings row holding the
6531
+ -- server on/off switch and the localhost-trust flag. It is added to all four
6532
+ -- site_settings policies so only authenticated ADMINs can read or write it;
6533
+ -- the MCP route itself reads it through the service-role client, which
6534
+ -- bypasses RLS.
6535
+ --
6536
+ -- Forward-only. Recreates the four site_settings policies idempotently, preserving
6537
+ -- every key already in each policy's sensitive array (note that
6538
+ -- language_detection_settings stays anon-READABLE and so is absent from the SELECT
6539
+ -- policy, exactly as migration 00000000000012 left it).
6540
+
6541
+ CREATE TABLE IF NOT EXISTS public.mcp_access_tokens (
6542
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
6543
+ name text NOT NULL,
6544
+ -- Lowercase hex SHA-256 of the plaintext token. Unique so a lookup is a single
6545
+ -- indexed equality probe and duplicate mints are impossible.
6546
+ token_hash text NOT NULL UNIQUE,
6547
+ -- Non-secret display fragment, e.g. "nbmcp_a1b2c3d4". Never enough to authenticate.
6548
+ token_prefix text NOT NULL,
6549
+ -- 'read' grants the read-only tools; 'write' additionally grants the mutating ones.
6550
+ scopes text[] NOT NULL DEFAULT ARRAY['read', 'write']::text[],
6551
+ created_by uuid REFERENCES auth.users (id) ON DELETE SET NULL,
6552
+ created_at timestamptz NOT NULL DEFAULT now(),
6553
+ last_used_at timestamptz,
6554
+ expires_at timestamptz,
6555
+ revoked_at timestamptz
6556
+ );
6557
+
6558
+ COMMENT ON TABLE public.mcp_access_tokens IS
6559
+ 'Bearer tokens for the Cortex AI MCP server at /api/mcp. Stores SHA-256 hashes only; plaintext is displayed once at mint time.';
6560
+
6561
+ CREATE INDEX IF NOT EXISTS mcp_access_tokens_token_hash_idx
6562
+ ON public.mcp_access_tokens (token_hash);
6563
+
6564
+ -- Orders the admin token list newest-first without a sort.
6565
+ CREATE INDEX IF NOT EXISTS mcp_access_tokens_created_at_idx
6566
+ ON public.mcp_access_tokens (created_at DESC);
6567
+
6568
+ ALTER TABLE public.mcp_access_tokens ENABLE ROW LEVEL SECURITY;
6569
+
6570
+ -- Tokens are credentials: admin-only, with no anon or WRITER access at all. The
6571
+ -- MCP route verifies them with the service-role client, which bypasses RLS.
6572
+ DROP POLICY IF EXISTS mcp_access_tokens_admin_select ON public.mcp_access_tokens;
6573
+ CREATE POLICY mcp_access_tokens_admin_select ON public.mcp_access_tokens
6574
+ FOR SELECT TO authenticated
6575
+ USING ((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role);
6576
+
6577
+ DROP POLICY IF EXISTS mcp_access_tokens_admin_insert ON public.mcp_access_tokens;
6578
+ CREATE POLICY mcp_access_tokens_admin_insert ON public.mcp_access_tokens
6579
+ FOR INSERT TO authenticated
6580
+ WITH CHECK ((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role);
6581
+
6582
+ DROP POLICY IF EXISTS mcp_access_tokens_admin_update ON public.mcp_access_tokens;
6583
+ CREATE POLICY mcp_access_tokens_admin_update ON public.mcp_access_tokens
6584
+ FOR UPDATE TO authenticated
6585
+ USING ((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role)
6586
+ WITH CHECK ((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role);
6587
+
6588
+ DROP POLICY IF EXISTS mcp_access_tokens_admin_delete ON public.mcp_access_tokens;
6589
+ CREATE POLICY mcp_access_tokens_admin_delete ON public.mcp_access_tokens
6590
+ FOR DELETE TO authenticated
6591
+ USING ((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role);
6592
+
6593
+ GRANT SELECT, INSERT, UPDATE, DELETE ON public.mcp_access_tokens TO authenticated;
6594
+ GRANT ALL ON public.mcp_access_tokens TO service_role;
6595
+
6596
+ -- Add cortex_ai_mcp_settings to the admin-only site_settings group (all four policies).
6597
+ DROP POLICY IF EXISTS site_settings_read_policy ON public.site_settings;
6598
+ CREATE POLICY site_settings_read_policy ON public.site_settings FOR SELECT USING (((key <> ALL (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) OR ((key = ANY (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT auth.role() AS role) = 'authenticated'::text) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role))));
6599
+
6600
+ DROP POLICY IF EXISTS site_settings_insert_policy ON public.site_settings;
6601
+ CREATE POLICY site_settings_insert_policy ON public.site_settings FOR INSERT TO authenticated WITH CHECK ((((key <> ALL (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = ANY (ARRAY['ADMIN'::public.user_role, 'WRITER'::public.user_role]))) OR ((key = ANY (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role))));
6602
+
6603
+ DROP POLICY IF EXISTS site_settings_update_policy ON public.site_settings;
6604
+ CREATE POLICY site_settings_update_policy ON public.site_settings FOR UPDATE TO authenticated USING ((((key <> ALL (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = ANY (ARRAY['ADMIN'::public.user_role, 'WRITER'::public.user_role]))) OR ((key = ANY (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role)))) WITH CHECK ((((key <> ALL (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = ANY (ARRAY['ADMIN'::public.user_role, 'WRITER'::public.user_role]))) OR ((key = ANY (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role))));
6605
+
6606
+ DROP POLICY IF EXISTS site_settings_delete_policy ON public.site_settings;
6607
+ CREATE POLICY site_settings_delete_policy ON public.site_settings FOR DELETE TO authenticated USING ((((key <> ALL (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = ANY (ARRAY['ADMIN'::public.user_role, 'WRITER'::public.user_role]))) OR ((key = ANY (ARRAY['cortex_ai_openrouter_api_key'::text, 'bot_protection_secret'::text, 'email_secret'::text, 'payment_secret'::text, 'language_detection_settings'::text, 'cortex_ai_pexels_api_key'::text, 'cortex_ai_unsplash_access_key'::text, 'cortex_ai_mcp_settings'::text])) AND (( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role))));
6608
+
6609
+
6610
+ -- >>> FROM: 00000000000018_site_scripts.sql <<<
6611
+ -- Site scripts: admin-authored JavaScript injected into every page of the public site.
6612
+ --
6613
+ -- Rich-text blocks can already carry an inline <script>, but that script belongs to
6614
+ -- one block on one page. This table is for behaviour that spans the site: chat
6615
+ -- widgets, third-party embeds, and the scroll/animation helpers that page classes
6616
+ -- rely on. Each row gets a name, an on/off switch, and a defined injection point.
6617
+ --
6618
+ -- NOT the same thing as \`site_settings.privacy_settings -> custom_scripts\`, which is
6619
+ -- a single consent-gated blob for marketing tags and only fires once a visitor
6620
+ -- accepts cookies. Rows here are functional site code and run unconditionally, so
6621
+ -- anything requiring consent belongs in that setting instead, not this table.
6622
+ --
6623
+ -- Scripts are emitted with the request's CSP nonce by the root layout, so they run
6624
+ -- under the site's existing Content-Security-Policy rather than forcing it open.
6625
+ --
6626
+ -- Security posture: this is arbitrary JavaScript on every page, so writes are
6627
+ -- ADMIN-only (WRITER is deliberately excluded, unlike most content tables) and the
6628
+ -- public may read only rows that are switched on, so a half-written draft is never
6629
+ -- served to a visitor.
6630
+
6631
+ CREATE TABLE IF NOT EXISTS public.site_scripts (
6632
+ id uuid DEFAULT gen_random_uuid() NOT NULL,
6633
+ name text NOT NULL,
6634
+ description text,
6635
+ -- Raw JavaScript, stored WITHOUT the surrounding <script> tag. The layout adds
6636
+ -- the tag so the nonce and attributes are always applied by us, never by the
6637
+ -- author. Ignored when \`src\` is set.
6638
+ code text DEFAULT ''::text NOT NULL,
6639
+ -- When set, an external script is loaded from this URL and \`code\` is ignored.
6640
+ src text,
6641
+ -- Where the tag is emitted. 'head' runs before first paint (blocking, use
6642
+ -- sparingly); 'body_end' runs once the markup exists and is the right default
6643
+ -- for anything that queries the DOM.
6644
+ placement text DEFAULT 'body_end'::text NOT NULL,
6645
+ -- Applies to external \`src\` scripts; inline code ignores it.
6646
+ load_strategy text DEFAULT 'default'::text NOT NULL,
6647
+ is_active boolean DEFAULT false NOT NULL,
6648
+ sort_order integer DEFAULT 0 NOT NULL,
6649
+ created_at timestamp with time zone DEFAULT now() NOT NULL,
6650
+ updated_at timestamp with time zone DEFAULT now() NOT NULL,
6651
+ CONSTRAINT site_scripts_pkey PRIMARY KEY (id),
6652
+ CONSTRAINT site_scripts_placement_check
6653
+ CHECK ((placement = ANY (ARRAY['head'::text, 'body_start'::text, 'body_end'::text]))),
6654
+ CONSTRAINT site_scripts_load_strategy_check
6655
+ CHECK ((load_strategy = ANY (ARRAY['default'::text, 'defer'::text, 'async'::text]))),
6656
+ -- An external script must be https so it cannot be downgraded in transit.
6657
+ CONSTRAINT site_scripts_src_scheme_check
6658
+ CHECK ((src IS NULL OR src ~ '^https://')),
6659
+ -- A row has to actually do something: inline code or an external src.
6660
+ CONSTRAINT site_scripts_has_payload_check
6661
+ CHECK ((src IS NOT NULL OR length(btrim(code)) > 0))
6662
+ );
6663
+
6664
+ COMMENT ON TABLE public.site_scripts IS
6665
+ 'Admin-authored JavaScript injected into the public site by the root layout, with the request CSP nonce applied. Only is_active rows are publicly readable; only ADMIN may write. Distinct from privacy_settings.custom_scripts, which is consent-gated marketing tags.';
6666
+
6667
+ CREATE INDEX IF NOT EXISTS site_scripts_active_placement_sort_idx
6668
+ ON public.site_scripts USING btree (is_active, placement, sort_order);
6669
+
6670
+ DROP TRIGGER IF EXISTS set_site_scripts_updated_at ON public.site_scripts;
6671
+ CREATE TRIGGER set_site_scripts_updated_at
6672
+ BEFORE UPDATE ON public.site_scripts
6673
+ FOR EACH ROW EXECUTE FUNCTION public.set_current_timestamp_updated_at();
6674
+
6675
+ ALTER TABLE public.site_scripts ENABLE ROW LEVEL SECURITY;
6676
+
6677
+ GRANT ALL ON TABLE public.site_scripts TO anon;
6678
+ GRANT ALL ON TABLE public.site_scripts TO authenticated;
6679
+ GRANT ALL ON TABLE public.site_scripts TO service_role;
6680
+
6681
+ -- Anonymous visitors need the active scripts to render the page. Inactive rows stay
6682
+ -- private so a half-written script is never exposed before it is switched on.
6683
+ DROP POLICY IF EXISTS "Public read active site scripts" ON public.site_scripts;
6684
+ CREATE POLICY "Public read active site scripts" ON public.site_scripts
6685
+ FOR SELECT TO authenticated, anon USING (is_active);
6686
+
6687
+ DROP POLICY IF EXISTS "Admins read all site scripts" ON public.site_scripts;
6688
+ CREATE POLICY "Admins read all site scripts" ON public.site_scripts
6689
+ FOR SELECT TO authenticated
6690
+ USING (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role));
6691
+
6692
+ DROP POLICY IF EXISTS "Admins insert site scripts" ON public.site_scripts;
6693
+ CREATE POLICY "Admins insert site scripts" ON public.site_scripts
6694
+ FOR INSERT TO authenticated
6695
+ WITH CHECK (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role));
6696
+
6697
+ DROP POLICY IF EXISTS "Admins update site scripts" ON public.site_scripts;
6698
+ CREATE POLICY "Admins update site scripts" ON public.site_scripts
6699
+ FOR UPDATE TO authenticated
6700
+ USING (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role))
6701
+ WITH CHECK (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role));
6702
+
6703
+ DROP POLICY IF EXISTS "Admins delete site scripts" ON public.site_scripts;
6704
+ CREATE POLICY "Admins delete site scripts" ON public.site_scripts
6705
+ FOR DELETE TO authenticated
6706
+ USING (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role));
6707
+
6708
+
6709
+ -- >>> FROM: 00000000000019_site_script_revisions.sql <<<
6710
+ -- Audit trail and undo for site scripts.
6711
+ --
6712
+ -- \`site_scripts\` ships arbitrary JavaScript to every visitor, which makes it the
6713
+ -- highest-privilege write in the CMS: a bad or malicious snippet can read cookies,
6714
+ -- watch checkout forms, or phone home. Content has Revision History for exactly this
6715
+ -- reason; code needs it more, not less. Every create/update/delete writes one row
6716
+ -- here, and every row is a complete, restorable snapshot — so this table is both the
6717
+ -- log ("who shipped what, when, from where") and the undo.
6718
+ --
6719
+ -- APPEND-ONLY BY CONSTRUCTION. There are no UPDATE or DELETE policies, and the
6720
+ -- trigger below rejects both even for the service role, which otherwise bypasses
6721
+ -- RLS. An audit trail that the compromised credential can rewrite is not an audit
6722
+ -- trail. Reverting therefore writes a NEW 'revert' row rather than removing history.
6723
+ --
6724
+ -- \`script_id\` and \`actor_user_id\` are deliberately PLAIN uuids with no foreign keys:
6725
+ -- an FK with ON DELETE SET NULL would have to UPDATE this table when a script or a
6726
+ -- profile is deleted, which the append-only trigger forbids. \`script_name\` is
6727
+ -- denormalised so a deleted script is still identifiable in the log.
6728
+
6729
+ CREATE TABLE IF NOT EXISTS public.site_script_revisions (
6730
+ id uuid DEFAULT gen_random_uuid() NOT NULL,
6731
+ script_id uuid,
6732
+ script_name text NOT NULL,
6733
+ revision_type text NOT NULL,
6734
+ -- Null when the actor could not be resolved (e.g. a localhost dev connection).
6735
+ actor_user_id uuid,
6736
+ -- Which surface made the change, so an unexpected edit can be traced back to
6737
+ -- the dashboard or to an MCP token.
6738
+ source text DEFAULT 'cms'::text NOT NULL,
6739
+ summary text,
6740
+ -- Full restorable state of the script at this revision. For 'delete' it is the
6741
+ -- state immediately BEFORE removal, so restoring it brings the script back.
6742
+ snapshot jsonb NOT NULL,
6743
+ created_at timestamp with time zone DEFAULT now() NOT NULL,
6744
+ CONSTRAINT site_script_revisions_pkey PRIMARY KEY (id),
6745
+ CONSTRAINT site_script_revisions_type_check
6746
+ CHECK ((revision_type = ANY (ARRAY['create'::text, 'update'::text, 'delete'::text, 'revert'::text]))),
6747
+ CONSTRAINT site_script_revisions_source_check
6748
+ CHECK ((source = ANY (ARRAY['cms'::text, 'mcp'::text]))),
6749
+ CONSTRAINT site_script_revisions_snapshot_is_object_check
6750
+ CHECK ((jsonb_typeof(snapshot) = 'object'))
6751
+ );
6752
+
6753
+ COMMENT ON TABLE public.site_script_revisions IS
6754
+ 'Append-only audit log and undo history for site_scripts. Each row is a restorable snapshot. UPDATE and DELETE are blocked by trigger, including for the service role.';
6755
+
6756
+ CREATE INDEX IF NOT EXISTS site_script_revisions_script_created_idx
6757
+ ON public.site_script_revisions USING btree (script_id, created_at DESC);
6758
+
6759
+ CREATE INDEX IF NOT EXISTS site_script_revisions_created_idx
6760
+ ON public.site_script_revisions USING btree (created_at DESC);
6761
+
6762
+ -- Enforced in the database rather than the application so it holds for every
6763
+ -- caller, including the service-role client the MCP server uses.
6764
+ CREATE OR REPLACE FUNCTION public.prevent_site_script_revision_rewrite() RETURNS trigger
6765
+ LANGUAGE plpgsql
6766
+ SET search_path = ''
6767
+ AS $$
6768
+ BEGIN
6769
+ RAISE EXCEPTION 'site_script_revisions is append-only; % is not permitted', TG_OP
6770
+ USING ERRCODE = 'restrict_violation';
6771
+ END;
6772
+ $$;
6773
+
6774
+ DROP TRIGGER IF EXISTS trg_site_script_revisions_append_only ON public.site_script_revisions;
6775
+ CREATE TRIGGER trg_site_script_revisions_append_only
6776
+ BEFORE UPDATE OR DELETE ON public.site_script_revisions
6777
+ FOR EACH ROW EXECUTE FUNCTION public.prevent_site_script_revision_rewrite();
6778
+
6779
+ ALTER TABLE public.site_script_revisions ENABLE ROW LEVEL SECURITY;
6780
+
6781
+ GRANT SELECT, INSERT ON TABLE public.site_script_revisions TO authenticated;
6782
+ GRANT ALL ON TABLE public.site_script_revisions TO service_role;
6783
+
6784
+ -- Read is ADMIN-only: snapshots contain the full source of scripts that may not be
6785
+ -- active yet, and the log itself reveals operational history.
6786
+ DROP POLICY IF EXISTS "Admins read site script revisions" ON public.site_script_revisions;
6787
+ CREATE POLICY "Admins read site script revisions" ON public.site_script_revisions
6788
+ FOR SELECT TO authenticated
6789
+ USING (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role));
6790
+
6791
+ DROP POLICY IF EXISTS "Admins insert site script revisions" ON public.site_script_revisions;
6792
+ CREATE POLICY "Admins insert site script revisions" ON public.site_script_revisions
6793
+ FOR INSERT TO authenticated
6794
+ WITH CHECK (((SELECT public.get_current_user_role()) = 'ADMIN'::public.user_role));
6795
+
6796
+
6516
6797
  -- Step D: Record the applied migrations in history (truncated in Step B) so
6517
6798
  -- \`npm run db:migrate:check\` reports up to date instead of listing every file as pending.
6518
6799
  INSERT INTO supabase_migrations.schema_migrations (version, name) VALUES
@@ -6532,7 +6813,10 @@ ON CONFLICT (product_id, version) DO NOTHING;
6532
6813
  ('00000000000013', 'youtube_nocookie_embeds'),
6533
6814
  ('00000000000014', 'site_themes'),
6534
6815
  ('00000000000015', 'scheduled_publishing'),
6535
- ('00000000000016', 'product_revisions_and_revision_baseline')
6816
+ ('00000000000016', 'product_revisions_and_revision_baseline'),
6817
+ ('00000000000017', 'cortex_ai_mcp_server'),
6818
+ ('00000000000018', 'site_scripts'),
6819
+ ('00000000000019', 'site_script_revisions')
6536
6820
  ON CONFLICT (version) DO NOTHING;
6537
6821
 
6538
6822
  -- Step E: Anchor preserved profiles