activerecord 6.0.2.2 → 6.0.3.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -0
  3. data/README.rdoc +1 -1
  4. data/lib/active_record.rb +1 -0
  5. data/lib/active_record/advisory_lock_base.rb +18 -0
  6. data/lib/active_record/aggregations.rb +0 -1
  7. data/lib/active_record/association_relation.rb +4 -11
  8. data/lib/active_record/associations.rb +1 -1
  9. data/lib/active_record/associations/alias_tracker.rb +0 -1
  10. data/lib/active_record/associations/association.rb +5 -9
  11. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +0 -2
  12. data/lib/active_record/associations/collection_proxy.rb +1 -2
  13. data/lib/active_record/associations/has_many_association.rb +0 -1
  14. data/lib/active_record/associations/join_dependency.rb +9 -0
  15. data/lib/active_record/associations/preloader.rb +0 -1
  16. data/lib/active_record/attribute_assignment.rb +0 -1
  17. data/lib/active_record/attribute_decorators.rb +0 -2
  18. data/lib/active_record/attribute_methods/before_type_cast.rb +0 -1
  19. data/lib/active_record/attribute_methods/dirty.rb +2 -2
  20. data/lib/active_record/attribute_methods/primary_key.rb +0 -2
  21. data/lib/active_record/attribute_methods/read.rb +0 -1
  22. data/lib/active_record/attribute_methods/serialization.rb +0 -1
  23. data/lib/active_record/attribute_methods/time_zone_conversion.rb +0 -2
  24. data/lib/active_record/attribute_methods/write.rb +0 -1
  25. data/lib/active_record/attributes.rb +0 -1
  26. data/lib/active_record/autosave_association.rb +3 -3
  27. data/lib/active_record/callbacks.rb +1 -2
  28. data/lib/active_record/coders/yaml_column.rb +0 -1
  29. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +0 -3
  30. data/lib/active_record/connection_adapters/abstract/database_statements.rb +17 -15
  31. data/lib/active_record/connection_adapters/abstract/query_cache.rb +0 -1
  32. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +1 -2
  33. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +27 -27
  34. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +47 -30
  35. data/lib/active_record/connection_adapters/abstract/transaction.rb +4 -5
  36. data/lib/active_record/connection_adapters/abstract_adapter.rb +7 -3
  37. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +12 -28
  38. data/lib/active_record/connection_adapters/connection_specification.rb +1 -2
  39. data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
  40. data/lib/active_record/connection_adapters/mysql/database_statements.rb +5 -11
  41. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +0 -1
  42. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +1 -2
  43. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +8 -8
  44. data/lib/active_record/connection_adapters/mysql2_adapter.rb +0 -1
  45. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -2
  46. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +0 -1
  47. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
  48. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +0 -1
  49. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +0 -1
  50. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
  51. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +0 -1
  52. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +0 -1
  53. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
  54. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +0 -1
  55. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +1 -1
  56. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +2 -2
  57. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +1 -1
  58. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
  59. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +15 -29
  60. data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
  61. data/lib/active_record/connection_adapters/postgresql_adapter.rb +5 -2
  62. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +5 -6
  63. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +0 -1
  64. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +3 -3
  65. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +8 -7
  66. data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
  67. data/lib/active_record/connection_handling.rb +10 -26
  68. data/lib/active_record/core.rb +0 -2
  69. data/lib/active_record/counter_cache.rb +4 -1
  70. data/lib/active_record/database_configurations/url_config.rb +0 -1
  71. data/lib/active_record/dynamic_matchers.rb +2 -3
  72. data/lib/active_record/explain.rb +0 -1
  73. data/lib/active_record/fixture_set/table_row.rb +0 -1
  74. data/lib/active_record/fixture_set/table_rows.rb +0 -1
  75. data/lib/active_record/fixtures.rb +0 -3
  76. data/lib/active_record/gem_version.rb +2 -2
  77. data/lib/active_record/inheritance.rb +0 -3
  78. data/lib/active_record/insert_all.rb +3 -3
  79. data/lib/active_record/internal_metadata.rb +1 -1
  80. data/lib/active_record/locking/optimistic.rb +0 -1
  81. data/lib/active_record/log_subscriber.rb +1 -1
  82. data/lib/active_record/middleware/database_selector.rb +0 -1
  83. data/lib/active_record/middleware/database_selector/resolver.rb +1 -2
  84. data/lib/active_record/migration.rb +4 -4
  85. data/lib/active_record/migration/command_recorder.rb +6 -18
  86. data/lib/active_record/migration/compatibility.rb +3 -3
  87. data/lib/active_record/migration/join_table.rb +0 -1
  88. data/lib/active_record/model_schema.rb +0 -2
  89. data/lib/active_record/nested_attributes.rb +0 -2
  90. data/lib/active_record/no_touching.rb +2 -2
  91. data/lib/active_record/null_relation.rb +0 -1
  92. data/lib/active_record/persistence.rb +4 -5
  93. data/lib/active_record/querying.rb +1 -1
  94. data/lib/active_record/railtie.rb +1 -1
  95. data/lib/active_record/railties/collection_cache_association_loading.rb +1 -1
  96. data/lib/active_record/reflection.rb +8 -8
  97. data/lib/active_record/relation.rb +12 -1
  98. data/lib/active_record/relation/batches.rb +0 -1
  99. data/lib/active_record/relation/calculations.rb +1 -1
  100. data/lib/active_record/relation/delegation.rb +7 -6
  101. data/lib/active_record/relation/finder_methods.rb +0 -1
  102. data/lib/active_record/relation/from_clause.rb +4 -0
  103. data/lib/active_record/relation/merger.rb +0 -1
  104. data/lib/active_record/relation/predicate_builder.rb +1 -5
  105. data/lib/active_record/relation/query_methods.rb +9 -7
  106. data/lib/active_record/relation/spawn_methods.rb +0 -1
  107. data/lib/active_record/relation/where_clause.rb +0 -1
  108. data/lib/active_record/result.rb +0 -1
  109. data/lib/active_record/schema_migration.rb +1 -1
  110. data/lib/active_record/scoping.rb +0 -1
  111. data/lib/active_record/scoping/default.rb +0 -1
  112. data/lib/active_record/scoping/named.rb +3 -4
  113. data/lib/active_record/store.rb +1 -1
  114. data/lib/active_record/suppressor.rb +2 -2
  115. data/lib/active_record/table_metadata.rb +16 -1
  116. data/lib/active_record/tasks/mysql_database_tasks.rb +0 -1
  117. data/lib/active_record/tasks/postgresql_database_tasks.rb +0 -1
  118. data/lib/active_record/tasks/sqlite_database_tasks.rb +0 -1
  119. data/lib/active_record/test_fixtures.rb +1 -1
  120. data/lib/active_record/timestamp.rb +0 -1
  121. data/lib/active_record/touch_later.rb +1 -2
  122. data/lib/active_record/transactions.rb +8 -8
  123. data/lib/active_record/type.rb +0 -1
  124. data/lib/active_record/type/adapter_specific_registry.rb +2 -5
  125. data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
  126. data/lib/active_record/type/serialized.rb +0 -1
  127. data/lib/active_record/type/type_map.rb +0 -1
  128. data/lib/active_record/type/unsigned_integer.rb +0 -1
  129. data/lib/active_record/validations.rb +2 -3
  130. data/lib/active_record/validations/associated.rb +1 -2
  131. data/lib/arel.rb +5 -1
  132. data/lib/arel/predications.rb +5 -6
  133. data/lib/arel/visitors/depth_first.rb +0 -1
  134. data/lib/arel/visitors/dot.rb +0 -1
  135. data/lib/arel/visitors/mssql.rb +0 -1
  136. data/lib/arel/visitors/oracle.rb +1 -2
  137. data/lib/arel/visitors/oracle12.rb +0 -1
  138. data/lib/arel/visitors/postgresql.rb +0 -1
  139. data/lib/arel/visitors/sqlite.rb +0 -1
  140. data/lib/arel/visitors/to_sql.rb +0 -1
  141. data/lib/arel/visitors/visitor.rb +0 -1
  142. data/lib/arel/visitors/where_sql.rb +0 -1
  143. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
  144. data/lib/rails/generators/active_record/migration.rb +0 -1
  145. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +1 -1
  146. data/lib/rails/generators/active_record/model/model_generator.rb +0 -1
  147. metadata +14 -13
@@ -19,7 +19,7 @@ module ActiveRecord
19
19
 
20
20
  def visit_ChangeColumnDefinition(o)
21
21
  column = o.column
22
- column.sql_type = type_to_sql(column.type, column.options)
22
+ column.sql_type = type_to_sql(column.type, **column.options)
23
23
  quoted_column_name = quote_column_name(o.name)
24
24
 
25
25
  change_column_sql = +"ALTER COLUMN #{quoted_column_name} TYPE #{column.sql_type}"
@@ -33,7 +33,7 @@ module ActiveRecord
33
33
  if options[:using]
34
34
  change_column_sql << " USING #{options[:using]}"
35
35
  elsif options[:cast_as]
36
- cast_as_type = type_to_sql(options[:cast_as], options)
36
+ cast_as_type = type_to_sql(options[:cast_as], **options)
37
37
  change_column_sql << " USING CAST(#{quoted_column_name} AS #{cast_as_type})"
38
38
  end
39
39
 
@@ -186,7 +186,7 @@ module ActiveRecord
186
186
 
187
187
  attr_reader :unlogged
188
188
 
189
- def initialize(*)
189
+ def initialize(*, **)
190
190
  super
191
191
  @unlogged = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables
192
192
  end
@@ -5,7 +5,6 @@ module ActiveRecord
5
5
  module PostgreSQL
6
6
  class SchemaDumper < ConnectionAdapters::SchemaDumper # :nodoc:
7
7
  private
8
-
9
8
  def extensions(stream)
10
9
  extensions = @connection.extensions
11
10
  if extensions.any?
@@ -74,7 +74,7 @@ module ActiveRecord
74
74
  INNER JOIN pg_index d ON t.oid = d.indrelid
75
75
  INNER JOIN pg_class i ON d.indexrelid = i.oid
76
76
  LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
77
- WHERE i.relkind = 'i'
77
+ WHERE i.relkind IN ('i', 'I')
78
78
  AND i.relname = #{index[:name]}
79
79
  AND t.relname = #{table[:name]}
80
80
  AND n.nspname = #{index[:schema]}
@@ -92,7 +92,7 @@ module ActiveRecord
92
92
  INNER JOIN pg_index d ON t.oid = d.indrelid
93
93
  INNER JOIN pg_class i ON d.indexrelid = i.oid
94
94
  LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
95
- WHERE i.relkind = 'i'
95
+ WHERE i.relkind IN ('i', 'I')
96
96
  AND d.indisprimary = 'f'
97
97
  AND t.relname = #{scope[:name]}
98
98
  AND n.nspname = #{scope[:schema]}
@@ -390,7 +390,7 @@ module ActiveRecord
390
390
  rename_table_indexes(table_name, new_name)
391
391
  end
392
392
 
393
- def add_column(table_name, column_name, type, options = {}) #:nodoc:
393
+ def add_column(table_name, column_name, type, **options) #:nodoc:
394
394
  clear_cache!
395
395
  super
396
396
  change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
@@ -439,7 +439,7 @@ module ActiveRecord
439
439
  end
440
440
 
441
441
  def add_index(table_name, column_name, options = {}) #:nodoc:
442
- index_name, index_type, index_columns_and_opclasses, index_options, index_algorithm, index_using, comment = add_index_options(table_name, column_name, options)
442
+ index_name, index_type, index_columns_and_opclasses, index_options, index_algorithm, index_using, comment = add_index_options(table_name, column_name, **options)
443
443
  execute("CREATE #{index_type} INDEX #{index_algorithm} #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} #{index_using} (#{index_columns_and_opclasses})#{index_options}").tap do
444
444
  execute "COMMENT ON INDEX #{quote_column_name(index_name)} IS #{quote(comment)}" if comment
445
445
  end
@@ -553,12 +553,12 @@ module ActiveRecord
553
553
  # requires that the ORDER BY include the distinct column.
554
554
  def columns_for_distinct(columns, orders) #:nodoc:
555
555
  order_columns = orders.reject(&:blank?).map { |s|
556
- # Convert Arel node to string
557
- s = s.to_sql unless s.is_a?(String)
558
- # Remove any ASC/DESC modifiers
559
- s.gsub(/\s+(?:ASC|DESC)\b/i, "")
560
- .gsub(/\s+NULLS\s+(?:FIRST|LAST)\b/i, "")
561
- }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" }
556
+ # Convert Arel node to string
557
+ s = visitor.compile(s) unless s.is_a?(String)
558
+ # Remove any ASC/DESC modifiers
559
+ s.gsub(/\s+(?:ASC|DESC)\b/i, "")
560
+ .gsub(/\s+NULLS\s+(?:FIRST|LAST)\b/i, "")
561
+ }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" }
562
562
 
563
563
  (order_columns << super).join(", ")
564
564
  end
@@ -613,8 +613,8 @@ module ActiveRecord
613
613
  PostgreSQL::SchemaCreation.new(self)
614
614
  end
615
615
 
616
- def create_table_definition(*args)
617
- PostgreSQL::TableDefinition.new(self, *args)
616
+ def create_table_definition(*args, **options)
617
+ PostgreSQL::TableDefinition.new(self, *args, **options)
618
618
  end
619
619
 
620
620
  def create_alter_table(name)
@@ -679,14 +679,14 @@ module ActiveRecord
679
679
  end
680
680
  end
681
681
 
682
- def add_column_for_alter(table_name, column_name, type, options = {})
682
+ def add_column_for_alter(table_name, column_name, type, **options)
683
683
  return super unless options.key?(:comment)
684
684
  [super, Proc.new { change_column_comment(table_name, column_name, options[:comment]) }]
685
685
  end
686
686
 
687
687
  def change_column_for_alter(table_name, column_name, type, options = {})
688
688
  td = create_table_definition(table_name)
689
- cd = td.new_column_definition(column_name, type, options)
689
+ cd = td.new_column_definition(column_name, type, **options)
690
690
  sqls = [schema_creation.accept(ChangeColumnDefinition.new(cd, column_name))]
691
691
  sqls << Proc.new { change_column_comment(table_name, column_name, options[:comment]) } if options.key?(:comment)
692
692
  sqls
@@ -711,20 +711,6 @@ module ActiveRecord
711
711
  "ALTER COLUMN #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL"
712
712
  end
713
713
 
714
- def add_timestamps_for_alter(table_name, options = {})
715
- options[:null] = false if options[:null].nil?
716
-
717
- if !options.key?(:precision) && supports_datetime_with_precision?
718
- options[:precision] = 6
719
- end
720
-
721
- [add_column_for_alter(table_name, :created_at, :datetime, options), add_column_for_alter(table_name, :updated_at, :datetime, options)]
722
- end
723
-
724
- def remove_timestamps_for_alter(table_name, options = {})
725
- [remove_column_for_alter(table_name, :updated_at), remove_column_for_alter(table_name, :created_at)]
726
- end
727
-
728
714
  def add_index_opclass(quoted_columns, **options)
729
715
  opclasses = options_for_index_columns(options[:opclass])
730
716
  quoted_columns.each do |name, column|
@@ -733,7 +719,7 @@ module ActiveRecord
733
719
  end
734
720
 
735
721
  def add_options_for_index_columns(quoted_columns, **options)
736
- quoted_columns = add_index_opclass(quoted_columns, options)
722
+ quoted_columns = add_index_opclass(quoted_columns, **options)
737
723
  super
738
724
  end
739
725
 
@@ -37,7 +37,6 @@ module ActiveRecord
37
37
  end
38
38
 
39
39
  protected
40
-
41
40
  def parts
42
41
  @parts ||= [@schema, @identifier].compact
43
42
  end
@@ -156,6 +156,10 @@ module ActiveRecord
156
156
  true
157
157
  end
158
158
 
159
+ def supports_partitioned_indexes?
160
+ database_version >= 110_000
161
+ end
162
+
159
163
  def supports_partial_index?
160
164
  true
161
165
  end
@@ -463,7 +467,6 @@ module ActiveRecord
463
467
  end
464
468
 
465
469
  private
466
-
467
470
  # See https://www.postgresql.org/docs/current/static/errcodes-appendix.html
468
471
  VALUE_LIMIT_VIOLATION = "22001"
469
472
  NUMERIC_VALUE_OUT_OF_RANGE = "22003"
@@ -630,7 +633,7 @@ module ActiveRecord
630
633
  SQL
631
634
 
632
635
  if oids
633
- query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
636
+ query += "WHERE t.oid IN (%s)" % oids.join(", ")
634
637
  else
635
638
  query += initializer.query_conditions_for_initial_load
636
639
  end
@@ -83,7 +83,9 @@ module ActiveRecord
83
83
 
84
84
 
85
85
  private
86
- def execute_batch(sql, name = nil)
86
+ def execute_batch(statements, name = nil)
87
+ sql = combine_multi_statements(statements)
88
+
87
89
  if preventing_writes? && write_query?(sql)
88
90
  raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
89
91
  end
@@ -108,11 +110,8 @@ module ActiveRecord
108
110
  end.compact
109
111
  end
110
112
 
111
- def build_truncate_statements(*table_names)
112
- truncate_tables = table_names.map do |table_name|
113
- "DELETE FROM #{quote_table_name(table_name)}"
114
- end
115
- combine_multi_statements(truncate_tables)
113
+ def build_truncate_statement(table_name)
114
+ "DELETE FROM #{quote_table_name(table_name)}"
116
115
  end
117
116
  end
118
117
  end
@@ -82,7 +82,6 @@ module ActiveRecord
82
82
  private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER
83
83
 
84
84
  private
85
-
86
85
  def _type_cast(value)
87
86
  case value
88
87
  when BigDecimal
@@ -55,7 +55,7 @@ module ActiveRecord
55
55
  def add_foreign_key(from_table, to_table, **options)
56
56
  alter_table(from_table) do |definition|
57
57
  to_table = strip_table_name_prefix_and_suffix(to_table)
58
- definition.foreign_key(to_table, options)
58
+ definition.foreign_key(to_table, **options)
59
59
  end
60
60
  end
61
61
 
@@ -87,8 +87,8 @@ module ActiveRecord
87
87
  SQLite3::SchemaCreation.new(self)
88
88
  end
89
89
 
90
- def create_table_definition(*args)
91
- SQLite3::TableDefinition.new(self, *args)
90
+ def create_table_definition(*args, **options)
91
+ SQLite3::TableDefinition.new(self, *args, **options)
92
92
  end
93
93
 
94
94
  def new_column_from_field(table_name, field)
@@ -101,7 +101,7 @@ module ActiveRecord
101
101
  def self.database_exists?(config)
102
102
  config = config.symbolize_keys
103
103
  if config[:database] == ":memory:"
104
- return true
104
+ true
105
105
  else
106
106
  database_file = defined?(Rails.root) ? File.expand_path(config[:database], Rails.root) : config[:database]
107
107
  File.exist?(database_file)
@@ -244,17 +244,17 @@ module ActiveRecord
244
244
  rename_table_indexes(table_name, new_name)
245
245
  end
246
246
 
247
- def add_column(table_name, column_name, type, options = {}) #:nodoc:
247
+ def add_column(table_name, column_name, type, **options) #:nodoc:
248
248
  if invalid_alter_table_type?(type, options)
249
249
  alter_table(table_name) do |definition|
250
- definition.column(column_name, type, options)
250
+ definition.column(column_name, type, **options)
251
251
  end
252
252
  else
253
253
  super
254
254
  end
255
255
  end
256
256
 
257
- def remove_column(table_name, column_name, type = nil, options = {}) #:nodoc:
257
+ def remove_column(table_name, column_name, type = nil, **options) #:nodoc:
258
258
  alter_table(table_name) do |definition|
259
259
  definition.remove_column column_name
260
260
  definition.foreign_keys.delete_if do |_, fk_options|
@@ -363,7 +363,8 @@ module ActiveRecord
363
363
  # See: https://www.sqlite.org/lang_altertable.html
364
364
  # SQLite has an additional restriction on the ALTER TABLE statement
365
365
  def invalid_alter_table_type?(type, options)
366
- type.to_sym == :primary_key || options[:primary_key]
366
+ type.to_sym == :primary_key || options[:primary_key] ||
367
+ options[:null] == false && options[:default].nil?
367
368
  end
368
369
 
369
370
  def alter_table(table_name, foreign_keys = foreign_keys(table_name), **options)
@@ -376,7 +377,7 @@ module ActiveRecord
376
377
  fk.options[:column] = column
377
378
  end
378
379
  to_table = strip_table_name_prefix_and_suffix(fk.to_table)
379
- definition.foreign_key(to_table, fk.options)
380
+ definition.foreign_key(to_table, **fk.options)
380
381
  end
381
382
 
382
383
  yield definition if block_given?
@@ -398,7 +399,7 @@ module ActiveRecord
398
399
  def copy_table(from, to, options = {})
399
400
  from_primary_key = primary_key(from)
400
401
  options[:id] = false
401
- create_table(to, options) do |definition|
402
+ create_table(to, **options) do |definition|
402
403
  @definition = definition
403
404
  if from_primary_key.is_a?(Array)
404
405
  @definition.primary_keys from_primary_key
@@ -48,7 +48,6 @@ module ActiveRecord
48
48
  end
49
49
 
50
50
  private
51
-
52
51
  def cache
53
52
  @cache[Process.pid]
54
53
  end
@@ -96,25 +96,10 @@ module ActiveRecord
96
96
  # # raises exception due to non-existent role
97
97
  # end
98
98
  #
99
- # For cases where you may want to connect to a database outside of the model,
100
- # you can use +connected_to+ with a +database+ argument. The +database+ argument
101
- # expects a symbol that corresponds to the database key in your config.
99
+ # The `database` kwarg is deprecated in 6.1 and will be removed in 6.2
102
100
  #
103
- # ActiveRecord::Base.connected_to(database: :animals_slow_replica) do
104
- # Dog.run_a_long_query # runs a long query while connected to the +animals_slow_replica+
105
- # end
106
- #
107
- # This will connect to a new database for the queries inside the block. By
108
- # default the `:writing` role will be used since all connections must be assigned
109
- # a role. If you would like to use a different role you can pass a hash to database:
110
- #
111
- # ActiveRecord::Base.connected_to(database: { readonly_slow: :animals_slow_replica }) do
112
- # # runs a long query while connected to the +animals_slow_replica+ using the readonly_slow role.
113
- # Dog.run_a_long_query
114
- # end
115
- #
116
- # When using the database key a new connection will be established every time. It is not
117
- # recommended to use this outside of one-off scripts.
101
+ # It is not recommended for use as it re-establishes a connection every
102
+ # time it is called.
118
103
  def connected_to(database: nil, role: nil, prevent_writes: false, &blk)
119
104
  if database && role
120
105
  raise ArgumentError, "connected_to can only accept a `database` or a `role` argument, but not both arguments."
@@ -131,12 +116,10 @@ module ActiveRecord
131
116
 
132
117
  with_handler(role, &blk)
133
118
  elsif role
134
- if role == writing_role
135
- with_handler(role.to_sym) do
136
- connection_handler.while_preventing_writes(prevent_writes, &blk)
137
- end
138
- else
139
- with_handler(role.to_sym, &blk)
119
+ prevent_writes = true if role == reading_role
120
+
121
+ with_handler(role.to_sym) do
122
+ connection_handler.while_preventing_writes(prevent_writes, &blk)
140
123
  end
141
124
  else
142
125
  raise ArgumentError, "must provide a `database` or a `role`."
@@ -263,10 +246,11 @@ module ActiveRecord
263
246
  :clear_all_connections!, :flush_idle_connections!, to: :connection_handler
264
247
 
265
248
  private
266
-
267
249
  def swap_connection_handler(handler, &blk) # :nodoc:
268
250
  old_handler, ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handler, handler
269
- yield
251
+ return_value = yield
252
+ return_value.load if return_value.is_a? ActiveRecord::Relation
253
+ return_value
270
254
  ensure
271
255
  ActiveRecord::Base.connection_handler = old_handler
272
256
  end
@@ -286,7 +286,6 @@ module ActiveRecord
286
286
  end
287
287
 
288
288
  private
289
-
290
289
  def cached_find_by_statement(key, &block)
291
290
  cache = @find_by_statement_cache[connection.prepared_statements]
292
291
  cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
@@ -554,7 +553,6 @@ module ActiveRecord
554
553
  end
555
554
 
556
555
  private
557
-
558
556
  # +Array#flatten+ will call +#to_ary+ (recursively) on each of the elements of
559
557
  # the array, and then rescues from the possible +NoMethodError+. If those elements are
560
558
  # +ActiveRecord::Base+'s, then this triggers the various +method_missing+'s that we have,
@@ -51,7 +51,10 @@ module ActiveRecord
51
51
 
52
52
  if touch
53
53
  names = touch if touch != true
54
- updates.merge!(touch_attributes_with_time(*names))
54
+ names = Array.wrap(names)
55
+ options = names.extract_options!
56
+ touch_updates = touch_attributes_with_time(*names, **options)
57
+ updates.merge!(touch_updates)
55
58
  end
56
59
 
57
60
  unscoped.where(primary_key => object.id).update_all(updates)
@@ -56,7 +56,6 @@ module ActiveRecord
56
56
  end
57
57
 
58
58
  private
59
-
60
59
  def build_url_hash(url)
61
60
  if url.nil? || /^jdbc:/.match?(url)
62
61
  { "url" => url }
@@ -49,9 +49,9 @@ module ActiveRecord
49
49
 
50
50
  attr_reader :model, :name, :attribute_names
51
51
 
52
- def initialize(model, name)
52
+ def initialize(model, method_name)
53
53
  @model = model
54
- @name = name.to_s
54
+ @name = method_name.to_s
55
55
  @attribute_names = @name.match(self.class.pattern)[1].split("_and_")
56
56
  @attribute_names.map! { |name| @model.attribute_aliases[name] || name }
57
57
  end
@@ -69,7 +69,6 @@ module ActiveRecord
69
69
  end
70
70
 
71
71
  private
72
-
73
72
  def body
74
73
  "#{finder}(#{attributes_hash})"
75
74
  end
@@ -36,7 +36,6 @@ module ActiveRecord
36
36
  end
37
37
 
38
38
  private
39
-
40
39
  def render_bind(attr)
41
40
  value = if attr.type.binary? && attr.value
42
41
  "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
@@ -48,7 +48,6 @@ module ActiveRecord
48
48
  end
49
49
 
50
50
  private
51
-
52
51
  def model_metadata
53
52
  @table_rows.model_metadata
54
53
  end
@@ -29,7 +29,6 @@ module ActiveRecord
29
29
  end
30
30
 
31
31
  private
32
-
33
32
  def build_table_rows_from(table_name, fixtures, config)
34
33
  now = config.default_timezone == :utc ? Time.now.utc : Time.now
35
34
 
@@ -464,7 +464,6 @@ module ActiveRecord
464
464
  end
465
465
 
466
466
  private
467
-
468
467
  def insert_class(class_names, name, klass)
469
468
  # We only want to deal with AR objects.
470
469
  if klass && klass < ActiveRecord::Base
@@ -570,7 +569,6 @@ module ActiveRecord
570
569
  end
571
570
 
572
571
  private
573
-
574
572
  def read_and_insert(fixtures_directory, fixture_files, class_names, connection) # :nodoc:
575
573
  fixtures_map = {}
576
574
  fixture_sets = fixture_files.map do |fixture_set_name|
@@ -666,7 +664,6 @@ module ActiveRecord
666
664
  end
667
665
 
668
666
  private
669
-
670
667
  def model_class=(class_name)
671
668
  if class_name.is_a?(Class) # TODO: Should be an AR::Base type class, or any?
672
669
  @model_class = class_name