public-activity / public_activity

Easy activity tracking for models - similar to Github's Public Activity
MIT License
2.97k stars 336 forks source link

Rescue from ActiveRecord::ConnectionNotEstablished #372

Closed gblair closed 2 years ago

gblair commented 2 years ago

Found this commit by @Judahmeek ref'd in comments on public-activity/public_activity#351 and thought why not just get the PR rolling. I hope this helps, @ur5us! I've confirmed this commit at least allows asset pre-compilation in Rails without bombing due to missing database(s).

oboxodo commented 2 years ago

@ur5us could we get this merged and a new gem released?

In the meantime, this is the ugly monkey patch we're using in our project:

# config/initializers/public_activity.rb
#
# === Ugly hack starts ===
# This is an ugly hack to make `PublicActivity::Activity` compatible with Rails 6.1.
# Starting with [this change](https://github.com/rails/rails/pull/40110), Rails wraps DB-specific connection errors
# in its own `ActiveRecord::ConnectionNotEstablished`.
# Sadly, `PublicActivity` is not yet ready for that and it depends on `PG::ConnectionBad` specifically. You can check
# that out in https://github.com/public-activity/public_activity/blob/adb8e75738f3997cfd941fd897bc4e647e4fcad3/lib/public_activity/orm/active_record/activity.rb#L45-L58
# Honestly, this is normally not an issue because the DB engine will be running. But this initialization file gets
# executed when loading the environment even when all we want is to precompile assets which doesn't really need the
# DB to be running. In particular, we don't want to have to run the DB engine in the CI server in our assets precompiling task.
module PublicActivityPatchForRails61
  def table_exists?
    super
  rescue ::ActiveRecord::ConnectionNotEstablished => e
    if name == "PublicActivity::ORM::ActiveRecord::Activity"
      raise ::PG::ConnectionBad.new(e)
    else
      raise
    end
  end
end
::ActiveRecord::Base.extend PublicActivityPatchForRails61
# === Ugly hack ends ===
ur5us commented 2 years ago

@oboxodo I keep meaning to do that but due to travel haven't had the time yet. I'll get to it in a bit, hopefully some time this weekend.

ur5us commented 2 years ago

Fixes https://github.com/public-activity/public_activity/issues/351