-- Create a table for routing rules
CREATE TABLE routing_rules (
tenant_id uuid,
id INTEGER,
prefix prefix_range,
route_to TEXT,
priority INTEGER,
PRIMARY KEY (tenant_id, id)
);
-- Create prefix index
CREATE INDEX idx_routing ON routing_rules USING gist(prefix);
-- Insert sample routing rules
INSERT INTO routing_rules (tenant_id, id, prefix, route_to, priority) VALUES
('123e4567-e89b-12d3-a456-426614174000', 1, '1212', 'london-gateway-1', 100),
('123e4567-e89b-12d3-a456-426614174000', 2, '1', 'us-gateway-main', 50),
('123e4567-e89b-12d3-a456-426614174000', 3, '91', 'india-gateway-1', 75),
('123e4567-e89b-12d3-a456-426614174000', 4, '44', 'uk-gateway-main', 80),
('123e4567-e89b-12d3-a456-426614174000', 5, '86', 'china-gateway-1', 70);
-- Find route for a number
SELECT route_to
FROM routing_rules
WHERE prefix @> '12125551234'
ORDER BY priority DESC, length(prefix) DESC
LIMIT 1;