USE pseudobot;

DELIMITER $$
CREATE PROCEDURE set_optimizer_switch_on() 
BEGIN
IF SUBSTRING(@@version,1,3) = '5.7' THEN
   SET optimizer_switch = 'derived_merge=on';
END IF;
END;$$

CREATE PROCEDURE create_user()
BEGIN
  DECLARE foo BIGINT DEFAULT 0;
  SELECT COUNT(*) INTO foo FROM mysql.user WHERE User = 'eighttrackAdmin' and  Host = 'localhost';
  IF foo = 0 THEN
    CREATE USER 'eighttrackAdmin'@'localhost' IDENTIFIED BY 'zeGAPgbH9HFbqmjRjmwzUDKv';
  END IF;
  SELECT COUNT(*) INTO foo FROM mysql.user WHERE User = 'eighttrackAdmin' and  Host = '%';
  IF foo = 0 THEN
    CREATE USER 'eighttrackAdmin'@'%' IDENTIFIED BY 'zeGAPgbH9HFbqmjRjmwzUDKv';
  END IF;
END ;$$

DELIMITER ;

CALL set_optimizer_switch_on();
DROP PROCEDURE set_optimizer_switch_on;

CALL create_user();
DROP PROCEDURE create_user;

GRANT SELECT,INSERT,UPDATE,DELETE ON eighttrack.* TO 'eighttrackAdmin'@'localhost';
GRANT SELECT,INSERT,UPDATE,DELETE ON eighttrack.* TO 'eighttrackAdmin'@'%';
FLUSH PRIVILEGES;