--source include/have_innodb.inc

SET @row_format = @@GLOBAL.innodb_default_row_format;
SET @large_prefix = @@GLOBAL.innodb_large_prefix;
SET @file_format = @@GLOBAL.innodb_file_format;
SET GLOBAL innodb_file_format = barracuda;

# set the variables
let $MYSQLD_DATADIR = `SELECT @@datadir`;

--echo # ###########################################################
--echo # Check with Import/Export tablespace with Default_row_format

# Set row_format=Compact
SET GLOBAL innodb_default_row_format=Compact;

# Check row_format=Compact
SELECT @@innodb_default_row_format;

# Check file_per_table=1
SELECT @@innodb_file_per_table;

CREATE TABLE tab(a INT) ENGINE=InnoDB;
--replace_column 3 # 5 # 6 # 7 # 8 # 9 # 10 # 12 #
SHOW TABLE STATUS LIKE 'tab';

INSERT INTO tab VALUES(1);
INSERT INTO tab VALUES(2);

# Check the rows
SELECT * FROM tab;

FLUSH TABLE tab FOR EXPORT;

# Take the backup of the ibd and cfg files
--copy_file $MYSQLD_DATADIR/test/tab.cfg $MYSQLD_DATADIR/tab.cfg
--copy_file $MYSQLD_DATADIR/test/tab.ibd $MYSQLD_DATADIR/tab.ibd

UNLOCK TABLES;

# Cleanup
DROP TABLE tab;

# Set the default_row_format=Dynamic
SET GLOBAL innodb_default_row_format=Dynamic;

CREATE TABLE tab(a INT) ENGINE=InnoDB;

# Remove the *.ibd file
ALTER TABLE tab DISCARD TABLESPACE;

# Move the *.ibd,*.cfg file into orginal location
--move_file $MYSQLD_DATADIR/tab.cfg $MYSQLD_DATADIR/test/tab.cfg
--move_file $MYSQLD_DATADIR/tab.ibd $MYSQLD_DATADIR/test/tab.ibd

--error ER_TABLE_SCHEMA_MISMATCH
ALTER TABLE tab IMPORT TABLESPACE;

# Take the backup of the ibd and cfg files
--copy_file $MYSQLD_DATADIR/test/tab.cfg $MYSQLD_DATADIR/tab.cfg
--copy_file $MYSQLD_DATADIR/test/tab.ibd $MYSQLD_DATADIR/tab.ibd

# Cleanup
DROP TABLE tab;

# Remove orphan files
--remove_file $MYSQLD_DATADIR/test/tab.cfg
--remove_file $MYSQLD_DATADIR/test/tab.ibd

# Set the default_row_format=Compact
SET GLOBAL innodb_default_row_format=Compact;

# Check row_format=Compact
SELECT @@innodb_default_row_format;

CREATE TABLE tab(a INT) ENGINE=InnoDB;
--replace_column 3 # 5 # 6 # 7 # 8 # 9 # 10 # 12 #
SHOW TABLE STATUS LIKE 'tab';

# Remove the *.ibd file
ALTER TABLE tab DISCARD TABLESPACE;

# Move the *ibd,*.cfg file into orginal location
--copy_file $MYSQLD_DATADIR/tab.cfg $MYSQLD_DATADIR/test/tab.ibd
--move_file $MYSQLD_DATADIR/tab.cfg $MYSQLD_DATADIR/test/tab.cfg

call mtr.add_suppression("InnoDB: Tried to read .* bytes at offset 0");

--error ER_INTERNAL_ERROR
ALTER TABLE tab IMPORT TABLESPACE;
--remove_file $MYSQLD_DATADIR/test/tab.ibd
--move_file $MYSQLD_DATADIR/tab.ibd $MYSQLD_DATADIR/test/tab.ibd

# Check import is successful (because same row_format)
ALTER TABLE tab IMPORT TABLESPACE;

# Check the rows
SELECT * FROM tab;

# Cleanup
DROP TABLE tab;

--echo # ###########################################################
# Check when Index Column size (3070 bytes) is too long, Change row_format
# Check when Index Column size (767 bytes), Change row_format
# Dynamic to Compact to Dynamic

# Set the default_row_format=Dynamic
SET GLOBAL innodb_default_row_format=Dynamic;
SET GLOBAL innodb_large_prefix=ON;

SELECT @@innodb_default_row_format;

CREATE TABLE tab(a INT PRIMARY KEY, b VARCHAR(5000), KEY idx1(b(3070))) ENGINE= InnoDB;
--replace_column 3 # 5 # 6 # 7 # 8 # 9 # 10 # 12 #
SHOW TABLE STATUS LIKE 'tab';

INSERT INTO tab(a,b) VALUES(1,'Check with max column size');

# Check by SELECT, no errors
SELECT * FROM tab;

# Change row_format to Compact
SET GLOBAL innodb_default_row_format=COMPACT;

# Check error ERROR 1709 (HY000): Index column size too large
-- error ER_INDEX_COLUMN_TOO_LONG
ALTER TABLE tab ROW_FORMAT=COMPACT;

# Cleanup
DROP TABLE tab;

# Change the default_row_format to default
SET GLOBAL innodb_default_row_format=Default;
SELECT @@innodb_default_row_format;
SET GLOBAL innodb_default_row_format=Dynamic;

# Change row_format to Dynamic
SELECT @@innodb_default_row_format;

CREATE TABLE tab(a INT PRIMARY KEY, b VARCHAR(5000), KEY idx1(b(767))) ENGINE= InnoDB;
--replace_column 3 # 5 # 6 # 7 # 8 # 9 # 10 # 12 #
SHOW TABLE STATUS LIKE 'tab';

INSERT INTO tab(a,b) VALUES(1,'Check with max column size');

# Check by SELECT, no errors
SELECT * FROM tab;

# Check no errors because Compact allows 767 bytes
ALTER TABLE tab ROW_FORMAT=COMPACT;
--replace_column 3 # 5 # 6 # 7 # 8 # 9 # 10 # 12 #
SHOW TABLE STATUS LIKE 'tab';

# Check by SELECT, no errors
SELECT * FROM tab;

# Check no errors
ALTER TABLE tab ROW_FORMAT=COMPRESSED;

# Check by SELECT, no errors
SELECT * FROM tab;

# Check no errors
ALTER TABLE tab ROW_FORMAT=Dynamic;
--replace_column 3 # 5 # 6 # 7 # 8 # 9 # 10 # 12 #
SHOW TABLE STATUS LIKE 'tab';

# Cleanup
DROP TABLE tab;

SET GLOBAL innodb_default_row_format =  @row_format;
SET GLOBAL innodb_large_prefix = @large_prefix;
SET GLOBAL innodb_file_format = @file_format;