-- Migration: Update sessions table for refresh token support
-- This migration handles existing sessions by adding default values for new required columns

-- First, make the new columns nullable to allow updating existing data
ALTER TABLE "session" ALTER COLUMN "refresh_token_hash" DROP NOT NULL;
ALTER TABLE "session" ALTER COLUMN "token_family" DROP NOT NULL;

-- Update existing sessions with placeholder values
-- These sessions will be invalidated and users will need to sign in again
UPDATE "session" 
SET 
  "refresh_token_hash" = 'legacy_session_' || id,
  "token_family" = 'legacy_family_' || id,
  "revoked_at" = NOW()
WHERE "refresh_token_hash" IS NULL;

-- Now make the columns NOT NULL again
ALTER TABLE "session" ALTER COLUMN "refresh_token_hash" SET NOT NULL;
ALTER TABLE "session" ALTER COLUMN "token_family" SET NOT NULL;
