activerecord 5.1.0 → 5.2.0.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 (260) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +410 -530
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -4
  5. data/examples/performance.rb +2 -0
  6. data/examples/simple.rb +2 -0
  7. data/lib/active_record/aggregations.rb +6 -5
  8. data/lib/active_record/association_relation.rb +4 -2
  9. data/lib/active_record/associations/alias_tracker.rb +23 -32
  10. data/lib/active_record/associations/association.rb +20 -21
  11. data/lib/active_record/associations/association_scope.rb +49 -49
  12. data/lib/active_record/associations/belongs_to_association.rb +12 -10
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +4 -7
  14. data/lib/active_record/associations/builder/association.rb +4 -7
  15. data/lib/active_record/associations/builder/belongs_to.rb +10 -6
  16. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  18. data/lib/active_record/associations/builder/has_many.rb +2 -0
  19. data/lib/active_record/associations/builder/has_one.rb +2 -0
  20. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  21. data/lib/active_record/associations/collection_association.rb +50 -41
  22. data/lib/active_record/associations/collection_proxy.rb +22 -39
  23. data/lib/active_record/associations/foreign_association.rb +2 -0
  24. data/lib/active_record/associations/has_many_association.rb +4 -2
  25. data/lib/active_record/associations/has_many_through_association.rb +12 -18
  26. data/lib/active_record/associations/has_one_association.rb +5 -1
  27. data/lib/active_record/associations/has_one_through_association.rb +8 -7
  28. data/lib/active_record/associations/join_dependency/join_association.rb +17 -64
  29. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  30. data/lib/active_record/associations/join_dependency/join_part.rb +2 -9
  31. data/lib/active_record/associations/join_dependency.rb +27 -44
  32. data/lib/active_record/associations/preloader/association.rb +53 -92
  33. data/lib/active_record/associations/preloader/through_association.rb +72 -73
  34. data/lib/active_record/associations/preloader.rb +17 -37
  35. data/lib/active_record/associations/singular_association.rb +14 -10
  36. data/lib/active_record/associations/through_association.rb +26 -11
  37. data/lib/active_record/associations.rb +68 -76
  38. data/lib/active_record/attribute_assignment.rb +2 -0
  39. data/lib/active_record/attribute_decorators.rb +3 -2
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  41. data/lib/active_record/attribute_methods/dirty.rb +24 -214
  42. data/lib/active_record/attribute_methods/primary_key.rb +10 -13
  43. data/lib/active_record/attribute_methods/query.rb +2 -0
  44. data/lib/active_record/attribute_methods/read.rb +8 -2
  45. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  47. data/lib/active_record/attribute_methods/write.rb +22 -19
  48. data/lib/active_record/attribute_methods.rb +48 -12
  49. data/lib/active_record/attributes.rb +7 -6
  50. data/lib/active_record/autosave_association.rb +8 -11
  51. data/lib/active_record/base.rb +2 -0
  52. data/lib/active_record/callbacks.rb +8 -6
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +2 -0
  55. data/lib/active_record/collection_cache_key.rb +14 -10
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +110 -35
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +175 -33
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +8 -2
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +13 -24
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +58 -3
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +165 -85
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +83 -97
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +118 -180
  69. data/lib/active_record/connection_adapters/column.rb +4 -2
  70. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +2 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +11 -17
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -23
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +2 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  98. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +4 -2
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -1
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +14 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +269 -126
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +64 -85
  116. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  118. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  119. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +18 -0
  120. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  122. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  123. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +71 -1
  124. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +92 -95
  125. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  126. data/lib/active_record/connection_handling.rb +4 -2
  127. data/lib/active_record/core.rb +39 -60
  128. data/lib/active_record/counter_cache.rb +3 -2
  129. data/lib/active_record/define_callbacks.rb +5 -3
  130. data/lib/active_record/dynamic_matchers.rb +9 -9
  131. data/lib/active_record/enum.rb +17 -13
  132. data/lib/active_record/errors.rb +42 -3
  133. data/lib/active_record/explain.rb +3 -1
  134. data/lib/active_record/explain_registry.rb +2 -0
  135. data/lib/active_record/explain_subscriber.rb +2 -0
  136. data/lib/active_record/fixture_set/file.rb +2 -0
  137. data/lib/active_record/fixtures.rb +67 -60
  138. data/lib/active_record/gem_version.rb +4 -2
  139. data/lib/active_record/inheritance.rb +9 -9
  140. data/lib/active_record/integration.rb +58 -19
  141. data/lib/active_record/internal_metadata.rb +2 -0
  142. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  143. data/lib/active_record/locking/optimistic.rb +8 -6
  144. data/lib/active_record/locking/pessimistic.rb +9 -6
  145. data/lib/active_record/log_subscriber.rb +46 -4
  146. data/lib/active_record/migration/command_recorder.rb +11 -9
  147. data/lib/active_record/migration/compatibility.rb +74 -22
  148. data/lib/active_record/migration/join_table.rb +2 -0
  149. data/lib/active_record/migration.rb +181 -137
  150. data/lib/active_record/model_schema.rb +73 -58
  151. data/lib/active_record/nested_attributes.rb +18 -6
  152. data/lib/active_record/no_touching.rb +3 -1
  153. data/lib/active_record/null_relation.rb +2 -0
  154. data/lib/active_record/persistence.rb +153 -18
  155. data/lib/active_record/query_cache.rb +17 -12
  156. data/lib/active_record/querying.rb +4 -2
  157. data/lib/active_record/railtie.rb +61 -3
  158. data/lib/active_record/railties/console_sandbox.rb +2 -0
  159. data/lib/active_record/railties/controller_runtime.rb +2 -0
  160. data/lib/active_record/railties/databases.rake +47 -37
  161. data/lib/active_record/readonly_attributes.rb +3 -2
  162. data/lib/active_record/reflection.rb +131 -204
  163. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  164. data/lib/active_record/relation/batches.rb +32 -17
  165. data/lib/active_record/relation/calculations.rb +58 -20
  166. data/lib/active_record/relation/delegation.rb +10 -29
  167. data/lib/active_record/relation/finder_methods.rb +74 -85
  168. data/lib/active_record/relation/from_clause.rb +2 -8
  169. data/lib/active_record/relation/merger.rb +51 -20
  170. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  171. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  172. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  173. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  174. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +54 -0
  175. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -6
  176. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  177. data/lib/active_record/relation/predicate_builder.rb +53 -78
  178. data/lib/active_record/relation/query_attribute.rb +9 -2
  179. data/lib/active_record/relation/query_methods.rb +101 -95
  180. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  181. data/lib/active_record/relation/spawn_methods.rb +3 -1
  182. data/lib/active_record/relation/where_clause.rb +65 -67
  183. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  184. data/lib/active_record/relation.rb +99 -202
  185. data/lib/active_record/result.rb +2 -0
  186. data/lib/active_record/runtime_registry.rb +2 -0
  187. data/lib/active_record/sanitization.rb +129 -121
  188. data/lib/active_record/schema.rb +4 -2
  189. data/lib/active_record/schema_dumper.rb +36 -26
  190. data/lib/active_record/schema_migration.rb +2 -0
  191. data/lib/active_record/scoping/default.rb +10 -7
  192. data/lib/active_record/scoping/named.rb +38 -12
  193. data/lib/active_record/scoping.rb +12 -10
  194. data/lib/active_record/secure_token.rb +2 -0
  195. data/lib/active_record/serialization.rb +2 -0
  196. data/lib/active_record/statement_cache.rb +22 -12
  197. data/lib/active_record/store.rb +3 -1
  198. data/lib/active_record/suppressor.rb +2 -0
  199. data/lib/active_record/table_metadata.rb +12 -3
  200. data/lib/active_record/tasks/database_tasks.rb +37 -25
  201. data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
  202. data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
  203. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  204. data/lib/active_record/timestamp.rb +5 -5
  205. data/lib/active_record/touch_later.rb +2 -0
  206. data/lib/active_record/transactions.rb +9 -7
  207. data/lib/active_record/translation.rb +2 -0
  208. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  209. data/lib/active_record/type/date.rb +2 -0
  210. data/lib/active_record/type/date_time.rb +2 -0
  211. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  212. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  213. data/lib/active_record/type/internal/timezone.rb +2 -0
  214. data/lib/active_record/type/json.rb +30 -0
  215. data/lib/active_record/type/serialized.rb +2 -0
  216. data/lib/active_record/type/text.rb +2 -0
  217. data/lib/active_record/type/time.rb +2 -0
  218. data/lib/active_record/type/type_map.rb +2 -0
  219. data/lib/active_record/type/unsigned_integer.rb +2 -0
  220. data/lib/active_record/type.rb +4 -1
  221. data/lib/active_record/type_caster/connection.rb +2 -0
  222. data/lib/active_record/type_caster/map.rb +3 -1
  223. data/lib/active_record/type_caster.rb +2 -0
  224. data/lib/active_record/validations/absence.rb +2 -0
  225. data/lib/active_record/validations/associated.rb +2 -0
  226. data/lib/active_record/validations/length.rb +2 -0
  227. data/lib/active_record/validations/presence.rb +2 -0
  228. data/lib/active_record/validations/uniqueness.rb +35 -5
  229. data/lib/active_record/validations.rb +2 -0
  230. data/lib/active_record/version.rb +2 -0
  231. data/lib/active_record.rb +11 -4
  232. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  233. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  234. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  235. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  236. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  237. data/lib/rails/generators/active_record/migration.rb +2 -0
  238. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  239. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  241. data/lib/rails/generators/active_record.rb +3 -1
  242. metadata +25 -37
  243. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  244. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  245. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  246. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  247. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  248. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  249. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  250. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  251. data/lib/active_record/attribute.rb +0 -240
  252. data/lib/active_record/attribute_mutation_tracker.rb +0 -113
  253. data/lib/active_record/attribute_set/builder.rb +0 -124
  254. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  255. data/lib/active_record/attribute_set.rb +0 -113
  256. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  257. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  258. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  259. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  260. data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,10 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  class TransactionState
4
- VALID_STATES = Set.new([:committed, :rolledback, nil])
5
-
6
6
  def initialize(state = nil)
7
7
  @state = state
8
+ @children = []
9
+ end
10
+
11
+ def add_child(state)
12
+ @children << state
8
13
  end
9
14
 
10
15
  def finalized?
@@ -19,15 +24,43 @@ module ActiveRecord
19
24
  @state == :rolledback
20
25
  end
21
26
 
27
+ def fully_completed?
28
+ completed?
29
+ end
30
+
22
31
  def completed?
23
32
  committed? || rolledback?
24
33
  end
25
34
 
26
35
  def set_state(state)
27
- unless VALID_STATES.include?(state)
36
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
37
+ The set_state method is deprecated and will be removed in
38
+ Rails 6.0. Please use rollback! or commit! to set transaction
39
+ state directly.
40
+ MSG
41
+ case state
42
+ when :rolledback
43
+ rollback!
44
+ when :committed
45
+ commit!
46
+ when nil
47
+ nullify!
48
+ else
28
49
  raise ArgumentError, "Invalid transaction state: #{state}"
29
50
  end
30
- @state = state
51
+ end
52
+
53
+ def rollback!
54
+ @children.each { |c| c.rollback! }
55
+ @state = :rolledback
56
+ end
57
+
58
+ def commit!
59
+ @state = :committed
60
+ end
61
+
62
+ def nullify!
63
+ @state = nil
31
64
  end
32
65
  end
33
66
 
@@ -57,7 +90,7 @@ module ActiveRecord
57
90
  end
58
91
 
59
92
  def rollback
60
- @state.set_state(:rolledback)
93
+ @state.rollback!
61
94
  end
62
95
 
63
96
  def rollback_records
@@ -72,7 +105,7 @@ module ActiveRecord
72
105
  end
73
106
 
74
107
  def commit
75
- @state.set_state(:committed)
108
+ @state.commit!
76
109
  end
77
110
 
78
111
  def before_commit_records
@@ -100,8 +133,11 @@ module ActiveRecord
100
133
  end
101
134
 
102
135
  class SavepointTransaction < Transaction
103
- def initialize(connection, savepoint_name, options, *args)
136
+ def initialize(connection, savepoint_name, parent_transaction, options, *args)
104
137
  super(connection, options, *args)
138
+
139
+ parent_transaction.state.add_child(@state)
140
+
105
141
  if options[:isolation]
106
142
  raise ActiveRecord::TransactionIsolationError, "cannot set transaction isolation in a nested transaction"
107
143
  end
@@ -155,7 +191,7 @@ module ActiveRecord
155
191
  if @stack.empty?
156
192
  RealTransaction.new(@connection, options, run_commit_callbacks: run_commit_callbacks)
157
193
  else
158
- SavepointTransaction.new(@connection, "active_record_#{@stack.size}", options,
194
+ SavepointTransaction.new(@connection, "active_record_#{@stack.size}", @stack.last, options,
159
195
  run_commit_callbacks: run_commit_callbacks)
160
196
  end
161
197
 
@@ -204,7 +240,7 @@ module ActiveRecord
204
240
  rollback_transaction if transaction
205
241
  else
206
242
  begin
207
- commit_transaction
243
+ commit_transaction if transaction
208
244
  rescue Exception
209
245
  rollback_transaction(transaction) unless transaction.state.completed?
210
246
  raise
@@ -1,11 +1,15 @@
1
- require "active_record/type"
1
+ # frozen_string_literal: true
2
+
2
3
  require "active_record/connection_adapters/determine_if_preparable_visitor"
3
4
  require "active_record/connection_adapters/schema_cache"
4
5
  require "active_record/connection_adapters/sql_type_metadata"
5
6
  require "active_record/connection_adapters/abstract/schema_dumper"
6
7
  require "active_record/connection_adapters/abstract/schema_creation"
8
+ require "active_support/concurrency/load_interlock_aware_monitor"
7
9
  require "arel/collectors/bind"
10
+ require "arel/collectors/composite"
8
11
  require "arel/collectors/sql_string"
12
+ require "arel/collectors/substitute_binds"
9
13
 
10
14
  module ActiveRecord
11
15
  module ConnectionAdapters # :nodoc:
@@ -68,7 +72,6 @@ module ActiveRecord
68
72
  include Quoting, DatabaseStatements, SchemaStatements
69
73
  include DatabaseLimits
70
74
  include QueryCache
71
- include ColumnDumper
72
75
  include Savepoints
73
76
 
74
77
  SIMPLE_INT = /\A\d+\z/
@@ -102,10 +105,11 @@ module ActiveRecord
102
105
  @logger = logger
103
106
  @config = config
104
107
  @pool = nil
108
+ @idle_since = Concurrent.monotonic_time
105
109
  @schema_cache = SchemaCache.new self
106
110
  @quoted_column_names, @quoted_table_names = {}, {}
107
111
  @visitor = arel_visitor
108
- @lock = Monitor.new
112
+ @lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
109
113
 
110
114
  if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
111
115
  @prepared_statements = true
@@ -115,6 +119,14 @@ module ActiveRecord
115
119
  end
116
120
  end
117
121
 
122
+ def migrations_paths
123
+ @config[:migrations_paths] || Migrator.migrations_paths
124
+ end
125
+
126
+ def migration_context
127
+ MigrationContext.new(migrations_paths)
128
+ end
129
+
118
130
  class Version
119
131
  include Comparable
120
132
 
@@ -127,51 +139,14 @@ module ActiveRecord
127
139
  end
128
140
  end
129
141
 
130
- class BindCollector < Arel::Collectors::Bind
131
- def compile(bvs, conn)
132
- casted_binds = bvs.map(&:value_for_database)
133
- super(casted_binds.map { |value| conn.quote(value) })
134
- end
135
- end
136
-
137
- class SQLString < Arel::Collectors::SQLString
138
- def compile(bvs, conn)
139
- super(bvs)
140
- end
141
- end
142
-
143
- def collector
144
- if prepared_statements
145
- SQLString.new
146
- else
147
- BindCollector.new
148
- end
149
- end
150
-
151
- def arel_visitor # :nodoc:
152
- Arel::Visitors::ToSql.new(self)
153
- end
154
-
155
142
  def valid_type?(type) # :nodoc:
156
143
  !native_database_types[type].nil?
157
144
  end
158
145
 
159
- def schema_creation
160
- SchemaCreation.new self
161
- end
162
-
163
- # Returns an array of +Column+ objects for the table specified by +table_name+.
164
- def columns(table_name) # :nodoc:
165
- table_name = table_name.to_s
166
- column_definitions(table_name).map do |field|
167
- new_column_from_field(table_name, field)
168
- end
169
- end
170
-
171
146
  # this method must only be called while holding connection pool's mutex
172
147
  def lease
173
148
  if in_use?
174
- msg = "Cannot lease connection, "
149
+ msg = "Cannot lease connection, ".dup
175
150
  if @owner == Thread.current
176
151
  msg << "it is already leased by the current thread."
177
152
  else
@@ -198,6 +173,7 @@ module ActiveRecord
198
173
  "Current thread: #{Thread.current}."
199
174
  end
200
175
 
176
+ @idle_since = Concurrent.monotonic_time
201
177
  @owner = nil
202
178
  else
203
179
  raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
@@ -217,6 +193,12 @@ module ActiveRecord
217
193
  end
218
194
  end
219
195
 
196
+ # Seconds since this connection was returned to the pool
197
+ def seconds_idle # :nodoc:
198
+ return 0 if in_use?
199
+ Concurrent.monotonic_time - @idle_since
200
+ end
201
+
220
202
  def unprepared_statement
221
203
  old_prepared_statements, @prepared_statements = @prepared_statements, false
222
204
  yield
@@ -230,16 +212,6 @@ module ActiveRecord
230
212
  self.class::ADAPTER_NAME
231
213
  end
232
214
 
233
- def supports_migrations? # :nodoc:
234
- true
235
- end
236
- deprecate :supports_migrations?
237
-
238
- def supports_primary_key? # :nodoc:
239
- true
240
- end
241
- deprecate :supports_primary_key?
242
-
243
215
  # Does this adapter support DDL rollbacks in transactions? That is, would
244
216
  # CREATE TABLE or ALTER TABLE get rolled back by a transaction?
245
217
  def supports_ddl_transactions?
@@ -308,6 +280,11 @@ module ActiveRecord
308
280
  false
309
281
  end
310
282
 
283
+ # Does this adapter support creating invalid constraints?
284
+ def supports_validate_constraints?
285
+ false
286
+ end
287
+
311
288
  # Does this adapter support creating foreign key constraints
312
289
  # in the same statement as creating the table?
313
290
  def supports_foreign_keys_in_create?
@@ -349,6 +326,11 @@ module ActiveRecord
349
326
  false
350
327
  end
351
328
 
329
+ # Does this adapter support foreign/external tables?
330
+ def supports_foreign_tables?
331
+ false
332
+ end
333
+
352
334
  # This is meant to be implemented by the adapters that support extensions
353
335
  def disable_extension(name)
354
336
  end
@@ -411,6 +393,19 @@ module ActiveRecord
411
393
  reset_transaction
412
394
  end
413
395
 
396
+ # Immediately forget this connection ever existed. Unlike disconnect!,
397
+ # this will not communicate with the server.
398
+ #
399
+ # After calling this method, the behavior of all other methods becomes
400
+ # undefined. This is called internally just before a forked process gets
401
+ # rid of a connection that belonged to its parent.
402
+ def discard!
403
+ # This should be overridden by concrete adapters.
404
+ #
405
+ # Prevent @connection's finalizer from touching the socket, or
406
+ # otherwise communicating with its server, when it is collected.
407
+ end
408
+
414
409
  # Reset the state of this connection, directing the DBMS to clear
415
410
  # transactions and other connection-related server-side state. Usually a
416
411
  # database-dependent operation.
@@ -436,10 +431,7 @@ module ActiveRecord
436
431
  # Checks whether the connection to the database is still active (i.e. not stale).
437
432
  # This is done under the hood by calling #active?. If the connection
438
433
  # is no longer active, then this method will reconnect to the database.
439
- def verify!(*ignored)
440
- if ignored.size > 0
441
- ActiveSupport::Deprecation.warn("Passing arguments to #verify method of the connection has no effect and has been deprecated. Please remove all arguments from the #verify method call.")
442
- end
434
+ def verify!
443
435
  reconnect! unless active?
444
436
  end
445
437
 
@@ -475,40 +467,12 @@ module ActiveRecord
475
467
  pool.checkin self
476
468
  end
477
469
 
478
- def type_map # :nodoc:
479
- @type_map ||= Type::TypeMap.new.tap do |mapping|
480
- initialize_type_map(mapping)
481
- end
482
- end
483
-
484
- def new_column(name, default, sql_type_metadata, null, table_name, default_function = nil, collation = nil) # :nodoc:
485
- Column.new(name, default, sql_type_metadata, null, table_name, default_function, collation)
486
- end
487
-
488
- def lookup_cast_type(sql_type) # :nodoc:
489
- type_map.lookup(sql_type)
470
+ def column_name_for_operation(operation, node) # :nodoc:
471
+ column_name_from_arel_node(node)
490
472
  end
491
473
 
492
- def column_name_for_operation(operation, node) # :nodoc:
493
- visitor.accept(node, collector).value
494
- end
495
-
496
- def combine_bind_parameters(
497
- from_clause: [],
498
- join_clause: [],
499
- where_clause: [],
500
- having_clause: [],
501
- limit: nil,
502
- offset: nil
503
- ) # :nodoc:
504
- result = from_clause + join_clause + where_clause + having_clause
505
- if limit
506
- result << limit
507
- end
508
- if offset
509
- result << offset
510
- end
511
- result
474
+ def column_name_from_arel_node(node) # :nodoc:
475
+ visitor.accept(node, Arel::Collectors::SQLString.new).value
512
476
  end
513
477
 
514
478
  def default_index_type?(index) # :nodoc:
@@ -516,8 +480,13 @@ module ActiveRecord
516
480
  end
517
481
 
518
482
  private
483
+ def type_map
484
+ @type_map ||= Type::TypeMap.new.tap do |mapping|
485
+ initialize_type_map(mapping)
486
+ end
487
+ end
519
488
 
520
- def initialize_type_map(m)
489
+ def initialize_type_map(m = type_map)
521
490
  register_class_with_limit m, %r(boolean)i, Type::Boolean
522
491
  register_class_with_limit m, %r(char)i, Type::String
523
492
  register_class_with_limit m, %r(binary)i, Type::Binary
@@ -535,6 +504,8 @@ module ActiveRecord
535
504
  m.alias_type %r(number)i, "decimal"
536
505
  m.alias_type %r(double)i, "float"
537
506
 
507
+ m.register_type %r(^json)i, Type::Json.new
508
+
538
509
  m.register_type(%r(decimal)i) do |sql_type|
539
510
  scale = extract_scale(sql_type)
540
511
  precision = extract_precision(sql_type)
@@ -550,7 +521,7 @@ module ActiveRecord
550
521
 
551
522
  def reload_type_map
552
523
  type_map.clear
553
- initialize_type_map(type_map)
524
+ initialize_type_map
554
525
  end
555
526
 
556
527
  def register_class_with_limit(mapping, key, klass)
@@ -579,12 +550,7 @@ module ActiveRecord
579
550
  end
580
551
 
581
552
  def extract_limit(sql_type)
582
- case sql_type
583
- when /^bigint/i
584
- 8
585
- when /\((.*)\)/
586
- $1.to_i
587
- end
553
+ $1.to_i if sql_type =~ /\((.*)\)/
588
554
  end
589
555
 
590
556
  def translate_exception_class(e, sql)
@@ -608,12 +574,14 @@ module ActiveRecord
608
574
  type_casted_binds: type_casted_binds,
609
575
  statement_name: statement_name,
610
576
  connection_id: object_id) do
577
+ begin
611
578
  @lock.synchronize do
612
579
  yield
613
580
  end
581
+ rescue => e
582
+ raise translate_exception_class(e, sql)
614
583
  end
615
- rescue => e
616
- raise translate_exception_class(e, sql)
584
+ end
617
585
  end
618
586
 
619
587
  def translate_exception(exception, message)
@@ -635,6 +603,24 @@ module ActiveRecord
635
603
  columns(table_name).detect { |c| c.name == column_name } ||
636
604
  raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}")
637
605
  end
606
+
607
+ def collector
608
+ if prepared_statements
609
+ Arel::Collectors::Composite.new(
610
+ Arel::Collectors::SQLString.new,
611
+ Arel::Collectors::Bind.new,
612
+ )
613
+ else
614
+ Arel::Collectors::SubstituteBinds.new(
615
+ self,
616
+ Arel::Collectors::SQLString.new,
617
+ )
618
+ end
619
+ end
620
+
621
+ def arel_visitor
622
+ Arel::Visitors::ToSql.new(self)
623
+ end
638
624
  end
639
625
  end
640
626
  end