-- Migration: Add agency user roles and collaborator tracking -- Purpose: Support owner/collaborator hierarchy for agency users -- 1. Add agency_role column to users table (owner or collaborator) ALTER TABLE users ADD COLUMN IF NOT EXISTS agency_role VARCHAR(50) DEFAULT 'owner' CHECK (agency_role IN ('owner', 'collaborator')); -- 2. Add created_by column to track which user created this collaborator ALTER TABLE users ADD COLUMN IF NOT EXISTS created_by UUID REFERENCES users(id) ON DELETE SET NULL; -- 3. Update existing ADMIN_AGENCIA users to have 'owner' agency_role UPDATE users SET agency_role = 'owner' WHERE role = 'ADMIN_AGENCIA' AND agency_role IS NULL; -- 4. Add collaborator_created_at to track when the collaborator was added ALTER TABLE users ADD COLUMN IF NOT EXISTS collaborator_created_at TIMESTAMP WITH TIME ZONE; -- 5. Create index for faster queries CREATE INDEX IF NOT EXISTS idx_users_agency_role ON users(tenant_id, agency_role); CREATE INDEX IF NOT EXISTS idx_users_created_by ON users(created_by);