--source include/have_innodb.inc
--source include/not_embedded.inc
--source include/have_metadata_lock_info.inc

--echo #
--echo # Test privileges for BACKUP STAGES
--echo #
--disable_service_connection

set sql_mode="";

GRANT RELOAD ON *.* TO user1@localhost;
GRANT CREATE, DROP ON *.* TO user2@localhost;

--connect(con1, localhost, user1)
BACKUP STAGE START;
BACKUP STAGE END;
--echo # change_user must release backup lock
BACKUP STAGE START;
BACKUP STAGE FLUSH;
SELECT lock_mode FROM information_schema.metadata_lock_info WHERE lock_type='Backup lock';
change_user user2;
SELECT lock_mode FROM information_schema.metadata_lock_info WHERE lock_type='Backup lock';
--disconnect con1
--source include/wait_until_disconnected.inc
--connection default

--echo # A very low privileged user (-> con4) cannot acquire the backup lock

--connect(con1, localhost, user2)
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
BACKUP STAGE START;
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
BACKUP STAGE FLUSH;
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
BACKUP STAGE BLOCK_DDL;
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
BACKUP STAGE BLOCK_COMMIT;
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
BACKUP STAGE END;
--disconnect con1
--source include/wait_until_disconnected.inc

--connection default
DROP USER user1@localhost, user2@localhost;

--echo #
--echo # Test using BACKUP STAGES in a SP
--echo #

--error ER_SP_BADSTATEMENT
create procedure foo42()
  BACKUP STAGE START;

--enable_service_connection
