paperclip/packages/plugins/plugin-llm-wiki/migrations/003_spaces.sql

214 lines
13 KiB
MySQL
Raw Normal View History

[codex] Add LLM Wiki plugin package to master (#5716) ## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies. > - The plugin system is the extension surface for optional product capabilities without baking every workflow into core. > - The LLM Wiki plugin package was reviewed in stacked PR #5592, which targeted `pap-9173-llm-wiki-rest`. > - The stack base PR #5597 merged to `master` before #5592 was merged into that branch, so the plugin package never reached `master`. > - A direct PR from `pap-9173-llm-wiki-rest` back to `master` would be noisy because that branch has diverged from current `master`. > - This pull request reapplies the reviewed `packages/plugins/plugin-llm-wiki/` package onto current `master` and updates Docker deps-stage manifest coverage. > - The branch intentionally no longer changes `pnpm-workspace.yaml` after maintainer feedback; because the new package is now a root workspace importer, the remaining integration question is how maintainers want the root lockfile handled under the current PR policy. ## What Changed - Added the LLM Wiki plugin package under `packages/plugins/plugin-llm-wiki/` from the merged PR #5592 head. - Preserved the post-review cleanup from #5592: generated design/screenshot artifacts are not committed, and `src/ui/index.tsx` / `src/wiki.ts` are small public entrypoints. - Added the new plugin package manifest to the Docker deps stage so policy can validate package manifest coverage. - Removed the earlier `pnpm-workspace.yaml` exclusion per maintainer request, so the plugin is included by the existing `packages/plugins/*` workspace glob. ## Verification Current head: - PGlite migration harness: ran migrations 001-003, verified old non-space distillation unique constraints were removed, inserted duplicate cursor and work-item keys in a second space, then reran migration 003 successfully - `node ./scripts/check-docker-deps-stage.mjs` - `git diff --check` Known current-head install result after removing the workspace exclusion: - `pnpm install --frozen-lockfile` fails because `pnpm-lock.yaml` has no importer for `packages/plugins/plugin-llm-wiki/package.json`. Previously verified on the same plugin source before the workspace-exclusion removal: - `pnpm --filter @paperclipai/plugin-sdk build` - `cd packages/plugins/plugin-llm-wiki && pnpm install --lockfile=false && pnpm test` ## Risks - The branch now includes `packages/plugins/plugin-llm-wiki` in the root workspace but does not update `pnpm-lock.yaml`. Root frozen install will fail until maintainers choose a lockfile path that fits repo policy. - Committing `pnpm-lock.yaml` directly on this PR conflicts with the current PR policy check, while excluding the package from `pnpm-workspace.yaml` was rejected in maintainer feedback. - The package includes UI code already reviewed in #5592; generated screenshot/design artifacts were intentionally removed per maintainer request, so visual review should regenerate screenshots locally if needed. - The package depends on plugin host support from #5597, which is already merged to `master`. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI GPT-5 Codex via Codex CLI, tool use and local code execution enabled; context window not exposed. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have run the targeted checks listed above - [x] I have added or updated tests where applicable - [ ] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge Stack context: #5592 was merged into `pap-9173-llm-wiki-rest` after #5597 had already merged that branch to `master`, so this follow-up PR is needed to carry the plugin package itself into `master`. Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-11 20:45:41 -05:00
CREATE TABLE IF NOT EXISTS plugin_llm_wiki_8f50da974f.wiki_spaces (
id uuid PRIMARY KEY,
company_id uuid NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
wiki_id text NOT NULL DEFAULT 'default',
slug text NOT NULL,
display_name text NOT NULL,
space_type text NOT NULL DEFAULT 'local_folder',
folder_mode text NOT NULL DEFAULT 'managed_subfolder',
root_folder_key text NOT NULL DEFAULT 'wiki-root',
path_prefix text,
configured_root_path text,
access_scope text NOT NULL DEFAULT 'shared',
owner_user_id text,
owner_agent_id uuid REFERENCES public.agents(id) ON DELETE SET NULL,
team_key text,
settings jsonb NOT NULL DEFAULT '{}'::jsonb,
status text NOT NULL DEFAULT 'active',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (company_id, wiki_id, slug)
);
CREATE INDEX IF NOT EXISTS wiki_spaces_company_status_idx
ON plugin_llm_wiki_8f50da974f.wiki_spaces (company_id, wiki_id, status);
WITH wiki_pairs AS (
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_instances
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_sources
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_pages
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_page_revisions
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_operations
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_query_sessions
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_distillation_runs
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_source_snapshots
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_page_bindings
)
INSERT INTO plugin_llm_wiki_8f50da974f.wiki_spaces
(id, company_id, wiki_id, slug, display_name, space_type, folder_mode, root_folder_key, path_prefix, access_scope, status)
SELECT (
substr(md5(company_id::text || ':' || wiki_id || ':default'), 1, 8) || '-' ||
substr(md5(company_id::text || ':' || wiki_id || ':default'), 9, 4) || '-' ||
'4' || substr(md5(company_id::text || ':' || wiki_id || ':default'), 14, 3) || '-' ||
'8' || substr(md5(company_id::text || ':' || wiki_id || ':default'), 18, 3) || '-' ||
substr(md5(company_id::text || ':' || wiki_id || ':default'), 21, 12)
)::uuid,
company_id,
wiki_id,
'default',
'default',
'local_folder',
'managed_subfolder',
'wiki-root',
NULL,
'shared',
'active'
FROM wiki_pairs
ON CONFLICT (company_id, wiki_id, slug) DO NOTHING;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings ADD COLUMN IF NOT EXISTS space_id uuid;
UPDATE plugin_llm_wiki_8f50da974f.wiki_sources t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_pages t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_page_revisions t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_operations t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_query_sessions t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_page_bindings t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings ALTER COLUMN space_id SET NOT NULL;
Fix LLM Wiki package and migration validation (#6010) ## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies. > - Plugins extend the control plane with optional capabilities such as LLM Wiki. > - LLM Wiki needs its package assets and plugin-owned database migrations to work when installed from the packaged plugin. > - The bundled spaces migration used validation-hostile dynamic SQL, and the packaged plugin could omit non-dist runtime assets. > - This pull request makes the LLM Wiki package include its required assets and cuts the spaces migration over to explicit, idempotent SQL that passes the production plugin database validator. > - The benefit is a simpler plugin install path that validates and applies the bundled LLM Wiki migrations without adding plugin-specific legacy handling to Paperclip core. ## What Changed - Added the LLM Wiki package asset allowlist so agents, migrations, skills, templates, dist output, and README are included when packaged. - Renamed the bootstrap `.gitignore` template to `gitignore.template` and updated the runtime lookup so package tooling does not drop the hidden template file. - Relaxed plugin migration validation to allow namespace-scoped `INSERT`/`UPDATE` backfills and `CREATE INDEX` statements while continuing to reject destructive or cross-namespace SQL. - Replaced the LLM Wiki spaces migration's dynamic constraint-drop DO block with explicit `DROP CONSTRAINT IF EXISTS` statements. - Replaced fragile regex-source dispatch in SQL reference extraction with explicit capture-group descriptors. - Added regression coverage that applies the bundled LLM Wiki migrations through the production validator and checks the expected constraints. ## Verification - `pnpm exec vitest run --project @paperclipai/server server/src/__tests__/plugin-database.test.ts --pool=forks --poolOptions.forks.isolate=true` - `pnpm --filter @paperclipai/plugin-llm-wiki build` - `git diff --check` - Confirmed `pnpm-lock.yaml` is not included in the branch diff. ## Risks - Low migration risk for current users: LLM Wiki spaces are new, so this intentionally cuts over the plugin migration instead of adding legacy handling in core. - Validator behavior is broader than before, but still requires fully qualified plugin namespace targets, blocks deletes/destructive DDL, and keeps public table access read-only and allowlisted. > Checked [`ROADMAP.md`](ROADMAP.md); this is a targeted plugin packaging/migration fix and does not duplicate planned core feature work. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5 based coding agent, tool-enabled local repo access, reasoning mode managed by the Paperclip/Codex runtime. Exact context window was not surfaced in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-15 10:20:02 -05:00
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
DROP CONSTRAINT IF EXISTS wiki_pages_company_id_wiki_id_path_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
DROP CONSTRAINT IF EXISTS paperclip_distillation_cursor_company_id_wiki_id_source_sco_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
DROP CONSTRAINT IF EXISTS paperclip_distillation_work_i_company_id_wiki_id_idempotenc_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
DROP CONSTRAINT IF EXISTS paperclip_page_bindings_company_id_wiki_id_page_path_key;
[codex] Add LLM Wiki plugin package to master (#5716) ## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies. > - The plugin system is the extension surface for optional product capabilities without baking every workflow into core. > - The LLM Wiki plugin package was reviewed in stacked PR #5592, which targeted `pap-9173-llm-wiki-rest`. > - The stack base PR #5597 merged to `master` before #5592 was merged into that branch, so the plugin package never reached `master`. > - A direct PR from `pap-9173-llm-wiki-rest` back to `master` would be noisy because that branch has diverged from current `master`. > - This pull request reapplies the reviewed `packages/plugins/plugin-llm-wiki/` package onto current `master` and updates Docker deps-stage manifest coverage. > - The branch intentionally no longer changes `pnpm-workspace.yaml` after maintainer feedback; because the new package is now a root workspace importer, the remaining integration question is how maintainers want the root lockfile handled under the current PR policy. ## What Changed - Added the LLM Wiki plugin package under `packages/plugins/plugin-llm-wiki/` from the merged PR #5592 head. - Preserved the post-review cleanup from #5592: generated design/screenshot artifacts are not committed, and `src/ui/index.tsx` / `src/wiki.ts` are small public entrypoints. - Added the new plugin package manifest to the Docker deps stage so policy can validate package manifest coverage. - Removed the earlier `pnpm-workspace.yaml` exclusion per maintainer request, so the plugin is included by the existing `packages/plugins/*` workspace glob. ## Verification Current head: - PGlite migration harness: ran migrations 001-003, verified old non-space distillation unique constraints were removed, inserted duplicate cursor and work-item keys in a second space, then reran migration 003 successfully - `node ./scripts/check-docker-deps-stage.mjs` - `git diff --check` Known current-head install result after removing the workspace exclusion: - `pnpm install --frozen-lockfile` fails because `pnpm-lock.yaml` has no importer for `packages/plugins/plugin-llm-wiki/package.json`. Previously verified on the same plugin source before the workspace-exclusion removal: - `pnpm --filter @paperclipai/plugin-sdk build` - `cd packages/plugins/plugin-llm-wiki && pnpm install --lockfile=false && pnpm test` ## Risks - The branch now includes `packages/plugins/plugin-llm-wiki` in the root workspace but does not update `pnpm-lock.yaml`. Root frozen install will fail until maintainers choose a lockfile path that fits repo policy. - Committing `pnpm-lock.yaml` directly on this PR conflicts with the current PR policy check, while excluding the package from `pnpm-workspace.yaml` was rejected in maintainer feedback. - The package includes UI code already reviewed in #5592; generated screenshot/design artifacts were intentionally removed per maintainer request, so visual review should regenerate screenshots locally if needed. - The package depends on plugin host support from #5597, which is already merged to `master`. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI GPT-5 Codex via Codex CLI, tool use and local code execution enabled; context window not exposed. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have run the targeted checks listed above - [x] I have added or updated tests where applicable - [ ] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge Stack context: #5592 was merged into `pap-9173-llm-wiki-rest` after #5597 had already merged that branch to `master`, so this follow-up PR is needed to carry the plugin package itself into `master`. Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-11 20:45:41 -05:00
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
DROP CONSTRAINT IF EXISTS wiki_pages_company_wiki_space_path_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
ADD CONSTRAINT wiki_pages_company_wiki_space_path_key UNIQUE (company_id, wiki_id, space_id, path);
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
DROP CONSTRAINT IF EXISTS distillation_cursors_company_wiki_space_scope_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
ADD CONSTRAINT distillation_cursors_company_wiki_space_scope_key UNIQUE (company_id, wiki_id, space_id, source_scope, scope_key, source_kind);
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
DROP CONSTRAINT IF EXISTS distillation_work_items_company_wiki_space_idempotency_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
ADD CONSTRAINT distillation_work_items_company_wiki_space_idempotency_key UNIQUE (company_id, wiki_id, space_id, idempotency_key);
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
DROP CONSTRAINT IF EXISTS page_bindings_company_wiki_space_page_path_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
ADD CONSTRAINT page_bindings_company_wiki_space_page_path_key UNIQUE (company_id, wiki_id, space_id, page_path);
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources
DROP CONSTRAINT IF EXISTS wiki_sources_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources
ADD CONSTRAINT wiki_sources_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
DROP CONSTRAINT IF EXISTS wiki_pages_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
ADD CONSTRAINT wiki_pages_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions
DROP CONSTRAINT IF EXISTS wiki_page_revisions_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions
ADD CONSTRAINT wiki_page_revisions_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations
DROP CONSTRAINT IF EXISTS wiki_operations_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations
ADD CONSTRAINT wiki_operations_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions
DROP CONSTRAINT IF EXISTS wiki_query_sessions_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions
ADD CONSTRAINT wiki_query_sessions_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
DROP CONSTRAINT IF EXISTS paperclip_distillation_cursors_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
ADD CONSTRAINT paperclip_distillation_cursors_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
DROP CONSTRAINT IF EXISTS paperclip_distillation_work_items_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
ADD CONSTRAINT paperclip_distillation_work_items_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs
DROP CONSTRAINT IF EXISTS paperclip_distillation_runs_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs
ADD CONSTRAINT paperclip_distillation_runs_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots
DROP CONSTRAINT IF EXISTS paperclip_source_snapshots_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots
ADD CONSTRAINT paperclip_source_snapshots_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
DROP CONSTRAINT IF EXISTS paperclip_page_bindings_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
ADD CONSTRAINT paperclip_page_bindings_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
CREATE INDEX IF NOT EXISTS wiki_sources_space_idx ON plugin_llm_wiki_8f50da974f.wiki_sources (company_id, wiki_id, space_id, created_at DESC);
CREATE INDEX IF NOT EXISTS wiki_operations_space_idx ON plugin_llm_wiki_8f50da974f.wiki_operations (company_id, wiki_id, space_id, created_at DESC);
CREATE INDEX IF NOT EXISTS wiki_query_sessions_space_idx ON plugin_llm_wiki_8f50da974f.wiki_query_sessions (company_id, wiki_id, space_id, updated_at DESC);
CREATE INDEX IF NOT EXISTS distillation_runs_space_idx ON plugin_llm_wiki_8f50da974f.paperclip_distillation_runs (company_id, wiki_id, space_id, created_at DESC);