Fix argument parsing and documentation in setup_test_db.sh

Fixed critical issues with argument parsing that prevented the script
from working correctly:

1. Fixed argument order - script now supports both:
   - ./setup_test_db.sh <command> [options]
   - ./setup_test_db.sh [options] <command>

2. Fixed --help option - now shows help instead of running commands

3. Updated README.md examples with correct syntax:
   - OLD: ./setup_test_db.sh --mode native start (wrong)
   - NEW: ./setup_test_db.sh start --mode native (correct)

The script now properly:
- Parses -h/--help anywhere and shows usage
- Handles options before or after the command
- Auto-detects mode when not specified
- Shows helpful connection info after setup

All examples in README.md updated with correct command syntax.
pull/5310/head
Rene Cannao 4 months ago
parent 3d827144e8
commit b3646b4798

@ -52,7 +52,7 @@ source ~/.bashrc
```bash
# 1. Setup test database on your MySQL server
./setup_test_db.sh --mode native start
./setup_test_db.sh start --mode native
# 2. Configure ProxySQL MCP module
./configure_mcp.sh --host 127.0.0.1 --port 3306 --user root --enable
@ -64,14 +64,14 @@ source ~/.bashrc
./stress_test.sh
# 5. Clean up (drop test database)
./setup_test_db.sh --mode native reset
./setup_test_db.sh reset --mode native
```
### Using Docker
```bash
# 1. Start test MySQL container
./setup_test_db.sh --mode docker start
./setup_test_db.sh start --mode docker
# 2. Configure ProxySQL MCP module
./configure_mcp.sh --host 127.0.0.1 --port 3307 --enable
@ -83,7 +83,7 @@ source ~/.bashrc
./stress_test.sh
# 5. Stop test MySQL container
./setup_test_db.sh --mode docker stop
./setup_test_db.sh stop --mode docker
```
### Auto-Detect Mode
@ -133,19 +133,19 @@ Supports both **Docker** and **native MySQL** modes:
./setup_test_db.sh start
# Use native MySQL with specific credentials
./setup_test_db.sh --mode native --host localhost --port 3306 --user root start
./setup_test_db.sh start --mode native --host localhost --port 3306
# Use Docker explicitly
./setup_test_db.sh --mode docker start
./setup_test_db.sh start --mode docker
# Check status
./setup_test_db.sh --mode native status
./setup_test_db.sh status --mode native
# Connect to test database
./setup_test_db.sh --mode native connect
./setup_test_db.sh connect --mode native
# Drop and recreate test database
./setup_test_db.sh --mode native reset
./setup_test_db.sh reset --mode native
```
**Environment Variables:**
@ -156,7 +156,8 @@ export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export TEST_DB_NAME=testdb
./setup_test_db.sh --mode native start
# Options can be specified on command line or via environment
./setup_test_db.sh start --mode native
```
## Manual Testing

@ -0,0 +1,105 @@
-- Test Database Schema for MCP Testing
CREATE DATABASE IF NOT EXISTS testdb;
USE testdb;
CREATE TABLE IF NOT EXISTS customers (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_email (email)
);
CREATE TABLE IF NOT EXISTS orders (
id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
order_date DATE,
total DECIMAL(10,2),
status VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES customers(id),
INDEX idx_customer (customer_id),
INDEX idx_status (status)
);
CREATE TABLE IF NOT EXISTS products (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(200),
category VARCHAR(50),
price DECIMAL(10,2),
stock INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_category (category)
);
CREATE TABLE IF NOT EXISTS order_items (
id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT DEFAULT 1,
price DECIMAL(10,2),
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
-- Insert sample customers
INSERT INTO customers (name, email) VALUES
('Alice Johnson', 'alice@example.com'),
('Bob Smith', 'bob@example.com'),
('Charlie Brown', 'charlie@example.com'),
('Diana Prince', 'diana@example.com'),
('Eve Davis', 'eve@example.com');
-- Insert sample products
INSERT INTO products (name, category, price, stock) VALUES
('Laptop', 'Electronics', 999.99, 50),
('Mouse', 'Electronics', 29.99, 200),
('Keyboard', 'Electronics', 79.99, 150),
('Desk Chair', 'Furniture', 199.99, 75),
('Coffee Mug', 'Kitchen', 12.99, 500);
-- Insert sample orders
INSERT INTO orders (customer_id, order_date, total, status) VALUES
(1, '2024-01-15', 1029.98, 'completed'),
(2, '2024-01-16', 79.99, 'shipped'),
(1, '2024-01-17', 212.98, 'pending'),
(3, '2024-01-18', 199.99, 'completed'),
(4, '2024-01-19', 1099.98, 'shipped');
-- Insert sample order items
INSERT INTO order_items (order_id, product_id, quantity, price) VALUES
(1, 1, 1, 999.99),
(1, 2, 1, 29.99),
(2, 3, 1, 79.99),
(3, 1, 1, 999.99),
(3, 3, 1, 79.99),
(3, 5, 3, 38.97),
(4, 4, 1, 199.99),
(5, 1, 1, 999.99),
(5, 4, 1, 199.99);
-- Create a view
CREATE OR REPLACE VIEW customer_orders AS
SELECT
c.id AS customer_id,
c.name AS customer_name,
COUNT(o.id) AS order_count,
SUM(o.total) AS total_spent
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name;
-- Create a stored procedure
DELIMITER //
CREATE PROCEDURE get_customer_stats(IN customer_id INT)
BEGIN
SELECT
c.name,
COUNT(o.id) AS order_count,
COALESCE(SUM(o.total), 0) AS total_spent
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE c.id = customer_id;
END //
DELIMITER ;

@ -3,27 +3,24 @@
# setup_test_db.sh - Create/setup a test MySQL database with sample data
#
# Usage:
# ./setup_test_db.sh start [options] # Start/setup test database
# ./setup_test_db.sh stop [options] # Stop test database (Docker only)
# ./setup_test_db.sh status [options] # Check status
# ./setup_test_db.sh connect [options] # Connect to test database
# ./setup_test_db.sh reset [options] # Reset/drop test database
# ./setup_test_db.sh [options] <command>
# ./setup_test_db.sh <command> [options]
#
# Options:
# --mode MODE Mode: docker or native (default: auto-detect)
# --host HOST MySQL host (native mode, default: 127.0.0.1)
# --port PORT MySQL port (native mode, default: 3306)
# --user USER MySQL user (native mode, default: root)
# --password PASS MySQL password (native mode, will prompt if empty)
# --database DB Database name (default: testdb)
# --docker-port PORT Port for Docker container (default: 3307)
# Commands:
# start Setup/start test database
# stop Stop test database (Docker only)
# status Check status
# connect Connect to test database shell
# reset Drop/recreate test database
#
# Environment Variables:
# MYSQL_HOST MySQL host (native mode)
# MYSQL_PORT MySQL port (native mode)
# MYSQL_USER MySQL user
# MYSQL_PASSWORD MySQL password
# TEST_DB_NAME Test database name
# Options:
# --mode MODE Mode: docker or native (default: auto-detect)
# --host HOST MySQL host (native mode, default: 127.0.0.1)
# --port PORT MySQL port (native mode, default: 3306)
# --user USER MySQL user (native mode, default: root)
# --password PASS MySQL password
# --database DB Database name (default: testdb)
# -h, --help Show help
#
set -e
@ -301,10 +298,10 @@ status_docker() {
show_docker_tables
elif docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo -e "${YELLOW}${NC} Docker container '${CONTAINER_NAME}' exists but is ${YELLOW}stopped${NC}"
echo "Start with: $0 --mode docker start"
echo "Start with: $0 start --mode docker"
else
echo -e "${RED}${NC} Docker container '${CONTAINER_NAME}' does not exist"
echo "Create with: $0 --mode docker start"
echo "Create with: $0 start --mode docker"
fi
}
@ -376,6 +373,9 @@ start_native() {
if ! test_native_connection; then
log_error "Cannot connect to MySQL server"
log_error "Please ensure MySQL is running and credentials are correct"
log_error " Host: ${NATIVE_HOST}"
log_error " Port: ${NATIVE_PORT}"
log_error " User: ${NATIVE_USER}"
exit 1
fi
@ -394,7 +394,7 @@ start_native() {
stop_native() {
log_warn "Native mode: Database is not stopped (it's managed by MySQL server)"
log_info "To remove the test database, use: $0 --mode native reset"
log_info "To remove the test database, use: $0 reset --mode native"
}
status_native() {
@ -437,7 +437,7 @@ reset_native() {
exec_mysql_native "DROP DATABASE IF EXISTS ${DATABASE_NAME};"
log_info "Database dropped. Recreate with: $0 --mode native start"
log_info "Database dropped. Recreate with: $0 start --mode native"
}
test_native_connection() {
@ -476,62 +476,9 @@ show_native_tables() {
# ========== Main Functions ==========
parse_args() {
local command="$1"
shift
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
MODE="$2"
shift 2
;;
--host)
NATIVE_HOST="$2"
shift 2
;;
--port)
if [ "$2" = "3307" ] || [ "$2" = "3306" ]; then
NATIVE_PORT="$2"
else
# Could be docker port
if [ "${MODE}" = "docker" ]; then
DOCKER_PORT="$2"
else
NATIVE_PORT="$2"
fi
fi
shift 2
;;
--docker-port)
DOCKER_PORT="$2"
shift 2
;;
--user)
NATIVE_USER="$2"
shift 2
;;
--password)
NATIVE_PASSWORD="$2"
shift 2
;;
--database)
DATABASE_NAME="$2"
DOCKER_DATABASE="$2"
shift 2
;;
*)
log_error "Unknown option: $1"
echo "Use $0 <command> --help for usage"
exit 1
;;
esac
done
}
show_usage() {
cat <<EOF
Usage: $0 <command> [options]
Usage: $0 [options] <command>
Commands:
start Setup/start test database
@ -544,11 +491,11 @@ Commands:
Options:
--mode MODE Mode: docker, native, or auto (default: auto)
--host HOST MySQL host for native mode (default: 127.0.0.1)
--port PORT MySQL port (default: 3306 native, 3307 docker)
--docker-port PORT Docker container port (default: 3307)
--port PORT MySQL port (default: 3306)
--user USER MySQL user (default: root)
--password PASS MySQL password
--database DB Database name (default: testdb)
-h, --help Show this help
Environment Variables:
MYSQL_HOST MySQL host (native mode)
@ -561,11 +508,9 @@ Examples:
# Auto-detect mode and setup
$0 start
# Use native MySQL with custom credentials
$0 --mode native --host localhost --port 3306 --user root start
# Use Docker mode explicitly
$0 --mode docker start
# Use native MySQL explicitly
$0 start --mode native
$0 start --mode native --host localhost --port 3306
# Check status
$0 status
@ -575,6 +520,9 @@ Examples:
# Drop and recreate test database
$0 reset
# Stop Docker container
$0 stop --mode docker
EOF
}
@ -588,18 +536,101 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
[ -n "${MYSQL_PASSWORD}" ] && NATIVE_PASSWORD="${MYSQL_PASSWORD}"
[ -n "${TEST_DB_NAME}" ] && DATABASE_NAME="${TEST_DB_NAME}"
# Check if no arguments
if [ $# -eq 0 ]; then
# Parse arguments
COMMAND=""
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
--mode)
MODE="$2"
shift 2
;;
--host)
NATIVE_HOST="$2"
shift 2
;;
--port)
if [ "$2" = "3307" ]; then
DOCKER_PORT="$2"
else
NATIVE_PORT="$2"
fi
shift 2
;;
--user)
NATIVE_USER="$2"
shift 2
;;
--password)
NATIVE_PASSWORD="$2"
shift 2
;;
--database)
DATABASE_NAME="$2"
DOCKER_DATABASE="$2"
shift 2
;;
start|stop|status|connect|reset|create-sql)
COMMAND="$1"
shift
# Continue parsing options after command
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
MODE="$2"
shift 2
;;
--host)
NATIVE_HOST="$2"
shift 2
;;
--port)
if [ "$2" = "3307" ]; then
DOCKER_PORT="$2"
else
NATIVE_PORT="$2"
fi
shift 2
;;
--user)
NATIVE_USER="$2"
shift 2
;;
--password)
NATIVE_PASSWORD="$2"
shift 2
;;
--database)
DATABASE_NAME="$2"
DOCKER_DATABASE="$2"
shift 2
;;
*)
log_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
break
;;
*)
log_error "Unknown option or command: $1"
show_usage
exit 1
;;
esac
done
# Check if command was provided
if [ -z "${COMMAND}" ]; then
show_usage
exit 1
fi
COMMAND="$1"
shift
# Parse remaining arguments
parse_args "$@"
# Detect mode if auto
DETECTED_MODE=$(detect_mode)
if [ "${MODE}" = "auto" ]; then
@ -646,12 +677,4 @@ case "${COMMAND}" in
create-sql)
create_init_sql
;;
--help|-h)
show_usage
;;
*)
log_error "Unknown command: ${COMMAND}"
show_usage
exit 1
;;
esac

Loading…
Cancel
Save