#
# Test for persistent corrupt bit for corrupted index and table
#
-- source include/have_innodb.inc
-- source include/have_innodb_16k.inc

# Issues with innodb_change_buffering_debug on Windows, so the test scenario
# cannot be created on windows
--source include/not_windows.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");
# This test setup is extracted from bug56680.test:
# The flag innodb_change_buffering_debug is only available in debug builds.
# It instructs InnoDB to try to evict pages from the buffer pool when
# change buffering is possible, so that the change buffer will be used
# whenever possible.
SET @innodb_change_buffering_debug_orig = @@innodb_change_buffering_debug;
SET GLOBAL innodb_change_buffering_debug = 1;

# Turn off Unique Check to create corrupted index with dup key
SET UNIQUE_CHECKS=0;

CREATE DATABASE pad;
let $i=338;
while ($i)
{
--eval CREATE TABLE pad.t$i(a INT PRIMARY KEY)ENGINE=InnoDB STATS_PERSISTENT=0;
  dec $i;
}

-- enable_query_log

set names utf8;

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

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_ā;

select @@unique_checks;
select @@innodb_change_buffering_debug;

# Create enough rows for the table, so that the insert buffer will be
# used for modifying the secondary index page. There must be multiple
# index pages, because changes to the root page are never buffered.

INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+1,z+1 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+10,z+10 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+20,z+20 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+50,z+50 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+100,z+100 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+200,z+200 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+400,z+400 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+800,z+800 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+1600,z+1600 FROM corrupt_bit_test_ā;
INSERT INTO corrupt_bit_test_ā SELECT 0,b,c+4000,z+4000 FROM corrupt_bit_test_ā;

select count(*) from corrupt_bit_test_ā;

CREATE INDEX idx3 ON corrupt_bit_test_ā(b, c);

# Create a dup key error on index "idxē" and "idxā" by inserting a dup value
INSERT INTO corrupt_bit_test_ā VALUES(13000,'x',1,1);

# creating an index should succeed even if other secondary indexes are corrupted
CREATE INDEX idx4 ON corrupt_bit_test_ā(b, z);

# Check table will find the unique indexes corrupted
# with dup key
check table corrupt_bit_test_ā;

# 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 one corrupted index before reboot
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_ā;

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

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

# Drop table
drop table corrupt_bit_test_ā;
DROP DATABASE pad;

SET GLOBAL innodb_change_buffering_debug = 0;
