Skip to content

Commit

Permalink
check that the rails timestamp vars are present and if so add appropr…
Browse files Browse the repository at this point in the history
…iate field/values to the sql.
  • Loading branch information
urimikhli committed Apr 14, 2016
1 parent f45eb88 commit e287013
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/upsert/merge_function/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ def selector_column_definitions
end

def setter_column_definitions
@time_stamp_cols = column_definitions.select{|cd| cd.name == 'created_at' || cd.name == 'updated_at'}
column_definitions.select { |cd| setter_keys.include?(cd.name) }
end

def update_column_definitions
@update_time_stamp_cols = column_definitions.select{|cd| cd.name == 'updated_at'}
setter_column_definitions.select { |cd| cd.name !~ CREATED_COL_REGEX }
end

Expand All @@ -107,6 +109,15 @@ def create!
names = setter_column_definitions.map(&:quoted_name).join(', ')
values = setter_column_definitions.map(&:to_setter_value).join(', ')
update_pair = update_column_definitions.map(&:to_setter).join(', ')
if @time_stamp_cols.count > 0
names += ', created_at, updated_at'
values += ", now(), now()"
end
@time_stamp_cols=[]
if @update_time_stamp_cols.count > 0
update_pair += ", updated_at = now()"
end
@update_time_stamp_cols=[]
connection.execute(%{
CREATE OR REPLACE FUNCTION #{name}(#{(selector_column_definitions.map(&:to_selector_arg) + setter_column_definitions.map(&:to_setter_arg) + hstore_delete_handlers.map(&:to_arg)).join(', ')}) RETURNS VOID AS
$$
Expand Down

0 comments on commit e287013

Please sign in to comment.