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


--replace_result $MYSQL_TMP_DIR MYSQL_TMP_DIR
eval SET @@global.keyring_file_data="$MYSQL_TMP_DIR/mysecret_keyring";

SHOW PLUGINS;
#
# MDEV-8138: strange results from encrypt-and-grep test
#
--let $MYSQLD_DATADIR=`select @@datadir`
#TODO: Robert - dlaczego tu jest ibdata1
--let ib1_IBD = $MYSQLD_DATADIR/ibdata1
--let t1_IBD = $MYSQLD_DATADIR/test/t1.ibd
--let SEARCH_RANGE = 10000000

--disable_warnings
SET GLOBAL innodb_file_per_table = ON;
SET GLOBAL innodb_file_format = `Barracuda`;
--enable_warnings

create table t1 (a varchar(255)) engine=innodb;

SELECT NAME, MIN_KEY_VERSION FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION = 0;
SELECT NAME, MIN_KEY_VERSION FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION <> 0;
SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES;
SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES;


--let $assert_text= Table t1 should be included in INNODB_TABLESPACES_ENCRYPTION yet - since it is not RK
--let $assert_cond= "[SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE NAME = \\"test/t1\\"]" = 0
--source include/assert.inc

insert t1 values (repeat('foobarsecret', 12));
select * from t1;

--echo # 1. Alter from unencrypted to RK
ALTER TABLE t1 encryption='KEYRING';
SELECT * FROM t1;

--let $assert_text= Table t1 should be the only one appearing in INNODB_TABLESPACES_ENCRYPTION
--let $assert_cond= "[SELECT NAME FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION = 0]" = "test/t1"
--source include/assert.inc

create table t2 (a varchar(255)) engine=innodb encryption='y';
insert t2 values (repeat('foobarsecret', 12));

--let $assert_text= Table t2 should be included in INNODB_TABLESPACES_ENCRYPTION yet - since it is not RK
--let $assert_cond= "[SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE NAME = \\"test/t2\\"]" = 0
--source include/assert.inc

--echo # 2. Alter from MK encryption to RK
ALTER TABLE t2 encryption='KEYRING';
SELECT * FROM t2;

--let $assert_text= Table t2 should be included in INNODB_TABLESPACES_ENCRYPTION with MIN_KEY_VERION 0 (encrypted)
--let $assert_cond= "[SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE NAME = \\"test/t2\\"]" = 1
--source include/assert.inc

--echo # 3. Alter from RK to MK
ALTER TABLE t1 encryption='Y';
SELECT * FROM t1;

--let $assert_text= Table t1 has been re_encrypted to Master key encryption - should disappear from INNODB_TABLESPACES_ENCRYPTION => thus only t2 should remain
--let $assert_cond= "[SELECT NAME FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION = 0]" = "test/t2"
--source include/assert.inc

--echo # 4. Alter from MK to unencrypted
ALTER TABLE t1 encryption='n';

--let $assert_text= t1 should be marked as unencrypted in INNODB_TABLESPACES_ENCRYPTION
--let $assert_cond= "[SELECT NAME FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION <> 0]" = "test/t1"
--source include/assert.inc
--let $assert_text= t2 should be marked as encrypted in INNODB_TABLESPACES_ENCRYPTION
--let $assert_cond= "[SELECT NAME FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION <> 0]" = "test/t2"

SELECT NAME, MIN_KEY_VERSION FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION;

--echo # 5. Alter from unencrypted to RK
ALTER TABLE t1 encryption='KEYRING';

--let $assert_text= Table t1 should be included in INNODB_TABLESPACES_ENCRYPTION
--let $assert_cond= "[SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE NAME = \\"test/t1\\"]" = 1
--source include/assert.inc

--echo # 6. Alter from RK to unencrypted
ALTER TABLE t1 encryption='n';
--let $assert_text= Table t1 should appear in INNODB_TABLESPACES_ENCRYPTION but with MIN_KEY_VERSION marked as UNENCRYPTED
--let $assert_cond= "[SELECT MIN_KEY_VERSION FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE NAME = \\"test/t1\\"]" <> 0
--source include/assert.inc

drop table t1,t2;

