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