db script sync fix

This commit is contained in:
2026-03-04 13:16:11 +01:00
parent 8f8ed0084e
commit 0bd2125f24
2 changed files with 18 additions and 15 deletions

View File

@@ -12,6 +12,10 @@ replace_in_sqlite_db() {
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
# Escape single quotes in the strings for SQL
local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g")
local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g")
# Get a list of all tables in the database
local TABLES
TABLES=$(sqlite3 "$DB_PATH" ".tables")
@@ -21,30 +25,27 @@ replace_in_sqlite_db() {
echo "Processing table: $TABLE"
# Get a list of all columns in the table
local COLUMNS
COLUMNS=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2}' | grep -v '^$')
sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2","$3}' | while IFS=, read -r COLUMN COLUMN_TYPE; do
# Skip empty lines
if [ -z "$COLUMN" ]; then
continue
fi
# Loop through each column
for COLUMN in $COLUMNS; do
# Check if the column is a text type (simplistic check)
local COLUMN_TYPE
COLUMN_TYPE=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' -v col="$COLUMN" '$2 == col {print $3}')
# Only proceed if the column type contains 'TEXT' (case-insensitive)
if [[ "$COLUMN_TYPE" =~ [Tt][Ee][Xx][Tt] ]]; then
# Check if the column is a string-based type (TEXT, VARCHAR, CHAR, CLOB, etc.)
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)"
# Perform the replacement
# Perform the replacement (handles infixes)
sqlite3 "$DB_PATH" "
UPDATE $TABLE
SET $COLUMN = replace($COLUMN, '$OLD_STRING', '$NEW_STRING')
WHERE $COLUMN LIKE '%$OLD_STRING%';
SET $COLUMN = replace($COLUMN, '$OLD_STRING_ESC', '$NEW_STRING_ESC')
WHERE $COLUMN LIKE '%$OLD_STRING_ESC%';
"
fi
done
done
echo "Replacement completed for all text-type columns in '$DB_PATH'."
echo "Replacement completed for all string-based columns in '$DB_PATH'."
}
#set script directory