diff --git a/deploy.sh b/deploy.sh index a9f4233..22d0847 100644 --- a/deploy.sh +++ b/deploy.sh @@ -45,41 +45,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 brute-force 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 - local OLD_STRING_ESC=$(echo "$OLD_STRING" | sed "s/'/''/g") - local NEW_STRING_ESC=$(echo "$NEW_STRING" | sed "s/'/''/g") + # Create a backup of the original database + local BACKUP_PATH="${DB_PATH}.bak" + cp "$DB_PATH" "$BACKUP_PATH" - # Get a list of all tables in the database - local TABLES - TABLES=$(sqlite3 "$DB_PATH" ".tables") + # Dump the database to SQL + local DUMP_PATH="${DB_PATH}.sql" + sqlite3 "$DB_PATH" ".dump" > "$DUMP_PATH" - # Loop through each table - for TABLE in $TABLES; do - echo "Processing table: $TABLE" + # Replace strings in the SQL dump + sed -i "s|$OLD_STRING|$NEW_STRING|g" "$DUMP_PATH" - # Get a list of all columns in the table - local COLUMNS - COLUMNS=$(sqlite3 "$DB_PATH" "PRAGMA table_info($TABLE);" | awk -F'|' '{print $2}' | grep -v '^$') + # Create a new database from the modified SQL dump + local NEW_DB_PATH="${DB_PATH}.new" + sqlite3 "$NEW_DB_PATH" < "$DUMP_PATH" - # Loop through each column - for COLUMN in $COLUMNS; do - echo " Processing column: $COLUMN" + # Replace the original database with the new one + mv "$NEW_DB_PATH" "$DB_PATH" - # Update every cell in the column, casting to TEXT to force string replacement - 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%'; - " - done - done + # Clean up temporary files + rm "$DUMP_PATH" - echo "replacement of '$OLD_STRING' with '$NEW_STRING' completed in '$DB_PATH'." + echo "Replacement completed in '$DB_PATH'. Original database backed up to '$BACKUP_PATH'." } #example usage: replace_in_sqlite_db "database.sqlite" "Europe/Amsterdam" "UTC" diff --git a/stacks/npm/data/database.sqlite b/stacks/npm/data/database.sqlite new file mode 100644 index 0000000..e4ab44d Binary files /dev/null and b/stacks/npm/data/database.sqlite differ