#
# Test for persistent corrupt bit for corrupted index and table
#
-- source include/have_innodb.inc
-- source include/not_embedded.inc
# This test needs debug server
-- source include/have_debug.inc

-- disable_query_log
call mtr.add_suppression("Flagged corruption of idx.*in CHECK TABLE");
-- enable_query_log

set names utf8;

SET UNIQUE_CHECKS=0;

CREATE TABLE corrupt_bit_test_ā(
       a INT AUTO_INCREMENT PRIMARY KEY,
       b CHAR(100),
       c INT,
       z INT,
       INDEX idx(b))
ENGINE=InnoDB;

INSERT INTO corrupt_bit_test_ā VALUES(0,'x',1, 1);

# This is the first unique index we intend to corrupt
CREATE UNIQUE INDEX idxā ON corrupt_bit_test_ā(c, b);

# This is the second unique index we intend to corrupt
CREATE UNIQUE INDEX idxē ON corrupt_bit_test_ā(z, b);

SELECT * FROM corrupt_bit_test_ā;

INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+1,z+1 FROM corrupt_bit_test_ā;

select count(*) from corrupt_bit_test_ā;

# This will flag all secondary indexes corrupted
SET @save_dbug = @@SESSION.debug_dbug;
SET debug_dbug = '+d,dict_set_index_corrupted';
check table corrupt_bit_test_ā;
SET debug_dbug = @save_dbug;

# Cannot create new indexes while corrupted indexes exist
--error ER_INDEX_CORRUPT
CREATE INDEX idx3 ON corrupt_bit_test_ā(b, c);
--error ER_INDEX_CORRUPT
CREATE INDEX idx4 ON corrupt_bit_test_ā(b, z);

# This selection intend to use the corrupted index. Expect to fail
-- error ER_INDEX_CORRUPT
select c from corrupt_bit_test_ā;

-- error ER_INDEX_CORRUPT
select z from corrupt_bit_test_ā;

show warnings;

# Since corrupted index is a secondary index, we only disable such
# index and allow other DML to proceed
insert into corrupt_bit_test_ā values (10001, "a", 20001, 20001);

# This does not use the corrupted index, expect to succeed
select * from corrupt_bit_test_ā use index(primary) where a = 10001;

# Some more DMLs
begin;
insert into corrupt_bit_test_ā values (10002, "a", 20002, 20002);
delete from corrupt_bit_test_ā where a = 10001;
insert into corrupt_bit_test_ā values (10001, "a", 20001, 20001);
rollback;

drop index idxā on corrupt_bit_test_ā;

check table corrupt_bit_test_ā;

set names utf8;

-- error ER_INDEX_CORRUPT
select z from corrupt_bit_test_ā;

show create table corrupt_bit_test_ā;

# Drop the corrupted index
drop index idxē on corrupt_bit_test_ā;

# Cannot create new indexes while a corrupt index exists.
--error ER_INDEX_CORRUPT
CREATE INDEX idx3 ON corrupt_bit_test_ā(b, c);
--error ER_INDEX_CORRUPT
CREATE INDEX idx4 ON corrupt_bit_test_ā(b, z);

show create table corrupt_bit_test_ā;
drop index idx on corrupt_bit_test_ā;

# Now that there exist no corrupted indexes, we can create new indexes.
CREATE INDEX idx3 ON corrupt_bit_test_ā(b, c);
CREATE INDEX idx4 ON corrupt_bit_test_ā(b, z);

# Now select back to normal
select z from corrupt_bit_test_ā limit 10;

# Drop table
drop table corrupt_bit_test_ā;
