-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not call table_exists? in configuration code #636
Comments
I'd like to say you should set
In the above, even if |
You're right. The solution can be to change that line in @track_associations ||= PaperTrail::VersionAssociation.connected? && PaperTrail::VersionAssociation.table_exist? It should not have any side effect, what do you think? |
I'm not familiar with the if @track_associations.nil?
@track_associations = PaperTrail::VersionAssociation.connected? &&
PaperTrail::VersionAssociation.table_exist?
end |
Excellent point. I never noticed that even though it seems obvious now. I suppose that value shouldn't be memoized. I'm thinking that the accessor method should look more like this: def track_associations
@track_associations && PaperTrail::VersionAssociation.table_exists?
end |
Here's the implementation I don't know if can cause some issue in your code. If your code is called before any connection is actually opened the expression Maybe you can ignore my suggestion and change the method to be def track_associations
# rescue false will ignore any database error raised by this code, e.g. during assets compilation.
@track_associations && PaperTrail::VersionAssociation.table_exists? rescue false
end In this way both issues are solved. |
The only concern I have with the @fabn - I'm guessing you are running your Re: using |
Yes, for that reason I think For this reason it's better to avoid this.
Yes, I'm playing with docker and I'd like to build my images with assets ready and everything else ready. Rails (>= 4) code allows precompilation without an active database connection and I think gems should follow this convention.
I'll try, meanwhile I'm using this hack to allow assets compilation in db less env, ugly but working... module PaperTrail
class Version < ActiveRecord::Base
# See https://github.com/airblade/paper_trail/issues/636
include PaperTrail::VersionConcern rescue nil
end
end |
Active Admin has also run into this issue. The solution was to not reference database information until it's actually needed: activeadmin/activeadmin@ba2d10f I read through the code, and it's not clear to me why module PaperTrail
module VersionConcern
extend ::ActiveSupport::Concern
included do
belongs_to :item, :polymorphic => true
# Since the test suite has test coverage for this, we want to declare
# the association when the test suite is running. This makes it pass when
# DB is not initialized prior to test runs such as when we run on Travis
# CI (there won't be a db in `test/dummy/db/`).
if PaperTrail.config.track_associations?
has_many :version_associations, :dependent => :destroy
end And here: # Saves associations if the join table for `VersionAssociation` exists.
def save_associations(version)
return unless PaperTrail.config.track_associations?
self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
assoc_version_args = {
:version_id => version.id,
:foreign_key_name => assoc.foreign_key
} Neither of which should care if the table exists. If the table doesn't exist, the So it seems like we should be fine with this: (which also allows people to set it to false) def track_associations
defined?(@track_associations) ? @track_associations : true
end |
This allows users who need to precompile assets without a database to configure `track_associations = false`. See discussion: #636
I will try to summarize the discussion so far. Never check database, default to truedef track_associations
defined?(@track_associations) ? @track_associations : true
end I think this would require configuration by users who do not track associations, or else they would get an error in Never check database, default to falsedef track_associations
if @track_associations.nil?
warn "PaperTrail.config.track_associations is not set. Defaulting to false."
false
else
@track_associations
end
end This is a breaking change, and would require configuration by everyone, but it's probably the simplest solution. Check the database if it is not configureddef track_associations
@track_associations.nil? ?
PaperTrail::VersionAssociation.table_exists? :
@track_associations
end This is not a breaking change, and allows people who need to precompile assets without a database to configure Check the database if not configured, but
|
Fixed by 90dc72c, hopefully to be included in a 4.0.1 release. |
This allows users who need to precompile assets without a database to configure `track_associations = false`. See discussion: #636
This allows users who need to precompile assets without a database to configure `track_associations = false`. See discussion: #636
I need to precompile assets in a db less environment. I'm not able to do that because this line trigger database connection and when building assets this make assets compilation raise a
PG::ConnectionBad
error.Tomorrow I'll try to see if configuring that value in PT initializers fixes the issue, by the way table_exists? should be avoided.
The text was updated successfully, but these errors were encountered: