--source include/have_innodb.inc
# embedded does not support restart
-- source include/not_embedded.inc
-- source include/not_valgrind.inc

call mtr.add_suppression("cannot be decrypted. Are you using correct keyring that contain the");
call mtr.add_suppression("InnoDB: Tablespace for table .* is set as discarded.");
call mtr.add_suppression("InnoDB: Cannot calculate statistics for table .* because the .ibd file is missing. Please refer to .* for how to resolve the issue.");
call mtr.add_suppression("InnoDB: Trying to access missing tablespace [0-9]+");
call mtr.add_suppression("InnoDB: Cannot save statistics for table .* because the .ibd file is missing.");


CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(255)) ENGINE=InnoDB ROW_FORMAT=COMPRESSED ENCRYPTION='KEYRING' ENCRYPTION_KEY_ID=4;
SHOW WARNINGS;
SHOW CREATE TABLE t1;
INSERT INTO t1 VALUES (1,'foobar'),(2,'barfoo');
let MYSQLD_DATADIR =`SELECT @@datadir`;
FLUSH TABLE t1 FOR EXPORT;
--echo # List before copying files
--list_files $MYSQLD_DATADIR/test
perl;
do "$ENV{MYSQL_TEST_DIR}/include/innodb-util.inc";
ib_backup_tablespaces("test", "t1");
EOF
UNLOCK TABLES;

ALTER TABLE t1 DISCARD TABLESPACE;

perl;
do "$ENV{MYSQL_TEST_DIR}/include/innodb-util.inc";
ib_discard_tablespaces("test", "t1");
ib_restore_tablespaces("test", "t1");
EOF

--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--let $restart_parameters=restart:--keyring-file-data=$MYSQLTEST_VARDIR/keys2.txt
--source include/restart_mysqld.inc

--let MYSQLD_DATADIR =`SELECT @@datadir`
--let t1_IBD = $MYSQLD_DATADIR/test/t1.ibd

--error ER_TABLE_SCHEMA_MISMATCH
ALTER TABLE t1 IMPORT TABLESPACE;
SHOW CREATE TABLE t1;
--error ER_TABLESPACE_DISCARDED
SELECT * FROM t1;
--echo # Tablespaces should be still encrypted

--let SEARCH_PATTERN=foobar
--let ABORT_ON=FOUND
--echo # t1 yes on expecting NOT FOUND
--let SEARCH_FILE=$t1_IBD
--source include/search_pattern_in_file.inc

--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--let $restart_parameters=restart: --keyring-file-data=$MYSQLTEST_VARDIR/keys1.txt
--source include/restart_mysqld.inc

DROP TABLE t1;

# reset system

--remove_file $MYSQLTEST_VARDIR/keys1.txt
--remove_file $MYSQLTEST_VARDIR/keys2.txt
