-- Migration: Add cluster_id to storage_backends for layered storage config
-- This enables storage backends to be associated with specific clusters
-- Priority order: User Override > Workspace Default > Cluster Default > Shared Storage

-- Add cluster_id column to storage_backends table
ALTER TABLE storage_backends ADD COLUMN IF NOT EXISTS cluster_id VARCHAR(36) REFERENCES clusters(id) ON DELETE SET NULL;

-- Index for faster lookups by cluster
CREATE INDEX IF NOT EXISTS idx_storage_backends_cluster ON storage_backends(cluster_id);

-- Composite index for cluster + default storage lookup
CREATE INDEX IF NOT EXISTS idx_storage_backends_default_cluster ON storage_backends(cluster_id, is_default) WHERE cluster_id IS NOT NULL;

-- Add comment for documentation
COMMENT ON COLUMN storage_backends.cluster_id IS 'Associated cluster ID for cluster-level default storage. NULL means workspace or shared storage.';