Skip to content

Commit 9615d82

Browse files
authored
chore(script): add check_bad_tables.sh (#18300)
* chore(script): add check_bad_tables.sh * Update check_bad_tables.sh * chore(script): add check_bad_tables.sh
1 parent 4396ba1 commit 9615d82

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

scripts/selfhost/check_bad_tables.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/bin/bash
2+
3+
# Initialize variables
4+
DSN=""
5+
SHOW_HELP=false
6+
7+
# Function to display help
8+
display_help() {
9+
cat <<EOF
10+
Usage: $0 [OPTIONS]
11+
12+
Options:
13+
--dsn <connection_string> Specify the DSN connection string for bendsql (overrides BENDSQL_DSN)
14+
--help Display this help message and exit
15+
16+
Environment Variables:
17+
BENDSQL_DSN Default DSN connection string if --dsn is not specified
18+
19+
This script performs the following operations:
20+
1. Lists all databases
21+
2. For each database, attempts to query its columns directly
22+
3. If that fails, lists all tables in the database
23+
4. For each table, attempts to query its columns
24+
5. Reports any databases/tables where column queries fail
25+
26+
Examples:
27+
$0 --dsn "user:password@localhost:5432/dbname"
28+
BENDSQL_DSN="user:password@localhost:5432/dbname" $0
29+
$0 --help
30+
EOF
31+
}
32+
33+
# Parse command line arguments
34+
while [[ "$#" -gt 0 ]]; do
35+
case $1 in
36+
--dsn) DSN="$2"; shift ;;
37+
--help) SHOW_HELP=true ;;
38+
*) echo "Error: Unknown parameter: $1"; display_help; exit 1 ;;
39+
esac
40+
shift
41+
done
42+
43+
# Show help if requested
44+
if [ "$SHOW_HELP" = true ]; then
45+
display_help
46+
exit 0
47+
fi
48+
49+
# Check for DSN in environment variable if not specified via command line
50+
if [ -z "$DSN" ] && [ -n "$BENDSQL_DSN" ]; then
51+
DSN="$BENDSQL_DSN"
52+
fi
53+
54+
# Function to execute bendsql query and handle errors
55+
execute_query() {
56+
local query="$1"
57+
local result
58+
local bendsql_cmd="bendsql"
59+
60+
# Add DSN if specified
61+
if [ -n "$DSN" ]; then
62+
bendsql_cmd="$bendsql_cmd --dsn=\"$DSN\""
63+
fi
64+
65+
bendsql_cmd="$bendsql_cmd --query=\"$query\""
66+
67+
# Use eval to properly handle quotes in the command
68+
result=$(eval "$bendsql_cmd" 2>&1)
69+
if [ $? -ne 0 ]; then
70+
return 1
71+
fi
72+
echo "$result" | tail -n +2 # Skip header row
73+
}
74+
75+
# Get all databases
76+
echo "Fetching list of databases..."
77+
databases=$(execute_query "SELECT name FROM system.databases")
78+
if [ $? -ne 0 ]; then
79+
echo "Error: Failed to get databases list" >&2
80+
exit 1
81+
fi
82+
83+
# Process each database
84+
while IFS= read -r db; do
85+
if [ -z "$db" ]; then
86+
continue
87+
fi
88+
89+
echo "Processing database: $db"
90+
91+
# Try to get columns directly
92+
columns_query="SELECT * FROM system.columns WHERE database = '$db'"
93+
if ! execute_query "$columns_query" >/dev/null 2>&1; then
94+
# If error, get tables first
95+
tables=$(execute_query "SELECT name FROM system.tables WHERE database = '$db'")
96+
if [ $? -ne 0 ]; then
97+
echo "Error: Failed to get tables for database '$db'" >&2
98+
continue
99+
fi
100+
101+
# Process each table
102+
while IFS= read -r table; do
103+
if [ -z "$table" ]; then
104+
continue
105+
fi
106+
107+
echo " Processing table: $table"
108+
# Try to get columns for this table
109+
table_columns_query="SELECT * FROM system.columns WHERE database = '$db' AND table = '$table'"
110+
if ! execute_query "$table_columns_query" >/dev/null 2>&1; then
111+
echo "Error encountered for database: '$db', table: '$table'"
112+
fi
113+
done <<< "$tables"
114+
fi
115+
done <<< "$databases"
116+
117+
echo "Processing complete."

0 commit comments

Comments
 (0)