-- =============================================================================
-- AURGANIZE V6.2 - SESSIONS TABLE ROLLBACK
-- =============================================================================
-- Migration: 000002_add_sessions (DOWN)
-- Description: Removes sessions table and related objects
-- Author: Aurganize Team
-- Date: 2025-12-11
-- Version: 2.1 (Aligned to Tenant-less Sessions Model)
-- =============================================================================
-- This rollback migration removes the sessions table and all associated
-- database objects (indexes, policies, constraints).
--
-- CRITICAL WARNINGS:
-- 1. This PERMANENTLY DELETES all user sessions
-- 2. All users will be logged out immediately
-- 3. Active refresh tokens become invalid
-- 4. Cannot be undone without database backup
-- 5. Production impact: Users must re-login
--
-- SAFE ROLLBACK ORDER:
-- 1. Disable RLS
-- 2. Drop policies
-- 3. Drop triggers (if any)
-- 4. Drop constraints (implicit via table drop)
-- 5. Drop indexes
-- 6. Drop table
-- =============================================================================


-- =============================================================================
-- SECTION 1: DISABLE ROW-LEVEL SECURITY
-- =============================================================================

ALTER TABLE IF EXISTS sessions DISABLE ROW LEVEL SECURITY;


-- =============================================================================
-- SECTION 2: DROP RLS POLICIES
-- =============================================================================
-- Note: Tenant isolation policy removed in Option B. Only user-based policy exists.

DROP POLICY IF EXISTS sessions_user_isolation ON sessions;


-- =============================================================================
-- SECTION 3: DROP TRIGGERS
-- =============================================================================
-- The updated model does NOT include updated_at → no update trigger exists.
-- Still kept for safety in case older deployments had it.

DROP TRIGGER IF EXISTS update_sessions_updated_at ON sessions;


-- =============================================================================
-- SECTION 4: DROP INDEXES
-- =============================================================================
-- Explicit drops included even though DROP TABLE will remove all dependent indexes.

DROP INDEX IF EXISTS idx_sessions_user_id;
DROP INDEX IF EXISTS idx_sessions_active;
DROP INDEX IF EXISTS idx_sessions_expires_at;
DROP INDEX IF EXISTS idx_sessions_ip_address;
DROP INDEX IF EXISTS idx_sessions_last_used;


-- =============================================================================
-- SECTION 5: DROP TABLE
-- =============================================================================
-- CASCADE ensures removal of dependencies (FKs, RLS metadata, etc.)

DROP TABLE IF EXISTS sessions CASCADE;


-- =============================================================================
-- SECTION 6: VERIFICATION BLOCK
-- =============================================================================

DO $$
DECLARE
    table_exists BOOLEAN;
BEGIN
    SELECT EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_schema = 'public'
        AND table_name = 'sessions'
    ) INTO table_exists;

    IF table_exists THEN
        RAISE WARNING 'WARNING: sessions table still exists after rollback!';
    ELSE
        RAISE NOTICE 'SUCCESS: sessions table removed completely';
    END IF;
END $$;


-- =============================================================================
-- END OF ROLLBACK MIGRATION 000002_add_sessions.down.sql
-- =============================================================================
