{"action":"created","comment":{"url":"https://api.github.com/repos/zdennis/activerecord-import/pulls/comments/51521582","id":51521582,"diff_hunk":"@@ -30,6 +31,92 @@ def post_sql_statements( table_name, options ) # :nodoc:\n     end\n   end\n \n+  # Add a column to be updated on duplicate key update\n+  def add_column_for_on_duplicate_key_update( column, options={} ) # :nodoc:\n+    arg = options[:on_duplicate_key_update]\n+    if arg.is_a?( Hash )\n+      columns = arg.fetch(:columns) { arg[:columns] = [] }\n+      case columns\n+      when Array then columns << column.to_sym unless columns.include?( column.to_sym )\n+      when Hash then columns[column.to_sym] = column.to_sym\n+      end\n+    elsif arg.is_a?( Array )\n+      arg << column.to_sym unless arg.include?( column.to_sym )\n+    end\n+  end\n+\n+  # Returns a generated ON CONFLICT DO UPDATE statement given the passed\n+  # in +args+.\n+  def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:\n+    arg = args.first\n+    if arg.is_a?( Array ) || arg.is_a?( String )\n+      arg = { :columns => arg }\n+    end\n+    return unless arg.is_a?( Hash )\n+\n+    constraint_name = arg[ :constraint_name ]\n+    conflict_target = arg[ :conflict_target ]\n+    columns         = arg.fetch( :columns, [] )\n+\n+    sql = \" ON CONFLICT \"\n+    conflict_target = sql_for_conflict_target( conflict_target, constraint_name )\n+\n+    if columns.respond_to?( :empty? ) && columns.empty?","path":"lib/activerecord-import/adapters/postgresql_adapter.rb","position":null,"original_position":41,"commit_id":"aec53417d154d6467d679999429955b73a4e5608","original_commit_id":"252702facfcf6145a9aee893e57f131d3220ca93","user":{"login":"jkowens","id":585943,"avatar_url":"https://avatars.githubusercontent.com/u/585943?v=3","gravatar_id":"","url":"https://api.github.com/users/jkowens","html_url":"https://github.com/jkowens","followers_url":"https://api.github.com/users/jkowens/followers","following_url":"https://api.github.com/users/jkowens/following{/other_user}","gists_url":"https://api.github.com/users/jkowens/gists{/gist_id}","starred_url":"https://api.github.com/users/jkowens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jkowens/subscriptions","organizations_url":"https://api.github.com/users/jkowens/orgs","repos_url":"https://api.github.com/users/jkowens/repos","events_url":"https://api.github.com/users/jkowens/events{/privacy}","received_events_url":"https://api.github.com/users/jkowens/received_events","type":"User","site_admin":false},"body":"@zdennis @triptec I took a stab at adding `:on_duplicate_key_ignore` for Postgres. As Zach mentioned, MySQL does offer the `ignore: true` option. Passing a hash with a :conflict_target as shown above works, but I thought just passing `true` would more than likely be what most used for this scenario:\r\n\r\n```\r\nCategory.import categories, on_duplicate_key_ignore: true, validate: false\r\n```\r\n\r\n`ON CONFLICT DO NOTHING` is the one time Postgres lets us get away without specifying a conflict target.","created_at":"2016-02-02T04:06:06Z","updated_at":"2016-02-02T04:06:06Z","html_url":"https://github.com/zdennis/activerecord-import/pull/218#discussion_r51521582","pull_request_url":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218","_links":{"self":{"href":"https://api.github.com/repos/zdennis/activerecord-import/pulls/comments/51521582"},"html":{"href":"https://github.com/zdennis/activerecord-import/pull/218#discussion_r51521582"},"pull_request":{"href":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218"}}},"pull_request":{"url":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218","id":55714037,"html_url":"https://github.com/zdennis/activerecord-import/pull/218","diff_url":"https://github.com/zdennis/activerecord-import/pull/218.diff","patch_url":"https://github.com/zdennis/activerecord-import/pull/218.patch","issue_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/218","number":218,"state":"open","locked":false,"title":"Add :on_duplicate_key_update support for Postgres","user":{"login":"jkowens","id":585943,"avatar_url":"https://avatars.githubusercontent.com/u/585943?v=3","gravatar_id":"","url":"https://api.github.com/users/jkowens","html_url":"https://github.com/jkowens","followers_url":"https://api.github.com/users/jkowens/followers","following_url":"https://api.github.com/users/jkowens/following{/other_user}","gists_url":"https://api.github.com/users/jkowens/gists{/gist_id}","starred_url":"https://api.github.com/users/jkowens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jkowens/subscriptions","organizations_url":"https://api.github.com/users/jkowens/orgs","repos_url":"https://api.github.com/users/jkowens/repos","events_url":"https://api.github.com/users/jkowens/events{/privacy}","received_events_url":"https://api.github.com/users/jkowens/received_events","type":"User","site_admin":false},"body":"Postgres 9.5 added UPSERT support. Resolves #187.\r\n\r\nProvides the following syntax:\r\n\r\n```\r\nCountry.import [country], validate: false, on_duplicate_key_update: { conflict_target: :country, columns: [:updated_at] }\r\n```\r\n\r\n`:conflict_target` takes either a single value or an array of column names that make up the unique constraint and `:columns` could be either an array or hash similar to the existing MySQL functionality.\r\n\r\nTo run the tests for the Postgres import with :on_duplicate_key_update the environment variable PG_VERSION=9.5 must be set:\r\n\r\n```\r\nAR_VERSION=4.2 PG_VERSION=9.5 bundle exec rake test:postgresql test:mysql\r\n```","created_at":"2016-01-12T04:41:08Z","updated_at":"2016-02-02T04:06:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"301009d33946ce255372e949b6c8e5b9765d3eea","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218/commits","review_comments_url":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218/comments","review_comment_url":"https://api.github.com/repos/zdennis/activerecord-import/pulls/comments{/number}","comments_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/218/comments","statuses_url":"https://api.github.com/repos/zdennis/activerecord-import/statuses/aec53417d154d6467d679999429955b73a4e5608","head":{"label":"jkowens:upsert","ref":"upsert","sha":"aec53417d154d6467d679999429955b73a4e5608","user":{"login":"jkowens","id":585943,"avatar_url":"https://avatars.githubusercontent.com/u/585943?v=3","gravatar_id":"","url":"https://api.github.com/users/jkowens","html_url":"https://github.com/jkowens","followers_url":"https://api.github.com/users/jkowens/followers","following_url":"https://api.github.com/users/jkowens/following{/other_user}","gists_url":"https://api.github.com/users/jkowens/gists{/gist_id}","starred_url":"https://api.github.com/users/jkowens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jkowens/subscriptions","organizations_url":"https://api.github.com/users/jkowens/orgs","repos_url":"https://api.github.com/users/jkowens/repos","events_url":"https://api.github.com/users/jkowens/events{/privacy}","received_events_url":"https://api.github.com/users/jkowens/received_events","type":"User","site_admin":false},"repo":{"id":49285868,"name":"activerecord-import","full_name":"jkowens/activerecord-import","owner":{"login":"jkowens","id":585943,"avatar_url":"https://avatars.githubusercontent.com/u/585943?v=3","gravatar_id":"","url":"https://api.github.com/users/jkowens","html_url":"https://github.com/jkowens","followers_url":"https://api.github.com/users/jkowens/followers","following_url":"https://api.github.com/users/jkowens/following{/other_user}","gists_url":"https://api.github.com/users/jkowens/gists{/gist_id}","starred_url":"https://api.github.com/users/jkowens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jkowens/subscriptions","organizations_url":"https://api.github.com/users/jkowens/orgs","repos_url":"https://api.github.com/users/jkowens/repos","events_url":"https://api.github.com/users/jkowens/events{/privacy}","received_events_url":"https://api.github.com/users/jkowens/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jkowens/activerecord-import","description":"Extraction of the ActiveRecord::Base#import functionality from ar-extensions for Rails 3 and beyon","fork":true,"url":"https://api.github.com/repos/jkowens/activerecord-import","forks_url":"https://api.github.com/repos/jkowens/activerecord-import/forks","keys_url":"https://api.github.com/repos/jkowens/activerecord-import/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jkowens/activerecord-import/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jkowens/activerecord-import/teams","hooks_url":"https://api.github.com/repos/jkowens/activerecord-import/hooks","issue_events_url":"https://api.github.com/repos/jkowens/activerecord-import/issues/events{/number}","events_url":"https://api.github.com/repos/jkowens/activerecord-import/events","assignees_url":"https://api.github.com/repos/jkowens/activerecord-import/assignees{/user}","branches_url":"https://api.github.com/repos/jkowens/activerecord-import/branches{/branch}","tags_url":"https://api.github.com/repos/jkowens/activerecord-import/tags","blobs_url":"https://api.github.com/repos/jkowens/activerecord-import/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jkowens/activerecord-import/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jkowens/activerecord-import/git/refs{/sha}","trees_url":"https://api.github.com/repos/jkowens/activerecord-import/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jkowens/activerecord-import/statuses/{sha}","languages_url":"https://api.github.com/repos/jkowens/activerecord-import/languages","stargazers_url":"https://api.github.com/repos/jkowens/activerecord-import/stargazers","contributors_url":"https://api.github.com/repos/jkowens/activerecord-import/contributors","subscribers_url":"https://api.github.com/repos/jkowens/activerecord-import/subscribers","subscription_url":"https://api.github.com/repos/jkowens/activerecord-import/subscription","commits_url":"https://api.github.com/repos/jkowens/activerecord-import/commits{/sha}","git_commits_url":"https://api.github.com/repos/jkowens/activerecord-import/git/commits{/sha}","comments_url":"https://api.github.com/repos/jkowens/activerecord-import/comments{/number}","issue_comment_url":"https://api.github.com/repos/jkowens/activerecord-import/issues/comments{/number}","contents_url":"https://api.github.com/repos/jkowens/activerecord-import/contents/{+path}","compare_url":"https://api.github.com/repos/jkowens/activerecord-import/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jkowens/activerecord-import/merges","archive_url":"https://api.github.com/repos/jkowens/activerecord-import/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jkowens/activerecord-import/downloads","issues_url":"https://api.github.com/repos/jkowens/activerecord-import/issues{/number}","pulls_url":"https://api.github.com/repos/jkowens/activerecord-import/pulls{/number}","milestones_url":"https://api.github.com/repos/jkowens/activerecord-import/milestones{/number}","notifications_url":"https://api.github.com/repos/jkowens/activerecord-import/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jkowens/activerecord-import/labels{/name}","releases_url":"https://api.github.com/repos/jkowens/activerecord-import/releases{/id}","deployments_url":"https://api.github.com/repos/jkowens/activerecord-import/deployments","created_at":"2016-01-08T17:20:44Z","updated_at":"2016-01-11T19:49:12Z","pushed_at":"2016-02-02T03:57:28Z","git_url":"git://github.com/jkowens/activerecord-import.git","ssh_url":"git@github.com:jkowens/activerecord-import.git","clone_url":"https://github.com/jkowens/activerecord-import.git","svn_url":"https://github.com/jkowens/activerecord-import","homepage":"http://www.continuousthinking.com","size":4002,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"zdennis:master","ref":"master","sha":"c0a4393a62fccd0c572266795f0c787ddf3a1775","user":{"login":"zdennis","id":967,"avatar_url":"https://avatars.githubusercontent.com/u/967?v=3","gravatar_id":"","url":"https://api.github.com/users/zdennis","html_url":"https://github.com/zdennis","followers_url":"https://api.github.com/users/zdennis/followers","following_url":"https://api.github.com/users/zdennis/following{/other_user}","gists_url":"https://api.github.com/users/zdennis/gists{/gist_id}","starred_url":"https://api.github.com/users/zdennis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zdennis/subscriptions","organizations_url":"https://api.github.com/users/zdennis/orgs","repos_url":"https://api.github.com/users/zdennis/repos","events_url":"https://api.github.com/users/zdennis/events{/privacy}","received_events_url":"https://api.github.com/users/zdennis/received_events","type":"User","site_admin":false},"repo":{"id":558790,"name":"activerecord-import","full_name":"zdennis/activerecord-import","owner":{"login":"zdennis","id":967,"avatar_url":"https://avatars.githubusercontent.com/u/967?v=3","gravatar_id":"","url":"https://api.github.com/users/zdennis","html_url":"https://github.com/zdennis","followers_url":"https://api.github.com/users/zdennis/followers","following_url":"https://api.github.com/users/zdennis/following{/other_user}","gists_url":"https://api.github.com/users/zdennis/gists{/gist_id}","starred_url":"https://api.github.com/users/zdennis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zdennis/subscriptions","organizations_url":"https://api.github.com/users/zdennis/orgs","repos_url":"https://api.github.com/users/zdennis/repos","events_url":"https://api.github.com/users/zdennis/events{/privacy}","received_events_url":"https://api.github.com/users/zdennis/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/zdennis/activerecord-import","description":"Extraction of the ActiveRecord::Base#import functionality from ar-extensions for Rails 3 and beyon","fork":false,"url":"https://api.github.com/repos/zdennis/activerecord-import","forks_url":"https://api.github.com/repos/zdennis/activerecord-import/forks","keys_url":"https://api.github.com/repos/zdennis/activerecord-import/keys{/key_id}","collaborators_url":"https://api.github.com/repos/zdennis/activerecord-import/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/zdennis/activerecord-import/teams","hooks_url":"https://api.github.com/repos/zdennis/activerecord-import/hooks","issue_events_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/events{/number}","events_url":"https://api.github.com/repos/zdennis/activerecord-import/events","assignees_url":"https://api.github.com/repos/zdennis/activerecord-import/assignees{/user}","branches_url":"https://api.github.com/repos/zdennis/activerecord-import/branches{/branch}","tags_url":"https://api.github.com/repos/zdennis/activerecord-import/tags","blobs_url":"https://api.github.com/repos/zdennis/activerecord-import/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/zdennis/activerecord-import/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/zdennis/activerecord-import/git/refs{/sha}","trees_url":"https://api.github.com/repos/zdennis/activerecord-import/git/trees{/sha}","statuses_url":"https://api.github.com/repos/zdennis/activerecord-import/statuses/{sha}","languages_url":"https://api.github.com/repos/zdennis/activerecord-import/languages","stargazers_url":"https://api.github.com/repos/zdennis/activerecord-import/stargazers","contributors_url":"https://api.github.com/repos/zdennis/activerecord-import/contributors","subscribers_url":"https://api.github.com/repos/zdennis/activerecord-import/subscribers","subscription_url":"https://api.github.com/repos/zdennis/activerecord-import/subscription","commits_url":"https://api.github.com/repos/zdennis/activerecord-import/commits{/sha}","git_commits_url":"https://api.github.com/repos/zdennis/activerecord-import/git/commits{/sha}","comments_url":"https://api.github.com/repos/zdennis/activerecord-import/comments{/number}","issue_comment_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/comments{/number}","contents_url":"https://api.github.com/repos/zdennis/activerecord-import/contents/{+path}","compare_url":"https://api.github.com/repos/zdennis/activerecord-import/compare/{base}...{head}","merges_url":"https://api.github.com/repos/zdennis/activerecord-import/merges","archive_url":"https://api.github.com/repos/zdennis/activerecord-import/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/zdennis/activerecord-import/downloads","issues_url":"https://api.github.com/repos/zdennis/activerecord-import/issues{/number}","pulls_url":"https://api.github.com/repos/zdennis/activerecord-import/pulls{/number}","milestones_url":"https://api.github.com/repos/zdennis/activerecord-import/milestones{/number}","notifications_url":"https://api.github.com/repos/zdennis/activerecord-import/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/zdennis/activerecord-import/labels{/name}","releases_url":"https://api.github.com/repos/zdennis/activerecord-import/releases{/id}","deployments_url":"https://api.github.com/repos/zdennis/activerecord-import/deployments","created_at":"2010-03-12T05:06:53Z","updated_at":"2016-02-02T03:16:31Z","pushed_at":"2016-02-02T03:57:28Z","git_url":"git://github.com/zdennis/activerecord-import.git","ssh_url":"git@github.com:zdennis/activerecord-import.git","clone_url":"https://github.com/zdennis/activerecord-import.git","svn_url":"https://github.com/zdennis/activerecord-import","homepage":"http://www.continuousthinking.com","size":4197,"stargazers_count":1336,"watchers_count":1336,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":354,"mirror_url":null,"open_issues_count":75,"forks":354,"open_issues":75,"watchers":1336,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218"},"html":{"href":"https://github.com/zdennis/activerecord-import/pull/218"},"issue":{"href":"https://api.github.com/repos/zdennis/activerecord-import/issues/218"},"comments":{"href":"https://api.github.com/repos/zdennis/activerecord-import/issues/218/comments"},"review_comments":{"href":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218/comments"},"review_comment":{"href":"https://api.github.com/repos/zdennis/activerecord-import/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/zdennis/activerecord-import/pulls/218/commits"},"statuses":{"href":"https://api.github.com/repos/zdennis/activerecord-import/statuses/aec53417d154d6467d679999429955b73a4e5608"}}}}