activerecord 5.1.5 → 5.2.1

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