activerecord-redshift-adapter 0.9.12 → 8.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. checksums.yaml +5 -13
  2. data/LICENSE +25 -1
  3. data/README.md +29 -86
  4. data/lib/active_record/connection_adapters/redshift_7_0/array_parser.rb +92 -0
  5. data/lib/active_record/connection_adapters/redshift_7_0/column.rb +17 -0
  6. data/lib/active_record/connection_adapters/redshift_7_0/database_statements.rb +232 -0
  7. data/lib/active_record/connection_adapters/redshift_7_0/oid/date_time.rb +36 -0
  8. data/lib/active_record/connection_adapters/redshift_7_0/oid/decimal.rb +15 -0
  9. data/lib/active_record/connection_adapters/redshift_7_0/oid/json.rb +41 -0
  10. data/lib/active_record/connection_adapters/redshift_7_0/oid/jsonb.rb +25 -0
  11. data/lib/active_record/connection_adapters/redshift_7_0/oid/type_map_initializer.rb +62 -0
  12. data/lib/active_record/connection_adapters/redshift_7_0/oid.rb +17 -0
  13. data/lib/active_record/connection_adapters/redshift_7_0/quoting.rb +99 -0
  14. data/lib/active_record/connection_adapters/redshift_7_0/referential_integrity.rb +17 -0
  15. data/lib/active_record/connection_adapters/redshift_7_0/schema_definitions.rb +70 -0
  16. data/lib/active_record/connection_adapters/redshift_7_0/schema_dumper.rb +17 -0
  17. data/lib/active_record/connection_adapters/redshift_7_0/schema_statements.rb +421 -0
  18. data/lib/active_record/connection_adapters/redshift_7_0/type_metadata.rb +39 -0
  19. data/lib/active_record/connection_adapters/redshift_7_0/utils.rb +81 -0
  20. data/lib/active_record/connection_adapters/redshift_7_0_adapter.rb +765 -0
  21. data/lib/active_record/connection_adapters/redshift_7_1/array_parser.rb +92 -0
  22. data/lib/active_record/connection_adapters/redshift_7_1/column.rb +17 -0
  23. data/lib/active_record/connection_adapters/redshift_7_1/database_statements.rb +180 -0
  24. data/lib/active_record/connection_adapters/redshift_7_1/oid/date_time.rb +36 -0
  25. data/lib/active_record/connection_adapters/redshift_7_1/oid/decimal.rb +15 -0
  26. data/lib/active_record/connection_adapters/redshift_7_1/oid/json.rb +41 -0
  27. data/lib/active_record/connection_adapters/redshift_7_1/oid/jsonb.rb +25 -0
  28. data/lib/active_record/connection_adapters/redshift_7_1/oid/type_map_initializer.rb +62 -0
  29. data/lib/active_record/connection_adapters/redshift_7_1/oid.rb +17 -0
  30. data/lib/active_record/connection_adapters/redshift_7_1/quoting.rb +161 -0
  31. data/lib/active_record/connection_adapters/redshift_7_1/referential_integrity.rb +17 -0
  32. data/lib/active_record/connection_adapters/redshift_7_1/schema_definitions.rb +70 -0
  33. data/lib/active_record/connection_adapters/redshift_7_1/schema_dumper.rb +17 -0
  34. data/lib/active_record/connection_adapters/redshift_7_1/schema_statements.rb +422 -0
  35. data/lib/active_record/connection_adapters/redshift_7_1/type_metadata.rb +43 -0
  36. data/lib/active_record/connection_adapters/redshift_7_1/utils.rb +81 -0
  37. data/lib/active_record/connection_adapters/redshift_7_1_adapter.rb +844 -0
  38. data/lib/active_record/connection_adapters/redshift_7_2/array_parser.rb +92 -0
  39. data/lib/active_record/connection_adapters/redshift_7_2/column.rb +17 -0
  40. data/lib/active_record/connection_adapters/redshift_7_2/database_statements.rb +180 -0
  41. data/lib/active_record/connection_adapters/redshift_7_2/oid/date_time.rb +36 -0
  42. data/lib/active_record/connection_adapters/redshift_7_2/oid/decimal.rb +15 -0
  43. data/lib/active_record/connection_adapters/redshift_7_2/oid/json.rb +41 -0
  44. data/lib/active_record/connection_adapters/redshift_7_2/oid/jsonb.rb +25 -0
  45. data/lib/active_record/connection_adapters/redshift_7_2/oid/type_map_initializer.rb +62 -0
  46. data/lib/active_record/connection_adapters/redshift_7_2/oid.rb +17 -0
  47. data/lib/active_record/connection_adapters/redshift_7_2/quoting.rb +164 -0
  48. data/lib/active_record/connection_adapters/redshift_7_2/referential_integrity.rb +17 -0
  49. data/lib/active_record/connection_adapters/redshift_7_2/schema_definitions.rb +70 -0
  50. data/lib/active_record/connection_adapters/redshift_7_2/schema_dumper.rb +17 -0
  51. data/lib/active_record/connection_adapters/redshift_7_2/schema_statements.rb +422 -0
  52. data/lib/active_record/connection_adapters/redshift_7_2/type_metadata.rb +43 -0
  53. data/lib/active_record/connection_adapters/redshift_7_2/utils.rb +81 -0
  54. data/lib/active_record/connection_adapters/redshift_7_2_adapter.rb +844 -0
  55. data/lib/active_record/connection_adapters/redshift_8_0/array_parser.rb +92 -0
  56. data/lib/active_record/connection_adapters/redshift_8_0/column.rb +17 -0
  57. data/lib/active_record/connection_adapters/redshift_8_0/database_statements.rb +181 -0
  58. data/lib/active_record/connection_adapters/redshift_8_0/oid/date_time.rb +36 -0
  59. data/lib/active_record/connection_adapters/redshift_8_0/oid/decimal.rb +15 -0
  60. data/lib/active_record/connection_adapters/redshift_8_0/oid/json.rb +41 -0
  61. data/lib/active_record/connection_adapters/redshift_8_0/oid/jsonb.rb +25 -0
  62. data/lib/active_record/connection_adapters/redshift_8_0/oid/type_map_initializer.rb +62 -0
  63. data/lib/active_record/connection_adapters/redshift_8_0/oid.rb +17 -0
  64. data/lib/active_record/connection_adapters/redshift_8_0/quoting.rb +164 -0
  65. data/lib/active_record/connection_adapters/redshift_8_0/referential_integrity.rb +17 -0
  66. data/lib/active_record/connection_adapters/redshift_8_0/schema_definitions.rb +70 -0
  67. data/lib/active_record/connection_adapters/redshift_8_0/schema_dumper.rb +17 -0
  68. data/lib/active_record/connection_adapters/redshift_8_0/schema_statements.rb +422 -0
  69. data/lib/active_record/connection_adapters/redshift_8_0/type_metadata.rb +43 -0
  70. data/lib/active_record/connection_adapters/redshift_8_0/utils.rb +81 -0
  71. data/lib/active_record/connection_adapters/redshift_8_0_adapter.rb +843 -0
  72. data/lib/active_record/connection_adapters/redshift_adapter.rb +13 -1286
  73. data/lib/active_record/tasks/redshift_7_0_tasks.rb +148 -0
  74. data/lib/active_record/tasks/redshift_7_1_tasks.rb +151 -0
  75. data/lib/active_record/tasks/redshift_7_2_tasks.rb +151 -0
  76. data/lib/active_record/tasks/redshift_8_0_tasks.rb +151 -0
  77. data/lib/active_record/tasks/redshift_tasks.rb +13 -0
  78. data/lib/activerecord-redshift-adapter.rb +13 -0
  79. metadata +110 -84
  80. data/.gitignore +0 -26
  81. data/Gemfile +0 -14
  82. data/Rakefile +0 -26
  83. data/activerecord-redshift-adapter.gemspec +0 -24
  84. data/lib/activerecord_redshift/table_manager.rb +0 -230
  85. data/lib/activerecord_redshift_adapter/version.rb +0 -4
  86. data/lib/activerecord_redshift_adapter.rb +0 -4
  87. data/lib/monkeypatch_activerecord.rb +0 -195
  88. data/lib/monkeypatch_arel.rb +0 -96
  89. data/spec/active_record/base_spec.rb +0 -37
  90. data/spec/active_record/connection_adapters/redshift_adapter_spec.rb +0 -97
  91. data/spec/dummy/config/database.example.yml +0 -12
  92. data/spec/spec_helper.rb +0 -33
@@ -0,0 +1,765 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record/connection_adapters/abstract_adapter'
4
+ require 'active_record/connection_adapters/statement_pool'
5
+
6
+ require 'active_record/connection_adapters/redshift_7_0/utils'
7
+ require 'active_record/connection_adapters/redshift_7_0/column'
8
+ require 'active_record/connection_adapters/redshift_7_0/oid'
9
+ require 'active_record/connection_adapters/redshift_7_0/quoting'
10
+ require 'active_record/connection_adapters/redshift_7_0/referential_integrity'
11
+ require 'active_record/connection_adapters/redshift_7_0/schema_definitions'
12
+ require 'active_record/connection_adapters/redshift_7_0/schema_dumper'
13
+ require 'active_record/connection_adapters/redshift_7_0/schema_statements'
14
+ require 'active_record/connection_adapters/redshift_7_0/type_metadata'
15
+ require 'active_record/connection_adapters/redshift_7_0/database_statements'
16
+
17
+ require 'active_record/tasks/database_tasks'
18
+
19
+ require 'pg'
20
+
21
+ require 'ipaddr'
22
+
23
+ module ActiveRecord
24
+ module ConnectionHandling # :nodoc:
25
+ RS_VALID_CONN_PARAMS = %i[host hostaddr port dbname user password connect_timeout
26
+ client_encoding options application_name fallback_application_name
27
+ keepalives keepalives_idle keepalives_interval keepalives_count
28
+ tty sslmode requiressl sslcompression sslcert sslkey
29
+ sslrootcert sslcrl requirepeer krbsrvname gsslib service].freeze
30
+
31
+ # Establishes a connection to the database that's used by all Active Record objects
32
+ def redshift_connection(config)
33
+ conn_params = config.symbolize_keys
34
+
35
+ conn_params.delete_if { |_, v| v.nil? }
36
+
37
+ # Map ActiveRecords param names to PGs.
38
+ conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
39
+ conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
40
+
41
+ # Forward only valid config params to PG::Connection.connect.
42
+ conn_params.keep_if { |k, _| RS_VALID_CONN_PARAMS.include?(k) }
43
+
44
+ # The postgres drivers don't allow the creation of an unconnected PG::Connection object,
45
+ # so just pass a nil connection object for the time being.
46
+ ConnectionAdapters::RedshiftAdapter.new(nil, logger, conn_params, config)
47
+ end
48
+ end
49
+
50
+ module ConnectionAdapters
51
+ # The PostgreSQL adapter works with the native C (https://bitbucket.org/ged/ruby-pg) driver.
52
+ #
53
+ # Options:
54
+ #
55
+ # * <tt>:host</tt> - Defaults to a Unix-domain socket in /tmp. On machines without Unix-domain sockets,
56
+ # the default is to connect to localhost.
57
+ # * <tt>:port</tt> - Defaults to 5432.
58
+ # * <tt>:username</tt> - Defaults to be the same as the operating system name of the user running the application.
59
+ # * <tt>:password</tt> - Password to be used if the server demands password authentication.
60
+ # * <tt>:database</tt> - Defaults to be the same as the user name.
61
+ # * <tt>:schema_search_path</tt> - An optional schema search path for the connection given
62
+ # as a string of comma-separated schema names. This is backward-compatible with the <tt>:schema_order</tt> option.
63
+ # * <tt>:encoding</tt> - An optional client encoding that is used in a <tt>SET client_encoding TO
64
+ # <encoding></tt> call on the connection.
65
+ # * <tt>:min_messages</tt> - An optional client min messages that is used in a
66
+ # <tt>SET client_min_messages TO <min_messages></tt> call on the connection.
67
+ # * <tt>:variables</tt> - An optional hash of additional parameters that
68
+ # will be used in <tt>SET SESSION key = val</tt> calls on the connection.
69
+ # * <tt>:insert_returning</tt> - Does nothing for Redshift.
70
+ #
71
+ # Any further options are used as connection parameters to libpq. See
72
+ # http://www.postgresql.org/docs/9.1/static/libpq-connect.html for the
73
+ # list of parameters.
74
+ #
75
+ # In addition, default connection parameters of libpq can be set per environment variables.
76
+ # See http://www.postgresql.org/docs/9.1/static/libpq-envars.html .
77
+ class RedshiftAdapter < AbstractAdapter
78
+ ADAPTER_NAME = 'Redshift'
79
+
80
+ NATIVE_DATABASE_TYPES = {
81
+ primary_key: 'integer identity primary key',
82
+ string: { name: 'varchar' },
83
+ text: { name: 'varchar' },
84
+ integer: { name: 'integer' },
85
+ float: { name: 'decimal' },
86
+ decimal: { name: 'decimal' },
87
+ datetime: { name: 'timestamp' },
88
+ time: { name: 'timestamp' },
89
+ date: { name: 'date' },
90
+ bigint: { name: 'bigint' },
91
+ boolean: { name: 'boolean' }
92
+ }.freeze
93
+
94
+ OID = Redshift::OID # :nodoc:
95
+
96
+ include Redshift::Quoting
97
+ include Redshift::ReferentialIntegrity
98
+ include Redshift::SchemaStatements
99
+ include Redshift::DatabaseStatements
100
+
101
+ def schema_creation # :nodoc:
102
+ Redshift::SchemaCreation.new self
103
+ end
104
+
105
+ def supports_index_sort_order?
106
+ false
107
+ end
108
+
109
+ def supports_partial_index?
110
+ false
111
+ end
112
+
113
+ def supports_transaction_isolation?
114
+ false
115
+ end
116
+
117
+ def supports_foreign_keys?
118
+ true
119
+ end
120
+
121
+ def supports_deferrable_constraints?
122
+ false
123
+ end
124
+
125
+ def supports_views?
126
+ true
127
+ end
128
+
129
+ def supports_virtual_columns?
130
+ false
131
+ end
132
+
133
+ def index_algorithms
134
+ { concurrently: 'CONCURRENTLY' }
135
+ end
136
+
137
+ class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
138
+ def initialize(connection, max)
139
+ super(max)
140
+ @connection = connection
141
+ @counter = 0
142
+ end
143
+
144
+ def next_key
145
+ "a#{@counter + 1}"
146
+ end
147
+
148
+ def []=(sql, key)
149
+ super.tap { @counter += 1 }
150
+ end
151
+
152
+ private
153
+
154
+ def dealloc(key)
155
+ @connection.query "DEALLOCATE #{key}" if connection_active?
156
+ rescue PG::Error
157
+ end
158
+
159
+ def connection_active?
160
+ @connection.status == PG::CONNECTION_OK
161
+ rescue PG::Error
162
+ false
163
+ end
164
+ end
165
+
166
+ # Initializes and connects a PostgreSQL adapter.
167
+ def initialize(connection, logger, connection_parameters, config)
168
+ super(connection, logger, config)
169
+
170
+ @visitor = Arel::Visitors::PostgreSQL.new self
171
+ @visitor.extend(ConnectionAdapters::DetermineIfPreparableVisitor) if defined?(ConnectionAdapters::DetermineIfPreparableVisitor)
172
+ @prepared_statements = false
173
+
174
+ @connection_parameters = connection_parameters
175
+
176
+ # @local_tz is initialized as nil to avoid warnings when connect tries to use it
177
+ @local_tz = nil
178
+ @table_alias_length = nil
179
+
180
+ connect
181
+ @statements = StatementPool.new @connection,
182
+ self.class.type_cast_config_to_integer(config[:statement_limit])
183
+
184
+ @type_map = Type::HashLookupTypeMap.new
185
+ initialize_type_map(type_map)
186
+ @local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first['TimeZone']
187
+ @use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : false
188
+ end
189
+
190
+ # Clears the prepared statements cache.
191
+ def clear_cache!(new_connection: false)
192
+ @statements.clear
193
+ end
194
+
195
+ def truncate(table_name, name = nil)
196
+ exec_query "TRUNCATE TABLE #{quote_table_name(table_name)}", name, []
197
+ end
198
+
199
+ # Is this connection alive and ready for queries?
200
+ def active?
201
+ @connection.query 'SELECT 1'
202
+ true
203
+ rescue PG::Error
204
+ false
205
+ end
206
+
207
+ def reload_type_map
208
+ type_map.clear
209
+ initialize_type_map
210
+ end
211
+
212
+ # Close then reopen the connection.
213
+ def reconnect!
214
+ super
215
+ @connection.reset
216
+ configure_connection
217
+ reload_type_map
218
+ end
219
+
220
+ def reset!
221
+ clear_cache!
222
+ reset_transaction
223
+ @connection.query 'ROLLBACK' unless @connection.transaction_status == ::PG::PQTRANS_IDLE
224
+ @connection.query 'DISCARD ALL'
225
+ configure_connection
226
+ end
227
+
228
+ # Disconnects from the database if already connected. Otherwise, this
229
+ # method does nothing.
230
+ def disconnect!
231
+ super
232
+ begin
233
+ @connection.close
234
+ rescue StandardError
235
+ nil
236
+ end
237
+ end
238
+
239
+ def native_database_types # :nodoc:
240
+ NATIVE_DATABASE_TYPES
241
+ end
242
+
243
+ # Returns true, since this connection adapter supports migrations.
244
+ def supports_migrations?
245
+ true
246
+ end
247
+
248
+ # Does PostgreSQL support finding primary key on non-Active Record tables?
249
+ def supports_primary_key? # :nodoc:
250
+ true
251
+ end
252
+
253
+ def supports_ddl_transactions?
254
+ true
255
+ end
256
+
257
+ def supports_explain?
258
+ true
259
+ end
260
+
261
+ def supports_extensions?
262
+ false
263
+ end
264
+
265
+ def supports_ranges?
266
+ false
267
+ end
268
+
269
+ def supports_materialized_views?
270
+ false
271
+ end
272
+
273
+ def supports_import?
274
+ true
275
+ end
276
+
277
+ def enable_extension(name); end
278
+
279
+ def disable_extension(name); end
280
+
281
+ def extension_enabled?(_name)
282
+ false
283
+ end
284
+
285
+ # Returns the configured supported identifier length supported by PostgreSQL
286
+ def table_alias_length
287
+ @table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i
288
+ end
289
+
290
+ # Set the authorized user for this session
291
+ def session_auth=(user)
292
+ clear_cache!
293
+ exec_query "SET SESSION AUTHORIZATION #{user}"
294
+ end
295
+
296
+ def use_insert_returning?
297
+ false
298
+ end
299
+
300
+ def valid_type?(type)
301
+ !native_database_types[type].nil?
302
+ end
303
+
304
+ def update_table_definition(table_name, base) # :nodoc:
305
+ Redshift::Table.new(table_name, base)
306
+ end
307
+
308
+ def lookup_cast_type(sql_type) # :nodoc:
309
+ oid = execute("SELECT #{quote(sql_type)}::regtype::oid", 'SCHEMA').first['oid'].to_i
310
+ super(oid)
311
+ end
312
+
313
+ def column_name_for_operation(operation, _node) # :nodoc:
314
+ OPERATION_ALIASES.fetch(operation) { operation.downcase }
315
+ end
316
+
317
+ OPERATION_ALIASES = { # :nodoc:
318
+ 'maximum' => 'max',
319
+ 'minimum' => 'min',
320
+ 'average' => 'avg'
321
+ }.freeze
322
+
323
+ protected
324
+
325
+ # Returns the version of the connected PostgreSQL server.
326
+ def redshift_version
327
+ @connection.server_version
328
+ end
329
+
330
+ def translate_exception(exception, message:, sql:, binds:)
331
+ return exception unless exception.respond_to?(:result)
332
+
333
+ if exception.is_a?(PG::DuplicateDatabase)
334
+ DatabaseAlreadyExists.new(message, sql: sql, binds: binds)
335
+ else
336
+ super
337
+ end
338
+ end
339
+
340
+ class << self
341
+ def initialize_type_map(m) # :nodoc:
342
+ m.register_type 'int2', Type::Integer.new(limit: 2)
343
+ m.register_type 'int4', Type::Integer.new(limit: 4)
344
+ m.register_type 'int8', Type::Integer.new(limit: 8)
345
+ m.alias_type 'oid', 'int2'
346
+ m.register_type 'float4', Type::Float.new
347
+ m.alias_type 'float8', 'float4'
348
+ m.register_type 'text', Type::Text.new
349
+ register_class_with_limit m, 'varchar', Type::String
350
+ m.alias_type 'char', 'varchar'
351
+ m.alias_type 'name', 'varchar'
352
+ m.alias_type 'bpchar', 'varchar'
353
+ m.register_type 'bool', Type::Boolean.new
354
+ m.alias_type 'timestamptz', 'timestamp'
355
+ m.register_type 'date', Type::Date.new
356
+ m.register_type 'time', Type::Time.new
357
+
358
+ m.register_type 'timestamp' do |_, _, sql_type|
359
+ precision = extract_precision(sql_type)
360
+ OID::DateTime.new(precision: precision)
361
+ end
362
+
363
+ m.register_type 'numeric' do |_, fmod, sql_type|
364
+ precision = extract_precision(sql_type)
365
+ scale = extract_scale(sql_type)
366
+
367
+ # The type for the numeric depends on the width of the field,
368
+ # so we'll do something special here.
369
+ #
370
+ # When dealing with decimal columns:
371
+ #
372
+ # places after decimal = fmod - 4 & 0xffff
373
+ # places before decimal = (fmod - 4) >> 16 & 0xffff
374
+ if fmod && (fmod - 4 & 0xffff) == 0
375
+ # FIXME: Remove this class, and the second argument to
376
+ # lookups on PG
377
+ Type::DecimalWithoutScale.new(precision: precision)
378
+ else
379
+ OID::Decimal.new(precision: precision, scale: scale)
380
+ end
381
+ end
382
+ end
383
+ end
384
+
385
+ private
386
+
387
+ def get_oid_type(oid, fmod, column_name, sql_type = '') # :nodoc:
388
+ load_additional_types(type_map, [oid]) unless type_map.key?(oid)
389
+
390
+ type_map.fetch(oid, fmod, sql_type) do
391
+ warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String."
392
+ Type::Value.new.tap do |cast_type|
393
+ type_map.register_type(oid, cast_type)
394
+ end
395
+ end
396
+ end
397
+
398
+ def type_map
399
+ @type_map ||= Type::HashLookupTypeMap.new
400
+ end
401
+
402
+ def initialize_type_map(m = type_map)
403
+ self.class.initialize_type_map(m)
404
+ load_additional_types(m)
405
+ end
406
+
407
+ def extract_limit(sql_type) # :nodoc:
408
+ case sql_type
409
+ when /^bigint/i, /^int8/i
410
+ 8
411
+ when /^smallint/i
412
+ 2
413
+ else
414
+ super
415
+ end
416
+ end
417
+
418
+ # Extracts the value from a PostgreSQL column default definition.
419
+ def extract_value_from_default(default) # :nodoc:
420
+ case default
421
+ # Quoted types
422
+ when /\A[(B]?'(.*)'::/m
423
+ Regexp.last_match(1).gsub(/''/, "'")
424
+ # Boolean types
425
+ when 'true', 'false'
426
+ default
427
+ # Numeric types
428
+ when /\A\(?(-?\d+(\.\d*)?)\)?\z/
429
+ Regexp.last_match(1)
430
+ # Object identifier types
431
+ when /\A-?\d+\z/
432
+ Regexp.last_match(1)
433
+ else # rubocop:disable Style/EmptyElse
434
+ # Anything else is blank, some user type, or some function
435
+ # and we can't know the value of that, so return nil.
436
+ nil
437
+ end
438
+ end
439
+
440
+ def extract_default_function(default_value, default) # :nodoc:
441
+ default if has_default_function?(default_value, default)
442
+ end
443
+
444
+ def has_default_function?(default_value, default) # :nodoc:
445
+ !default_value && (/\w+\(.*\)/ === default)
446
+ end
447
+
448
+ def load_additional_types(type_map, oids = nil) # :nodoc:
449
+ initializer = OID::TypeMapInitializer.new(type_map)
450
+
451
+ load_types_queries(initializer, oids) do |query|
452
+ execute_and_clear(query, 'SCHEMA', []) do |records|
453
+ initializer.run(records)
454
+ end
455
+ end
456
+ end
457
+
458
+ def load_types_queries(_initializer, oids)
459
+ query =
460
+ if supports_ranges?
461
+ <<-SQL
462
+ SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype
463
+ FROM pg_type as t
464
+ LEFT JOIN pg_range as r ON oid = rngtypid
465
+ SQL
466
+ else
467
+ <<-SQL
468
+ SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, t.typtype, t.typbasetype
469
+ FROM pg_type as t
470
+ SQL
471
+ end
472
+
473
+ if oids
474
+ yield query + 'WHERE t.oid::integer IN (%s)' % oids.join(', ')
475
+ else
476
+ yield query
477
+ end
478
+ end
479
+
480
+ FEATURE_NOT_SUPPORTED = '0A000' # :nodoc:
481
+
482
+ def execute_and_clear(sql, name, binds, prepare: false, async: false)
483
+ result =
484
+ if without_prepared_statement?(binds)
485
+ exec_no_cache(sql, name, [])
486
+ elsif !prepare
487
+ exec_no_cache(sql, name, binds)
488
+ else
489
+ exec_cache(sql, name, binds)
490
+ end
491
+
492
+ ret = yield result
493
+ result.clear
494
+ ret
495
+ end
496
+
497
+ def exec_no_cache(sql, name, binds)
498
+ materialize_transactions
499
+
500
+ # make sure we carry over any changes to ActiveRecord.default_timezone that have been
501
+ # made since we established the connection
502
+ update_typemap_for_default_timezone
503
+
504
+ type_casted_binds = type_casted_binds(binds)
505
+ log(sql, name, binds, type_casted_binds) do
506
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
507
+ @connection.exec_params(sql, type_casted_binds)
508
+ end
509
+ end
510
+ end
511
+
512
+ def exec_cache(sql, name, binds)
513
+ materialize_transactions
514
+ update_typemap_for_default_timezone
515
+
516
+ stmt_key = prepare_statement(sql, binds)
517
+ type_casted_binds = type_casted_binds(binds)
518
+
519
+ log(sql, name, binds, type_casted_binds, stmt_key) do
520
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
521
+ @connection.exec_prepared(stmt_key, type_casted_binds)
522
+ end
523
+ end
524
+ rescue ActiveRecord::StatementInvalid => e
525
+ raise unless is_cached_plan_failure?(e)
526
+ raise ActiveRecord::PreparedStatementCacheExpired, e.cause.message if in_transaction?
527
+
528
+ @lock.synchronize do
529
+ # outside of transactions we can simply flush this query and retry
530
+ @statements.delete sql_key(sql)
531
+ end
532
+
533
+ retry
534
+ end
535
+
536
+ # Annoyingly, the code for prepared statements whose return value may
537
+ # have changed is FEATURE_NOT_SUPPORTED.
538
+ #
539
+ # This covers various different error types so we need to do additional
540
+ # work to classify the exception definitively as a
541
+ # ActiveRecord::PreparedStatementCacheExpired
542
+ #
543
+ # Check here for more details:
544
+ # https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
545
+ CACHED_PLAN_HEURISTIC = 'cached plan must not change result type'
546
+ def is_cached_plan_failure?(e)
547
+ pgerror = e.cause
548
+ code = pgerror.result.result_error_field(PG::PG_DIAG_SQLSTATE)
549
+ code == FEATURE_NOT_SUPPORTED && pgerror.message.include?(CACHED_PLAN_HEURISTIC)
550
+ rescue StandardError
551
+ false
552
+ end
553
+
554
+ # Returns the statement identifier for the client side cache
555
+ # of statements
556
+ def sql_key(sql)
557
+ "#{schema_search_path}-#{sql}"
558
+ end
559
+
560
+ # Prepare the statement if it hasn't been prepared, return
561
+ # the statement key.
562
+ def prepare_statement(sql, binds)
563
+ @lock.synchronize do
564
+ sql_key = sql_key(sql)
565
+ unless @statements.key? sql_key
566
+ nextkey = @statements.next_key
567
+ begin
568
+ @connection.prepare nextkey, sql
569
+ rescue StandardError => e
570
+ raise translate_exception_class(e, sql, binds)
571
+ end
572
+ # Clear the queue
573
+ @connection.get_last_result
574
+ @statements[sql_key] = nextkey
575
+ end
576
+ @statements[sql_key]
577
+ end
578
+ end
579
+
580
+ # Connects to a PostgreSQL server and sets up the adapter depending on the
581
+ # connected server's characteristics.
582
+ def connect
583
+ @connection = PG.connect(@connection_parameters)
584
+ configure_connection
585
+ add_pg_encoders
586
+ add_pg_decoders
587
+ end
588
+
589
+ # Configures the encoding, verbosity, schema search path, and time zone of the connection.
590
+ # This is called by #connect and should not be called manually.
591
+ def configure_connection
592
+ @connection.set_client_encoding(@config[:encoding]) if @config[:encoding]
593
+ self.schema_search_path = @config[:schema_search_path] || @config[:schema_order]
594
+
595
+ variables = @config.fetch(:variables, {}).stringify_keys
596
+
597
+ # If using Active Record's time zone support configure the connection to return
598
+ # TIMESTAMP WITH ZONE types in UTC.
599
+ unless variables['timezone']
600
+ if ActiveRecord.default_timezone == :utc
601
+ variables['timezone'] = 'UTC'
602
+ elsif @local_tz
603
+ variables['timezone'] = @local_tz
604
+ end
605
+ end
606
+
607
+ # SET statements from :variables config hash
608
+ # https://www.postgresql.org/docs/current/static/sql-set.html
609
+ variables.map do |k, v|
610
+ if [':default', :default].include?(v)
611
+ # Sets the value to the global or compile default
612
+ execute("SET #{k} TO DEFAULT", 'SCHEMA')
613
+ elsif !v.nil?
614
+ execute("SET #{k} TO #{quote(v)}", 'SCHEMA')
615
+ end
616
+ end
617
+ end
618
+
619
+ def last_insert_id_result(sequence_name) # :nodoc:
620
+ exec_query("SELECT currval('#{sequence_name}')", 'SQL')
621
+ end
622
+
623
+ # Returns the list of a table's column names, data types, and default values.
624
+ #
625
+ # The underlying query is roughly:
626
+ # SELECT column.name, column.type, default.value
627
+ # FROM column LEFT JOIN default
628
+ # ON column.table_id = default.table_id
629
+ # AND column.num = default.column_num
630
+ # WHERE column.table_id = get_table_id('table_name')
631
+ # AND column.num > 0
632
+ # AND NOT column.is_dropped
633
+ # ORDER BY column.num
634
+ #
635
+ # If the table name is not prefixed with a schema, the database will
636
+ # take the first match from the schema search path.
637
+ #
638
+ # Query implementation notes:
639
+ # - format_type includes the column size constraint, e.g. varchar(50)
640
+ # - ::regclass is a function that gives the id for a table name
641
+ def column_definitions(table_name) # :nodoc:
642
+ query(<<-END_SQL, 'SCHEMA')
643
+ SELECT a.attname, format_type(a.atttypid, a.atttypmod),
644
+ pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
645
+ FROM pg_attribute a LEFT JOIN pg_attrdef d
646
+ ON a.attrelid = d.adrelid AND a.attnum = d.adnum
647
+ WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
648
+ AND a.attnum > 0 AND NOT a.attisdropped
649
+ ORDER BY a.attnum
650
+ END_SQL
651
+ end
652
+
653
+ def extract_table_ref_from_insert_sql(sql)
654
+ sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
655
+ Regexp.last_match(1)&.strip
656
+ end
657
+
658
+ def arel_visitor
659
+ Arel::Visitors::PostgreSQL.new(self)
660
+ end
661
+
662
+ def build_statement_pool
663
+ StatementPool.new(@connection, self.class.type_cast_config_to_integer(@config[:statement_limit]))
664
+ end
665
+
666
+ def can_perform_case_insensitive_comparison_for?(column)
667
+ @case_insensitive_cache ||= {}
668
+ @case_insensitive_cache[column.sql_type] ||= begin
669
+ sql = <<~SQL
670
+ SELECT exists(
671
+ SELECT * FROM pg_proc
672
+ WHERE proname = 'lower'
673
+ AND proargtypes = ARRAY[#{quote column.sql_type}::regtype]::oidvector
674
+ ) OR exists(
675
+ SELECT * FROM pg_proc
676
+ INNER JOIN pg_cast
677
+ ON ARRAY[casttarget]::oidvector = proargtypes
678
+ WHERE proname = 'lower'
679
+ AND castsource = #{quote column.sql_type}::regtype
680
+ )
681
+ SQL
682
+ execute_and_clear(sql, 'SCHEMA', []) do |result|
683
+ result.getvalue(0, 0)
684
+ end
685
+ end
686
+ end
687
+
688
+ def add_pg_encoders
689
+ map = PG::TypeMapByClass.new
690
+ map[Integer] = PG::TextEncoder::Integer.new
691
+ map[TrueClass] = PG::TextEncoder::Boolean.new
692
+ map[FalseClass] = PG::TextEncoder::Boolean.new
693
+ @connection.type_map_for_queries = map
694
+ end
695
+
696
+ def update_typemap_for_default_timezone
697
+ return if @default_timezone == ActiveRecord.default_timezone || !@timestamp_decoder
698
+
699
+ decoder_class =
700
+ if ActiveRecord.default_timezone == :utc
701
+ PG::TextDecoder::TimestampUtc
702
+ else
703
+ PG::TextDecoder::TimestampWithoutTimeZone
704
+ end
705
+
706
+ @timestamp_decoder = decoder_class.new(@timestamp_decoder.to_h)
707
+ @connection.type_map_for_results.add_coder(@timestamp_decoder)
708
+ @default_timezone = ActiveRecord.default_timezone
709
+
710
+ # if default timezone has changed, we need to reconfigure the connection
711
+ # (specifically, the session time zone)
712
+ configure_connection
713
+ end
714
+
715
+ def add_pg_decoders
716
+ @default_timezone = nil
717
+ @timestamp_decoder = nil
718
+
719
+ coders_by_name = {
720
+ 'int2' => PG::TextDecoder::Integer,
721
+ 'int4' => PG::TextDecoder::Integer,
722
+ 'int8' => PG::TextDecoder::Integer,
723
+ 'oid' => PG::TextDecoder::Integer,
724
+ 'float4' => PG::TextDecoder::Float,
725
+ 'float8' => PG::TextDecoder::Float,
726
+ 'bool' => PG::TextDecoder::Boolean
727
+ }
728
+
729
+ if defined?(PG::TextDecoder::TimestampUtc)
730
+ # Use native PG encoders available since pg-1.1
731
+ coders_by_name['timestamp'] = PG::TextDecoder::TimestampUtc
732
+ coders_by_name['timestamptz'] = PG::TextDecoder::TimestampWithTimeZone
733
+ end
734
+
735
+ known_coder_types = coders_by_name.keys.map { |n| quote(n) }
736
+ query = <<~SQL % known_coder_types.join(', ')
737
+ SELECT t.oid, t.typname
738
+ FROM pg_type as t
739
+ WHERE t.typname IN (%s)
740
+ SQL
741
+ coders = execute_and_clear(query, 'SCHEMA', []) do |result|
742
+ result.filter_map { |row| construct_coder(row, coders_by_name[row['typname']]) }
743
+ end
744
+
745
+ map = PG::TypeMapByOid.new
746
+ coders.each { |coder| map.add_coder(coder) }
747
+ @connection.type_map_for_results = map
748
+
749
+ # extract timestamp decoder for use in update_typemap_for_default_timezone
750
+ @timestamp_decoder = coders.find { |coder| coder.name == 'timestamp' }
751
+ update_typemap_for_default_timezone
752
+ end
753
+
754
+ def construct_coder(row, coder_class)
755
+ return unless coder_class
756
+
757
+ coder_class.new(oid: row['oid'].to_i, name: row['typname'])
758
+ end
759
+
760
+ def create_table_definition(*args) # :nodoc:
761
+ Redshift::TableDefinition.new(self, *args)
762
+ end
763
+ end
764
+ end
765
+ end