-- Migration 0008: Register workspace_id columns added via direct SQL in CC-044
-- These columns were added manually (ALTER TABLE). This migration records them for Drizzle tracking.
-- All ALTER TABLE statements use IF NOT EXISTS for idempotency.

ALTER TABLE "organizations" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "transactions" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "documents" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "tasks" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "goals" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "decisions" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "opportunities" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "observations" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "ai_requests" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "research_briefs" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;
ALTER TABLE "saved_segments" ADD COLUMN IF NOT EXISTS "workspace_id" uuid;

-- Create indexes for workspace-scoped queries on high-traffic tables
CREATE INDEX IF NOT EXISTS "organizations_workspace_idx" ON "organizations" ("workspace_id");
CREATE INDEX IF NOT EXISTS "tasks_workspace_idx" ON "tasks" ("workspace_id");
CREATE INDEX IF NOT EXISTS "goals_workspace_idx" ON "goals" ("workspace_id");
CREATE INDEX IF NOT EXISTS "transactions_workspace_idx" ON "transactions" ("workspace_id");
