activerecord 5.2.3

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 (244) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +937 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +217 -0
  5. data/examples/performance.rb +185 -0
  6. data/examples/simple.rb +15 -0
  7. data/lib/active_record.rb +188 -0
  8. data/lib/active_record/aggregations.rb +283 -0
  9. data/lib/active_record/association_relation.rb +40 -0
  10. data/lib/active_record/associations.rb +1860 -0
  11. data/lib/active_record/associations/alias_tracker.rb +81 -0
  12. data/lib/active_record/associations/association.rb +299 -0
  13. data/lib/active_record/associations/association_scope.rb +168 -0
  14. data/lib/active_record/associations/belongs_to_association.rb +130 -0
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
  16. data/lib/active_record/associations/builder/association.rb +140 -0
  17. data/lib/active_record/associations/builder/belongs_to.rb +163 -0
  18. data/lib/active_record/associations/builder/collection_association.rb +82 -0
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +135 -0
  20. data/lib/active_record/associations/builder/has_many.rb +17 -0
  21. data/lib/active_record/associations/builder/has_one.rb +30 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +42 -0
  23. data/lib/active_record/associations/collection_association.rb +513 -0
  24. data/lib/active_record/associations/collection_proxy.rb +1131 -0
  25. data/lib/active_record/associations/foreign_association.rb +13 -0
  26. data/lib/active_record/associations/has_many_association.rb +144 -0
  27. data/lib/active_record/associations/has_many_through_association.rb +227 -0
  28. data/lib/active_record/associations/has_one_association.rb +120 -0
  29. data/lib/active_record/associations/has_one_through_association.rb +45 -0
  30. data/lib/active_record/associations/join_dependency.rb +262 -0
  31. data/lib/active_record/associations/join_dependency/join_association.rb +60 -0
  32. data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
  33. data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
  34. data/lib/active_record/associations/preloader.rb +193 -0
  35. data/lib/active_record/associations/preloader/association.rb +131 -0
  36. data/lib/active_record/associations/preloader/through_association.rb +107 -0
  37. data/lib/active_record/associations/singular_association.rb +73 -0
  38. data/lib/active_record/associations/through_association.rb +121 -0
  39. data/lib/active_record/attribute_assignment.rb +88 -0
  40. data/lib/active_record/attribute_decorators.rb +90 -0
  41. data/lib/active_record/attribute_methods.rb +492 -0
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +78 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +150 -0
  44. data/lib/active_record/attribute_methods/primary_key.rb +143 -0
  45. data/lib/active_record/attribute_methods/query.rb +42 -0
  46. data/lib/active_record/attribute_methods/read.rb +85 -0
  47. data/lib/active_record/attribute_methods/serialization.rb +90 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
  49. data/lib/active_record/attribute_methods/write.rb +68 -0
  50. data/lib/active_record/attributes.rb +266 -0
  51. data/lib/active_record/autosave_association.rb +498 -0
  52. data/lib/active_record/base.rb +329 -0
  53. data/lib/active_record/callbacks.rb +353 -0
  54. data/lib/active_record/coders/json.rb +15 -0
  55. data/lib/active_record/coders/yaml_column.rb +50 -0
  56. data/lib/active_record/collection_cache_key.rb +53 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1068 -0
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +72 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +540 -0
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +145 -0
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +200 -0
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +685 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1396 -0
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +628 -0
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +887 -0
  70. data/lib/active_record/connection_adapters/column.rb +91 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
  73. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -0
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +863 -0
  118. data/lib/active_record/connection_adapters/schema_cache.rb +118 -0
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +573 -0
  127. data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
  128. data/lib/active_record/connection_handling.rb +145 -0
  129. data/lib/active_record/core.rb +559 -0
  130. data/lib/active_record/counter_cache.rb +218 -0
  131. data/lib/active_record/define_callbacks.rb +22 -0
  132. data/lib/active_record/dynamic_matchers.rb +122 -0
  133. data/lib/active_record/enum.rb +244 -0
  134. data/lib/active_record/errors.rb +380 -0
  135. data/lib/active_record/explain.rb +50 -0
  136. data/lib/active_record/explain_registry.rb +32 -0
  137. data/lib/active_record/explain_subscriber.rb +34 -0
  138. data/lib/active_record/fixture_set/file.rb +82 -0
  139. data/lib/active_record/fixtures.rb +1065 -0
  140. data/lib/active_record/gem_version.rb +17 -0
  141. data/lib/active_record/inheritance.rb +283 -0
  142. data/lib/active_record/integration.rb +155 -0
  143. data/lib/active_record/internal_metadata.rb +45 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  145. data/lib/active_record/locale/en.yml +48 -0
  146. data/lib/active_record/locking/optimistic.rb +198 -0
  147. data/lib/active_record/locking/pessimistic.rb +89 -0
  148. data/lib/active_record/log_subscriber.rb +137 -0
  149. data/lib/active_record/migration.rb +1378 -0
  150. data/lib/active_record/migration/command_recorder.rb +240 -0
  151. data/lib/active_record/migration/compatibility.rb +217 -0
  152. data/lib/active_record/migration/join_table.rb +17 -0
  153. data/lib/active_record/model_schema.rb +521 -0
  154. data/lib/active_record/nested_attributes.rb +600 -0
  155. data/lib/active_record/no_touching.rb +58 -0
  156. data/lib/active_record/null_relation.rb +68 -0
  157. data/lib/active_record/persistence.rb +763 -0
  158. data/lib/active_record/query_cache.rb +45 -0
  159. data/lib/active_record/querying.rb +70 -0
  160. data/lib/active_record/railtie.rb +226 -0
  161. data/lib/active_record/railties/console_sandbox.rb +7 -0
  162. data/lib/active_record/railties/controller_runtime.rb +56 -0
  163. data/lib/active_record/railties/databases.rake +377 -0
  164. data/lib/active_record/readonly_attributes.rb +24 -0
  165. data/lib/active_record/reflection.rb +1044 -0
  166. data/lib/active_record/relation.rb +629 -0
  167. data/lib/active_record/relation/batches.rb +287 -0
  168. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  169. data/lib/active_record/relation/calculations.rb +417 -0
  170. data/lib/active_record/relation/delegation.rb +147 -0
  171. data/lib/active_record/relation/finder_methods.rb +565 -0
  172. data/lib/active_record/relation/from_clause.rb +26 -0
  173. data/lib/active_record/relation/merger.rb +193 -0
  174. data/lib/active_record/relation/predicate_builder.rb +152 -0
  175. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  176. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  177. data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
  178. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
  179. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  180. data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
  181. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  182. data/lib/active_record/relation/query_attribute.rb +45 -0
  183. data/lib/active_record/relation/query_methods.rb +1231 -0
  184. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  185. data/lib/active_record/relation/spawn_methods.rb +77 -0
  186. data/lib/active_record/relation/where_clause.rb +186 -0
  187. data/lib/active_record/relation/where_clause_factory.rb +34 -0
  188. data/lib/active_record/result.rb +149 -0
  189. data/lib/active_record/runtime_registry.rb +24 -0
  190. data/lib/active_record/sanitization.rb +222 -0
  191. data/lib/active_record/schema.rb +70 -0
  192. data/lib/active_record/schema_dumper.rb +255 -0
  193. data/lib/active_record/schema_migration.rb +56 -0
  194. data/lib/active_record/scoping.rb +106 -0
  195. data/lib/active_record/scoping/default.rb +152 -0
  196. data/lib/active_record/scoping/named.rb +213 -0
  197. data/lib/active_record/secure_token.rb +40 -0
  198. data/lib/active_record/serialization.rb +22 -0
  199. data/lib/active_record/statement_cache.rb +121 -0
  200. data/lib/active_record/store.rb +211 -0
  201. data/lib/active_record/suppressor.rb +61 -0
  202. data/lib/active_record/table_metadata.rb +82 -0
  203. data/lib/active_record/tasks/database_tasks.rb +337 -0
  204. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  205. data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
  206. data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
  207. data/lib/active_record/timestamp.rb +153 -0
  208. data/lib/active_record/touch_later.rb +64 -0
  209. data/lib/active_record/transactions.rb +502 -0
  210. data/lib/active_record/translation.rb +24 -0
  211. data/lib/active_record/type.rb +79 -0
  212. data/lib/active_record/type/adapter_specific_registry.rb +136 -0
  213. data/lib/active_record/type/date.rb +9 -0
  214. data/lib/active_record/type/date_time.rb +9 -0
  215. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  216. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  217. data/lib/active_record/type/internal/timezone.rb +17 -0
  218. data/lib/active_record/type/json.rb +30 -0
  219. data/lib/active_record/type/serialized.rb +71 -0
  220. data/lib/active_record/type/text.rb +11 -0
  221. data/lib/active_record/type/time.rb +21 -0
  222. data/lib/active_record/type/type_map.rb +62 -0
  223. data/lib/active_record/type/unsigned_integer.rb +17 -0
  224. data/lib/active_record/type_caster.rb +9 -0
  225. data/lib/active_record/type_caster/connection.rb +33 -0
  226. data/lib/active_record/type_caster/map.rb +23 -0
  227. data/lib/active_record/validations.rb +93 -0
  228. data/lib/active_record/validations/absence.rb +25 -0
  229. data/lib/active_record/validations/associated.rb +60 -0
  230. data/lib/active_record/validations/length.rb +26 -0
  231. data/lib/active_record/validations/presence.rb +68 -0
  232. data/lib/active_record/validations/uniqueness.rb +238 -0
  233. data/lib/active_record/version.rb +10 -0
  234. data/lib/rails/generators/active_record.rb +19 -0
  235. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  236. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  237. data/lib/rails/generators/active_record/migration.rb +35 -0
  238. data/lib/rails/generators/active_record/migration/migration_generator.rb +78 -0
  239. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  240. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
  241. data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
  242. data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
  243. data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
  244. metadata +333 -0
@@ -0,0 +1,380 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # = Active Record Errors
5
+ #
6
+ # Generic Active Record exception class.
7
+ class ActiveRecordError < StandardError
8
+ end
9
+
10
+ # Raised when the single-table inheritance mechanism fails to locate the subclass
11
+ # (for example due to improper usage of column that
12
+ # {ActiveRecord::Base.inheritance_column}[rdoc-ref:ModelSchema::ClassMethods#inheritance_column]
13
+ # points to).
14
+ class SubclassNotFound < ActiveRecordError
15
+ end
16
+
17
+ # Raised when an object assigned to an association has an incorrect type.
18
+ #
19
+ # class Ticket < ActiveRecord::Base
20
+ # has_many :patches
21
+ # end
22
+ #
23
+ # class Patch < ActiveRecord::Base
24
+ # belongs_to :ticket
25
+ # end
26
+ #
27
+ # # Comments are not patches, this assignment raises AssociationTypeMismatch.
28
+ # @ticket.patches << Comment.new(content: "Please attach tests to your patch.")
29
+ class AssociationTypeMismatch < ActiveRecordError
30
+ end
31
+
32
+ # Raised when unserialized object's type mismatches one specified for serializable field.
33
+ class SerializationTypeMismatch < ActiveRecordError
34
+ end
35
+
36
+ # Raised when adapter not specified on connection (or configuration file
37
+ # +config/database.yml+ misses adapter field).
38
+ class AdapterNotSpecified < ActiveRecordError
39
+ end
40
+
41
+ # Raised when Active Record cannot find database adapter specified in
42
+ # +config/database.yml+ or programmatically.
43
+ class AdapterNotFound < ActiveRecordError
44
+ end
45
+
46
+ # Raised when connection to the database could not been established (for example when
47
+ # {ActiveRecord::Base.connection=}[rdoc-ref:ConnectionHandling#connection]
48
+ # is given a +nil+ object).
49
+ class ConnectionNotEstablished < ActiveRecordError
50
+ end
51
+
52
+ # Raised when Active Record cannot find a record by given id or set of ids.
53
+ class RecordNotFound < ActiveRecordError
54
+ attr_reader :model, :primary_key, :id
55
+
56
+ def initialize(message = nil, model = nil, primary_key = nil, id = nil)
57
+ @primary_key = primary_key
58
+ @model = model
59
+ @id = id
60
+
61
+ super(message)
62
+ end
63
+ end
64
+
65
+ # Raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
66
+ # {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!]
67
+ # methods when a record is invalid and can not be saved.
68
+ class RecordNotSaved < ActiveRecordError
69
+ attr_reader :record
70
+
71
+ def initialize(message = nil, record = nil)
72
+ @record = record
73
+ super(message)
74
+ end
75
+ end
76
+
77
+ # Raised by {ActiveRecord::Base#destroy!}[rdoc-ref:Persistence#destroy!]
78
+ # when a call to {#destroy}[rdoc-ref:Persistence#destroy!]
79
+ # would return false.
80
+ #
81
+ # begin
82
+ # complex_operation_that_internally_calls_destroy!
83
+ # rescue ActiveRecord::RecordNotDestroyed => invalid
84
+ # puts invalid.record.errors
85
+ # end
86
+ #
87
+ class RecordNotDestroyed < ActiveRecordError
88
+ attr_reader :record
89
+
90
+ def initialize(message = nil, record = nil)
91
+ @record = record
92
+ super(message)
93
+ end
94
+ end
95
+
96
+ # Superclass for all database execution errors.
97
+ #
98
+ # Wraps the underlying database error as +cause+.
99
+ class StatementInvalid < ActiveRecordError
100
+ def initialize(message = nil)
101
+ super(message || $!.try(:message))
102
+ end
103
+ end
104
+
105
+ # Defunct wrapper class kept for compatibility.
106
+ # StatementInvalid wraps the original exception now.
107
+ class WrappedDatabaseException < StatementInvalid
108
+ end
109
+
110
+ # Raised when a record cannot be inserted or updated because it would violate a uniqueness constraint.
111
+ class RecordNotUnique < WrappedDatabaseException
112
+ end
113
+
114
+ # Raised when a record cannot be inserted or updated because it references a non-existent record.
115
+ class InvalidForeignKey < WrappedDatabaseException
116
+ end
117
+
118
+ # Raised when a foreign key constraint cannot be added because the column type does not match the referenced column type.
119
+ class MismatchedForeignKey < StatementInvalid
120
+ def initialize(
121
+ adapter = nil,
122
+ message: nil,
123
+ sql: nil,
124
+ binds: nil,
125
+ table: nil,
126
+ foreign_key: nil,
127
+ target_table: nil,
128
+ primary_key: nil,
129
+ primary_key_column: nil
130
+ )
131
+ if table
132
+ type = primary_key_column.bigint? ? :bigint : primary_key_column.type
133
+ msg = <<-EOM.squish
134
+ Column `#{foreign_key}` on table `#{table}` does not match column `#{primary_key}` on `#{target_table}`,
135
+ which has type `#{primary_key_column.sql_type}`.
136
+ To resolve this issue, change the type of the `#{foreign_key}` column on `#{table}` to be :#{type}.
137
+ (For example `t.#{type} :#{foreign_key}`).
138
+ EOM
139
+ else
140
+ msg = <<-EOM.squish
141
+ There is a mismatch between the foreign key and primary key column types.
142
+ Verify that the foreign key column type and the primary key of the associated table match types.
143
+ EOM
144
+ end
145
+ if message
146
+ msg << "\nOriginal message: #{message}"
147
+ end
148
+ super(msg)
149
+ end
150
+ end
151
+
152
+ # Raised when a record cannot be inserted or updated because it would violate a not null constraint.
153
+ class NotNullViolation < StatementInvalid
154
+ end
155
+
156
+ # Raised when a record cannot be inserted or updated because a value too long for a column type.
157
+ class ValueTooLong < StatementInvalid
158
+ end
159
+
160
+ # Raised when values that executed are out of range.
161
+ class RangeError < StatementInvalid
162
+ end
163
+
164
+ # Raised when number of bind variables in statement given to +:condition+ key
165
+ # (for example, when using {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] method)
166
+ # does not match number of expected values supplied.
167
+ #
168
+ # For example, when there are two placeholders with only one value supplied:
169
+ #
170
+ # Location.where("lat = ? AND lng = ?", 53.7362)
171
+ class PreparedStatementInvalid < ActiveRecordError
172
+ end
173
+
174
+ # Raised when a given database does not exist.
175
+ class NoDatabaseError < StatementInvalid
176
+ end
177
+
178
+ # Raised when PostgreSQL returns 'cached plan must not change result type' and
179
+ # we cannot retry gracefully (e.g. inside a transaction)
180
+ class PreparedStatementCacheExpired < StatementInvalid
181
+ end
182
+
183
+ # Raised on attempt to save stale record. Record is stale when it's being saved in another query after
184
+ # instantiation, for example, when two users edit the same wiki page and one starts editing and saves
185
+ # the page before the other.
186
+ #
187
+ # Read more about optimistic locking in ActiveRecord::Locking module
188
+ # documentation.
189
+ class StaleObjectError < ActiveRecordError
190
+ attr_reader :record, :attempted_action
191
+
192
+ def initialize(record = nil, attempted_action = nil)
193
+ if record && attempted_action
194
+ @record = record
195
+ @attempted_action = attempted_action
196
+ super("Attempted to #{attempted_action} a stale object: #{record.class.name}.")
197
+ else
198
+ super("Stale object error.")
199
+ end
200
+ end
201
+ end
202
+
203
+ # Raised when association is being configured improperly or user tries to use
204
+ # offset and limit together with
205
+ # {ActiveRecord::Base.has_many}[rdoc-ref:Associations::ClassMethods#has_many] or
206
+ # {ActiveRecord::Base.has_and_belongs_to_many}[rdoc-ref:Associations::ClassMethods#has_and_belongs_to_many]
207
+ # associations.
208
+ class ConfigurationError < ActiveRecordError
209
+ end
210
+
211
+ # Raised on attempt to update record that is instantiated as read only.
212
+ class ReadOnlyRecord < ActiveRecordError
213
+ end
214
+
215
+ # {ActiveRecord::Base.transaction}[rdoc-ref:Transactions::ClassMethods#transaction]
216
+ # uses this exception to distinguish a deliberate rollback from other exceptional situations.
217
+ # Normally, raising an exception will cause the
218
+ # {.transaction}[rdoc-ref:Transactions::ClassMethods#transaction] method to rollback
219
+ # the database transaction *and* pass on the exception. But if you raise an
220
+ # ActiveRecord::Rollback exception, then the database transaction will be rolled back,
221
+ # without passing on the exception.
222
+ #
223
+ # For example, you could do this in your controller to rollback a transaction:
224
+ #
225
+ # class BooksController < ActionController::Base
226
+ # def create
227
+ # Book.transaction do
228
+ # book = Book.new(params[:book])
229
+ # book.save!
230
+ # if today_is_friday?
231
+ # # The system must fail on Friday so that our support department
232
+ # # won't be out of job. We silently rollback this transaction
233
+ # # without telling the user.
234
+ # raise ActiveRecord::Rollback, "Call tech support!"
235
+ # end
236
+ # end
237
+ # # ActiveRecord::Rollback is the only exception that won't be passed on
238
+ # # by ActiveRecord::Base.transaction, so this line will still be reached
239
+ # # even on Friday.
240
+ # redirect_to root_url
241
+ # end
242
+ # end
243
+ class Rollback < ActiveRecordError
244
+ end
245
+
246
+ # Raised when attribute has a name reserved by Active Record (when attribute
247
+ # has name of one of Active Record instance methods).
248
+ class DangerousAttributeError < ActiveRecordError
249
+ end
250
+
251
+ # Raised when unknown attributes are supplied via mass assignment.
252
+ UnknownAttributeError = ActiveModel::UnknownAttributeError
253
+
254
+ # Raised when an error occurred while doing a mass assignment to an attribute through the
255
+ # {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
256
+ # The exception has an +attribute+ property that is the name of the offending attribute.
257
+ class AttributeAssignmentError < ActiveRecordError
258
+ attr_reader :exception, :attribute
259
+
260
+ def initialize(message = nil, exception = nil, attribute = nil)
261
+ super(message)
262
+ @exception = exception
263
+ @attribute = attribute
264
+ end
265
+ end
266
+
267
+ # Raised when there are multiple errors while doing a mass assignment through the
268
+ # {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=]
269
+ # method. The exception has an +errors+ property that contains an array of AttributeAssignmentError
270
+ # objects, each corresponding to the error while assigning to an attribute.
271
+ class MultiparameterAssignmentErrors < ActiveRecordError
272
+ attr_reader :errors
273
+
274
+ def initialize(errors = nil)
275
+ @errors = errors
276
+ end
277
+ end
278
+
279
+ # Raised when a primary key is needed, but not specified in the schema or model.
280
+ class UnknownPrimaryKey < ActiveRecordError
281
+ attr_reader :model
282
+
283
+ def initialize(model = nil, description = nil)
284
+ if model
285
+ message = "Unknown primary key for table #{model.table_name} in model #{model}."
286
+ message += "\n#{description}" if description
287
+ @model = model
288
+ super(message)
289
+ else
290
+ super("Unknown primary key.")
291
+ end
292
+ end
293
+ end
294
+
295
+ # Raised when a relation cannot be mutated because it's already loaded.
296
+ #
297
+ # class Task < ActiveRecord::Base
298
+ # end
299
+ #
300
+ # relation = Task.all
301
+ # relation.loaded? # => true
302
+ #
303
+ # # Methods which try to mutate a loaded relation fail.
304
+ # relation.where!(title: 'TODO') # => ActiveRecord::ImmutableRelation
305
+ # relation.limit!(5) # => ActiveRecord::ImmutableRelation
306
+ class ImmutableRelation < ActiveRecordError
307
+ end
308
+
309
+ # TransactionIsolationError will be raised under the following conditions:
310
+ #
311
+ # * The adapter does not support setting the isolation level
312
+ # * You are joining an existing open transaction
313
+ # * You are creating a nested (savepoint) transaction
314
+ #
315
+ # The mysql2 and postgresql adapters support setting the transaction isolation level.
316
+ class TransactionIsolationError < ActiveRecordError
317
+ end
318
+
319
+ # TransactionRollbackError will be raised when a transaction is rolled
320
+ # back by the database due to a serialization failure or a deadlock.
321
+ #
322
+ # See the following:
323
+ #
324
+ # * https://www.postgresql.org/docs/current/static/transaction-iso.html
325
+ # * https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html#error_er_lock_deadlock
326
+ class TransactionRollbackError < StatementInvalid
327
+ end
328
+
329
+ # SerializationFailure will be raised when a transaction is rolled
330
+ # back by the database due to a serialization failure.
331
+ class SerializationFailure < TransactionRollbackError
332
+ end
333
+
334
+ # Deadlocked will be raised when a transaction is rolled
335
+ # back by the database when a deadlock is encountered.
336
+ class Deadlocked < TransactionRollbackError
337
+ end
338
+
339
+ # IrreversibleOrderError is raised when a relation's order is too complex for
340
+ # +reverse_order+ to automatically reverse.
341
+ class IrreversibleOrderError < ActiveRecordError
342
+ end
343
+
344
+ # LockWaitTimeout will be raised when lock wait timeout exceeded.
345
+ class LockWaitTimeout < StatementInvalid
346
+ end
347
+
348
+ # StatementTimeout will be raised when statement timeout exceeded.
349
+ class StatementTimeout < StatementInvalid
350
+ end
351
+
352
+ # QueryCanceled will be raised when canceling statement due to user request.
353
+ class QueryCanceled < StatementInvalid
354
+ end
355
+
356
+ # UnknownAttributeReference is raised when an unknown and potentially unsafe
357
+ # value is passed to a query method when allow_unsafe_raw_sql is set to
358
+ # :disabled. For example, passing a non column name value to a relation's
359
+ # #order method might cause this exception.
360
+ #
361
+ # When working around this exception, caution should be taken to avoid SQL
362
+ # injection vulnerabilities when passing user-provided values to query
363
+ # methods. Known-safe values can be passed to query methods by wrapping them
364
+ # in Arel.sql.
365
+ #
366
+ # For example, with allow_unsafe_raw_sql set to :disabled, the following
367
+ # code would raise this exception:
368
+ #
369
+ # Post.order("length(title)").first
370
+ #
371
+ # The desired result can be accomplished by wrapping the known-safe string
372
+ # in Arel.sql:
373
+ #
374
+ # Post.order(Arel.sql("length(title)")).first
375
+ #
376
+ # Again, such a workaround should *not* be used when passing user-provided
377
+ # values, such as request parameters or model attributes to query methods.
378
+ class UnknownAttributeReference < ActiveRecordError
379
+ end
380
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/explain_registry"
4
+
5
+ module ActiveRecord
6
+ module Explain
7
+ # Executes the block with the collect flag enabled. Queries are collected
8
+ # asynchronously by the subscriber and returned.
9
+ def collecting_queries_for_explain # :nodoc:
10
+ ExplainRegistry.collect = true
11
+ yield
12
+ ExplainRegistry.queries
13
+ ensure
14
+ ExplainRegistry.reset
15
+ end
16
+
17
+ # Makes the adapter execute EXPLAIN for the tuples of queries and bindings.
18
+ # Returns a formatted string ready to be logged.
19
+ def exec_explain(queries) # :nodoc:
20
+ str = queries.map do |sql, binds|
21
+ msg = "EXPLAIN for: #{sql}".dup
22
+ unless binds.empty?
23
+ msg << " "
24
+ msg << binds.map { |attr| render_bind(attr) }.inspect
25
+ end
26
+ msg << "\n"
27
+ msg << connection.explain(sql, binds)
28
+ end.join("\n")
29
+
30
+ # Overriding inspect to be more human readable, especially in the console.
31
+ def str.inspect
32
+ self
33
+ end
34
+
35
+ str
36
+ end
37
+
38
+ private
39
+
40
+ def render_bind(attr)
41
+ value = if attr.type.binary? && attr.value
42
+ "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
43
+ else
44
+ connection.type_cast(attr.value_for_database)
45
+ end
46
+
47
+ [attr.name, value]
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/per_thread_registry"
4
+
5
+ module ActiveRecord
6
+ # This is a thread locals registry for EXPLAIN. For example
7
+ #
8
+ # ActiveRecord::ExplainRegistry.queries
9
+ #
10
+ # returns the collected queries local to the current thread.
11
+ #
12
+ # See the documentation of ActiveSupport::PerThreadRegistry
13
+ # for further details.
14
+ class ExplainRegistry # :nodoc:
15
+ extend ActiveSupport::PerThreadRegistry
16
+
17
+ attr_accessor :queries, :collect
18
+
19
+ def initialize
20
+ reset
21
+ end
22
+
23
+ def collect?
24
+ @collect
25
+ end
26
+
27
+ def reset
28
+ @collect = false
29
+ @queries = []
30
+ end
31
+ end
32
+ end