#
# This test includes tests for backup and restore of tables that use
# tablespaces.
#
# Note: This test is limited to the falcon storage engine only.
#

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

let $MYSQLD_BACKUPDIR=`select @@backupdir`;

--disable_warnings
#
# note: DROP TABLE statements are just for the case Falcon has some problems
# with handling DROP DATABASE and doesn't delete all the tables immediatley
# - there were random test failures which could be caused by such anomalies,
# see BUG#36134.
#
DROP TABLE IF EXISTS backup_ts.not_ts;
DROP TABLE IF EXISTS backup_ts.has_ts;
DROP DATABASE IF EXISTS backup_ts;
--error 0,1
--remove_file $MYSQLD_BACKUPDIR/backup_ts.bak;
--enable_warnings

CREATE DATABASE backup_ts;

--echo #
--echo # Test 1 - Test backup of database with a table that uses a tablespace
--echo #
--echo Test 1:
--echo Create a tablespace.
CREATE TABLESPACE bup_ts ADD DATAFILE 'bup_ts.dat' COMMENT = 'test tablespace backup' ENGINE=FALCON;

--echo Create tables and add data.
CREATE TABLE backup_ts.not_ts (id int, comment char(40)) ENGINE=FALCON;
CREATE TABLE backup_ts.has_ts (id int, comment char(40))
                               TABLESPACE bup_ts ENGINE=FALCON;

# Create some data.
INSERT INTO backup_ts.not_ts VALUES (1, "no tablespace 1");
INSERT INTO backup_ts.not_ts VALUES (2, "no tablespace 2");
INSERT INTO backup_ts.not_ts VALUES (3, "no tablespace 3");
INSERT INTO backup_ts.not_ts VALUES (4, "no tablespace 4");
INSERT INTO backup_ts.not_ts VALUES (5, "no tablespace 5");

INSERT INTO backup_ts.has_ts VALUES (51, "Table has a tablespace 51");
INSERT INTO backup_ts.has_ts VALUES (52, "Table has a tablespace 52");
INSERT INTO backup_ts.has_ts VALUES (53, "Table has a tablespace 53");
INSERT INTO backup_ts.has_ts VALUES (54, "Table has a tablespace 54");
INSERT INTO backup_ts.has_ts VALUES (55, "Table has a tablespace 55");

# Backup the database with tablespace.
--replace_column 1 #
BACKUP DATABASE backup_ts TO 'backup_ts.bak';

# Drop the database
--echo Drop the database.
DROP DATABASE backup_ts;

# Restore the database and ensure there are no errors.
--echo Now restore the database.
--replace_column 1 #
RESTORE FROM 'backup_ts.bak';

--echo Show data
SELECT * FROM backup_ts.not_ts;
SELECT * FROM backup_ts.has_ts;

DROP DATABASE backup_ts;

--echo #
--echo # Test 2 - Test that restore of tablespaces work when the 
--echo #          tablespace has been modified since backup was made

--echo #
--echo # Test 2.1: Backup ts with lowercase name, restore after 
--echo # dropping it and creating a ts with uppercase name
--echo #

--echo Create ts with lowercase name
CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;
--echo Create a database using the ts
CREATE DATABASE backup_ts;
USE backup_ts;

CREATE TABLE t (s1 INT) ENGINE=FALCON TABLESPACE tb;
INSERT INTO t VALUES (1);

--echo
--echo Backup database
--echo
--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_lc.bak';

--echo Drop database and recreate ts with uppercase name
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
CREATE TABLESPACE TB ADD DATAFILE 'tblspace' ENGINE=FALCON;

--echo
--echo Restore database
--replace_column 1 #
RESTORE FROM 'ts_lc.bak';

--echo
--echo Do inverse: Backup with uppercase ts, restore with lowercase ts
--echo
--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_uc.bak';

--echo Drop database and recreate ts with lowecase name
DROP DATABASE backup_ts;
DROP TABLESPACE TB ENGINE=FALCON;
CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;

--echo
--echo Restore database
--echo
--replace_column 1 #
RESTORE FROM 'ts_uc.bak';

DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
--remove_file $MYSQLD_BACKUPDIR/ts_lc.bak
--remove_file $MYSQLD_BACKUPDIR/ts_uc.bak

--echo #
--echo # Test 2.2: Backup ts with lowercase filename, restore after 
--echo # dropping it and creating a ts with uppercase filename
--echo #

--echo Create ts with lowercase filename
CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;

--echo Create a database using the ts
CREATE DATABASE backup_ts;
USE backup_ts;

CREATE TABLE t (s1 INT) ENGINE=FALCON TABLESPACE tb;
INSERT INTO t VALUES (1);

--echo
--echo Backup database
--echo
--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_lcfile.bak';

--echo Drop database and recreate ts with uppercase filename
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
CREATE TABLESPACE tb ADD DATAFILE 'TBLSPACE' ENGINE=FALCON;

--echo
--echo Restore database
--echo
--replace_column 1 #
RESTORE FROM 'ts_lcfile.bak';

--echo
--echo Do inverse: Backup with uppercase filename, 
--echo restore with lowercase ts filename
--echo
--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_ucfile.bak';

--echo Drop database and recreate ts with lowercase filename
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;

--echo
--echo Restore database
--echo
--replace_column 1 #
RESTORE FROM 'ts_ucfile.bak';

--remove_file $MYSQLD_BACKUPDIR/ts_lcfile.bak
--remove_file $MYSQLD_BACKUPDIR/ts_ucfile.bak
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;


--echo #
--echo # Test 2.3: Backup ts, restore after dropping it and creating a ts with
--echo # a different extent_size
--echo #

CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;
CREATE DATABASE backup_ts;
USE backup_ts;
CREATE TABLE t (s1 INT) ENGINE=FALCON TABLESPACE tb;
INSERT INTO t VALUES (1);

--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_extent.bak';

DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
CREATE TABLESPACE tb ADD DATAFILE 'tblspace' EXTENT_SIZE = 1024 ENGINE=FALCON;

--replace_column 1 #
RESTORE FROM 'ts_extent.bak';

--echo
--echo Do inverse: Backup with extent_size 1024, restore without setting it
--echo
--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_extent2.bak';

--echo Drop database and recreate ts with lowecase name
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;

--echo
--echo Restore database
--echo
--replace_column 1 #
RESTORE FROM 'ts_extent2.bak';

--remove_file $MYSQLD_BACKUPDIR/ts_extent.bak
--remove_file $MYSQLD_BACKUPDIR/ts_extent2.bak
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;

--echo #
--echo # Test 2.4: Backup ts, restore after dropping it and creating a ts with
--echo # a different filename
--echo #

CREATE TABLESPACE tb ADD DATAFILE 'tblspace' ENGINE=FALCON;
CREATE DATABASE backup_ts;
USE backup_ts;
CREATE TABLE t (s1 INT) ENGINE=FALCON TABLESPACE tb;
INSERT INTO t VALUES (1);

--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_newfname.bak';

DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;
CREATE TABLESPACE tb ADD DATAFILE 'tblspace2' ENGINE=FALCON;

--replace_column 1 #
RESTORE FROM 'ts_newfname.bak';

--remove_file $MYSQLD_BACKUPDIR/ts_newfname.bak
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;

--echo
--echo # Test 3: Restore after deleting a tablespace the backed up database 
--echo #         depends on.
--echo

CREATE TABLESPACE `TB` ADD DATAFILE 'tblspace' ENGINE=FALCON;
CREATE DATABASE backup_ts;
USE backup_ts;

CREATE TABLE t (s1 INT) ENGINE=FALCON TABLESPACE tb;
INSERT INTO t VALUES (1);

--replace_column 1 #
BACKUP DATABASE backup_ts TO 'ts_drop.bak';

--echo
--echo # Show tablespace info
--query_vertical SELECT t1.tablespace_name, t1.engine, t1.tablespace_type, t2.file_name, t2.file_type FROM INFORMATION_SCHEMA.TABLESPACES AS t1, INFORMATION_SCHEMA.FILES AS t2 WHERE t1.tablespace_name = t2.tablespace_name AND t1.tablespace_name = 'TB';
--echo

DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;

--echo
--echo # Restore and verify database content
--echo

--replace_column 1 #
RESTORE FROM 'ts_drop.bak';

SHOW TABLES IN backup_ts;
SELECT * FROM backup_ts.t;

--echo
--echo # Show tablespace info
--query_vertical SELECT t1.tablespace_name, t1.engine, t1.tablespace_type, t2.file_name, t2.file_type FROM INFORMATION_SCHEMA.TABLESPACES AS t1, INFORMATION_SCHEMA.FILES AS t2 WHERE t1.tablespace_name = t2.tablespace_name AND t1.tablespace_name = 'TB';
--echo

--remove_file $MYSQLD_BACKUPDIR/ts_drop.bak
DROP DATABASE backup_ts;
DROP TABLESPACE tb ENGINE=FALCON;


