update
This commit is contained in:
43
prepdb.sh
43
prepdb.sh
@@ -20,19 +20,26 @@ replace_in_sqlite_db() {
|
|||||||
local OLD_STRING_ESC=$(printf '%s\n' "$OLD_STRING" | sed "s/'/''/g")
|
local OLD_STRING_ESC=$(printf '%s\n' "$OLD_STRING" | sed "s/'/''/g")
|
||||||
local NEW_STRING_ESC=$(printf '%s\n' "$NEW_STRING" | sed "s/'/''/g")
|
local NEW_STRING_ESC=$(printf '%s\n' "$NEW_STRING" | sed "s/'/''/g")
|
||||||
|
|
||||||
# Begin a transaction
|
# Begin a transaction at the start
|
||||||
sqlite3 "$DB_PATH" "BEGIN TRANSACTION;"
|
sqlite3 "$DB_PATH" "BEGIN TRANSACTION;" || {
|
||||||
|
echo "Error: Failed to begin transaction"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
# Get a list of all tables in the database
|
# Get a list of all tables in the database
|
||||||
local TABLES
|
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
|
# Loop through each table
|
||||||
for TABLE in $TABLES; do
|
for TABLE in $TABLES; do
|
||||||
echo "Processing table: $TABLE"
|
echo "Processing table: $TABLE"
|
||||||
|
|
||||||
# Get a list of all non-BLOB columns in the 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") {
|
if ($3 != "BLOB") {
|
||||||
print $2
|
print $2
|
||||||
@@ -41,18 +48,34 @@ replace_in_sqlite_db() {
|
|||||||
if [ -n "$COLUMN" ]; then
|
if [ -n "$COLUMN" ]; then
|
||||||
echo " Processing column: $COLUMN"
|
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
|
# Update only rows where the column contains OLD_STRING
|
||||||
sqlite3 "$DB_PATH" "
|
sqlite3 "$DB_PATH" "
|
||||||
UPDATE $TABLE
|
UPDATE $ESCAPED_TABLE
|
||||||
SET $COLUMN = replace(CAST($COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
SET $ESCAPED_COLUMN = replace(CAST($ESCAPED_COLUMN AS TEXT), '$OLD_STRING_ESC', '$NEW_STRING_ESC')
|
||||||
WHERE CAST($COLUMN AS TEXT) LIKE '%$OLD_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
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
# Commit the transaction
|
# Commit the transaction at the end
|
||||||
sqlite3 "$DB_PATH" "COMMIT;"
|
sqlite3 "$DB_PATH" "COMMIT;" || {
|
||||||
|
echo "Error committing transaction"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'."
|
echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'."
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user