DROP FUNCTION IF EXISTS mocha_get_instance_by_global_identifier;

CREATE FUNCTION mocha_get_instance_by_global_identifier
(
    p_global_identifier CHAR(40)
)
RETURNS INT
RETURN (SELECT id FROM mocha_instances WHERE tenant_id = mocha_get_current_tenant() AND UPPER(global_identifier) = UPPER(mocha_normalize_uuid(p_global_identifier)) ORDER BY id DESC LIMIT 1);