another database function script fix, this time for json compat.

This commit is contained in:
2026-03-04 13:20:52 +01:00
parent 0bd2125f24
commit d9181075a1

View File

@@ -12,6 +12,11 @@ replace_in_sqlite_db() {
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..." echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
# Enable the json1 extension
sqlite3 "$DB_PATH" "SELECT load_extension('json1');" || {
echo "Warning: json1 extension not available. JSON fields will not be processed."
}
# Escape single quotes in the strings 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")
@@ -35,17 +40,36 @@ replace_in_sqlite_db() {
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 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)" echo " Updating column: $COLUMN (type: $COLUMN_TYPE)"
# Perform the replacement (handles infixes) # Check if the column contains JSON data
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. Using json_replace()."
# Use json_replace() to handle JSON strings
sqlite3 "$DB_PATH" "
UPDATE $TABLE
SET $COLUMN = json_replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC')
WHERE json_valid($COLUMN) = 1;
"
else
echo " Column $COLUMN is not JSON. Using replace()."
# Use replace() for non-JSON strings
sqlite3 "$DB_PATH" " sqlite3 "$DB_PATH" "
UPDATE $TABLE UPDATE $TABLE
SET $COLUMN = replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC') SET $COLUMN = replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC')
WHERE $COLUMN LIKE '%$OLD_STRING_ESC%'; WHERE $COLUMN LIKE '%$OLD_STRING_ESC%';
" "
fi fi
fi
done done
done done
echo "Replacement completed for all string-based columns in '$DB_PATH'." echo "Replacement completed for all string-based columns (including JSON) in '$DB_PATH'."
} }
#set script directory #set script directory