Skip to content

Commit

Permalink
FIX: Importing with pgbouncer failed
Browse files Browse the repository at this point in the history
Checking if all records have been imported uses a temp table in PostgreSQL. This fails when pgbouncer is used unless the temp table is created inside a transaction.
  • Loading branch information
gschlager committed Mar 26, 2020
1 parent c94b63b commit d216483
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions script/import_scripts/base.rb
Expand Up @@ -219,24 +219,28 @@ def create_group(opts, import_id)
def all_records_exist?(type, import_ids)
return false if import_ids.empty?

connection = ActiveRecord::Base.connection.raw_connection
connection.exec('CREATE TEMP TABLE import_ids(val text PRIMARY KEY)')
ActiveRecord::Base.transaction do
begin
connection = ActiveRecord::Base.connection.raw_connection
connection.exec('CREATE TEMP TABLE import_ids(val text PRIMARY KEY)')

import_id_clause = import_ids.map { |id| "('#{PG::Connection.escape_string(id.to_s)}')" }.join(",")
import_id_clause = import_ids.map { |id| "('#{PG::Connection.escape_string(id.to_s)}')" }.join(",")

connection.exec("INSERT INTO import_ids VALUES #{import_id_clause}")
connection.exec("INSERT INTO import_ids VALUES #{import_id_clause}")

existing = "#{type.to_s.classify}CustomField".constantize
existing = existing.where(name: 'import_id')
.joins('JOIN import_ids ON val = value')
.count
existing = "#{type.to_s.classify}CustomField".constantize
existing = existing.where(name: 'import_id')
.joins('JOIN import_ids ON val = value')
.count

if existing == import_ids.length
puts "Skipping #{import_ids.length} already imported #{type}"
true
if existing == import_ids.length
puts "Skipping #{import_ids.length} already imported #{type}"
true
end
ensure
connection.exec('DROP TABLE import_ids') unless connection.nil?
end
end
ensure
connection.exec('DROP TABLE import_ids') unless connection.nil?
end

def created_user(user)
Expand Down

0 comments on commit d216483

Please sign in to comment.