update
This commit is contained in:
37
prepdb.sh
37
prepdb.sh
@@ -20,7 +20,7 @@ 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 at the start
|
||||
# Begin a transaction
|
||||
sqlite3 "$DB_PATH" "BEGIN TRANSACTION;" || {
|
||||
echo "Error: Failed to begin transaction"
|
||||
exit 1
|
||||
@@ -38,8 +38,16 @@ replace_in_sqlite_db() {
|
||||
for TABLE in $TABLES; do
|
||||
echo "Processing table: $TABLE"
|
||||
|
||||
# Escape table name if it's a reserved keyword
|
||||
local ESCAPED_TABLE
|
||||
if [[ "$TABLE" =~ ^(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_TABLE="\"$TABLE\""
|
||||
else
|
||||
ESCAPED_TABLE="$TABLE"
|
||||
fi
|
||||
|
||||
# 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($ESCAPED_TABLE);" 2>/dev/null | awk -F'|' '
|
||||
{
|
||||
if ($3 != "BLOB") {
|
||||
print $2
|
||||
@@ -50,7 +58,6 @@ replace_in_sqlite_db() {
|
||||
|
||||
# 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
|
||||
@@ -58,26 +65,24 @@ replace_in_sqlite_db() {
|
||||
fi
|
||||
|
||||
# Update only rows where the column contains OLD_STRING
|
||||
sqlite3 "$DB_PATH" "
|
||||
if ! sqlite3 "$DB_PATH" "
|
||||
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
|
||||
}
|
||||
" 2>/dev/null; then
|
||||
echo " Warning: Failed to update table $TABLE, column $COLUMN"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# 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'."
|
||||
# Commit the transaction
|
||||
if ! sqlite3 "$DB_PATH" "COMMIT;" 2>/dev/null; then
|
||||
echo "Warning: Failed to commit transaction. Some changes may not have been applied."
|
||||
sqlite3 "$DB_PATH" "ROLLBACK;" 2>/dev/null
|
||||
else
|
||||
echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'."
|
||||
fi
|
||||
}
|
||||
|
||||
#set script directory
|
||||
|
||||
Reference in New Issue
Block a user