
# Embedded server doesn't support external clients
--source include/not_embedded.inc

# check that CSV engine was compiled in, as the result of the test
# depends on the presence of the log tables (which are CSV-based).
--source include/have_csv.inc

#
# Clean up after previous tests
#

--disable_warnings
DROP TABLE IF EXISTS t1, `t``1`, `t 1`;
drop view if exists v1;
drop database if exists client_test_db;
# Repair any tables in mysql, sometimes the slow_log is marked as crashed
# after server has been killed
--exec $MYSQL_CHECK --repair  --databases mysql > /dev/null 2>&1
--enable_warnings

#
# Bug #13783  mysqlcheck tries to optimize and analyze information_schema
#
--replace_result 'Table is already up to date' OK
--exec $MYSQL_CHECK --all-databases --analyze --optimize
--replace_result 'Table is already up to date' OK
--exec $MYSQL_CHECK --analyze --optimize  --databases test information_schema mysql
--exec $MYSQL_CHECK --analyze --optimize information_schema schemata

#
# Bug #16502: mysqlcheck tries to check views
#
create table t1 (a int);
create view v1 as select * from t1;
--replace_result 'Table is already up to date' OK
--exec $MYSQL_CHECK --analyze --optimize --databases test
--replace_result 'Table is already up to date' OK
--exec $MYSQL_CHECK --all-in-1 --analyze --optimize --databases test
drop view v1;
drop table t1;

#
# Bug #30654: mysqlcheck fails during upgrade of tables whose names include backticks
#
create table `t``1`(a int);
create table `t 1`(a int);
--replace_result 'Table is already up to date' OK
--exec $MYSQL_CHECK --databases test
drop table `t``1`, `t 1`;

#
# Bug#25347: mysqlcheck -A -r doesn't repair table marked as crashed
#
create database d_bug25347;
use d_bug25347;
create table t_bug25347 (a int);
create view v_bug25347 as select * from t_bug25347;
insert into t_bug25347 values (1),(2),(3);
flush tables;
let $MYSQLD_DATADIR= `select @@datadir`;
--echo removing and creating
--remove_file $MYSQLD_DATADIR/d_bug25347/t_bug25347.MYI
--write_file $MYSQLD_DATADIR/d_bug25347/t_bug25347.MYI
EOF
--exec $MYSQL_CHECK --repair --databases d_bug25347
--error 130
insert into t_bug25347 values (4),(5),(6);
--exec $MYSQL_CHECK --repair --use-frm --databases d_bug25347
insert into t_bug25347 values (7),(8),(9);
select * from t_bug25347;
select * from v_bug25347;
drop view v_bug25347;
drop table t_bug25347;
drop database d_bug25347;
use test;

#
# Bug#39541 CHECK TABLE on information_schema myisam tables produces error
#
create view v1 as select * from information_schema.routines;
check table v1, information_schema.routines;
drop view v1;

#
# Bug#37527: mysqlcheck fails to report entire database 
# when frm file corruption
#
CREATE TABLE t1(a INT);
CREATE TABLE t2(a INT);
# backup then null t1.frm
--copy_file $MYSQLD_DATADIR/test/t1.frm $MYSQLD_DATADIR/test/t1.frm.bak
--remove_file $MYSQLD_DATADIR/test/t1.frm
--write_file $MYSQLD_DATADIR/test/t1.frm
EOF
--exec $MYSQL_CHECK test
# restore t1.frm
--remove_file $MYSQLD_DATADIR/test/t1.frm
--copy_file $MYSQLD_DATADIR/test/t1.frm.bak $MYSQLD_DATADIR/test/t1.frm
--remove_file $MYSQLD_DATADIR/test/t1.frm.bak
DROP TABLE t1, t2;


--echo End of 5.0 tests


#
# Bug #30679: 5.1 name encoding not performed for views during upgrade
#
create table t1(a int);
create view v1 as select * from t1;
show tables;
let $MYSQLD_DATADIR= `select @@datadir`;
--copy_file $MYSQLD_DATADIR/test/v1.frm $MYSQLD_DATADIR/test/v-1.frm
show tables;
--exec $MYSQL_CHECK --check-upgrade --fix-table-names --databases test
show tables;
drop view v1, `v-1`;
drop table t1;

#
# Bug #33094: Error in upgrading from 5.0 to 5.1 when table contains triggers
# Bug #41385: Crash when attempting to repair a #mysql50# upgraded table with
#             triggers
#
SET NAMES utf8;
CREATE TABLE `#mysql50#@` (a INT);
SHOW TABLES;
SET NAMES DEFAULT;
--echo mysqlcheck --fix-table-names --databases test
--exec $MYSQL_CHECK --fix-table-names --databases test
SET NAMES utf8;
SHOW TABLES;
DROP TABLE `@`;

CREATE TABLE `я` (a INT);
SET NAMES DEFAULT;
--echo mysqlcheck --default-character-set="latin1" --databases test
# Error returned depends on platform, replace it with "Table doesn't exist"
--replace_result "Can't find file: './test/@003f.frm' (errno: 22)" "Table doesn't exist" "Table 'test.?' doesn't exist" "Table doesn't exist"
--exec $MYSQL_CHECK --default-character-set="latin1" --databases test
--echo mysqlcheck --default-character-set="utf8" --databases test
--exec $MYSQL_CHECK --default-character-set="utf8" --databases test
SET NAMES utf8;
DROP TABLE `я`;
SET NAMES DEFAULT;

CREATE DATABASE `#mysql50#a@b`;
USE `#mysql50#a@b`;
CREATE TABLE `#mysql50#c@d` (a INT);
CREATE TABLE t1 (a INT);

# Create 5.0 like triggers
let $MYSQLD_DATADIR= `select @@datadir`;
--write_file $MYSQLD_DATADIR/a@b/c@d.TRG
TYPE=TRIGGERS
triggers='CREATE DEFINER=`root`@`localhost` TRIGGER tr1 BEFORE INSERT ON `c@d` FOR EACH ROW SET NEW.a = 10 * NEW.a'
sql_modes=0
definers='root@localhost'
EOF
--write_file $MYSQLD_DATADIR/a@b/tr1.TRN
TYPE=TRIGGERNAME
trigger_table=c@d
EOF
--write_file $MYSQLD_DATADIR/a@b/t1.TRG
TYPE=TRIGGERS
triggers='CREATE DEFINER=`root`@`localhost` TRIGGER tr2 BEFORE INSERT ON `a@b`.t1 FOR EACH ROW SET NEW.a = 100 * NEW.a'
sql_modes=0
definers='root@localhost'
EOF
--write_file $MYSQLD_DATADIR/a@b/tr2.TRN
TYPE=TRIGGERNAME
trigger_table=t1
EOF

SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
  WHERE TRIGGER_SCHEMA="#mysql50#a@b" ORDER BY trigger_name;

--echo mysqlcheck --fix-db-names --fix-table-names --all-databases
--exec $MYSQL_CHECK --fix-db-names --fix-table-names --all-databases

USE `a@b`;
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
  WHERE TRIGGER_SCHEMA="a@b" ORDER BY trigger_name;

INSERT INTO `c@d` VALUES (2), (1);
SELECT * FROM `c@d`;
INSERT INTO t1 VALUES (3), (5);
SELECT * FROM t1;

DROP DATABASE `a@b`;

USE test;


--echo End of 5.1 tests
