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

# Test that it is not possible to rollback restore.  That is, that an
# automatic commit is performed as part of restore.  This test both
# tests the drivers individually and in combination.  It also tests
# different combinations of turning autocommit on and off before
# backup and restore.  This test the behavior reported in BUG#34210

CREATE DATABASE commit_test;
USE commit_test;

# Test using InnoDB  (Consistent snapshot driver)
SET @@autocommit=0;
CREATE TABLE t1 (s1 CHAR(2)) ENGINE=innodb;
INSERT INTO t1 VALUES ('a1');

replace_column 1 #;
BACKUP DATABASE commit_test TO '81';

replace_column 1 #;
RESTORE FROM '81' OVERWRITE;

SELECT * FROM t1;
ROLLBACK;
SELECT * FROM t1;
COMMIT;

# Test using InnoDB  (Consistent snapshot driver) and Memory (default driver)
SET @@autocommit=0;
CREATE TABLE t2 (s1 CHAR(2)) ENGINE=Memory;
INSERT INTO t1 VALUES ('a2');
INSERT INTO t2 VALUES ('a2');

replace_column 1 #;
BACKUP DATABASE commit_test TO '82';

replace_column 1 #;
RESTORE FROM '82' OVERWRITE;

SELECT * FROM t1;
SELECT * FROM t2;
ROLLBACK;
SELECT * FROM t1;
SELECT * FROM t2;
COMMIT;


# Test using InnoDB, Memory and MyISAM (Native driver)
SET @@autocommit=0;
CREATE TABLE t3 (s1 CHAR(2));
INSERT INTO t1 VALUES ('a3');
INSERT INTO t2 VALUES ('a3');
INSERT INTO t3 VALUES ('a3');

replace_column 1 #;
BACKUP DATABASE commit_test TO '83';

replace_column 1 #;
RESTORE FROM '83' OVERWRITE;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
ROLLBACK;
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
COMMIT;

# Test using InnoDB, Memory, MyISAM and Falcon (Consistent Snapshot)
SET @@autocommit=0;
CREATE TABLE t4 (s1 CHAR(2)) ENGINE=falcon;
INSERT INTO t1 VALUES ('a4');
INSERT INTO t2 VALUES ('a4');
INSERT INTO t3 VALUES ('a4');
INSERT INTO t4 VALUES ('a4');

replace_column 1 #;
BACKUP DATABASE commit_test TO '84';

replace_column 1 #;
RESTORE FROM '84' OVERWRITE;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
ROLLBACK;
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
COMMIT;


# Test doing backup/restore with auto-commit on
SET @@autocommit=1;
INSERT INTO t1 VALUES ('a5');
INSERT INTO t2 VALUES ('a5');
INSERT INTO t3 VALUES ('a5');
INSERT INTO t4 VALUES ('a5');

replace_column 1 #;
BACKUP DATABASE commit_test TO '85';

replace_column 1 #;
RESTORE FROM '85' OVERWRITE;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
ROLLBACK;
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
COMMIT;

# Test doing backup with auto-commit on and restore with auto-commit off
SET @@autocommit=1;
CREATE TABLE t5 (s1 CHAR(2)) ENGINE=falcon;
INSERT INTO t1 VALUES ('a6');
INSERT INTO t2 VALUES ('a6');
INSERT INTO t3 VALUES ('a6');
INSERT INTO t4 VALUES ('a6');
INSERT INTO t5 VALUES ('a6');

replace_column 1 #;
BACKUP DATABASE commit_test TO '86';

SET @@autocommit=0;
replace_column 1 #;
RESTORE FROM '86' OVERWRITE;

#Test of scenario from BUG#34205
DROP TABLE t5;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
ROLLBACK;
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
COMMIT;


# Test doing backup with auto-commit off and restore with auto-commit on
SET @@autocommit=0;
INSERT INTO t1 VALUES ('a7');
INSERT INTO t2 VALUES ('a7');
INSERT INTO t3 VALUES ('a7');
INSERT INTO t4 VALUES ('a7');

replace_column 1 #;
BACKUP DATABASE commit_test TO '87';

SET @@autocommit=1;
replace_column 1 #;
RESTORE FROM '87' OVERWRITE;

SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
ROLLBACK;
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
COMMIT;

# Clean-up
DROP DATABASE commit_test;
COMMIT;
