-- 931_add_refresh_tokens_updated_at.sql
-- Add updated_at column to refresh_tokens table to match GORM model



-- Add updated_at column if it doesn't exist
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_schema = 'public' 
        AND table_name = 'refresh_tokens' 
        AND column_name = 'updated_at'
    ) THEN
        ALTER TABLE public.refresh_tokens 
        ADD COLUMN updated_at TIMESTAMPTZ DEFAULT NOW();
        
        -- Update existing rows to have updated_at = created_at
        UPDATE public.refresh_tokens 
        SET updated_at = created_at 
        WHERE updated_at IS NULL;
        
        -- Make it NOT NULL after setting values
        ALTER TABLE public.refresh_tokens 
        ALTER COLUMN updated_at SET NOT NULL;
        
        RAISE NOTICE 'Added updated_at column to refresh_tokens table';
    ELSE
        RAISE NOTICE 'Column updated_at already exists in refresh_tokens table';
    END IF;
END $$;



