59 lines
2.1 KiB
Bash
59 lines
2.1 KiB
Bash
# sqlite db replace function
|
|
replace_in_sqlite_db() {
|
|
local DB_PATH="$1"
|
|
local OLD_STRING="$2"
|
|
local NEW_STRING="$3"
|
|
|
|
# Check if the database file exists
|
|
if [ ! -f "$DB_PATH" ]; then
|
|
echo "Error: Database file '$DB_PATH' does not exist."
|
|
return 1
|
|
fi
|
|
|
|
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
|
|
|
# Get a list of all tables in the database
|
|
local TABLES
|
|
TABLES=$(sqlite3 "$DB_PATH" ".tables")
|
|
|
|
# Loop through each table
|
|
for TABLE in $TABLES; do
|
|
echo "Processing table: $TABLE"
|
|
|
|
# Get a list of all columns in the table
|
|
local COLUMNS
|
|
COLUMNS=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2}' | grep -v '^$')
|
|
|
|
# Loop through each column
|
|
for COLUMN in $COLUMNS; do
|
|
# Check if the column is a text type (simplistic check)
|
|
local COLUMN_TYPE
|
|
COLUMN_TYPE=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' -v col="$COLUMN" '$2 == col {print $3}')
|
|
|
|
# Only proceed if the column type contains 'TEXT' (case-insensitive)
|
|
if [[ "$COLUMN_TYPE" =~ [Tt][Ee][Xx][Tt] ]]; then
|
|
echo " Updating column: $COLUMN (type: $COLUMN_TYPE)"
|
|
|
|
# Perform the replacement
|
|
sqlite3 "$DB_PATH" "
|
|
UPDATE $TABLE
|
|
SET $COLUMN = replace($COLUMN, '$OLD_STRING', '$NEW_STRING')
|
|
WHERE $COLUMN LIKE '%$OLD_STRING%';
|
|
"
|
|
fi
|
|
done
|
|
done
|
|
|
|
echo "Replacement completed for all text-type columns in '$DB_PATH'."
|
|
}
|
|
|
|
#set script directory
|
|
scriptdir="$(dirname "$(realpath "$0")")"
|
|
|
|
#example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC"
|
|
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "sdgserver.online" "?domain?"
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "192.168.2.132" "?localip?"
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "z5fGWz2i0q" "?adminpass?"
|
|
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "0.0.0.0/0" "?localip?"
|