whoops forgot an update
This commit is contained in:
58
deploy.sh
58
deploy.sh
@@ -48,28 +48,60 @@ replace_in_sqlite_db() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Starting safe replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
echo "Starting replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
|
||||||
|
|
||||||
# Create a backup of the original database
|
# Create a backup of the original database
|
||||||
local BACKUP_PATH="${DB_PATH}.bak"
|
local BACKUP_PATH="${DB_PATH}.bak"
|
||||||
cp "$DB_PATH" "$BACKUP_PATH"
|
cp "$DB_PATH" "$BACKUP_PATH"
|
||||||
|
|
||||||
# Dump the database to SQL
|
# Escape single quotes for SQL
|
||||||
local DUMP_PATH="${DB_PATH}.sql"
|
local OLD_STRING_ESC=$(printf '%s\n' "$OLD_STRING" | sed "s/'/''/g")
|
||||||
sqlite3 "$DB_PATH" ".dump" > "$DUMP_PATH"
|
local NEW_STRING_ESC=$(printf '%s\n' "$NEW_STRING" | sed "s/'/''/g")
|
||||||
|
|
||||||
# Replace strings in the SQL dump
|
# Get a list of all tables in the database
|
||||||
sed -i "s|$OLD_STRING|$NEW_STRING|g" "$DUMP_PATH"
|
local TABLES
|
||||||
|
TABLES=$(sqlite3 "$DB_PATH" ".tables")
|
||||||
|
|
||||||
# Create a new database from the modified SQL dump
|
# Loop through each table
|
||||||
local NEW_DB_PATH="${DB_PATH}.new"
|
for TABLE in $TABLES; do
|
||||||
sqlite3 "$NEW_DB_PATH" < "$DUMP_PATH"
|
echo "Processing table: $TABLE"
|
||||||
|
|
||||||
# Replace the original database with the new one
|
# Escape table name if it's a reserved keyword
|
||||||
mv "$NEW_DB_PATH" "$DB_PATH"
|
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
|
||||||
|
|
||||||
# Clean up temporary files
|
# Get a list of all non-BLOB columns in the table
|
||||||
rm "$DUMP_PATH"
|
sqlite3 "$DB_PATH" "PRAGMA table_info($ESCAPED_TABLE);" 2>/dev/null | awk -F'|' '
|
||||||
|
{
|
||||||
|
if ($3 != "BLOB") {
|
||||||
|
print $2
|
||||||
|
}
|
||||||
|
}' | while read -r COLUMN; do
|
||||||
|
if [ -n "$COLUMN" ]; then
|
||||||
|
echo " Processing column: $COLUMN"
|
||||||
|
|
||||||
|
# Escape column name if it's a reserved keyword
|
||||||
|
local ESCAPED_COLUMN
|
||||||
|
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
|
||||||
|
# Run each update in its own connection to avoid locking issues
|
||||||
|
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%';
|
||||||
|
"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
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