LibrePortal/scripts/database/tables/db_display_tables.sh
librelad c6dd2659be refactor(de-sudo): apps DB access via runInstallOp, not sudo
The apps SQLite DB ($docker_dir/$db_file) is owned by the manager user, so
read/write it AS the manager via runInstallOp instead of sudo (root). 48 call
sites across 28 scripts. In rooted this drops root->manager (correct owner);
in rootless it's the manager too (using runFileOp/dockerinstall here was the
'unable to open database' bug). The broken 'command -v sudo sqlite3' check
lines are left untouched (separate pre-existing issue).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-24 16:23:33 +01:00

61 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
databaseDisplayTables()
{
if [[ "$toollistalltables" == [yY] ]]; then
local table_number=0
local selected_table=""
local sorted_tables=()
isHeader "View Database Table Data"
while true; do
# Check if sqlite3 is available
if ! command -v sudo sqlite3 &> /dev/null; then
isNotice "sqlite3 command not found. Make sure it's installed."
fi
# Ensure the database file exists
if [ ! -f "$docker_dir/$db_file" ]; then
isNotice "Database file not found: $docker_dir/$db_file"
fi
# Get a list of existing tables in the database
local tables_list=($(runInstallOp sqlite3 "$docker_dir/$db_file" ".tables"))
# Sort the tables alphabetically
sorted_tables=($(printf "%s\n" "${tables_list[@]}" | sort))
# Check if there are any tables in the database
if [ "${#sorted_tables[@]}" -eq 0 ]; then
isError "No tables found in the database."
fi
table_number=0
for table_name in "${sorted_tables[@]}"; do
table_number=$((table_number+1))
echo "$table_number. $table_name"
done
echo ""
isQuestion "Enter the number of the table to view data (x to exit): "
read -p "" selected_table
if [[ "$selected_table" == "x" ]]; then
echo ""
echo "Exiting."
elif [[ "$selected_table" =~ ^[0-9]+$ ]] && ((selected_table >= 1)) && ((selected_table <= "${#sorted_tables[@]}")); then
selected_table="${sorted_tables[$((selected_table-1))]}"
# Display all data for the selected table with formatted output
isHeader "Displaying $selected_table Table Data"
runInstallOp sqlite3 -column -header "$docker_dir/$db_file" "SELECT * FROM $selected_table;"
echo ""
isQuestion "Press Enter to continue..."
read -p "" input
else
isNotice "Invalid table number. Please try again."
fi
done
fi
}