SET @tableExists := (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'nfse_tentativas_ip'
);

SET @sql := IF(@tableExists = 0,
'CREATE TABLE nfse_tentativas_ip (
id INT NOT NULL AUTO_INCREMENT,
ip VARCHAR(45) DEFAULT NULL,
tentativas INT DEFAULT 0,
bloqueado TINYINT(1) DEFAULT 0,
ultima_tentativa DATETIME DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;',
'SELECT "Tabela nfse_tentativas_ip já existe."'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;