# ==== Purpose ====
#
# Test that @@GLOBAL.ENFORCE_GTID_CONSISTENCY works as expected.
# When this variable is set to 1, the following should hold:
#
#  - CREATE TABLE ... SELECT should fail, unless it has the TEMPORARY
#    keyword.
#
#  - Non-transactional DML should fail, but non-transactional DDL
#    should succeed.
#
#  - CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE are only allowed
#    outside transactions.
#
# The restrictions only apply when @@SESSION.SQL_LOG_BIN =
# 0. Everything is allowed when @@SESSION.SQL_LOG_BIN = 1.
#
# ==== Implementation ====
#
# We cannot start the server with --enforce-gtid-consistency=1
# initially, because check-testcase updates MyISAM tables.  Instead,
# we let the server start with the option off, then we restart the
# server with the option on.
#
# Once the server is running with the option on, we simply test all
# conditions one by one.
#
# ==== References ====
#
# Added in WL#3584 - Global Transaction Identifiers

# This test will switch ENFORCE_GTID_CONSISTENCY to 1 during execution.
--source include/not_gtid_enabled.inc

# Use this so that we can use rpl_restart_server.inc later.
--let $rpl_server_count= 1
--let $rpl_topology= none
--source include/rpl_init.inc
--source include/rpl_default_connections.inc

--echo ==== Variable is read-only and default is off ====

--let $assert_text= ENFORCE_GTID_CONSISTENCY should default to 0
--let $assert_cond= [SELECT @@GLOBAL.ENFORCE_GTID_CONSISTENCY] = 0
--source include/assert.inc

--error ER_INCORRECT_GLOBAL_LOCAL_VAR
SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1;

--error ER_INCORRECT_GLOBAL_LOCAL_VAR
SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 0;

--echo ==== Restart server ====

--let $rpl_server_number= 1
--let $rpl_server_parameters= --enforce-gtid-consistency=1
--source include/rpl_restart_server.inc

--let $assert_text= ENFORCE_GTID_CONSISTENCY should be set to 1 at restart
--let $assert_cond= [SELECT @@GLOBAL.ENFORCE_GTID_CONSISTENCY] = 1
--source include/assert.inc

--echo ==== CREATE TABLE .... SELECT not allowed ====

--error ER_GTID_UNSAFE_CREATE_SELECT
CREATE TABLE t1 (a INT) ENGINE = InnoDB SELECT 1;

--error ER_GTID_UNSAFE_CREATE_SELECT
CREATE TABLE t1 (a INT) ENGINE = MyISAM SELECT 1;

# Temporary tables should be ok
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB SELECT 1;
CREATE TEMPORARY TABLE t2 (a INT) ENGINE = InnoDB SELECT 1;
DROP TEMPORARY TABLE t1, t2;

# No problem if we turn off binlogging.
SET SQL_LOG_BIN = 0;
CREATE TABLE t1 (a INT) ENGINE = InnoDB SELECT 1;
CREATE TABLE t2 (a INT) ENGINE = MyISAM SELECT 1;
DROP TABLE t1;
DROP TABLE t2;
SET SQL_LOG_BIN = 1;

--echo ==== Non-transactional updates not allowed ====
--echo Non-transactional updates are allowed when row binlog format
--echo is used and all non-transactional tables on a transaction are
--echo temporary.

CREATE TABLE t1 (a INT) ENGINE = MyISAM;
CREATE TEMPORARY TABLE t2 (a INT) ENGINE = MyISAM;

INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (1);

CREATE TABLE t3 (a INT) ENGINE = InnoDB;
--eval CREATE TRIGGER trig BEFORE INSERT ON t3 FOR EACH ROW BEGIN INSERT INTO t1 VALUES (1); END
--error ER_GTID_UNSAFE_NON_TRANSACTIONAL_TABLE
INSERT INTO t3 VALUES (1);

# No problem if we turn off binlogging.
SET SQL_LOG_BIN = 0;
INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (1);
INSERT INTO t3 VALUES (1);
SET SQL_LOG_BIN = 1;

ALTER TABLE t1 ADD COLUMN (b INT);
ALTER TABLE t2 ADD COLUMN (b INT);
DROP TABLE t1, t2, t3;

--echo ==== [CREATE|DROP] TEMPORARY TABLE inside a transaction ====

CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
ALTER TABLE t1 ADD COLUMN (b INT);
DROP TABLE t1;

CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
DROP TEMPORARY TABLE t1;

# CREATE TEMPORARY TABLE is not allowed in a transaction.
BEGIN;
--error ER_GTID_UNSAFE_CREATE_DROP_TEMPORARY_TABLE_IN_TRANSACTION
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
COMMIT;

SET AUTOCOMMIT = 0;
--error ER_GTID_UNSAFE_CREATE_DROP_TEMPORARY_TABLE_IN_TRANSACTION
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;

SET AUTOCOMMIT = 1;
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;

# ALTER TABLE is not affected because it always does implicit commit.
BEGIN;
ALTER TABLE t1 ADD COLUMN (b INT);
COMMIT;

SET AUTOCOMMIT = 0;
ALTER TABLE t1 ADD COLUMN (c INT);

SET AUTOCOMMIT = 1;
ALTER TABLE t1 ADD COLUMN (d INT);

# DROP TEMPORARY TABLE is not allowed inside transaction.
BEGIN;
--error ER_GTID_UNSAFE_CREATE_DROP_TEMPORARY_TABLE_IN_TRANSACTION
DROP TEMPORARY TABLE t1;
COMMIT;

SET AUTOCOMMIT = 0;
--error ER_GTID_UNSAFE_CREATE_DROP_TEMPORARY_TABLE_IN_TRANSACTION
DROP TEMPORARY TABLE t1;
COMMIT;

# DROP TEMPORARY TABLE is allowed outside transactions.
SET AUTOCOMMIT = 1;
DROP TEMPORARY TABLE t1;

# DROP TABLE is not affected because it always does an implicit commit.
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
BEGIN;
DROP TABLE t1;
COMMIT;

CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
SET AUTOCOMMIT = 0;
DROP TABLE t1;
SET AUTOCOMMIT = 1;

# No problem if we turn off binlogging.
SET SQL_LOG_BIN = 0;
BEGIN;
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
CREATE TEMPORARY TABLE t2 (a INT) ENGINE = MyISAM;
COMMIT;
BEGIN;
DROP TEMPORARY TABLE t1;
DROP TEMPORARY TABLE t2;
COMMIT;
SET AUTOCOMMIT = 0;
CREATE TEMPORARY TABLE t1 (a INT) ENGINE = InnoDB;
CREATE TEMPORARY TABLE t2 (a INT) ENGINE = MyISAM;
COMMIT;
DROP TEMPORARY TABLE t1;
DROP TEMPORARY TABLE t2;
COMMIT;
SET AUTOCOMMIT = 1;
SET SQL_LOG_BIN = 1;

# Revert to default enforce-gtid-consistency value
--echo ==== Restart server ====
--let $rpl_server_number= 1
--let $rpl_server_parameters= --enforce-gtid-consistency=0
--source include/rpl_restart_server.inc

--let $assert_text= ENFORCE_GTID_CONSISTENCY should be set to 0 at restart
--let $assert_cond= [SELECT @@GLOBAL.ENFORCE_GTID_CONSISTENCY] = 0
--source include/assert.inc

--source include/rpl_end.inc
