#!/bin/sh
#
# Copyright (c) 2005 MySQL AB
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

echo "######################################################################"
echo "#"
echo "# Test BUG#12166 - Unable to index very large tables"
echo "#"

#######################################################################
#
# Settings. CAUTION: Paths are not space safe.
#

# BASEDIR="install directory"
# DATADIR="databases directory e.g. $BASEDIR/var"

MYSQLD="$BASEDIR/libexec/mysqld"
MYSQLC="$BASEDIR/bin/mysql"
MYSQLA="$BASEDIR/bin/mysqladmin"
MYSQLT="$BASEDIR/bin/mysqltest"

PORT_1="${MYSQL_TCP_PORT:+--port=$MYSQL_TCP_PORT}"
SOCK_1="${MYSQL_UNIX_PORT:+--socket=$MYSQL_UNIX_PORT}"
USER_1="-u root -D test"
CLNT_1="-v -f"
DATA_1="--basedir=$BASEDIR --datadir=$DATADIR"
SERV_1="--log-error --core"
DBUG_1= #"--debug=t:d:i:O,$DATADIR/mysqld.trace"

cd $DATADIR || exit $?
rm -f *.trace *.err core* test/#sql*

echo "#######################################################################"
echo "#"
echo "# Special for this test case: Use a small tmpdir."
echo "#"
echo "# This is not portable. Find a way to implement it on your machine."
echo "#"
#
# Mounting /mnt/tmpfs1, a file system with 1 MB of space only
# The corresponding line in /etc/fstab looks like:
# tmpfs   /mnt/tmpfs1   tmpfs   user,size=1m   0   0
#
umount /mnt/tmpfs1 2>/dev/null ||:   # unmount if mounted, ignore errors
mount /mnt/tmpfs1 || exit $?         # mount, stop if error
#
# Add the new temdir to the server flags.
#
SERV_1="$SERV_1 --tmpdir=/mnt/tmpfs1"
#
#
echo "# tmpdir is successfully set up."
echo
sleep 1


echo "######################################################################"
echo "#"
echo "# Starting database server."
"$MYSQLD" $PORT_1 $SOCK_1 $DATA_1 $SERV_1 $DBUG_1 &
SERV_1_PID=$!
echo "Process_id $SERV_1_PID"
echo
sleep 3


echo "######################################################################"
echo "#"
echo "# Starting client."
"$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1 <<EOF
    drop table if exists t1;
    create table t1 (c1 int auto_increment primary key, c2 char(100));
    insert into t1 (c2) values ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbccccccccccccccccccdddddddddddddddddee');
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;

    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;

    update t1 set c2=concat(c1,c2);
    insert into t1 (c2) select c2 from t1;
    insert into t1 (c2) select c2 from t1;
    system ls -la test/t1* /mnt/tmpfs1;
    system echo;
    alter table t1 add index (c2);
    show warnings;
    system ls -la test/t1* /mnt/tmpfs1;
    system df -k /mnt/tmpfs1;
    system echo;
    show create table t1;
EOF
echo
sleep 1


echo "######################################################################"
echo "#"
echo "# Stopping database server."
"$MYSQLA" $PORT_1 $SOCK_1 -u root shutdown
sleep 3


echo "# End of Test."
echo "#"
echo "######################################################################"

