DROP PROCEDURE IF EXISTS mocha_get_tenant_by_name;

CREATE PROCEDURE mocha_get_tenant_by_name
(
    IN p_tenant_name VARCHAR(32),
    IN p_effective_date DATETIME
)
BEGIN
    DECLARE z_effective_date DATETIME;

    IF p_effective_date IS NULL THEN
        SET z_effective_date = NOW();
    ELSE
        SET z_effective_date = p_effective_date;
    END IF;

    SELECT * FROM mocha_tenants
    WHERE tenant_name = p_tenant_name
    AND effective_date <= z_effective_date;

END;
