Skip to content

Commit 1c59af4

Browse files
Moj 450 update with prefetch primary key (localytics#19)
* 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
1 parent a49fe3b commit 1c59af4

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/active_record/connection_adapters/odbc_adapter.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ def new_column(name, default, sql_type_metadata, null, native_type = nil)
146146
::ODBCAdapter::Column.new(name, default, sql_type_metadata, null, native_type)
147147
end
148148

149+
#Snowflake doesn't have a mechanism to return the primary key on inserts, it needs prefetched
150+
def prefetch_primary_key?(table_name = nil)
151+
true
152+
end
153+
154+
def next_sequence_value(table_name = nil)
155+
exec_query("SELECT #{table_name}.NEXTVAL as new_id").first["new_id"]
156+
end
157+
149158
protected
150159

151160
#Snowflake ODBC Adapter specific

lib/odbc_adapter/concerns/insert_attribute_stripper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module ODBCAdapter
22
module InsertAttributeStripper
33
extend ActiveSupport::Concern
4-
include EasyIdentified
54

65
included do
76
alias_method :pre_insert_attribute_stripper_save, :save
@@ -29,11 +28,11 @@ def save_internal(base_function, **options, &block)
2928
self[column.name] = nil
3029
end
3130
end
32-
if stripped_attributes.any? then generate_id end
3331
else
3432
stripped_attributes = {}
3533
end
3634
first_call_result = base_function.call(**options, &block)
35+
return false if first_call_result == false
3736
if stripped_attributes.any?
3837
restore_stripped_attributes(stripped_attributes)
3938
return base_function.call(**options, &block)

0 commit comments

Comments
 (0)