fix db again part 2
This commit is contained in:
46
prepdb.sh
46
prepdb.sh
@@ -10,11 +10,14 @@ replace_in_sqlite_db() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Starting brute-force replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
echo "Starting safe replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
||||||
|
|
||||||
# Escape single quotes for SQL
|
# Escape single quotes for SQL
|
||||||
local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g")
|
local OLD_STRING_ESC=$(printf "%s" "$OLD_STRING" | sed "s/'/''/g")
|
||||||
local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g")
|
local NEW_STRING_ESC=$(printf "%s" "$NEW_STRING" | sed "s/'/''/g")
|
||||||
|
|
||||||
|
# Begin a transaction for safety
|
||||||
|
sqlite3 "$DB_PATH" "BEGIN TRANSACTION;"
|
||||||
|
|
||||||
# Get a list of all tables in the database
|
# Get a list of all tables in the database
|
||||||
local TABLES
|
local TABLES
|
||||||
@@ -24,31 +27,38 @@ replace_in_sqlite_db() {
|
|||||||
for TABLE in $TABLES; do
|
for TABLE in $TABLES; do
|
||||||
echo "Processing table: $TABLE"
|
echo "Processing table: $TABLE"
|
||||||
|
|
||||||
# Get a list of all columns in the table
|
# Get a list of all non-BLOB columns in the table
|
||||||
local COLUMNS
|
sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '
|
||||||
COLUMNS=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2}' | grep -v '^$')
|
{
|
||||||
|
if ($3 != "BLOB") {
|
||||||
|
print $2
|
||||||
|
}
|
||||||
|
}' | while read -r COLUMN; do
|
||||||
|
# Skip empty lines
|
||||||
|
if [ -n "$COLUMN" ]; then
|
||||||
|
echo " Processing column: $COLUMN"
|
||||||
|
|
||||||
# Loop through each column
|
# Update only rows where the column contains OLD_STRING
|
||||||
for COLUMN in $COLUMNS; do
|
sqlite3 "$DB_PATH" "
|
||||||
echo " Processing column: $COLUMN"
|
UPDATE $TABLE
|
||||||
|
SET $COLUMN = replace(CAST($COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
||||||
# Update every cell in the column, casting to TEXT to force string replacement
|
WHERE CAST($COLUMN AS TEXT) LIKE '%$OLD_STRING_ESC%';
|
||||||
sqlite3 "$DB_PATH" "
|
"
|
||||||
UPDATE $TABLE
|
fi
|
||||||
SET $COLUMN = replace(CAST($COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
|
||||||
WHERE CAST($COLUMN AS TEXT) LIKE '%$OLD_STRING_ESC%';
|
|
||||||
"
|
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "replacement of '$OLD_STRING' with '$NEW_STRING' completed in '$DB_PATH'."
|
# Commit the transaction
|
||||||
|
sqlite3 "$DB_PATH" "COMMIT;"
|
||||||
|
|
||||||
|
echo "Safe replacement completed in '$DB_PATH'."
|
||||||
}
|
}
|
||||||
|
|
||||||
#set script directory
|
#set script directory
|
||||||
scriptdir="$(dirname "$(realpath "$0")")"
|
scriptdir="$(dirname "$(realpath "$0")")"
|
||||||
|
|
||||||
#example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC"
|
#example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC"
|
||||||
|
cp $scriptdir/stacks/npm/data/database-rep.sqlit $scriptdir/stacks/npm/data/databse.sqlite
|
||||||
replace_in_sqlite_db "$scriptdir/stacks/npm/data/database.sqlite" "sdgserver.online" "?domain?"
|
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" "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" "z5fGWz2i0q" "?adminpass?"
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user