-- ============================================================================
-- SCRIPT: 01_auditlog_prep.sql
-- DESCRIPTION: Modifies the 'auditlog' table Primary Key to include 'clock'.
--              This is REQUIRED for range partitioning by 'clock'.
-- ============================================================================

DO $$
BEGIN
    -- Check if PK needs modification
    -- Original PK is typically on (auditid) named 'auditlog_pkey'
    IF EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'auditlog_pkey' 
        AND conrelid = 'auditlog'::regclass
    ) THEN
        -- Verify if 'clock' is already in PK (basic check)
        -- Realistically, if 'auditlog_pkey' exists on default Zabbix, it's just (auditid).
        
        RAISE NOTICE 'Dropping existing Primary Key on auditlog...';
        ALTER TABLE auditlog DROP CONSTRAINT auditlog_pkey;
        
        RAISE NOTICE 'Creating new Primary Key on auditlog (auditid, clock)...';
        ALTER TABLE auditlog ADD PRIMARY KEY (auditid, clock);
    ELSE
        RAISE NOTICE 'Constraint auditlog_pkey not found. Skipping or already modified.';
    END IF;
END $$;
