-
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
issue with primary key other than id
#868
Conversation
t.text :object | ||
t.datetime :created_at | ||
end | ||
add_index :orange_versions, [:item_type, :item_id] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
orange is largely modeled after banana, without the Kitchen namespace, and changed to use a string item_id
ugh. I don't know why it is complaining about this
that is a hash, it should not have spaces |
@@ -31,7 +31,8 @@ def reify(version, options) | |||
klass = version_reification_class(version, attrs) | |||
# The `dup` option always returns a new object, otherwise we should | |||
# attempt to look for the item outside of default scope(s). | |||
if options[:dup] || (item_found = klass.unscoped.find_by_id(version.item_id)).nil? | |||
find_cond = { klass.primary_key => version.item_id } | |||
if options[:dup] || (item_found = klass.unscoped.where(find_cond).first).nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part looks good to me.
before_create do | ||
self.uuid ||= SecureRandom.uuid | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of calling this model Orange
, what about CustomPrimaryKeyRecord
?
We have so many models in our test suite it's getting hard to remember how they differ.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please require "securerandom"
at the top of this file.
@@ -0,0 +1,3 @@ | |||
class OrangeVersion < PaperTrail::Version | |||
self.table_name = "orange_versions" | |||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The orange_versions
table is necessary because you need a table whose item_id
column is a string, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
t.datetime :created_at | ||
end | ||
add_index :orange_versions, [:item_type, :item_id] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we commit changes to 20110208155312_set_up_test_tables.rb
, we also must commit the corresponding changes to test/dummy/db/schema.rb
.
This project follows the Ruby Style Guide, see https://github.com/bbatsov/ruby-style-guide#collections |
has_paper_trail class_name: "OrangeVersion" | ||
default_scope -> { where(name: "orange") } | ||
|
||
self.primary_key = :uuid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this the first declaration within the model?
well, quoting # good - space after { and before }
{ one: 1, two: 2 }
# good - no space after { and before }
{one: 1, two: 2} but I have changed it. |
I'm not sure how to get the schema.rb updated, I have had trouble getting the tests to run locally in many cases. I think I have addressed the rest of the feedback, can you help me with the schema.rb bit? |
hm. item_id as string doesn't seem to work so well with postgres. investigating further ... |
I think I ususally |
okay, so this error:
was just an issue with how I was specifying the uuid primary key, in a way that wasn't supported by older activerecord, and that does surface an issue that if you have a versions table with a string versions.item_id, it can only be used with string primary key fields in postgres. if you have an application you perhaps started with traditional numeric id fields but are transitioning to a uuid (or any string) primary key, you won't be able to use the same versions table to track both sorts of models. I don't have a solution for that (perhaps smart type casting in a query somewhere - not sure what capability activerecord even offers in that regard), just noting it as it occurs to me. |
seems to be largely passing, I think feedback is addressed. |
Correct, and fixed by #870. Please rebase. It would be great if you could get the tests green before we review again. Let us know if you have trouble, thanks. |
💥 rebased, green |
self.primary_key = :uuid | ||
|
||
has_paper_trail class_name: "CustomPrimaryKeyRecordVersion" | ||
default_scope -> { where(name: "custom_primary_key_record") } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of this default_scope?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is to prevent the record from being returned on version.item
. this bug surfaces in the else
path of https://github.com/airblade/paper_trail/blob/v5.2.2/lib/paper_trail/reifier.rb#L25-L40
version.item
returns nil when the item is destroyed, and when a default scope on the model prevents the association from returning it. I added this default scope to test both of those cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok that makes sense. Please add your explanation as a comment above the default_scope
.
Also, please add a line in CHANGELOG.md
and I think we'll be done here.
This all looks good, my only question is what is the purpose of the |
…eird default scope
…R 3 supports - previous way worked with 4 and 5 but not 3
added explanatory comment and CHANGELOG entry |
Merged, thanks! |
cool, thanks |
* support reifying using primary_key field other than 'id' * custom_primary_key_record - model to use a uuid primary key, with a weird default scope * test custom_primary_key_record also with destroyed record * name index on custom_primary_key_record_versions item; default is too long for mysql * specify custom_pk_record uuid as a string primary key in a way that AR 3 supports - previous way worked with 4 and 5 but not 3 * update schema for custom_primary_key_records * note fix in changelog
attempting to reify records with a primary key other than
id
can run into an errorNoMethodError: undefined method 'find_by_id'
when the Version#item association does not exist, due to either not being in the default scope, or the record no longer existing.this fixes use of
find_by_id
to instead use whatever the primary key is.I tried to heed rubocop warnings and follow existing testing conventions, but that may need some changes. I had some trouble testing the full matrix, will see what travis does with this PR.