spine-framework 0.3.16 → 0.3.18

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 (269) hide show
  1. package/.framework/cli/commands/create-app.ts +1 -1
  2. package/.framework/cli/commands/dev.ts +215 -33
  3. package/.framework/cli/index.ts +37 -75
  4. package/.framework/functions/_shared/app-manifest.ts +8 -15
  5. package/.framework/functions/_shared/db.ts +11 -15
  6. package/.framework/functions/_shared/index.ts +7 -16
  7. package/.framework/functions/_shared/principal.ts +4 -62
  8. package/.framework/functions/_shared/resolve-ids.ts +6 -6
  9. package/.framework/functions/_shared/webhook-registry.ts +43 -1
  10. package/.framework/functions/admin-funnel.ts +104 -0
  11. package/.framework/functions/admin-install-funnel.ts +225 -0
  12. package/.framework/functions/admin-signals.ts +241 -0
  13. package/.framework/functions/integration-routes.ts +3 -7
  14. package/.framework/migrations/001_schema.sql +406 -0
  15. package/.framework/migrations/002_rls_and_functions.sql +710 -0
  16. package/.framework/migrations/003_system_data.sql +520 -0
  17. package/.framework/migrations/004_enhancements.sql +43 -0
  18. package/.framework/public/_redirects +3 -0
  19. package/.framework/public/spine-logo.jpg +0 -0
  20. package/.framework/public/spine-logo.svg +3 -0
  21. package/{scripts → .framework/scripts}/assemble-frontend.sh +8 -5
  22. package/{scripts → .framework/scripts}/assemble-functions.sh +4 -47
  23. package/{scripts → .framework/scripts}/assemble.sh +2 -8
  24. package/{scripts → .framework/scripts}/boundary-check.sh +5 -9
  25. package/{scripts → .framework/scripts}/check-core-integrity.sh +0 -0
  26. package/.framework/seeds/link_types/account_cli_instances.json +9 -0
  27. package/.framework/seeds/types/funnel_signal_use.json +98 -0
  28. package/.framework/seeds/types/spine_instance.json +109 -0
  29. package/.framework/src/App.tsx +11 -21
  30. package/.framework/src/components/CustomAppLoader.tsx +2 -19
  31. package/.framework/src/components/ui/accordion.tsx +1 -1
  32. package/.framework/src/components/ui/alert-dialog.tsx +1 -1
  33. package/.framework/src/components/ui/aspect-ratio.tsx +1 -1
  34. package/.framework/src/components/ui/avatar.tsx +1 -1
  35. package/.framework/src/components/ui/badge.tsx +2 -2
  36. package/.framework/src/components/ui/breadcrumb.tsx +2 -2
  37. package/.framework/src/components/ui/button-group.tsx +2 -2
  38. package/.framework/src/components/ui/button.tsx +2 -2
  39. package/.framework/src/components/ui/chart.tsx +6 -1
  40. package/.framework/src/components/ui/checkbox.tsx +1 -1
  41. package/.framework/src/components/ui/collapsible.tsx +1 -1
  42. package/.framework/src/components/ui/context-menu.tsx +1 -1
  43. package/.framework/src/components/ui/dialog.tsx +1 -1
  44. package/.framework/src/components/ui/direction.tsx +17 -1
  45. package/.framework/src/components/ui/dropdown-menu.tsx +1 -1
  46. package/.framework/src/components/ui/hover-card.tsx +1 -1
  47. package/.framework/src/components/ui/item.tsx +2 -2
  48. package/.framework/src/components/ui/label.tsx +1 -1
  49. package/.framework/src/components/ui/menubar.tsx +1 -1
  50. package/.framework/src/components/ui/navigation-menu.tsx +1 -1
  51. package/.framework/src/components/ui/popover.tsx +1 -1
  52. package/.framework/src/components/ui/progress.tsx +1 -1
  53. package/.framework/src/components/ui/radio-group.tsx +1 -1
  54. package/.framework/src/components/ui/scroll-area.tsx +1 -1
  55. package/.framework/src/components/ui/select.tsx +1 -1
  56. package/.framework/src/components/ui/separator.tsx +1 -1
  57. package/.framework/src/components/ui/sheet.tsx +1 -1
  58. package/.framework/src/components/ui/sidebar.tsx +6 -6
  59. package/.framework/src/components/ui/slider.tsx +1 -1
  60. package/.framework/src/components/ui/switch.tsx +1 -1
  61. package/.framework/src/components/ui/tabs.tsx +1 -1
  62. package/.framework/src/components/ui/toggle-group.tsx +1 -1
  63. package/.framework/src/components/ui/toggle.tsx +1 -1
  64. package/.framework/src/components/ui/tooltip.tsx +1 -1
  65. package/.framework/src/pages/auth/LoginPage.tsx +10 -9
  66. package/LICENSE.md +63 -93
  67. package/README.md +254 -129
  68. package/custom/.devin/skills/install-app/SKILL.md +39 -0
  69. package/custom/.devin/workflows/app-install.yaml +83 -0
  70. package/custom/README.md +79 -0
  71. package/docs/APPS.md +232 -0
  72. package/docs/ARCHITECTURE.md +246 -0
  73. package/docs/INSTALL.md +155 -0
  74. package/docs/README.md +170 -0
  75. package/docs/design-schema-spec.md +1837 -0
  76. package/docs/dev-guides/01-create-an-app.md +183 -0
  77. package/docs/dev-guides/02-create-a-function.md +178 -0
  78. package/docs/dev-guides/03-create-a-component.md +178 -0
  79. package/docs/dev-guides/04-webhook-handlers.md +129 -0
  80. package/docs/dev-guides/05-testing.md +168 -0
  81. package/docs/dev-guides/README.md +40 -0
  82. package/docs/npm-packages-plan.md +288 -0
  83. package/docs/spine-core--api-reference.md +822 -0
  84. package/package.json +78 -150
  85. package/.framework/cli/bin.cjs +0 -14
  86. package/.framework/cli/commands/claim.ts +0 -430
  87. package/.framework/cli/commands/init.ts +0 -219
  88. package/.framework/cli/commands/install-app.ts +0 -685
  89. package/.framework/cli/commands/migrate.ts +0 -178
  90. package/.framework/cli/commands/publish.ts +0 -191
  91. package/.framework/cli/commands/status.ts +0 -268
  92. package/.framework/cli/commands/uninstall-app.ts +0 -166
  93. package/.framework/cli/commands/update-app.ts +0 -397
  94. package/.framework/cli/welcome.cjs +0 -45
  95. package/.framework/config/signal-client.ts +0 -147
  96. package/.framework/config/signals.ts +0 -70
  97. package/.framework/docs/STABILITY.md +0 -52
  98. package/.framework/docs/examples/custom-app/README.md +0 -77
  99. package/.framework/docs/examples/custom-function/README.md +0 -27
  100. package/.framework/docs/examples/custom-function/handler.ts +0 -48
  101. package/.framework/docs/examples/custom-webhook/README.md +0 -68
  102. package/.framework/functions/handler-registry.ts +0 -22
  103. package/.framework/functions/signal.ts +0 -168
  104. package/.framework/migrations/000_foundation.sql +0 -1302
  105. package/.framework/migrations/001_seed.sql +0 -163
  106. package/.framework/migrations/002_seed_constraints.sql +0 -19
  107. package/.framework/migrations/003_auth_user_trigger.sql +0 -67
  108. package/.framework/migrations/004_app_version_tracking.sql +0 -8
  109. package/.framework/migrations/005_pipelines_slug_stages.sql +0 -21
  110. package/.framework/src/pages/auth/RegisterPage.tsx +0 -163
  111. package/CHANGELOG.md +0 -68
  112. package/STRUCTURE.md +0 -150
  113. package/bin/spine-framework.cjs +0 -62
  114. package/bin/welcome.cjs +0 -58
  115. package/bin/ws-shim.ts +0 -10
  116. package/config/components.json +0 -25
  117. package/config/deno.lock +0 -108
  118. package/config/package-lock.json +0 -17183
  119. package/config/postcss.config.cjs +0 -10
  120. package/config/tailwind.config.cjs +0 -78
  121. package/config/tsconfig.build.json +0 -32
  122. package/config/tsconfig.cli.json +0 -18
  123. package/config/tsconfig.json +0 -41
  124. package/config/tsconfig.node.json +0 -17
  125. package/config/tsconfig.node.tsbuildinfo +0 -1
  126. package/config/tsconfig.tsbuildinfo +0 -1
  127. package/config/typedoc.json +0 -16
  128. package/config/vite.config.d.ts +0 -2
  129. package/config/vite.config.ts +0 -71
  130. package/dist/cli/commands/agents.d.ts +0 -39
  131. package/dist/cli/commands/agents.d.ts.map +0 -1
  132. package/dist/cli/commands/auth.d.ts +0 -36
  133. package/dist/cli/commands/auth.d.ts.map +0 -1
  134. package/dist/cli/commands/claim.d.ts +0 -38
  135. package/dist/cli/commands/claim.d.ts.map +0 -1
  136. package/dist/cli/commands/create-app.d.ts +0 -23
  137. package/dist/cli/commands/create-app.d.ts.map +0 -1
  138. package/dist/cli/commands/dev.d.ts +0 -24
  139. package/dist/cli/commands/dev.d.ts.map +0 -1
  140. package/dist/cli/commands/doctor.d.ts +0 -42
  141. package/dist/cli/commands/doctor.d.ts.map +0 -1
  142. package/dist/cli/commands/generate.d.ts +0 -36
  143. package/dist/cli/commands/generate.d.ts.map +0 -1
  144. package/dist/cli/commands/init.d.ts +0 -20
  145. package/dist/cli/commands/init.d.ts.map +0 -1
  146. package/dist/cli/commands/install-app.d.ts +0 -30
  147. package/dist/cli/commands/install-app.d.ts.map +0 -1
  148. package/dist/cli/commands/items.d.ts +0 -45
  149. package/dist/cli/commands/items.d.ts.map +0 -1
  150. package/dist/cli/commands/migrate.d.ts +0 -21
  151. package/dist/cli/commands/migrate.d.ts.map +0 -1
  152. package/dist/cli/commands/migrations.d.ts +0 -41
  153. package/dist/cli/commands/migrations.d.ts.map +0 -1
  154. package/dist/cli/commands/pipelines.d.ts +0 -40
  155. package/dist/cli/commands/pipelines.d.ts.map +0 -1
  156. package/dist/cli/commands/publish.d.ts +0 -23
  157. package/dist/cli/commands/publish.d.ts.map +0 -1
  158. package/dist/cli/commands/status.d.ts +0 -23
  159. package/dist/cli/commands/status.d.ts.map +0 -1
  160. package/dist/cli/commands/system.d.ts +0 -29
  161. package/dist/cli/commands/system.d.ts.map +0 -1
  162. package/dist/cli/commands/test.d.ts +0 -46
  163. package/dist/cli/commands/test.d.ts.map +0 -1
  164. package/dist/cli/commands/uninstall-app.d.ts +0 -23
  165. package/dist/cli/commands/uninstall-app.d.ts.map +0 -1
  166. package/dist/cli/commands/update-app.d.ts +0 -24
  167. package/dist/cli/commands/update-app.d.ts.map +0 -1
  168. package/dist/cli/context.d.ts +0 -88
  169. package/dist/cli/context.d.ts.map +0 -1
  170. package/dist/cli/env-loader.d.ts +0 -14
  171. package/dist/cli/env-loader.d.ts.map +0 -1
  172. package/dist/cli/index.d.ts +0 -44
  173. package/dist/cli/index.d.ts.map +0 -1
  174. package/dist/config/signals.d.ts +0 -82
  175. package/dist/config/signals.d.ts.map +0 -1
  176. package/dist/functions/_shared/agent-runner.d.ts +0 -156
  177. package/dist/functions/_shared/agent-runner.d.ts.map +0 -1
  178. package/dist/functions/_shared/app-manifest.d.ts +0 -68
  179. package/dist/functions/_shared/app-manifest.d.ts.map +0 -1
  180. package/dist/functions/_shared/audit.d.ts +0 -91
  181. package/dist/functions/_shared/audit.d.ts.map +0 -1
  182. package/dist/functions/_shared/db.d.ts +0 -125
  183. package/dist/functions/_shared/db.d.ts.map +0 -1
  184. package/dist/functions/_shared/index.d.ts +0 -299
  185. package/dist/functions/_shared/index.d.ts.map +0 -1
  186. package/dist/functions/_shared/middleware.d.ts +0 -315
  187. package/dist/functions/_shared/middleware.d.ts.map +0 -1
  188. package/dist/functions/_shared/permissions.d.ts +0 -626
  189. package/dist/functions/_shared/permissions.d.ts.map +0 -1
  190. package/dist/functions/_shared/pipeline-runner.d.ts +0 -124
  191. package/dist/functions/_shared/pipeline-runner.d.ts.map +0 -1
  192. package/dist/functions/_shared/principal.d.ts +0 -284
  193. package/dist/functions/_shared/principal.d.ts.map +0 -1
  194. package/dist/functions/_shared/resolve-ids.d.ts +0 -10
  195. package/dist/functions/_shared/resolve-ids.d.ts.map +0 -1
  196. package/dist/functions/_shared/schema-utils.d.ts +0 -181
  197. package/dist/functions/_shared/schema-utils.d.ts.map +0 -1
  198. package/dist/functions/_shared/testing.d.ts +0 -172
  199. package/dist/functions/_shared/testing.d.ts.map +0 -1
  200. package/dist/functions/_shared/trigger-engine.d.ts +0 -140
  201. package/dist/functions/_shared/trigger-engine.d.ts.map +0 -1
  202. package/dist/functions/_shared/webhook-registration.d.ts +0 -81
  203. package/dist/functions/_shared/webhook-registration.d.ts.map +0 -1
  204. package/dist/functions/_shared/webhook-registry.d.ts +0 -41
  205. package/dist/functions/_shared/webhook-registry.d.ts.map +0 -1
  206. package/dist/functions/account-nodes.d.ts +0 -48
  207. package/dist/functions/account-nodes.d.ts.map +0 -1
  208. package/dist/functions/admin-data.d.ts +0 -178
  209. package/dist/functions/admin-data.d.ts.map +0 -1
  210. package/dist/functions/ai-agents.d.ts +0 -125
  211. package/dist/functions/ai-agents.d.ts.map +0 -1
  212. package/dist/functions/api-keys.d.ts +0 -140
  213. package/dist/functions/api-keys.d.ts.map +0 -1
  214. package/dist/functions/apps.d.ts +0 -163
  215. package/dist/functions/apps.d.ts.map +0 -1
  216. package/dist/functions/auth.d.ts +0 -74
  217. package/dist/functions/auth.d.ts.map +0 -1
  218. package/dist/functions/debug-auth.d.ts +0 -33
  219. package/dist/functions/debug-auth.d.ts.map +0 -1
  220. package/dist/functions/embeddings.d.ts +0 -205
  221. package/dist/functions/embeddings.d.ts.map +0 -1
  222. package/dist/functions/handler-registry.d.ts +0 -22
  223. package/dist/functions/handler-registry.d.ts.map +0 -1
  224. package/dist/functions/integration-routes.d.ts +0 -45
  225. package/dist/functions/integration-routes.d.ts.map +0 -1
  226. package/dist/functions/integrations.d.ts +0 -124
  227. package/dist/functions/integrations.d.ts.map +0 -1
  228. package/dist/functions/item-progress.d.ts +0 -41
  229. package/dist/functions/item-progress.d.ts.map +0 -1
  230. package/dist/functions/logs.d.ts +0 -162
  231. package/dist/functions/logs.d.ts.map +0 -1
  232. package/dist/functions/observability.d.ts +0 -123
  233. package/dist/functions/observability.d.ts.map +0 -1
  234. package/dist/functions/pipeline-executions.d.ts +0 -190
  235. package/dist/functions/pipeline-executions.d.ts.map +0 -1
  236. package/dist/functions/pipelines.d.ts +0 -171
  237. package/dist/functions/pipelines.d.ts.map +0 -1
  238. package/dist/functions/prompt-configs.d.ts +0 -125
  239. package/dist/functions/prompt-configs.d.ts.map +0 -1
  240. package/dist/functions/roles.d.ts +0 -118
  241. package/dist/functions/roles.d.ts.map +0 -1
  242. package/dist/functions/signal.d.ts +0 -73
  243. package/dist/functions/signal.d.ts.map +0 -1
  244. package/dist/functions/system-cron.d.ts +0 -65
  245. package/dist/functions/system-cron.d.ts.map +0 -1
  246. package/dist/functions/system.d.ts +0 -29
  247. package/dist/functions/system.d.ts.map +0 -1
  248. package/dist/functions/tests.d.ts +0 -28
  249. package/dist/functions/tests.d.ts.map +0 -1
  250. package/dist/functions/timers.d.ts +0 -139
  251. package/dist/functions/timers.d.ts.map +0 -1
  252. package/dist/functions/triggers.d.ts +0 -203
  253. package/dist/functions/triggers.d.ts.map +0 -1
  254. package/dist/functions/types.d.ts +0 -151
  255. package/dist/functions/types.d.ts.map +0 -1
  256. package/dist/src/types/types.d.ts +0 -364
  257. package/dist/src/types/types.d.ts.map +0 -1
  258. package/index.html +0 -13
  259. package/netlify.toml +0 -36
  260. package/package-project.json +0 -71
  261. package/scripts/ingest-chunks.cjs +0 -202
  262. package/scripts/kb-chunk-parser.cjs +0 -312
  263. package/scripts/kb-chunk-parser.ts +0 -330
  264. package/scripts/load-test-app-install.ts +0 -484
  265. package/scripts/netlify-dev-wrapper.sh +0 -22
  266. package/vitest.config.ts +0 -45
  267. /package/{scripts → .framework/scripts}/app-install-cli.ts +0 -0
  268. /package/{scripts → .framework/scripts}/build-manifest.sh +0 -0
  269. /package/{scripts → .framework/scripts}/verify-integrity.sh +0 -0
@@ -0,0 +1,710 @@
1
+ -- Migration 002: Functions, RLS Policies, Indexes, and Foreign Keys
2
+ -- Consolidated from migrations 003, 004, 005, 006, 012, 014
3
+
4
+ -- ============================================
5
+ -- FUNCTIONS
6
+ -- ============================================
7
+
8
+ -- Account hierarchy traversal
9
+ CREATE OR REPLACE FUNCTION public.get_accessible_accounts(actor_id uuid)
10
+ RETURNS TABLE (account_id uuid) AS $$
11
+ BEGIN
12
+ IF EXISTS (
13
+ SELECT 1 FROM public.people p
14
+ JOIN public.roles r ON p.role_id = r.id
15
+ WHERE p.id = actor_id AND r.slug = 'system_admin'
16
+ ) THEN
17
+ RETURN QUERY SELECT a.id FROM public.accounts a;
18
+ RETURN;
19
+ END IF;
20
+ RETURN QUERY
21
+ WITH RECURSIVE account_tree AS (
22
+ SELECT p.account_id as id FROM public.people p WHERE p.id = actor_id
23
+ UNION ALL
24
+ SELECT ap.descendant_id
25
+ FROM public.account_paths ap
26
+ JOIN account_tree at ON ap.ancestor_id = at.id
27
+ )
28
+ SELECT id FROM account_tree;
29
+ END;
30
+ $$ LANGUAGE plpgsql SECURITY DEFINER;
31
+
32
+ -- Get current actor ID from auth
33
+ CREATE OR REPLACE FUNCTION public.current_actor_id()
34
+ RETURNS uuid AS $$
35
+ DECLARE
36
+ v_auth_id UUID;
37
+ v_person_id UUID;
38
+ BEGIN
39
+ v_auth_id := auth.uid();
40
+
41
+ -- Look up internal person ID from auth_uid
42
+ SELECT p.id INTO v_person_id
43
+ FROM public.people p
44
+ WHERE p.auth_uid = v_auth_id::text AND p.is_active = true
45
+ LIMIT 1;
46
+
47
+ RETURN v_person_id;
48
+ END;
49
+ $$ LANGUAGE plpgsql SECURITY DEFINER STABLE;
50
+
51
+ -- Check if person is system admin
52
+ CREATE OR REPLACE FUNCTION public.person_is_system_admin(person_uuid uuid)
53
+ RETURNS boolean AS $$
54
+ BEGIN
55
+ RETURN EXISTS (
56
+ SELECT 1 FROM public.people p
57
+ JOIN public.roles r ON p.role_id = r.id
58
+ WHERE p.id = person_uuid AND r.slug = 'system_admin'
59
+ );
60
+ END;
61
+ $$ LANGUAGE plpgsql STABLE;
62
+
63
+ -- Update account paths on account changes
64
+ CREATE OR REPLACE FUNCTION public.update_account_paths()
65
+ RETURNS TRIGGER AS $$
66
+ BEGIN
67
+ IF TG_OP = 'INSERT' THEN
68
+ IF NEW.parent_id IS NOT NULL THEN
69
+ INSERT INTO public.account_paths (ancestor_id, descendant_id, depth)
70
+ SELECT ancestor_id, NEW.id, depth + 1 FROM public.account_paths WHERE descendant_id = NEW.parent_id;
71
+ INSERT INTO public.account_paths (ancestor_id, descendant_id, depth) VALUES (NEW.parent_id, NEW.id, 1);
72
+ END IF;
73
+ RETURN NEW;
74
+ END IF;
75
+ IF TG_OP = 'UPDATE' AND OLD.parent_id IS DISTINCT FROM NEW.parent_id THEN
76
+ DELETE FROM public.account_paths WHERE descendant_id = NEW.id;
77
+ IF NEW.parent_id IS NOT NULL THEN
78
+ INSERT INTO public.account_paths (ancestor_id, descendant_id, depth)
79
+ SELECT ancestor_id, NEW.id, depth + 1 FROM public.account_paths WHERE descendant_id = NEW.parent_id;
80
+ INSERT INTO public.account_paths (ancestor_id, descendant_id, depth) VALUES (NEW.parent_id, NEW.id, 1);
81
+ END IF;
82
+ RETURN NEW;
83
+ END IF;
84
+ IF TG_OP = 'DELETE' THEN
85
+ DELETE FROM public.account_paths WHERE descendant_id = OLD.id;
86
+ RETURN OLD;
87
+ END IF;
88
+ RETURN NULL;
89
+ END;
90
+ $$ LANGUAGE plpgsql;
91
+
92
+ -- Trigger for account paths
93
+ CREATE TRIGGER account_paths_trigger
94
+ AFTER INSERT OR UPDATE OR DELETE ON public.accounts
95
+ FOR EACH ROW EXECUTE FUNCTION public.update_account_paths();
96
+
97
+ -- set_updated_at trigger function (from 014_webhook_registry.sql)
98
+ CREATE OR REPLACE FUNCTION public.set_updated_at()
99
+ RETURNS TRIGGER AS $$
100
+ BEGIN
101
+ NEW.updated_at = now();
102
+ RETURN NEW;
103
+ END;
104
+ $$ LANGUAGE plpgsql;
105
+
106
+ -- ============================================
107
+ -- FOREIGN KEYS
108
+ -- ============================================
109
+
110
+ -- accounts FKs
111
+ ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_parent
112
+ FOREIGN KEY (parent_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
113
+ ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_type
114
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
115
+ ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_app
116
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
117
+ ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_created_by
118
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
119
+ ALTER TABLE public.accounts ADD CONSTRAINT fk_accounts_updated_by
120
+ FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
121
+
122
+ -- people FKs
123
+ ALTER TABLE public.people ADD CONSTRAINT people_account_id_fkey
124
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
125
+ ALTER TABLE public.people ADD CONSTRAINT people_app_id_fkey
126
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
127
+ ALTER TABLE public.people ADD CONSTRAINT people_role_id_fkey
128
+ FOREIGN KEY (role_id) REFERENCES public.roles(id) ON DELETE SET NULL;
129
+ ALTER TABLE public.people ADD CONSTRAINT people_type_id_fkey
130
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
131
+ ALTER TABLE public.people ADD CONSTRAINT people_created_by_fkey
132
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
133
+
134
+ -- types FK
135
+ ALTER TABLE public.types ADD CONSTRAINT fk_types_app
136
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
137
+
138
+ -- apps FKs
139
+ ALTER TABLE public.apps ADD CONSTRAINT fk_apps_account
140
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
141
+ ALTER TABLE public.apps ADD CONSTRAINT fk_apps_owner_account
142
+ FOREIGN KEY (owner_account_id) REFERENCES public.accounts(id) ON DELETE SET NULL;
143
+
144
+ -- roles FKs
145
+ ALTER TABLE public.roles ADD CONSTRAINT fk_roles_app
146
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
147
+ ALTER TABLE public.roles ADD CONSTRAINT fk_roles_account
148
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
149
+
150
+ -- items FKs
151
+ ALTER TABLE public.items ADD CONSTRAINT fk_items_type
152
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
153
+ ALTER TABLE public.items ADD CONSTRAINT fk_items_account
154
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
155
+ ALTER TABLE public.items ADD CONSTRAINT fk_items_app
156
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE SET NULL;
157
+ ALTER TABLE public.items ADD CONSTRAINT fk_items_created_by
158
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
159
+ ALTER TABLE public.items ADD CONSTRAINT fk_items_updated_by
160
+ FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
161
+
162
+ -- threads FKs
163
+ ALTER TABLE public.threads ADD CONSTRAINT fk_threads_type
164
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
165
+ ALTER TABLE public.threads ADD CONSTRAINT fk_threads_account
166
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
167
+ ALTER TABLE public.threads ADD CONSTRAINT fk_threads_created_by
168
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
169
+ ALTER TABLE public.threads ADD CONSTRAINT fk_threads_updated_by
170
+ FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
171
+
172
+ -- messages FKs
173
+ ALTER TABLE public.messages ADD CONSTRAINT fk_messages_type
174
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
175
+ ALTER TABLE public.messages ADD CONSTRAINT fk_messages_thread
176
+ FOREIGN KEY (thread_id) REFERENCES public.threads(id) ON DELETE CASCADE;
177
+ ALTER TABLE public.messages ADD CONSTRAINT fk_messages_person
178
+ FOREIGN KEY (person_id) REFERENCES public.people(id) ON DELETE SET NULL;
179
+ ALTER TABLE public.messages ADD CONSTRAINT fk_messages_created_by
180
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
181
+
182
+ -- links FKs
183
+ ALTER TABLE public.links ADD CONSTRAINT fk_links_type
184
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
185
+ ALTER TABLE public.links ADD CONSTRAINT fk_links_account
186
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
187
+ ALTER TABLE public.links ADD CONSTRAINT fk_links_link_type
188
+ FOREIGN KEY (link_type_id) REFERENCES public.link_types(id) ON DELETE SET NULL;
189
+ ALTER TABLE public.links ADD CONSTRAINT fk_links_created_by
190
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
191
+ ALTER TABLE public.links ADD CONSTRAINT fk_links_updated_by
192
+ FOREIGN KEY (updated_by) REFERENCES public.people(id) ON DELETE SET NULL;
193
+
194
+ -- attachments FKs
195
+ ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_type
196
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
197
+ ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_account
198
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
199
+ ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_uploaded_by
200
+ FOREIGN KEY (uploaded_by) REFERENCES public.people(id) ON DELETE SET NULL;
201
+ ALTER TABLE public.attachments ADD CONSTRAINT fk_attachments_created_by
202
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
203
+
204
+ -- watchers FKs
205
+ ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_type
206
+ FOREIGN KEY (type_id) REFERENCES public.types(id) ON DELETE RESTRICT;
207
+ ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_account
208
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
209
+ ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_person
210
+ FOREIGN KEY (person_id) REFERENCES public.people(id) ON DELETE CASCADE;
211
+ ALTER TABLE public.watchers ADD CONSTRAINT fk_watchers_created_by
212
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
213
+
214
+ -- link_types FK
215
+ ALTER TABLE public.link_types ADD CONSTRAINT fk_link_types_app
216
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
217
+
218
+ -- pipelines FKs
219
+ ALTER TABLE public.pipelines ADD CONSTRAINT fk_pipelines_app
220
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
221
+ ALTER TABLE public.pipelines ADD CONSTRAINT fk_pipelines_account
222
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
223
+ ALTER TABLE public.pipelines ADD CONSTRAINT fk_pipelines_created_by
224
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
225
+
226
+ -- pipeline_executions FKs
227
+ ALTER TABLE public.pipeline_executions ADD CONSTRAINT fk_pe_pipeline
228
+ FOREIGN KEY (pipeline_id) REFERENCES public.pipelines(id) ON DELETE CASCADE;
229
+ ALTER TABLE public.pipeline_executions ADD CONSTRAINT fk_pe_account
230
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
231
+ ALTER TABLE public.pipeline_executions ADD CONSTRAINT fk_pe_created_by
232
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
233
+
234
+ -- triggers FKs
235
+ ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_app
236
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
237
+ ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_account
238
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
239
+ ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_pipeline
240
+ FOREIGN KEY (pipeline_id) REFERENCES public.pipelines(id) ON DELETE SET NULL;
241
+ ALTER TABLE public.triggers ADD CONSTRAINT fk_triggers_created_by
242
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
243
+
244
+ -- trigger_executions FK
245
+ ALTER TABLE public.trigger_executions ADD CONSTRAINT fk_te_trigger
246
+ FOREIGN KEY (trigger_id) REFERENCES public.triggers(id) ON DELETE CASCADE;
247
+
248
+ -- timers FKs
249
+ ALTER TABLE public.timers ADD CONSTRAINT fk_timers_app
250
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
251
+ ALTER TABLE public.timers ADD CONSTRAINT fk_timers_account
252
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
253
+ ALTER TABLE public.timers ADD CONSTRAINT fk_timers_pipeline
254
+ FOREIGN KEY (pipeline_id) REFERENCES public.pipelines(id) ON DELETE SET NULL;
255
+ ALTER TABLE public.timers ADD CONSTRAINT fk_timers_created_by
256
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
257
+
258
+ -- ai_agents FKs
259
+ ALTER TABLE public.ai_agents ADD CONSTRAINT fk_ai_agents_app
260
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
261
+ ALTER TABLE public.ai_agents ADD CONSTRAINT fk_ai_agents_account
262
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
263
+ ALTER TABLE public.ai_agents ADD CONSTRAINT fk_ai_agents_created_by
264
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
265
+
266
+ -- embeddings FK
267
+ ALTER TABLE public.embeddings ADD CONSTRAINT fk_embeddings_account
268
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
269
+
270
+ -- integrations FKs
271
+ ALTER TABLE public.integrations ADD CONSTRAINT fk_integrations_app
272
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
273
+ ALTER TABLE public.integrations ADD CONSTRAINT fk_integrations_account
274
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
275
+ ALTER TABLE public.integrations ADD CONSTRAINT fk_integrations_created_by
276
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
277
+
278
+ -- prompt_configs FKs
279
+ ALTER TABLE public.prompt_configs ADD CONSTRAINT fk_prompt_configs_app
280
+ FOREIGN KEY (app_id) REFERENCES public.apps(id) ON DELETE CASCADE;
281
+ ALTER TABLE public.prompt_configs ADD CONSTRAINT fk_prompt_configs_account
282
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
283
+ ALTER TABLE public.prompt_configs ADD CONSTRAINT fk_prompt_configs_created_by
284
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
285
+
286
+ -- api_keys FKs
287
+ ALTER TABLE public.api_keys ADD CONSTRAINT fk_api_keys_integration
288
+ FOREIGN KEY (integration_id) REFERENCES public.integrations(id) ON DELETE SET NULL;
289
+ ALTER TABLE public.api_keys ADD CONSTRAINT fk_api_keys_account
290
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
291
+ ALTER TABLE public.api_keys ADD CONSTRAINT fk_api_keys_created_by
292
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
293
+
294
+ -- api_key_usage_logs FKs
295
+ ALTER TABLE public.api_key_usage_logs ADD CONSTRAINT fk_akul_api_key
296
+ FOREIGN KEY (api_key_id) REFERENCES public.api_keys(id) ON DELETE CASCADE;
297
+ ALTER TABLE public.api_key_usage_logs ADD CONSTRAINT fk_akul_account
298
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
299
+
300
+ -- logs FKs
301
+ ALTER TABLE public.logs ADD CONSTRAINT fk_logs_person
302
+ FOREIGN KEY (person_id) REFERENCES public.people(id) ON DELETE SET NULL;
303
+ ALTER TABLE public.logs ADD CONSTRAINT fk_logs_account
304
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
305
+
306
+ -- actions FKs
307
+ ALTER TABLE public.actions ADD CONSTRAINT fk_actions_account
308
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
309
+ ALTER TABLE public.actions ADD CONSTRAINT fk_actions_machine_principal
310
+ FOREIGN KEY (default_machine_principal_id) REFERENCES public.api_keys(id) ON DELETE SET NULL;
311
+ ALTER TABLE public.actions ADD CONSTRAINT fk_actions_created_by
312
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
313
+
314
+ -- schedules FKs
315
+ ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_account
316
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
317
+ ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_action
318
+ FOREIGN KEY (action_id) REFERENCES public.actions(id) ON DELETE CASCADE;
319
+ ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_machine
320
+ FOREIGN KEY (machine_principal_id) REFERENCES public.api_keys(id) ON DELETE SET NULL;
321
+ ALTER TABLE public.schedules ADD CONSTRAINT fk_schedules_created_by
322
+ FOREIGN KEY (created_by) REFERENCES public.people(id) ON DELETE SET NULL;
323
+
324
+ -- schedule_executions FKs
325
+ ALTER TABLE public.schedule_executions ADD CONSTRAINT fk_se_schedule
326
+ FOREIGN KEY (schedule_id) REFERENCES public.schedules(id) ON DELETE CASCADE;
327
+ ALTER TABLE public.schedule_executions ADD CONSTRAINT fk_se_account
328
+ FOREIGN KEY (account_id) REFERENCES public.accounts(id) ON DELETE CASCADE;
329
+ ALTER TABLE public.schedule_executions ADD CONSTRAINT fk_se_machine
330
+ FOREIGN KEY (machine_principal_id) REFERENCES public.api_keys(id) ON DELETE SET NULL;
331
+
332
+ -- ============================================
333
+ -- INDEXES
334
+ -- ============================================
335
+
336
+ -- account_paths indexes
337
+ CREATE INDEX idx_account_paths_ancestor ON public.account_paths(ancestor_id);
338
+ CREATE INDEX idx_account_paths_descendant ON public.account_paths(descendant_id);
339
+
340
+ -- accounts
341
+ CREATE INDEX idx_accounts_parent_id ON public.accounts(parent_id);
342
+ CREATE INDEX idx_accounts_type_id ON public.accounts(type_id);
343
+ CREATE INDEX idx_accounts_active ON public.accounts(is_active);
344
+
345
+ -- people
346
+ CREATE INDEX idx_people_account ON public.people(account_id);
347
+ CREATE INDEX idx_people_auth_uid ON public.people(auth_uid);
348
+ CREATE INDEX idx_people_email ON public.people(email);
349
+ CREATE INDEX idx_people_role ON public.people(role_id);
350
+ CREATE INDEX idx_people_active ON public.people(is_active);
351
+
352
+ -- types
353
+ CREATE INDEX idx_types_slug ON public.types(slug);
354
+ CREATE INDEX idx_types_kind ON public.types(kind);
355
+
356
+ -- apps
357
+ CREATE INDEX idx_apps_slug ON public.apps(slug);
358
+ CREATE INDEX idx_apps_owner ON public.apps(owner_account_id);
359
+ CREATE UNIQUE INDEX idx_apps_route_prefix ON public.apps(route_prefix)
360
+ WHERE is_active = true AND route_prefix IS NOT NULL;
361
+
362
+ -- roles
363
+ CREATE INDEX idx_roles_slug ON public.roles(slug);
364
+
365
+ -- items
366
+ CREATE INDEX idx_items_type ON public.items(type_id);
367
+ CREATE INDEX idx_items_account ON public.items(account_id);
368
+ CREATE INDEX idx_items_active ON public.items(is_active);
369
+ CREATE INDEX idx_items_created_at ON public.items(created_at);
370
+
371
+ -- threads
372
+ CREATE INDEX idx_threads_type ON public.threads(type_id);
373
+ CREATE INDEX idx_threads_account ON public.threads(account_id);
374
+ CREATE INDEX idx_threads_target ON public.threads(target_type, target_id);
375
+ CREATE INDEX idx_threads_active ON public.threads(is_active);
376
+
377
+ -- messages
378
+ CREATE INDEX idx_messages_thread ON public.messages(thread_id);
379
+ CREATE INDEX idx_messages_type ON public.messages(type_id);
380
+ CREATE INDEX idx_messages_person ON public.messages(person_id);
381
+
382
+ -- links
383
+ CREATE INDEX idx_links_type ON public.links(type_id);
384
+ CREATE INDEX idx_links_account ON public.links(account_id);
385
+ CREATE INDEX idx_links_source ON public.links(source_type, source_id);
386
+ CREATE INDEX idx_links_target ON public.links(target_type, target_id);
387
+
388
+ -- attachments
389
+ CREATE INDEX idx_attachments_type ON public.attachments(type_id);
390
+ CREATE INDEX idx_attachments_account ON public.attachments(account_id);
391
+
392
+ -- watchers
393
+ CREATE INDEX idx_watchers_type ON public.watchers(type_id);
394
+ CREATE INDEX idx_watchers_target ON public.watchers(target_type, target_id);
395
+ CREATE INDEX idx_watchers_person ON public.watchers(person_id);
396
+
397
+ -- link_types
398
+ CREATE INDEX idx_link_types_slug ON public.link_types(slug);
399
+
400
+ -- pipelines
401
+ CREATE INDEX idx_pipelines_app ON public.pipelines(app_id);
402
+ CREATE INDEX idx_pipelines_account ON public.pipelines(account_id);
403
+
404
+ -- pipeline_executions
405
+ CREATE INDEX idx_pe_pipeline ON public.pipeline_executions(pipeline_id);
406
+ CREATE INDEX idx_pe_account ON public.pipeline_executions(account_id);
407
+ CREATE INDEX idx_pe_status ON public.pipeline_executions(status);
408
+
409
+ -- triggers
410
+ CREATE INDEX idx_triggers_app ON public.triggers(app_id);
411
+ CREATE INDEX idx_triggers_account ON public.triggers(account_id);
412
+ CREATE INDEX idx_triggers_pipeline ON public.triggers(pipeline_id);
413
+
414
+ -- trigger_executions
415
+ CREATE INDEX idx_te_trigger ON public.trigger_executions(trigger_id);
416
+
417
+ -- timers
418
+ CREATE INDEX idx_timers_app ON public.timers(app_id);
419
+ CREATE INDEX idx_timers_account ON public.timers(account_id);
420
+ CREATE INDEX idx_timers_pipeline ON public.timers(pipeline_id);
421
+
422
+ -- ai_agents
423
+ CREATE INDEX idx_ai_agents_app ON public.ai_agents(app_id);
424
+ CREATE INDEX idx_ai_agents_account ON public.ai_agents(account_id);
425
+
426
+ -- embeddings
427
+ CREATE INDEX idx_embeddings_account ON public.embeddings(account_id);
428
+ CREATE INDEX idx_embeddings_model_doc ON public.embeddings(model_id, document_id);
429
+
430
+ -- integrations
431
+ CREATE INDEX idx_integrations_app ON public.integrations(app_id);
432
+ CREATE INDEX idx_integrations_account ON public.integrations(account_id);
433
+
434
+ -- prompt_configs
435
+ CREATE INDEX idx_prompt_configs_slug ON public.prompt_configs(slug);
436
+ CREATE INDEX idx_prompt_configs_app ON public.prompt_configs(app_id);
437
+ CREATE INDEX idx_prompt_configs_account ON public.prompt_configs(account_id);
438
+
439
+ -- api_keys
440
+ CREATE INDEX idx_api_keys_integration ON public.api_keys(integration_id);
441
+ CREATE INDEX idx_api_keys_account ON public.api_keys(account_id);
442
+ CREATE INDEX idx_api_keys_key_value ON public.api_keys(key_value);
443
+
444
+ -- api_key_usage_logs
445
+ CREATE INDEX idx_akul_api_key ON public.api_key_usage_logs(api_key_id);
446
+ CREATE INDEX idx_akul_account ON public.api_key_usage_logs(account_id);
447
+ CREATE INDEX idx_akul_created ON public.api_key_usage_logs(created_at);
448
+
449
+ -- logs
450
+ CREATE INDEX idx_logs_account ON public.logs(account_id);
451
+ CREATE INDEX idx_logs_level ON public.logs(level);
452
+ CREATE INDEX idx_logs_created ON public.logs(created_at);
453
+ CREATE INDEX idx_logs_source ON public.logs(source_type, source_id);
454
+
455
+ -- actions
456
+ CREATE INDEX idx_actions_account ON public.actions(account_id);
457
+ CREATE INDEX idx_actions_slug ON public.actions(slug);
458
+
459
+ -- schedules
460
+ CREATE INDEX idx_schedules_account ON public.schedules(account_id);
461
+ CREATE INDEX idx_schedules_action ON public.schedules(action_id);
462
+ CREATE INDEX idx_schedules_next_run ON public.schedules(next_run_at);
463
+
464
+ -- schedule_executions
465
+ CREATE INDEX idx_se_schedule ON public.schedule_executions(schedule_id);
466
+ CREATE INDEX idx_se_account ON public.schedule_executions(account_id);
467
+
468
+ -- test tables
469
+ CREATE INDEX idx_test_results_run_id ON public.test_results(run_id);
470
+ CREATE INDEX idx_test_runs_suite ON public.test_runs(suite);
471
+ CREATE INDEX idx_test_runs_started_at ON public.test_runs(started_at DESC);
472
+
473
+ -- item_progress
474
+ CREATE INDEX idx_item_progress_person ON public.item_progress(person_id);
475
+ CREATE INDEX idx_item_progress_item ON public.item_progress(item_id);
476
+ CREATE INDEX idx_item_progress_account ON public.item_progress(account_id);
477
+ CREATE INDEX idx_item_progress_type ON public.item_progress(type_id);
478
+ CREATE INDEX idx_item_progress_status ON public.item_progress(status);
479
+ CREATE INDEX idx_item_progress_data_gin ON public.item_progress USING gin(data);
480
+ CREATE INDEX idx_item_progress_active ON public.item_progress(account_id, is_active) WHERE is_active = true;
481
+
482
+ -- webhook_handlers
483
+ CREATE INDEX idx_webhook_handlers_name ON public.webhook_handlers(name) WHERE is_active = true AND is_deleted = false;
484
+ CREATE INDEX idx_webhook_handlers_account ON public.webhook_handlers(account_id) WHERE is_deleted = false;
485
+ CREATE INDEX idx_webhook_handlers_events ON public.webhook_handlers USING GIN(events) WHERE is_active = true;
486
+
487
+ -- ============================================
488
+ -- ENABLE RLS ON ALL TABLES
489
+ -- ============================================
490
+
491
+ ALTER TABLE public.accounts ENABLE ROW LEVEL SECURITY;
492
+ ALTER TABLE public.people ENABLE ROW LEVEL SECURITY;
493
+ ALTER TABLE public.types ENABLE ROW LEVEL SECURITY;
494
+ ALTER TABLE public.apps ENABLE ROW LEVEL SECURITY;
495
+ ALTER TABLE public.roles ENABLE ROW LEVEL SECURITY;
496
+ ALTER TABLE public.items ENABLE ROW LEVEL SECURITY;
497
+ ALTER TABLE public.threads ENABLE ROW LEVEL SECURITY;
498
+ ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
499
+ ALTER TABLE public.links ENABLE ROW LEVEL SECURITY;
500
+ ALTER TABLE public.attachments ENABLE ROW LEVEL SECURITY;
501
+ ALTER TABLE public.watchers ENABLE ROW LEVEL SECURITY;
502
+ ALTER TABLE public.link_types ENABLE ROW LEVEL SECURITY;
503
+ ALTER TABLE public.pipelines ENABLE ROW LEVEL SECURITY;
504
+ ALTER TABLE public.pipeline_executions ENABLE ROW LEVEL SECURITY;
505
+ ALTER TABLE public.triggers ENABLE ROW LEVEL SECURITY;
506
+ ALTER TABLE public.trigger_executions ENABLE ROW LEVEL SECURITY;
507
+ ALTER TABLE public.timers ENABLE ROW LEVEL SECURITY;
508
+ ALTER TABLE public.ai_agents ENABLE ROW LEVEL SECURITY;
509
+ ALTER TABLE public.embeddings ENABLE ROW LEVEL SECURITY;
510
+ ALTER TABLE public.integrations ENABLE ROW LEVEL SECURITY;
511
+ ALTER TABLE public.prompt_configs ENABLE ROW LEVEL SECURITY;
512
+ ALTER TABLE public.api_keys ENABLE ROW LEVEL SECURITY;
513
+ ALTER TABLE public.api_key_usage_logs ENABLE ROW LEVEL SECURITY;
514
+ ALTER TABLE public.logs ENABLE ROW LEVEL SECURITY;
515
+ ALTER TABLE public.actions ENABLE ROW LEVEL SECURITY;
516
+ ALTER TABLE public.schedules ENABLE ROW LEVEL SECURITY;
517
+ ALTER TABLE public.schedule_executions ENABLE ROW LEVEL SECURITY;
518
+ ALTER TABLE public.item_progress ENABLE ROW LEVEL SECURITY;
519
+ ALTER TABLE public.webhook_handlers ENABLE ROW LEVEL SECURITY;
520
+
521
+ -- ============================================
522
+ -- RLS POLICIES
523
+ -- ============================================
524
+
525
+ -- accounts
526
+ CREATE POLICY accounts_access ON public.accounts FOR ALL
527
+ USING (id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
528
+ OR public.current_actor_id() IS NULL);
529
+
530
+ -- people
531
+ CREATE POLICY people_access ON public.people FOR ALL
532
+ USING (account_id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
533
+ OR public.current_actor_id() IS NULL);
534
+
535
+ -- types
536
+ CREATE POLICY types_access ON public.types FOR ALL
537
+ USING (public.current_actor_id() IS NOT NULL OR true);
538
+
539
+ -- apps
540
+ CREATE POLICY apps_access ON public.apps FOR ALL
541
+ USING (is_system = true OR owner_account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
542
+ OR auth.uid() IS NULL);
543
+
544
+ -- roles
545
+ CREATE POLICY roles_read ON public.roles FOR SELECT USING (true);
546
+ CREATE POLICY roles_modify ON public.roles FOR ALL
547
+ USING ((is_system = true AND public.person_is_system_admin(public.current_actor_id()))
548
+ OR is_system = false OR public.current_actor_id() IS NULL);
549
+
550
+ -- items (with platform scope visibility from 012_rls_scope.sql)
551
+ CREATE POLICY items_access ON public.items FOR ALL
552
+ USING (
553
+ account_id IN (SELECT public.get_accessible_accounts(public.current_actor_id()))
554
+ OR (design_schema->>'scope' = 'platform' AND public.current_actor_id() IS NOT NULL)
555
+ OR public.current_actor_id() IS NULL
556
+ );
557
+
558
+ -- threads (with platform scope visibility from 012_rls_scope.sql)
559
+ CREATE POLICY threads_access ON public.threads FOR ALL
560
+ USING (
561
+ account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
562
+ OR (design_schema->>'scope' = 'platform' AND auth.uid() IS NOT NULL)
563
+ OR auth.uid() IS NULL
564
+ );
565
+
566
+ -- messages
567
+ CREATE POLICY messages_access ON public.messages FOR ALL
568
+ USING (EXISTS (SELECT 1 FROM public.threads t
569
+ WHERE t.id = messages.thread_id
570
+ AND t.account_id IN (SELECT public.get_accessible_accounts(auth.uid())))
571
+ OR auth.uid() IS NULL);
572
+
573
+ -- links (with platform scope visibility from 012_rls_scope.sql)
574
+ CREATE POLICY links_access ON public.links FOR ALL
575
+ USING (
576
+ account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
577
+ OR (design_schema->>'scope' = 'platform' AND auth.uid() IS NOT NULL)
578
+ OR auth.uid() IS NULL
579
+ );
580
+
581
+ -- attachments (with platform scope visibility from 012_rls_scope.sql)
582
+ CREATE POLICY attachments_access ON public.attachments FOR ALL
583
+ USING (
584
+ account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
585
+ OR (design_schema->>'scope' = 'platform' AND auth.uid() IS NOT NULL)
586
+ OR auth.uid() IS NULL
587
+ );
588
+
589
+ -- watchers
590
+ CREATE POLICY watchers_access ON public.watchers FOR ALL
591
+ USING (auth.uid() IS NOT NULL OR true);
592
+
593
+ -- link_types
594
+ CREATE POLICY link_types_access ON public.link_types FOR ALL
595
+ USING (app_id IS NULL
596
+ OR EXISTS (SELECT 1 FROM public.apps a WHERE a.id = link_types.app_id
597
+ AND a.owner_account_id IN (SELECT public.get_accessible_accounts(auth.uid())))
598
+ OR auth.uid() IS NULL);
599
+
600
+ -- pipelines
601
+ CREATE POLICY pipelines_access ON public.pipelines FOR ALL
602
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
603
+ OR auth.uid() IS NULL);
604
+
605
+ -- pipeline_executions
606
+ CREATE POLICY pe_access ON public.pipeline_executions FOR ALL
607
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
608
+ OR auth.uid() IS NULL);
609
+
610
+ -- triggers
611
+ CREATE POLICY triggers_access ON public.triggers FOR ALL
612
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
613
+ OR auth.uid() IS NULL);
614
+
615
+ -- trigger_executions
616
+ CREATE POLICY te_access ON public.trigger_executions FOR ALL
617
+ USING (EXISTS (SELECT 1 FROM public.triggers t
618
+ WHERE t.id = trigger_executions.trigger_id
619
+ AND t.account_id IN (SELECT public.get_accessible_accounts(auth.uid())))
620
+ OR auth.uid() IS NULL);
621
+
622
+ -- timers
623
+ CREATE POLICY timers_access ON public.timers FOR ALL
624
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
625
+ OR auth.uid() IS NULL);
626
+
627
+ -- ai_agents
628
+ CREATE POLICY ai_agents_access ON public.ai_agents FOR ALL
629
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
630
+ OR auth.uid() IS NULL);
631
+
632
+ -- embeddings
633
+ CREATE POLICY embeddings_access ON public.embeddings FOR ALL
634
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
635
+ OR auth.uid() IS NULL);
636
+
637
+ -- integrations
638
+ CREATE POLICY integrations_access ON public.integrations FOR ALL
639
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
640
+ OR auth.uid() IS NULL);
641
+
642
+ -- prompt_configs
643
+ CREATE POLICY prompt_configs_access ON public.prompt_configs FOR ALL
644
+ USING ((app_id IS NULL OR EXISTS (SELECT 1 FROM public.apps a
645
+ WHERE a.id = prompt_configs.app_id
646
+ AND a.owner_account_id IN (SELECT public.get_accessible_accounts(auth.uid()))))
647
+ OR auth.uid() IS NULL);
648
+
649
+ -- api_keys
650
+ CREATE POLICY api_keys_access ON public.api_keys FOR ALL
651
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
652
+ OR id = auth.uid() OR auth.uid() IS NULL);
653
+
654
+ -- api_key_usage_logs
655
+ CREATE POLICY akul_access ON public.api_key_usage_logs FOR ALL
656
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
657
+ OR auth.uid() IS NULL);
658
+
659
+ -- logs
660
+ CREATE POLICY logs_access ON public.logs FOR ALL
661
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
662
+ OR auth.uid() IS NULL);
663
+
664
+ -- actions
665
+ CREATE POLICY actions_access ON public.actions FOR ALL
666
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
667
+ OR auth.uid() IS NULL);
668
+
669
+ -- schedules
670
+ CREATE POLICY schedules_access ON public.schedules FOR ALL
671
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
672
+ OR auth.uid() IS NULL);
673
+
674
+ -- schedule_executions
675
+ CREATE POLICY se_access ON public.schedule_executions FOR ALL
676
+ USING (account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
677
+ OR auth.uid() IS NULL);
678
+
679
+ -- item_progress
680
+ CREATE POLICY item_progress_access ON public.item_progress FOR ALL
681
+ USING (
682
+ account_id IN (SELECT public.get_accessible_accounts(auth.uid()))
683
+ OR auth.uid() IS NULL
684
+ );
685
+
686
+ -- webhook_handlers
687
+ CREATE POLICY webhook_handlers_system_admin ON public.webhook_handlers
688
+ FOR ALL
689
+ TO authenticated
690
+ USING (
691
+ (SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin'
692
+ )
693
+ WITH CHECK (
694
+ (SELECT raw_user_meta_data->>'role' FROM auth.users WHERE id = auth.uid()) = 'system_admin'
695
+ );
696
+
697
+ CREATE POLICY webhook_handlers_service_role ON public.webhook_handlers
698
+ FOR ALL
699
+ TO service_role
700
+ USING (true)
701
+ WITH CHECK (true);
702
+
703
+ -- ============================================
704
+ -- TRIGGERS FOR UPDATED_AT
705
+ -- ============================================
706
+
707
+ CREATE TRIGGER webhook_handlers_updated_at
708
+ BEFORE UPDATE ON public.webhook_handlers
709
+ FOR EACH ROW
710
+ EXECUTE FUNCTION public.set_updated_at();