-- Tablas para el seguimiento de las operaciones de SIM sin importar -- la cmpañia. CREATE TYPE order_types AS ENUM ('activate','preactivate','cancel','pause','reactivate','unknown'); CREATE TYPE order_status AS ENUM ( 'pending', -- Mensaje creado/enviado a RabbitMQ 'running', -- Consumidor ha cogido el mensaje (opcional) 'finished', -- Procesado correctamente 'failed', -- Falló, pero podría reintentarse (Pasar a delay?) 'dlx' -- Falló definitivamente y está en Dead Letter Exchange ); CREATE TABLE IF NOT EXISTS order_tracking ( order_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, correlation_id VARCHAR(255) NOT NULL, -- ID compartido con RabbitMQ (message_id) exchange VARCHAR(100), -- Exchange al que se envia (de momento solo hay 1 principal sin contar delay y dlx) routing_key VARCHAR(100), -- Routing key del mensaje payload JSONB, -- Duda si es optimo guardar la copia, es útil en caso de fallo -- Campos de reintentos? status order_status NOT NULL DEFAULT 'PENDING', retry_count INT DEFAULT 0, error_message TEXT, -- Razón del fallo -- error_stacktrace TEXT, -- Recomendación, no se hasta que punto es necesario start_date TIMESTAMP NOT NULL DEFAULT now(), update_date TIMESTAMP NOT NULL DEFAULT now(), finished_date TIMESTAMP ) CREATE TABALE IF NOT EXISTS order_history( history_id SERIAL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, order_id UUID REFERENCES queue_operations(operation_id) ON DELETE CASCADE, previous_status order_status NOT NULL, -- Siempre hay un estado anterior, para casos excepcioneale "unknown" new_status order_status NOT NULL, change_reason TEXT, change_date TIMESTAMP NOT NULL DEFAULT NOW(), );