Files
CloudDeploy/prepdb.sh
2026-03-05 01:50:45 +01:00

117 lines
4.9 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."
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")
# Start a transaction with immediate mode for better locking
if ! sqlite3 "$DB_PATH" "BEGIN IMMEDIATE TRANSACTION;"; then
echo "Error: Failed to begin transaction"
exit 1
fi
# Get a list of all tables in the database
local TABLES
TABLES=$(sqlite3 "$DB_PATH" ".tables") || {
echo "Error: Failed to get list of tables"
sqlite3 "$DB_PATH" "ROLLBACK;"
exit 1
}
# Track if we made any changes
local CHANGES_MADE=false
# 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
if 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%';
" 2>/dev/null; then
CHANGES_MADE=true
else
echo " Warning: Failed to update table $TABLE, column $COLUMN"
fi
fi
done
done
# Try to commit the transaction with retries
local RETRIES=3
local COMMIT_SUCCESS=false
while [ $RETRIES -gt 0 ]; do
if sqlite3 "$DB_PATH" "COMMIT;" 2>/dev/null; then
COMMIT_SUCCESS=true
break
else
echo "Warning: Failed to commit transaction, retrying ($RETRIES attempts left)..."
sleep 1
((RETRIES--))
fi
done
if [ "$COMMIT_SUCCESS" = false ]; then
echo "Error: Failed to commit transaction after multiple attempts"
sqlite3 "$DB_PATH" "ROLLBACK;" 2>/dev/null
exit 1
else
echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'."
fi
}
#set script directory
scriptdir="$(dirname "$(realpath "$0")")"
#example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC"
cp $scriptdir/stacks/npm/data/database-pre.sqlite $scriptdir/stacks/npm/data/database.sqlite
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?"