Skip to content
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

Set the primary key during #copy_table if necessary. Fixes [#2312] #6186

Merged
merged 1 commit into from May 15, 2012
Merged

Set the primary key during #copy_table if necessary. Fixes [#2312] #6186

merged 1 commit into from May 15, 2012

Conversation

scally
Copy link
Contributor

@scally scally commented May 7, 2012

Updated the pull request and retested against rails:master, as requested

@scally
Copy link
Contributor Author

scally commented May 7, 2012

@rafaelfranca here's the updated pull request for [#2312], as commented on here #2312

@@ -522,7 +522,10 @@ def move_table(from, to, options = {}, &block) #:nodoc:
end

def copy_table(from, to, options = {}) #:nodoc:
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
options.merge!(:primary_key => primary_key(from)) if primary_key(from).to_s != 'id'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to use merge here. This should be fine

options[:primary_key] = primary_key(from) if primary_key(from).to_s != 'id'

@scally
Copy link
Contributor Author

scally commented May 7, 2012

Okay, making that change and retesting. I'll open a new pull request.

@scally scally closed this May 7, 2012
@rafaelfranca
Copy link
Member

@anydiem you don't need to open a new pull request. You can update this one pushing to the same branch.

@scally scally reopened this May 7, 2012
@scally
Copy link
Contributor Author

scally commented May 7, 2012

@rafaelfranca OK, I've made the requested changes, the old & new tests are still passing. How's this?

options[:primary_key] = primary_key(from) if primary_key(from).to_s != 'id'
unless options[:primary_key]
options[:id] = columns(from).detect{|c| c.name == 'id'}.present? && 'id' == primary_key(from).to_s
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could cache the calls to primary_key(from)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is a nice refactoring

@scally
Copy link
Contributor Author

scally commented May 7, 2012

@carlosantoniodasilva @rafaelfranca OK, that's been done too. How is it looking now?

options[:primary_key] = from_primary_key if from_primary_key != 'id'
unless options[:primary_key]
options[:id] = columns(from).detect{|c| c.name == 'id'}.present? && from_primary_key == 'id'
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: before it was being tested for primary_key(from).to_s, now to_s is gone.. is that a problem or everything is working fine - ie all tests passing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all tests are passing.

I couldn't think of a good reason for to_s to remain. It's not necessary to guard against nil in a string comparison, and the column name should already be a string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, great. Thanks!

@scally
Copy link
Contributor Author

scally commented May 14, 2012

@carlosantoniodasilva @rafaelfranca Is there anything else that still needs doing on this request? Is it ready to be pulled?

@rafaelfranca
Copy link
Member

Seems good. Could you squash the commits in one?

@scally
Copy link
Contributor Author

scally commented May 14, 2012

@rafaelfranca OK, done. How's that?

@rafaelfranca
Copy link
Member

Ok. I'll merge it later.

@carlosantoniodasilva
Copy link
Member

@anydiem Thanks 👍

rafaelfranca added a commit that referenced this pull request May 15, 2012
Set the primary key during #copy_table if necessary. Fixes [#2312]
@rafaelfranca rafaelfranca merged commit 8ad58af into rails:master May 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants