We've tried some debugging recently where we capture the state of models as they
change. This is somewhat useful but in practice it's been difficult to figure
out what the user actually did to trigger based on the state alone. It also
takes up a massive amount of space in the database compared to all other data.
Instead, I want to try recording all requests that are likely to change the
database (POST, PATCH, PUT, DELETE). Recording the request path, method, params,
and current user are hopefully enough to replay what they've done so we can have
reproducible test cases.
To actually reproduce these, you need to get the database into a state where it
has the old state, but the current requests
# restore current prod backup
bin/restore_db
# dump just the http_requests table
pg_dump --format custom --data-only --table http_requests tomato_development > tmp/http_requests.dump
# restore old backup
bin/restore_db some/old/backup.dump
rails db:migrate
# restore just the http_requests table
cat tmp/http_requests.dump | pg_restore --dbname tomato_development
Then, to replay the tests, use this script with rails runner and select the
HttpRequest object you want to replay:
class Replay
include Warden::Test::Helpers
attr_reader :session
def initialize
app = Rails.application
app.reload_routes_unless_loaded
@session = ActionDispatch::Integration::Session.new(app)
@session.host = "localhost"
ActionController::Base.allow_forgery_protection = false
end
def call
requests = HttpRequest.where(id: [1, 2, 3])
requests.each do |req|
puts "processing: #{req.method}#{req.params}"
login_as(req.user)
session.send(req.method.downcase.to_sym, req.path, params: req.params)
end
end
end
Replay.new.call
The above code took a while to workout. There may be better options but I this
is the best I could find. https://stackoverflow.com/q/79881226/638966