Skip to content

Commit

Permalink
Moj 450 update with prefetch primary key (localytics#19)
Browse files Browse the repository at this point in the history
* MOJ-343 Active Record does support, even if it's not well documented, a method of automatically getting IDs from the database. Implemented this native method to deal with an issue with ActiveStorage.

* MOJ-450 Removed the EasyIdentified concern & corrected a minor bug
  • Loading branch information
ACerka-Springbuk committed Dec 19, 2022
1 parent a49fe3b commit 1c59af4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ def new_column(name, default, sql_type_metadata, null, native_type = nil)
::ODBCAdapter::Column.new(name, default, sql_type_metadata, null, native_type)
end

#Snowflake doesn't have a mechanism to return the primary key on inserts, it needs prefetched
def prefetch_primary_key?(table_name = nil)
true
end

def next_sequence_value(table_name = nil)
exec_query("SELECT #{table_name}.NEXTVAL as new_id").first["new_id"]
end

protected

#Snowflake ODBC Adapter specific
Expand Down
3 changes: 1 addition & 2 deletions lib/odbc_adapter/concerns/insert_attribute_stripper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module ODBCAdapter
module InsertAttributeStripper
extend ActiveSupport::Concern
include EasyIdentified

included do
alias_method :pre_insert_attribute_stripper_save, :save
Expand Down Expand Up @@ -29,11 +28,11 @@ def save_internal(base_function, **options, &block)
self[column.name] = nil
end
end
if stripped_attributes.any? then generate_id end
else
stripped_attributes = {}
end
first_call_result = base_function.call(**options, &block)
return false if first_call_result == false
if stripped_attributes.any?
restore_stripped_attributes(stripped_attributes)
return base_function.call(**options, &block)
Expand Down

0 comments on commit 1c59af4

Please sign in to comment.