Module: ActiveRecord::Import::PostgreSQLAdapter

Includes:
ImportSupport, OnDuplicateKeyUpdateSupport
Included in:
ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/activerecord-import/adapters/postgresql_adapter.rb

Constant Summary collapse

MIN_VERSION_FOR_UPSERT =
90_500

Instance Method Summary collapse

Methods included from ImportSupport

#supports_import?

Instance Method Details

#add_column_for_on_duplicate_key_update(column, options = {}) ⇒ Object

Add a column to be updated on duplicate key update



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 112

def add_column_for_on_duplicate_key_update( column, options = {} ) # :nodoc:
  arg = options[:on_duplicate_key_update]
  case arg
  when Hash
    columns = arg.fetch( :columns ) { arg[:columns] = [] }
    case columns
    when Array then columns << column.to_sym unless columns.include?( column.to_sym )
    when Hash then columns[column.to_sym] = column.to_sym
    end
  when Array
    arg << column.to_sym unless arg.include?( column.to_sym )
  end
end

#database_versionObject



227
228
229
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 227

def database_version
  defined?(postgresql_version) ? postgresql_version : super
end

#duplicate_key_update_error?(exception) ⇒ Boolean

Return true if the statement is a duplicate key record error

Returns:

  • (Boolean)


215
216
217
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 215

def duplicate_key_update_error?(exception) # :nodoc:
  exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('duplicate key')
end

#insert_many(sql, values, options = {}, *args) ⇒ Object

:nodoc:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 9

def insert_many( sql, values, options = {}, *args ) # :nodoc:
  number_of_inserts = 1
  returned_values = {}
  ids = []
  results = []

  base_sql, post_sql = case sql
                       when String
                         [sql, '']
                       when Array
                         [sql.shift, sql.join( ' ' )]
  end

  sql2insert = base_sql + values.join( ',' ) + post_sql

  selections = returning_selections(options)
  if selections.blank? || (options[:no_returning] && !options[:recursive])
    insert( sql2insert, *args )
  else
    returned_values = if selections.size > 1
      # Select composite columns
      db_result = select_all( sql2insert, *args )
      { values: db_result.rows, columns: db_result.columns }
    else
      { values: select_values( sql2insert, *args ) }
    end
    clear_query_cache if query_cache_enabled
  end

  if options[:returning].blank?
    ids = Array(returned_values[:values])
  elsif options[:primary_key].blank?
    options[:returning_columns] ||= returned_values[:columns]
    results = Array(returned_values[:values])
  else
    # split primary key and returning columns
    ids, results, options[:returning_columns] = split_ids_and_results(returned_values, options)
  end

  ActiveRecord::Import::Result.new([], number_of_inserts, ids, results)
end

#next_value_for_sequence(sequence_name) ⇒ Object



73
74
75
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 73

def next_value_for_sequence(sequence_name)
  %{nextval('#{sequence_name}')}
end

#post_sql_statements(table_name, options) ⇒ Object

:nodoc:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 77

def post_sql_statements( table_name, options ) # :nodoc:
  sql = []

  if supports_on_duplicate_key_update?
    # Options :recursive and :on_duplicate_key_ignore are mutually exclusive
    if (options[:ignore] || options[:on_duplicate_key_ignore]) && !options[:on_duplicate_key_update] && !options[:recursive]
      sql << sql_for_on_duplicate_key_ignore( table_name, options[:on_duplicate_key_ignore] )
    end
  elsif logger && options[:on_duplicate_key_ignore] && !options[:on_duplicate_key_update]
    logger.warn "Ignoring on_duplicate_key_ignore because it is not supported by the database."
  end

  sql += super(table_name, options)

  selections = returning_selections(options)
  unless selections.blank? || (options[:no_returning] && !options[:recursive])
    sql << " RETURNING #{selections.join(', ')}"
  end

  sql
end

#returning_selections(options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 99

def returning_selections(options)
  selections = []
  column_names = Array(options[:model].column_names)

  selections += Array(options[:primary_key]) if options[:primary_key].present?
  selections += Array(options[:returning]) if options[:returning].present?

  selections.map do |selection|
    column_names.include?(selection.to_s) ? "\"#{selection}\"" : selection
  end
end

#split_ids_and_results(selections, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 51

def split_ids_and_results( selections, options )
  ids = []
  returning_values = []

  columns = Array(selections[:columns])
  values = Array(selections[:values])
  id_indexes = Array(options[:primary_key]).map { |key| columns.index(key) }
  returning_columns = columns.reject.with_index { |_, index| id_indexes.include?(index) }
  returning_indexes = returning_columns.map { |column| columns.index(column) }

  values.each do |value|
    value_array = Array(value)
    ids << id_indexes.map { |index| value_array[index] }
    returning_values << returning_indexes.map { |index| value_array[index] }
  end

  ids.map!(&:first) if id_indexes.size == 1
  returning_values.map!(&:first) if returning_columns.size == 1

  [ids, returning_values, returning_columns]
end

#sql_for_conflict_target(args = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 196

def sql_for_conflict_target( args = {} )
  constraint_name = args[:constraint_name]
  conflict_target = args[:conflict_target]
  index_predicate = args[:index_predicate]
  if constraint_name.present?
    "ON CONSTRAINT #{constraint_name} "
  elsif conflict_target.present?
    sql = "(#{Array( conflict_target ).reject( &:blank? ).join( ', ' )}) "
    sql += "WHERE #{index_predicate} " if index_predicate
    sql
  end
end

#sql_for_default_conflict_target(table_name, primary_key) ⇒ Object



209
210
211
212
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 209

def sql_for_default_conflict_target( table_name, primary_key )
  conflict_target = Array(primary_key).join(', ')
  "(#{conflict_target}) " if conflict_target.present?
end

#sql_for_on_duplicate_key_ignore(table_name, *args) ⇒ Object

Returns a generated ON CONFLICT DO NOTHING statement given the passed in args.



128
129
130
131
132
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 128

def sql_for_on_duplicate_key_ignore( table_name, *args ) # :nodoc:
  arg = args.first
  conflict_target = sql_for_conflict_target( arg ) if arg.is_a?( Hash )
  " ON CONFLICT #{conflict_target}DO NOTHING"
end

#sql_for_on_duplicate_key_update(table_name, *args) ⇒ Object

Returns a generated ON CONFLICT DO UPDATE statement given the passed in args.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 136

def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
  arg, model, primary_key, locking_column = args
  arg = { columns: arg } if arg.is_a?( Array ) || arg.is_a?( String )
  return unless arg.is_a?( Hash )

  sql = ' ON CONFLICT '.dup
  conflict_target = sql_for_conflict_target( arg )

  columns = arg.fetch( :columns, [] )
  condition = arg[:condition]
  if columns.respond_to?( :empty? ) && columns.empty?
    return sql << "#{conflict_target}DO NOTHING"
  end

  conflict_target ||= sql_for_default_conflict_target( table_name, primary_key )
  unless conflict_target
    raise ArgumentError, 'Expected :conflict_target or :constraint_name to be specified'
  end

  sql << "#{conflict_target}DO UPDATE SET "
  case columns
  when Array
    sql << sql_for_on_duplicate_key_update_as_array( table_name, model, locking_column, columns )
  when Hash
    sql << sql_for_on_duplicate_key_update_as_hash( table_name, model, locking_column, columns )
  when String
    sql << columns
  else
    raise ArgumentError, 'Expected :columns to be an Array or Hash'
  end

  sql << " WHERE #{condition}" if condition.present?

  sql
end

#sql_for_on_duplicate_key_update_as_array(table_name, model, locking_column, arr) ⇒ Object

:nodoc:



172
173
174
175
176
177
178
179
180
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 172

def sql_for_on_duplicate_key_update_as_array( table_name, model, locking_column, arr ) # :nodoc:
  results = arr.map do |column|
    original_column_name = model.attribute_alias?( column ) ? model.attribute_alias( column ) : column
    qc = quote_column_name( original_column_name )
    "#{qc}=EXCLUDED.#{qc}"
  end
  increment_locking_column!(table_name, results, locking_column)
  results.join( ',' )
end

#sql_for_on_duplicate_key_update_as_hash(table_name, model, locking_column, hsh) ⇒ Object

:nodoc:



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 182

def sql_for_on_duplicate_key_update_as_hash( table_name, model, locking_column, hsh ) # :nodoc:
  results = hsh.map do |column1, column2|
    original_column1_name = model.attribute_alias?( column1 ) ? model.attribute_alias( column1 ) : column1
    qc1 = quote_column_name( original_column1_name )

    original_column2_name = model.attribute_alias?( column2 ) ? model.attribute_alias( column2 ) : column2
    qc2 = quote_column_name( original_column2_name )

    "#{qc1}=EXCLUDED.#{qc2}"
  end
  increment_locking_column!(table_name, results, locking_column)
  results.join( ',' )
end

#supports_on_duplicate_key_update?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 219

def supports_on_duplicate_key_update?
  database_version >= MIN_VERSION_FOR_UPSERT
end

#supports_setting_primary_key_of_imported_objects?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 223

def supports_setting_primary_key_of_imported_objects?
  true
end