db fix again

This commit is contained in:
2026-03-05 01:26:10 +01:00
parent ad0d3d7f99
commit bc079a7874
2 changed files with 17 additions and 35 deletions

View File

@@ -7,51 +7,33 @@ replace_in_sqlite_db() {
# Check if the database file exists # Check if the database file exists
if [ ! -f "$DB_PATH" ]; then if [ ! -f "$DB_PATH" ]; then
echo "Error: Database file '$DB_PATH' does not exist." echo "Error: Database file '$DB_PATH' does not exist."
return 1 exit 1
fi fi
echo "Starting safe replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..." echo "Starting safe replacement of '$OLD_STRING' with '$NEW_STRING' in '$DB_PATH'..."
# Escape single quotes for SQL # Create a backup of the original database
local OLD_STRING_ESC=$(printf "%s" "$OLD_STRING" | sed "s/'/''/g") local BACKUP_PATH="${DB_PATH}.bak"
local NEW_STRING_ESC=$(printf "%s" "$NEW_STRING" | sed "s/'/''/g") cp "$DB_PATH" "$BACKUP_PATH"
# Begin a transaction for safety # Dump the database to SQL
sqlite3 "$DB_PATH" "BEGIN TRANSACTION;" local DUMP_PATH="${DB_PATH}.sql"
sqlite3 "$DB_PATH" ".dump" > "$DUMP_PATH"
# Get a list of all tables in the database # Replace strings in the SQL dump
local TABLES sed -i "s|$OLD_STRING|$NEW_STRING|g" "$DUMP_PATH"
TABLES=$(sqlite3 "$DB_PATH" ".tables")
# Loop through each table # Create a new database from the modified SQL dump
for TABLE in $TABLES; do local NEW_DB_PATH="${DB_PATH}.new"
echo "Processing table: $TABLE" sqlite3 "$NEW_DB_PATH" < "$DUMP_PATH"
# Get a list of all non-BLOB columns in the table # Replace the original database with the new one
sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' ' mv "$NEW_DB_PATH" "$DB_PATH"
{
if ($3 != "BLOB") {
print $2
}
}' | while read -r COLUMN; do
# Skip empty lines
if [ -n "$COLUMN" ]; then
echo " Processing column: $COLUMN"
# Update only rows where the column contains OLD_STRING # Clean up temporary files
sqlite3 "$DB_PATH" " rm "$DUMP_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%';
"
fi
done
done
# Commit the transaction echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'."
sqlite3 "$DB_PATH" "COMMIT;"
echo "Safe replacement completed in '$DB_PATH'."
} }
#set script directory #set script directory

Binary file not shown.