########################################################################
# Tests for Implement LOCK FOR BACKUP (MDEV-5336)
########################################################################
# Check a non transactional table per ENGINE = Aria TRANSACTIONAL = 0.
#

--source include/not_embedded.inc
# As non transactional engine we use Aria with TRANSACTIONAL = 0
--source include/have_aria.inc

# Following connections are used in a few of the following tests
connect (con1,localhost,root,,);

SET SESSION lock_wait_timeout = 1;

--echo #-----------------------------------------------------------------------
--echo # Single-threaded tests
--echo #-----------------------------------------------------------------------

--echo # Show the fate and impact of some SELECT /HANDLER ... READ
--echo # sliding through the sequence.
CREATE TABLE t_permanent_aria (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
INSERT INTO t_permanent_aria SET col1 = 1;
BACKUP STAGE START;
SELECT COUNT(*) FROM t_permanent_aria;
HANDLER t_permanent_aria OPEN;
HANDLER t_permanent_aria READ FIRST;
HANDLER t_permanent_aria CLOSE;
BACKUP STAGE FLUSH;
SELECT COUNT(*) FROM t_permanent_aria;
HANDLER t_permanent_aria OPEN;
HANDLER t_permanent_aria READ FIRST;
HANDLER t_permanent_aria CLOSE;
BACKUP STAGE BLOCK_DDL;
SELECT COUNT(*) FROM t_permanent_aria;
HANDLER t_permanent_aria OPEN;
HANDLER t_permanent_aria READ FIRST;
HANDLER t_permanent_aria CLOSE;
BACKUP STAGE BLOCK_COMMIT;
SELECT COUNT(*) FROM t_permanent_aria;
HANDLER t_permanent_aria OPEN;
HANDLER t_permanent_aria READ FIRST;
HANDLER t_permanent_aria CLOSE;
BACKUP STAGE END;

--echo # In case the backup lock is taken by the current connection than
--echo # - DML modifying some permanent table is not allowed
BACKUP STAGE START;
SET AUTOCOMMIT = 0;
--error ER_BACKUP_LOCK_IS_ACTIVE
INSERT INTO t_permanent_aria SET col1 = 1;
SET AUTOCOMMIT = 1;
--error ER_BACKUP_LOCK_IS_ACTIVE
INSERT INTO t_permanent_aria SET col1 = 1;

--echo # - DDL creating or renaming a permanent table or a procedure is not
--echo #   allowed.
--echo #   The latter tries to modify a permanent system table.

--error ER_BACKUP_LOCK_IS_ACTIVE
CREATE TABLE throw_away (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
--error ER_BACKUP_LOCK_IS_ACTIVE
RENAME TABLE t_permanent_aria To throw_away;
--echo # - DDL creating a temporary table is allowed.
CREATE TEMPORARY TABLE t_temporary_aria (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
--echo # - DML modifying that temporary table is allowed.
INSERT INTO t_temporary_aria SET col1 = 1;
SELECT COUNT(*) FROM t_temporary_aria;
BACKUP STAGE END;

--echo # Show the fate and impact of some auto committed INSERT into temporary
--echo # table sliding through the sequence.
SET AUTOCOMMIT = 1;
BACKUP STAGE START;
INSERT INTO t_temporary_aria SET col1 = 1;
BACKUP STAGE FLUSH;
INSERT INTO t_temporary_aria SET col1 = 1;
BACKUP STAGE BLOCK_DDL;
INSERT INTO t_temporary_aria SET col1 = 1;
BACKUP STAGE BLOCK_COMMIT;
INSERT INTO t_temporary_aria SET col1 = 1;
BACKUP STAGE END;
SELECT COUNT(*) FROM t_temporary_aria;

--echo # Show the fate and impact of some DROP/CREATE TEMPORARY TABLE sliding
--echo # through the sequence.

SET AUTOCOMMIT = 1;
BACKUP STAGE START;
DROP TEMPORARY TABLE t_temporary_aria;
CREATE TEMPORARY TABLE t_temporary_aria (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
BACKUP STAGE FLUSH;
DROP TEMPORARY TABLE t_temporary_aria;
CREATE TEMPORARY TABLE t_temporary_aria (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
BACKUP STAGE BLOCK_DDL;
DROP TEMPORARY TABLE t_temporary_aria;
CREATE TEMPORARY TABLE t_temporary_aria (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
BACKUP STAGE BLOCK_COMMIT;
DROP TEMPORARY TABLE t_temporary_aria;
CREATE TEMPORARY TABLE t_temporary_aria (col1 INT) ENGINE = Aria TRANSACTIONAL = 0;
BACKUP STAGE END;
--echo # Show that even more DDL on the temporary table is allowed.
BACKUP STAGE START;
TRUNCATE t_temporary_aria;
ALTER TABLE t_temporary_aria ADD COLUMN col2 INT;
ALTER TABLE t_temporary_aria ADD KEY idx(col2);
BACKUP STAGE END;

DROP TABLE t_permanent_aria;

--echo #-----------------------------------------------------------------------
--echo # Show that non transactional tables locks with BACKUP STAGE FLUSH
--echo #-----------------------------------------------------------------------

set session lock_wait_timeout=default;
create table t1 (a int) engine=aria transactional=0;
insert into t1 values (1), (2);

connection con1;
backup stage start;
backup stage flush;
connection default;
select * from t1;
--error ER_LOCK_WAIT_TIMEOUT
SET STATEMENT lock_wait_timeout=0 FOR INSERT INTO t1 values (3);
--send INSERT INTO t1 values (4)
connection con1;
backup stage end;
connection default;
--reap # send
select * from t1;
drop table t1;

--echo #-----------------------------------------------------------------------
--echo # Show that transactional tables doesn't lock with BACKUP STAGE FLUSH
--echo #-----------------------------------------------------------------------

set session lock_wait_timeout=default;
create table t1 (a int) engine=aria transactional=1 page_checksum=1;
insert into t1 values (1), (2);

connection con1;
backup stage start;
backup stage flush;
connection default;
INSERT INTO t1 values (4);
connection con1;
backup stage end;
connection default;
select * from t1;
drop table t1;

--echo #
--echo # Cleanup
--echo #

disconnect con1;
