--source include/not_windows.inc
--source include/not_embedded.inc
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/big_test.inc

--echo # Bug #18285007	COPY OF TABLES WITH INNODB FTS
--echo #	FROM WINDOWS TO LINUX CAUSES CRASH

LET $regexp=/FTS_([0-9a-f_]+)([A-Z_]+)/FTS_AUX_\2/;

--echo # Simulating old non-windows(< 5.6.16) data directory

SET GLOBAL DEBUG='+d,innodb_test_wrong_fts_aux_table_name';
CREATE TABLE t1(a TEXT, b TEXT, FULLTEXT(a, b))engine=innodb;
INSERT INTO t1 VALUES('TEST1', 'TEST2');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
--source include/restart_mysqld.inc

# Set flags2 for all the table.
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
DROP TABLE t1;

--echo # Simulating old windows (< 5.6.16) data directory

let $MYSQLD_DATADIR= `select @@datadir`;
SET GLOBAL DEBUG='+d,innodb_test_wrong_fts_aux_table_name';
SET GLOBAL DEBUG='+d,innodb_test_wrong_windows_fts_aux_table_name';
CREATE TABLE t1(a TEXT, b TEXT, FULLTEXT(a, b))engine=innodb;
INSERT INTO t1 VALUES('TEST1', 'TEST2');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
--source include/restart_mysqld.inc

# Renaming the tables.
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
DROP TABLE t1;

--echo # Simulation current non windows data directory

CREATE TABLE t1(a TEXT, b TEXT, FULLTEXT(a, b))engine=innodb;
INSERT INTO t1 VALUES('TEST1', 'TEST2');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
--source include/restart_mysqld.inc

SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
DROP TABLE t1;

--echo # Non windows aux table flag failure and rebuild the corrupted index.

# Remove the orphaned table
-- source include/restart_mysqld.inc

call mtr.add_suppression("\\[Warning\\] InnoDB: Parent table of FTS auxiliary table .* not found.");
call mtr.add_suppression("\\[Warning\\] InnoDB: Setting aux table .* to hex format failed.");

call mtr.add_suppression("\\[ERROR\\] InnoDB: Index .* of table .* is corrupted");

SET GLOBAL DEBUG='+d,innodb_test_wrong_fts_aux_table_name';
CREATE TABLE t1(a TEXT, b TEXT, c TEXT, FULLTEXT `AB` (a, b), FULLTEXT `C1` (c));
INSERT INTO t1 VALUES('TEST1', 'TEST2', 'TEXT3');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2', 'TEXT5');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

let $restart_parameters = restart: --debug=d,aux_table_flag_fail;
--source include/restart_mysqld.inc

--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

CHECK TABLE t1;
--error ER_INNODB_INDEX_CORRUPT
ALTER TABLE t1 ADD FULLTEXT(b);

let $restart_parameters = restart;
-- source include/restart_mysqld.inc

--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

--echo # Drop the corrupted index.
ALTER TABLE t1 DROP INDEX `AB`;
ALTER TABLE t1 DROP INDEX `C1`;

-- echo # Rebuild the index.
ALTER TABLE t1 ADD FULLTEXT(a, b);
ALTER TABLE t1 ADD FULLTEXT(c);

--echo # Read the record using fts index.
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

DROP TABLE t1;

--echo # Non windows parent table flag failure.

SET GLOBAL DEBUG='+d,innodb_test_wrong_fts_aux_table_name';
CREATE TABLE t1(a TEXT, b TEXT, c TEXT, FULLTEXT(a, b), FULLTEXT(c));
INSERT INTO t1 VALUES('TEST1', 'TEST2', 'TEXT3');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2', 'TEXT5');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

--source include/shutdown_mysqld.inc
# 137 == 0x80 + SIGKILL
--error 137
--exec $MYSQLD_CMD --debug="d,parent_table_flag_fail"

--source include/start_mysqld.inc

SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');
DROP TABLE t1;

--echo # Drop FTS table and rename the common tables.

# Remove the orphaned table
-- source include/restart_mysqld.inc

SET GLOBAL DEBUG='+d,innodb_test_wrong_fts_aux_table_name';
SET GLOBAL DEBUG='+d,innodb_test_wrong_windows_fts_aux_table_name';
CREATE TABLE t1(a TEXT, b TEXT, c TEXT, FULLTEXT f1(a, b));
INSERT INTO t1 VALUES('TEST1', 'TEST2', 'TEXT3');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2', 'TEXT5');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
alter table t1 drop index f1;
SET SESSION debug='-d,innodb_test_wrong_fts_sys_table_name';
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;

--source include/restart_mysqld.inc
# Renaming the common tables.
--replace_regex $regexp
SELECT SUBSTRING(name, LOCATE('_', name) - 3, 5) AS prefix, name
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE name LIKE '%FTS_%' ORDER BY 1, 2;
SELECT * FROM t1;
DROP TABLE t1;

--echo # Rename failure for old windows data directory and rebuild the
--echo # corrupted index

call mtr.add_suppression("\\[Warning\\] InnoDB: Failed to rename one aux table .* will revert all successful rename operations.");

call mtr.add_suppression("\\[Warning\\] InnoDB: Rollback operations on all aux tables of table .* All the fts index associated with the table are marked as corrupted. Please rebuild the index again.");

call mtr.add_suppression("\\[ERROR\\] InnoDB: Flagged corruption of .* in table .* in DROP ORPHANED TABLE");

SET GLOBAL DEBUG='+d,innodb_test_wrong_fts_aux_table_name';
SET GLOBAL DEBUG='+d,innodb_test_wrong_windows_fts_aux_table_name';
CREATE TABLE t1(a TEXT, b TEXT, c TEXT, FULLTEXT `AB`(a, b), FULLTEXT `C1`(c));
INSERT INTO t1 VALUES('TEST1', 'TEST2', 'TEXT3');
INSERT INTO t1 VALUES('TEXT1', 'TEXT2', 'TEXT5');
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

let $restart_parameters = restart: --debug=d,rename_aux_table_fail;
--source include/restart_mysqld.inc

# Will fail to alter table
--error ER_INNODB_FT_AUX_NOT_HEX_ID
ALTER TABLE t1 ADD FULLTEXT(b);

--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');

let $restart_parameters = restart;
--source include/restart_mysqld.inc

--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
--error ER_TABLE_HAS_NO_FT
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

--echo # Drop the corrupted index.
ALTER TABLE t1 DROP INDEX `AB`;
ALTER TABLE t1 DROP INDEX `C1`;

-- echo # Rebuild the index.
ALTER TABLE t1 ADD FULLTEXT(a, b);
ALTER TABLE t1 ADD FULLTEXT(c);

--echo # Read the record using fts index.
SELECT * FROM t1 WHERE MATCH(a, b) AGAINST ('TEST2');
SELECT * FROM t1 WHERE MATCH(c) AGAINST ('TEXT5');

TRUNCATE TABLE t1;
DROP TABLE t1;
--source include/restart_mysqld.inc
--list_files $MYSQLD_DATADIR/test
