it should work this time. for the love of god.
This commit is contained in:
76
prepdb.sh
76
prepdb.sh
@@ -10,15 +10,9 @@ replace_in_sqlite_db() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if jq is installed
|
echo "Starting brute-force replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
||||||
if ! command -v jq &> /dev/null; then
|
|
||||||
echo "Error: 'jq' is not installed. Please install it first (e.g., 'sudo apt install jq')."
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in all string-based columns in '$DB_PATH'..."
|
# Escape single quotes for SQL
|
||||||
|
|
||||||
# Escape single quotes in the strings for SQL
|
|
||||||
local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g")
|
local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g")
|
||||||
local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g")
|
local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g")
|
||||||
|
|
||||||
@@ -31,71 +25,23 @@ replace_in_sqlite_db() {
|
|||||||
echo "Processing table: $TABLE"
|
echo "Processing table: $TABLE"
|
||||||
|
|
||||||
# Get a list of all columns in the 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
|
local COLUMNS
|
||||||
# Skip empty lines
|
COLUMNS=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2}' | grep -v '^$')
|
||||||
if [ -z "$COLUMN" ]; then
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if the column is a string-based type (TEXT, VARCHAR, CHAR, CLOB, etc.)
|
# Loop through each column
|
||||||
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
|
for COLUMN in $COLUMNS; do
|
||||||
echo " Processing column: $COLUMN (type: $COLUMN_TYPE)"
|
echo " Processing column: $COLUMN"
|
||||||
|
|
||||||
# Check if the column contains JSON data
|
# Update every cell in the column, casting to TEXT to force string replacement
|
||||||
local IS_JSON=$(sqlite3 "$DB_PATH" "
|
|
||||||
SELECT COUNT(*) FROM $TABLE
|
|
||||||
WHERE json_valid($COLUMN) = 1 LIMIT 1;
|
|
||||||
")
|
|
||||||
|
|
||||||
if [ "$IS_JSON" -gt 0 ]; then
|
|
||||||
echo " Detected JSON data in column $COLUMN. Processing with jq..."
|
|
||||||
|
|
||||||
# Create a temporary table to store updated JSON
|
|
||||||
sqlite3 "$DB_PATH" "
|
|
||||||
CREATE TABLE IF NOT EXISTS temp_$TABLE (rowid INTEGER PRIMARY KEY, new_$COLUMN TEXT);
|
|
||||||
"
|
|
||||||
|
|
||||||
# Export JSON data, modify with jq, and store in temp table
|
|
||||||
sqlite3 "$DB_PATH" "SELECT rowid, $COLUMN FROM $TABLE WHERE json_valid($COLUMN) = 1;" | while read -r ROWID JSON_DATA; do
|
|
||||||
# Skip the header line (rowid|column)
|
|
||||||
if [ "$ROWID" != "rowid" ]; then
|
|
||||||
# Replace the string in the JSON using jq
|
|
||||||
local MODIFIED_JSON
|
|
||||||
MODIFIED_JSON=$(echo "$JSON_DATA" | jq --arg old_str "$OLD_STRING" --arg new_str "$NEW_STRING" '
|
|
||||||
walk(if type == "string" then gsub($old_str; $new_str) else . end)
|
|
||||||
')
|
|
||||||
|
|
||||||
# Insert the modified JSON into the temp table
|
|
||||||
sqlite3 "$DB_PATH" "
|
|
||||||
INSERT INTO temp_$TABLE (rowid, new_$COLUMN) VALUES ($ROWID, '$MODIFIED_JSON');
|
|
||||||
"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# Update the original table with the modified JSON
|
|
||||||
sqlite3 "$DB_PATH" "
|
sqlite3 "$DB_PATH" "
|
||||||
UPDATE $TABLE
|
UPDATE $TABLE
|
||||||
SET $COLUMN = (SELECT new_$COLUMN FROM temp_$TABLE WHERE temp_$TABLE.rowid = $TABLE.rowid)
|
SET $COLUMN = replace(CAST($COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
||||||
WHERE rowid IN (SELECT rowid FROM temp_$TABLE);
|
WHERE CAST($COLUMN AS TEXT) LIKE '%$OLD_STRING_ESC%';
|
||||||
"
|
"
|
||||||
|
|
||||||
# Drop the temporary table
|
|
||||||
sqlite3 "$DB_PATH" "DROP TABLE temp_$TABLE;"
|
|
||||||
else
|
|
||||||
echo " Column $COLUMN is not JSON. Using replace()."
|
|
||||||
|
|
||||||
# Use replace() for non-JSON strings
|
|
||||||
sqlite3 "$DB_PATH" "
|
|
||||||
UPDATE $TABLE
|
|
||||||
SET $COLUMN = replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
|
||||||
WHERE $COLUMN LIKE '%$OLD_STRING_ESC%';
|
|
||||||
"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Replacement completed for all string-based columns (including JSON) in '$DB_PATH'."
|
echo "replacement of '$OLD_STRING' with '$NEW_STRING' completed in '$DB_PATH'."
|
||||||
}
|
}
|
||||||
|
|
||||||
#set script directory
|
#set script directory
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user