use provisioning;
#set autocommit=0;
drop procedure if exists reset_voip_aig_autoincrement;
delimiter ;;
create procedure reset_voip_aig_autoincrement() begin

  SELECT COALESCE(MAX(id),1) into @max_aig_id FROM voip_aig_sequence;
  SELECT COALESCE(MAX(group_id),1) into @max_group_id FROM voip_allowed_ip_groups;
  SELECT (GREATEST(@max_aig_id,@max_group_id) + 1) into @max;

  set @alter_statement = concat('ALTER TABLE voip_aig_sequence AUTO_INCREMENT = ', @max);
  PREPARE stmt FROM @alter_statement;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;

end ;;
delimiter ;
call reset_voip_aig_autoincrement();
drop procedure reset_voip_aig_autoincrement;
