# 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'..." # Escape single quotes in the strings for SQL local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g") local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g") # 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 sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2","$3}' | while IFS=, read -r COLUMN COLUMN_TYPE; do # Skip empty lines if [ -z "$COLUMN" ]; then continue fi # Check if the column is a string-based type (TEXT, VARCHAR, CHAR, CLOB, etc.) if [[ "$COLUMN_TYPE" =~ [Tt][Ee][Xx][Tt]|[Vv][Aa][Rr][Cc][Hh][Aa][Rr]|[Cc][Hh][Aa][Rr]|[Cc][Ll][Oo][Bb]|[Ss][Tt][Rr][Ii][Nn][Gg] ]]; then echo " Updating column: $COLUMN (type: $COLUMN_TYPE)" # Perform the replacement (handles infixes) sqlite3 "$DB_PATH" " UPDATE $TABLE SET $COLUMN = replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC') WHERE $COLUMN LIKE '%$OLD_STRING_ESC%'; " fi done done echo "Replacement completed for all string-based 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?"