From 38f9045ebd04428cb04f2c56c746a1e79c4d5e21 Mon Sep 17 00:00:00 2001 From: SDGDen Date: Thu, 5 Mar 2026 01:46:34 +0100 Subject: [PATCH] update --- prepdb.sh | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/prepdb.sh b/prepdb.sh index 943fb84..e04ffe5 100644 --- a/prepdb.sh +++ b/prepdb.sh @@ -20,19 +20,26 @@ replace_in_sqlite_db() { local OLD_STRING_ESC=$(printf '%s\n' "$OLD_STRING" | sed "s/'/''/g") local NEW_STRING_ESC=$(printf '%s\n' "$NEW_STRING" | sed "s/'/''/g") - # Begin a transaction - sqlite3 "$DB_PATH" "BEGIN TRANSACTION;" + # Begin a transaction at the start + sqlite3 "$DB_PATH" "BEGIN TRANSACTION;" || { + echo "Error: Failed to begin transaction" + exit 1 + } # Get a list of all tables in the database local TABLES - TABLES=$(sqlite3 "$DB_PATH" ".tables") + TABLES=$(sqlite3 "$DB_PATH" ".tables") || { + echo "Error: Failed to get list of tables" + sqlite3 "$DB_PATH" "ROLLBACK;" + exit 1 + } # Loop through each table for TABLE in $TABLES; do echo "Processing table: $TABLE" # Get a list of all non-BLOB columns in the table - sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' ' + sqlite3 "$DB_PATH" "PRAGMA table_info(\"$TABLE\");" | awk -F'|' ' { if ($3 != "BLOB") { print $2 @@ -41,18 +48,34 @@ replace_in_sqlite_db() { if [ -n "$COLUMN" ]; then echo " Processing column: $COLUMN" + # Escape column name if it's a reserved keyword + local ESCAPED_COLUMN + local ESCAPED_TABLE="\"$TABLE\"" + 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 sqlite3 "$DB_PATH" " - UPDATE $TABLE - SET $COLUMN = replace(CAST($COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC') - WHERE CAST($COLUMN AS TEXT) LIKE '%$OLD_STRING_ESC%'; - " + 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%'; + " || { + echo "Error updating table $TABLE, column $COLUMN" + sqlite3 "$DB_PATH" "ROLLBACK;" + exit 1 + } fi done done - # Commit the transaction - sqlite3 "$DB_PATH" "COMMIT;" + # Commit the transaction at the end + sqlite3 "$DB_PATH" "COMMIT;" || { + echo "Error committing transaction" + exit 1 + } echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'." }