--source include/not_valgrind.inc

--echo #
--echo # Bug#26997500 - UPGRADE FAILS DUE TO ORPHAN .FRM AND CANNOT BE RESUMED
--echo #

--echo # Set different paths for --datadir
let $MYSQLD_DATADIR1 = $MYSQL_TMP_DIR/data57;

--echo # Copy the remote tablespace & DB zip files from suite location to working location.
--copy_file $MYSQLTEST_VARDIR/std_data/data57.zip $MYSQL_TMP_DIR/data57.zip

--echo # Check that the file exists in the working folder.
--file_exists $MYSQL_TMP_DIR/data57.zip

--echo # Unzip the zip file.
--exec unzip -qo $MYSQL_TMP_DIR/data57.zip -d $MYSQL_TMP_DIR

--echo # These files are added to test error scenario, delete from for upgrade testing.
--remove_file $MYSQL_TMP_DIR/data57/test/55_temporal.frm
--remove_file $MYSQL_TMP_DIR/data57/test/55_temporal.MYD
--remove_file $MYSQL_TMP_DIR/data57/test/55_temporal.MYI
--echo # Remove myisam partitioned tables. There are used for negative testing.
--remove_files_wildcard $MYSQL_TMP_DIR/data57/partitions *
--rmdir $MYSQL_TMP_DIR/data57/partitions
--force-rmdir $MYSQL_TMP_DIR/data57/mismatch_frms

--echo # Create an orphan .frm
--copy_file $MYSQL_TMP_DIR/data57/sakila/actor.frm $MYSQL_TMP_DIR/data57/sakila/zzz.frm

let $MYSQLD_LOG= $MYSQLTEST_VARDIR/log/innodb_upgrade_orphan.log;

--source include/shutdown_mysqld.inc

--echo # start server and we expect failure because of orphan .frm(sakila/zzz.frm)
--error 1
--exec $MYSQLD --secure-file-priv="" --loose-skip-log-bin --read-only --skip-log-replica-updates --skip-replica-preserve-commit-order --datadir=$MYSQLD_DATADIR1 --log-error=$MYSQLD_LOG --innodb-page-size=16k

--echo # Remove orphan .frm
--remove_file $MYSQL_TMP_DIR/data57/sakila/zzz.frm

--echo # Retry upgrade on the failed datadir
--exec echo "restart: --loose-skip-log-bin --read-only --skip-log-replica-updates --skip-replica-preserve-commit-order --datadir=$MYSQLD_DATADIR1 --log-error=$MYSQLD_LOG --innodb-page-size=16k" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--enable_reconnect
--let $wait_counter= 10000
--source include/wait_until_connected_again.inc

--echo # Sanity Testing after Server start
CREATE SCHEMA test123;
CREATE TABLE test123.t1(a int);
INSERT INTO test123.t1 values(1);
SELECT * FROM test123.t1;
DROP SCHEMA test123;

SHOW DATABASES;
--echo # Check table structures
SHOW CREATE TABLE mysql.user;

SHOW CREATE TABLE mysql.innodb_table_stats;
SHOW CREATE TABLE mysql.innodb_index_stats;

--sorted_result
SELECT COLUMN_NAME,CHARACTER_MAXIMUM_LENGTH
  FROM INFORMATION_SCHEMA.COLUMNS col
    JOIN INFORMATION_SCHEMA.TABLES tab ON col.table_name=tab.table_name
  WHERE col.TABLE_NAME LIKE '%innodb_%_stats'
    AND col.COLUMN_NAME LIKE 'table_name';

SHOW CREATE TABLE test.t_compressed;
SHOW CREATE TABLE test.t_compressed2;
SHOW CREATE TABLE test.t_compressed3;
SHOW CREATE TABLE test.t_dynamic;
SHOW CREATE TABLE test.t_index;

SHOW CREATE TABLE test.vt2;
SELECT * FROM test.vt2;
SHOW CREATE TABLE test.t_gen_stored;
SHOW CREATE TABLE test.jemp;
SHOW CREATE TABLE test.t_gen_stored_myisam;
SHOW CREATE TABLE test.t_gen_stored_myisam2;
SHOW CREATE TABLE test.jemp_myisam;


--sorted_result
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, ENGINE, VERSION, ROW_FORMAT
 FROM INFORMATION_SCHEMA.tables WHERE table_schema='test';
--echo # Check for views
--echo # Invalid view
SHOW CREATE VIEW aview.view_invalid;
--echo # Valid views
SHOW CREATE VIEW aview.view_2;
SHOW CREATE VIEW aview.second_view;
SELECT * FROM aview.view_2;
SELECT * FROM aview.second_view;

SHOW CREATE VIEW aview.mixed_view;
SHOW CREATE VIEW aview.mixed_view2;
SELECT * FROM aview.mixed_view2;

--echo # View with different definer
SHOW CREATE VIEW aview.view_user;
SHOW CREATE VIEW aview.view_user2;
--echo # View with different character_set and connection collation
SHOW CREATE VIEW aview.view_character_set;

--echo # Check for General tablespace
SHOW CREATE TABLE tablespace.t2;
SHOW CREATE TABLE tablespace.t3;
SELECT * FROM tablespace.t3;
SHOW CREATE TABLE tablespace.t4;

--echo # Check for table with blackhole engine
SHOW CREATE TABLE test.t_blackhole;

--echo # Check for table with special character in name
SHOW CREATE TABLE `test`.`t_sc~!@#$%^&*(`;

--echo # Check for JSON types
SHOW CREATE TABLE test.t_json;

--echo # Check for geometry data types
SHOW CREATE TABLE test.geom;

--echo # Restart on the upgraded data dir
--replace_result $MYSQLD_DATADIR1 MYSQLD_DATADIR1 $MYSQLD_LOG MYSQLD_LOG
let $restart_parameters = "restart: --loose-skip-log-bin --skip-log-replica-updates --skip-replica-preserve-commit-order --datadir=$MYSQLD_DATADIR1 --log-error=$MYSQLD_LOG --innodb-page-size=16k";
--let $wait_counter= 3000
--source include/restart_mysqld.inc

--echo # Execute FTS queries that modify FTS pages
select count(*) from sakila.film_text;
select * from sakila.film_text where film_id = 984;
select * from sakila.film_text where match(title,description) against("SCISSORHANDS");
delete from sakila.film_text where film_id = 984;
SET GLOBAL innodb_optimize_fulltext_only=ON;
optimize table sakila.film_text;

let $restart_parameters =;
--echo # Restart the server with default options.
--let $shutdown_server_timeout= 300
--let $wait_counter= 3000
--source include/restart_mysqld.inc
--force-rmdir $MYSQLD_DATADIR1
--remove_file $MYSQL_TMP_DIR/data57.zip
