# 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." exit 1 fi echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..." # Create a backup of the original database local BACKUP_PATH="${DB_PATH}.bak" cp "$DB_PATH" "$BACKUP_PATH" # Escape single quotes for SQL local OLD_STRING_ESC=$(printf '%s\n' "$OLD_STRING" | sed "s/'/''/g") local NEW_STRING_ESC=$(printf '%s\n' "$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" # Escape table name if it's a reserved keyword local ESCAPED_TABLE if [[ "$TABLE" =~ ^(index|group|order|table|view|database|schema|trigger|transaction|commit|rollback|savepoint|release|alter|create|drop|insert|update|delete|select|from|where|join|union|intersect|except|limit|offset|order|by|group|having|as|with|replace|cast|case|when|then|else|end|and|or|not|is|null|between|in|like|glob|match|regexp|collate|exists|unique|primary|key|foreign|references|check|constraint|default|collate|asc|desc|on|using|natural|left|right|full|outer|cross|inner)$ ]]; then ESCAPED_TABLE="\"$TABLE\"" else ESCAPED_TABLE="$TABLE" fi # Get a list of all non-BLOB columns in the table sqlite3 "$DB_PATH" "PRAGMA table_info($ESCAPED_TABLE);" 2>/dev/null | awk -F'|' ' { if ($3 != "BLOB") { print $2 } }' | while read -r COLUMN; do if [ -n "$COLUMN" ]; then echo " Processing column: $COLUMN" # Escape column name if it's a reserved keyword local ESCAPED_COLUMN if [[ "$COLUMN" =~ ^(index|group|order|table|view|database|schema|trigger|transaction|commit|rollback|savepoint|release|alter|create|drop|insert|update|delete|select|from|where|join|union|intersect|except|limit|offset|order|by|group|having|as|with|replace|cast|case|when|then|else|end|and|or|not|is|null|between|in|like|glob|match|regexp|collate|exists|unique|primary|key|foreign|references|check|constraint|default|collate|asc|desc|on|using|natural|left|right|full|outer|cross|inner)$ ]]; then ESCAPED_COLUMN="\"$COLUMN\"" else ESCAPED_COLUMN="$COLUMN" fi # Update only rows where the column contains OLD_STRING # Run each update in its own connection to avoid locking issues sqlite3 "$DB_PATH" " UPDATE $ESCAPED_TABLE SET $ESCAPED_COLUMN = replace(CAST($ESCAPED_COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC') WHERE CAST($ESCAPED_COLUMN AS TEXT) LIKE '%$OLD_STRING_ESC%'; " fi done done echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'." } #set script directory scriptdir="$(dirname "$(realpath "$0")")" #example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC" cp $scriptdir/stacks/dashboard/www/app-pre.sqlite $scriptdir/stacks/dashboard/www/app.sqlite replace_in_sqlite_db "$scriptdir/stacks/dashboard/www/app.sqlite" "mydomain.com" "" replace_in_sqlite_db "$scriptdir/stacks/dashboard/www/app.sqlite" "192.168.2.132" "" replace_in_sqlite_db "$scriptdir/stacks/dashboard/www/app.sqlite" "blah" "" replace_in_sqlite_db "$scriptdir/stacks/dashboard/www/app.sqlite" "0.0.0.0/0" ""