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

make rspec test fail with current transaction is aborted error #39

Closed
williamherry opened this issue Jun 24, 2014 · 9 comments
Closed

Comments

@williamherry
Copy link

not fail every time and not fail if run it individually, after days of dig, I find some useful info in postgresql log

ERROR:  function upsert2_0_3_subscribe_items_sel_uniq_index_set_action2974300400(unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown) does not exist at character 8
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
STATEMENT:  SELECT upsert2_0_3_subscribe_items_SEL_uniq_index_SET_action2974300400($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ERROR:  current transaction is aborted, commands ignored until end of transaction block
STATEMENT:..
..          CREATE OR REPLACE FUNCTION upsert2_0_3_subscribe_items_SEL_uniq_index_SET_action2974300400("uniq_index_sel" character varying(255), "action_set" character varying(255), "created_at_set" character varying(255), "receiver_id_set" integer, "receiver_type_set" character varying(255), "resource_id_set" integer, "resource_type_set" character varying(255), "sender_id_set" integer, "sender_type_set" character varying(255), "uniq_index_set" character varying(255), "updated_at_set" character varying(255)) RETURNS VOID AS

seems use that function before create it?

@raviolicode
Copy link
Collaborator

Which version of Ruby are you using?

@williamherry
Copy link
Author

@raviolicode ruby 2.1.2p95

@williamherry
Copy link
Author

@raviolicode I find out what code make this error occur

Upsert.batch(ActiveRecord::Base.connection, :subscribe_items) do |upsert|
        User.where(id: sender_ids).each do |sender|
          sender.timeline_items.with_status(:normal).order(created_at: :desc).where(action: [1,2,6,7,8]).limit(10).each do |timeline_item|
            action = action_type(timeline_item.action)
            record = {.
              sender_id: sender.id,
              sender_type: sender.class.name,
              receiver_id: receiver_id,
              receiver_type: User.name,
              resource_type: timeline_item.resource.class.name,
              resource_id: timeline_item.resource.id,
              action: action,
              created_at: timeline_item.created_at.to_s,
              updated_at: timeline_item.updated_at.to_s
            }
            selector = { uniq_index: SubscribeItem.new(record)}.set_uniq_index_value}
            upsert.row(selector, record)
          end
        end

if I change

selector = { uniq_index: SubscribeItem.new(record)}.set_uniq_index_value}

to

selector = { uniq_index: SubscribeItem.new(record)}}

that error not show up any more, this code is not write by me, maybe not the right way to use upsert
I am gonna ask my coworker who write this code in a moment

@seamusabshere
Copy link
Owner

hi @williamherry what does the SubscribeItem#set_uniq_index_value method do? (perhaps it is a UUID generator?)

@williamherry
Copy link
Author

@seamusabshere

  def set_uniq_index_value
    self.uniq_index = case self.action.to_sym
    when :post, :follow, :upvote
      "#{ sender_id }-#{ sender_type }-#{ action }-#{ resource_type }-#{ resource_id }-#{ receiver_id }-#{ receiver_type }"
    when :hot
      "#{ sender_id }-#{ sender_type }-#{ action }-#{ resource_type }-#{ resource_id }-#{ receiver_id }-#{ receiver_type }-#{ (created_at || Time.now).to_date }"
    when :recommend
      "#{ action }-#{ resource_type }-#{ resource_id }"
    end
  end

@seamusabshere
Copy link
Owner

try this:

def set_uniq_index_value
  self.uniq_index = case self.action.to_sym
  when :post, :follow, :upvote
    "#{ sender_id }-#{ sender_type }-#{ action }-#{ resource_type }-#{ resource_id }-#{ receiver_id }-#{ receiver_type }"
  when :hot
    "#{ sender_id }-#{ sender_type }-#{ action }-#{ resource_type }-#{ resource_id }-#{ receiver_id }-#{ receiver_type }-#{ (created_at || Time.now).to_date }"
  when :recommend
    "#{ action }-#{ resource_type }-#{ resource_id }"
  else
    raise "don't know what to do with #{action.inspect}"
  end
end

i have a feeling set_uniq_index_value sometimes is returning nil?

@williamherry
Copy link
Author

@seamusabshere nothing raise and still get that error, but I change to

def set_uniq_index_value
  self.uniq_index = (0...8).map { (65 + rand(26)).chr }.join
end

that error not occur, I am gonna logger that value and see if I can find something

Update

run it second time still get that error

@williamherry
Copy link
Author

@seamusabshere I looked the uniq_index, all seems fine

@williamherry
Copy link
Author

change database cleaner strategy to deletion, solve my problem, thanks for you help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants