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

Rails 7.1 compatibility #601

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composite_primary_keys.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ Gem::Specification.new do |s|

# Dependencies
s.required_ruby_version = '>= 2.7.0'
s.add_dependency('activerecord', '~> 7.0.2')
s.add_dependency('activerecord', '~> 7.1.0.alpha')
s.add_development_dependency('rake')
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module DatabaseStatements
def sql_for_insert(sql, pk, binds) # :nodoc:
def sql_for_insert(sql, pk, binds, _returning = nil) # :nodoc:
if pk.nil?
# Extract the table from the insert sql. Yuck.
table_ref = extract_table_ref_from_insert_sql(sql)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ActiveRecord
module ConnectionAdapters
module SQLServer
module DatabaseStatements
def sql_for_insert(sql, pk, binds)
def sql_for_insert(sql, pk, binds, _returning = nil)
if pk.nil?
table_name = query_requires_identity_insert?(sql)
pk = primary_key(table_name)
Expand Down
5 changes: 4 additions & 1 deletion lib/composite_primary_keys/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def _delete_record(constraints) # :nodoc:
def _create_record(attribute_names = self.attribute_names)
attribute_names = attributes_for_create(attribute_names)

returning_columns_for_insert = nil

new_id = self.class._insert_record(
attributes_with_values(attribute_names)
attributes_with_values(attribute_names),
returning_columns_for_insert
Copy link
Author

Choose a reason for hiding this comment

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

This seems like a low-effort "fix" from my side.

For reference, upstream changed this from

       new_id = self.class._insert_record(
         attributes_with_values(attribute_names)
       )

       self.id ||= new_id if @primary_key

to

       returning_columns = self.class._returning_columns_for_insert

       returning_values = self.class._insert_record(
         attributes_with_values(attribute_names),
         returning_columns
       )

       returning_columns.zip(returning_values).each do |column, value|
         _write_attribute(column, value) if !_read_attribute(column)
       end if returning_values

)

# CPK
Expand Down
Loading