activerecord 6.0.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 (340) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1086 -0
  3. data/MIT-LICENSE +22 -0
  4. data/README.rdoc +219 -0
  5. data/examples/performance.rb +185 -0
  6. data/examples/simple.rb +15 -0
  7. data/lib/active_record.rb +195 -0
  8. data/lib/active_record/aggregations.rb +285 -0
  9. data/lib/active_record/association_relation.rb +49 -0
  10. data/lib/active_record/associations.rb +1865 -0
  11. data/lib/active_record/associations/alias_tracker.rb +81 -0
  12. data/lib/active_record/associations/association.rb +340 -0
  13. data/lib/active_record/associations/association_scope.rb +166 -0
  14. data/lib/active_record/associations/belongs_to_association.rb +124 -0
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +36 -0
  16. data/lib/active_record/associations/builder/association.rb +136 -0
  17. data/lib/active_record/associations/builder/belongs_to.rb +130 -0
  18. data/lib/active_record/associations/builder/collection_association.rb +72 -0
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +114 -0
  20. data/lib/active_record/associations/builder/has_many.rb +19 -0
  21. data/lib/active_record/associations/builder/has_one.rb +64 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +44 -0
  23. data/lib/active_record/associations/collection_association.rb +498 -0
  24. data/lib/active_record/associations/collection_proxy.rb +1128 -0
  25. data/lib/active_record/associations/foreign_association.rb +20 -0
  26. data/lib/active_record/associations/has_many_association.rb +136 -0
  27. data/lib/active_record/associations/has_many_through_association.rb +220 -0
  28. data/lib/active_record/associations/has_one_association.rb +118 -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 +80 -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 +201 -0
  35. data/lib/active_record/associations/preloader/association.rb +133 -0
  36. data/lib/active_record/associations/preloader/through_association.rb +116 -0
  37. data/lib/active_record/associations/singular_association.rb +59 -0
  38. data/lib/active_record/associations/through_association.rb +121 -0
  39. data/lib/active_record/attribute_assignment.rb +85 -0
  40. data/lib/active_record/attribute_decorators.rb +90 -0
  41. data/lib/active_record/attribute_methods.rb +420 -0
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +81 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +221 -0
  44. data/lib/active_record/attribute_methods/primary_key.rb +136 -0
  45. data/lib/active_record/attribute_methods/query.rb +41 -0
  46. data/lib/active_record/attribute_methods/read.rb +47 -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 +61 -0
  50. data/lib/active_record/attributes.rb +279 -0
  51. data/lib/active_record/autosave_association.rb +512 -0
  52. data/lib/active_record/base.rb +328 -0
  53. data/lib/active_record/callbacks.rb +339 -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/connection_adapters/abstract/connection_pool.rb +1175 -0
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +85 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +516 -0
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +155 -0
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +251 -0
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +713 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1475 -0
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +772 -0
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +830 -0
  69. data/lib/active_record/connection_adapters/column.rb +95 -0
  70. data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +202 -0
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +146 -0
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +184 -0
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  108. data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
  109. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
  114. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
  115. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  116. data/lib/active_record/connection_adapters/postgresql_adapter.rb +953 -0
  117. data/lib/active_record/connection_adapters/schema_cache.rb +141 -0
  118. data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
  119. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -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 +103 -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 +137 -0
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +561 -0
  127. data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
  128. data/lib/active_record/connection_handling.rb +274 -0
  129. data/lib/active_record/core.rb +603 -0
  130. data/lib/active_record/counter_cache.rb +193 -0
  131. data/lib/active_record/database_configurations.rb +233 -0
  132. data/lib/active_record/database_configurations/database_config.rb +37 -0
  133. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  134. data/lib/active_record/database_configurations/url_config.rb +79 -0
  135. data/lib/active_record/define_callbacks.rb +22 -0
  136. data/lib/active_record/dynamic_matchers.rb +122 -0
  137. data/lib/active_record/enum.rb +274 -0
  138. data/lib/active_record/errors.rb +388 -0
  139. data/lib/active_record/explain.rb +50 -0
  140. data/lib/active_record/explain_registry.rb +32 -0
  141. data/lib/active_record/explain_subscriber.rb +34 -0
  142. data/lib/active_record/fixture_set/file.rb +82 -0
  143. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  144. data/lib/active_record/fixture_set/render_context.rb +17 -0
  145. data/lib/active_record/fixture_set/table_row.rb +153 -0
  146. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  147. data/lib/active_record/fixtures.rb +738 -0
  148. data/lib/active_record/gem_version.rb +17 -0
  149. data/lib/active_record/inheritance.rb +293 -0
  150. data/lib/active_record/insert_all.rb +179 -0
  151. data/lib/active_record/integration.rb +207 -0
  152. data/lib/active_record/internal_metadata.rb +53 -0
  153. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  154. data/lib/active_record/locale/en.yml +48 -0
  155. data/lib/active_record/locking/optimistic.rb +197 -0
  156. data/lib/active_record/locking/pessimistic.rb +89 -0
  157. data/lib/active_record/log_subscriber.rb +118 -0
  158. data/lib/active_record/middleware/database_selector.rb +75 -0
  159. data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
  160. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  161. data/lib/active_record/migration.rb +1397 -0
  162. data/lib/active_record/migration/command_recorder.rb +284 -0
  163. data/lib/active_record/migration/compatibility.rb +244 -0
  164. data/lib/active_record/migration/join_table.rb +17 -0
  165. data/lib/active_record/model_schema.rb +545 -0
  166. data/lib/active_record/nested_attributes.rb +600 -0
  167. data/lib/active_record/no_touching.rb +65 -0
  168. data/lib/active_record/null_relation.rb +68 -0
  169. data/lib/active_record/persistence.rb +967 -0
  170. data/lib/active_record/query_cache.rb +52 -0
  171. data/lib/active_record/querying.rb +82 -0
  172. data/lib/active_record/railtie.rb +263 -0
  173. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  174. data/lib/active_record/railties/console_sandbox.rb +7 -0
  175. data/lib/active_record/railties/controller_runtime.rb +51 -0
  176. data/lib/active_record/railties/databases.rake +527 -0
  177. data/lib/active_record/readonly_attributes.rb +24 -0
  178. data/lib/active_record/reflection.rb +1042 -0
  179. data/lib/active_record/relation.rb +860 -0
  180. data/lib/active_record/relation/batches.rb +290 -0
  181. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  182. data/lib/active_record/relation/calculations.rb +424 -0
  183. data/lib/active_record/relation/delegation.rb +130 -0
  184. data/lib/active_record/relation/finder_methods.rb +561 -0
  185. data/lib/active_record/relation/from_clause.rb +26 -0
  186. data/lib/active_record/relation/merger.rb +184 -0
  187. data/lib/active_record/relation/predicate_builder.rb +150 -0
  188. data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
  189. data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
  190. data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
  191. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
  192. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
  193. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
  194. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  195. data/lib/active_record/relation/query_attribute.rb +50 -0
  196. data/lib/active_record/relation/query_methods.rb +1371 -0
  197. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  198. data/lib/active_record/relation/spawn_methods.rb +77 -0
  199. data/lib/active_record/relation/where_clause.rb +190 -0
  200. data/lib/active_record/relation/where_clause_factory.rb +33 -0
  201. data/lib/active_record/result.rb +168 -0
  202. data/lib/active_record/runtime_registry.rb +24 -0
  203. data/lib/active_record/sanitization.rb +214 -0
  204. data/lib/active_record/schema.rb +61 -0
  205. data/lib/active_record/schema_dumper.rb +270 -0
  206. data/lib/active_record/schema_migration.rb +60 -0
  207. data/lib/active_record/scoping.rb +106 -0
  208. data/lib/active_record/scoping/default.rb +151 -0
  209. data/lib/active_record/scoping/named.rb +217 -0
  210. data/lib/active_record/secure_token.rb +40 -0
  211. data/lib/active_record/serialization.rb +22 -0
  212. data/lib/active_record/statement_cache.rb +148 -0
  213. data/lib/active_record/store.rb +290 -0
  214. data/lib/active_record/suppressor.rb +61 -0
  215. data/lib/active_record/table_metadata.rb +75 -0
  216. data/lib/active_record/tasks/database_tasks.rb +506 -0
  217. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  218. data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
  219. data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
  220. data/lib/active_record/test_databases.rb +23 -0
  221. data/lib/active_record/test_fixtures.rb +224 -0
  222. data/lib/active_record/timestamp.rb +167 -0
  223. data/lib/active_record/touch_later.rb +66 -0
  224. data/lib/active_record/transactions.rb +493 -0
  225. data/lib/active_record/translation.rb +24 -0
  226. data/lib/active_record/type.rb +78 -0
  227. data/lib/active_record/type/adapter_specific_registry.rb +129 -0
  228. data/lib/active_record/type/date.rb +9 -0
  229. data/lib/active_record/type/date_time.rb +9 -0
  230. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  231. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  232. data/lib/active_record/type/internal/timezone.rb +17 -0
  233. data/lib/active_record/type/json.rb +30 -0
  234. data/lib/active_record/type/serialized.rb +71 -0
  235. data/lib/active_record/type/text.rb +11 -0
  236. data/lib/active_record/type/time.rb +21 -0
  237. data/lib/active_record/type/type_map.rb +62 -0
  238. data/lib/active_record/type/unsigned_integer.rb +17 -0
  239. data/lib/active_record/type_caster.rb +9 -0
  240. data/lib/active_record/type_caster/connection.rb +34 -0
  241. data/lib/active_record/type_caster/map.rb +20 -0
  242. data/lib/active_record/validations.rb +94 -0
  243. data/lib/active_record/validations/absence.rb +25 -0
  244. data/lib/active_record/validations/associated.rb +60 -0
  245. data/lib/active_record/validations/length.rb +26 -0
  246. data/lib/active_record/validations/presence.rb +68 -0
  247. data/lib/active_record/validations/uniqueness.rb +226 -0
  248. data/lib/active_record/version.rb +10 -0
  249. data/lib/arel.rb +58 -0
  250. data/lib/arel/alias_predication.rb +9 -0
  251. data/lib/arel/attributes.rb +22 -0
  252. data/lib/arel/attributes/attribute.rb +37 -0
  253. data/lib/arel/collectors/bind.rb +24 -0
  254. data/lib/arel/collectors/composite.rb +31 -0
  255. data/lib/arel/collectors/plain_string.rb +20 -0
  256. data/lib/arel/collectors/sql_string.rb +20 -0
  257. data/lib/arel/collectors/substitute_binds.rb +28 -0
  258. data/lib/arel/crud.rb +42 -0
  259. data/lib/arel/delete_manager.rb +18 -0
  260. data/lib/arel/errors.rb +9 -0
  261. data/lib/arel/expressions.rb +29 -0
  262. data/lib/arel/factory_methods.rb +49 -0
  263. data/lib/arel/insert_manager.rb +49 -0
  264. data/lib/arel/math.rb +45 -0
  265. data/lib/arel/nodes.rb +68 -0
  266. data/lib/arel/nodes/and.rb +32 -0
  267. data/lib/arel/nodes/ascending.rb +23 -0
  268. data/lib/arel/nodes/binary.rb +52 -0
  269. data/lib/arel/nodes/bind_param.rb +36 -0
  270. data/lib/arel/nodes/case.rb +55 -0
  271. data/lib/arel/nodes/casted.rb +50 -0
  272. data/lib/arel/nodes/comment.rb +29 -0
  273. data/lib/arel/nodes/count.rb +12 -0
  274. data/lib/arel/nodes/delete_statement.rb +45 -0
  275. data/lib/arel/nodes/descending.rb +23 -0
  276. data/lib/arel/nodes/equality.rb +18 -0
  277. data/lib/arel/nodes/extract.rb +24 -0
  278. data/lib/arel/nodes/false.rb +16 -0
  279. data/lib/arel/nodes/full_outer_join.rb +8 -0
  280. data/lib/arel/nodes/function.rb +44 -0
  281. data/lib/arel/nodes/grouping.rb +8 -0
  282. data/lib/arel/nodes/in.rb +8 -0
  283. data/lib/arel/nodes/infix_operation.rb +80 -0
  284. data/lib/arel/nodes/inner_join.rb +8 -0
  285. data/lib/arel/nodes/insert_statement.rb +37 -0
  286. data/lib/arel/nodes/join_source.rb +20 -0
  287. data/lib/arel/nodes/matches.rb +18 -0
  288. data/lib/arel/nodes/named_function.rb +23 -0
  289. data/lib/arel/nodes/node.rb +50 -0
  290. data/lib/arel/nodes/node_expression.rb +13 -0
  291. data/lib/arel/nodes/outer_join.rb +8 -0
  292. data/lib/arel/nodes/over.rb +15 -0
  293. data/lib/arel/nodes/regexp.rb +16 -0
  294. data/lib/arel/nodes/right_outer_join.rb +8 -0
  295. data/lib/arel/nodes/select_core.rb +67 -0
  296. data/lib/arel/nodes/select_statement.rb +41 -0
  297. data/lib/arel/nodes/sql_literal.rb +16 -0
  298. data/lib/arel/nodes/string_join.rb +11 -0
  299. data/lib/arel/nodes/table_alias.rb +27 -0
  300. data/lib/arel/nodes/terminal.rb +16 -0
  301. data/lib/arel/nodes/true.rb +16 -0
  302. data/lib/arel/nodes/unary.rb +45 -0
  303. data/lib/arel/nodes/unary_operation.rb +20 -0
  304. data/lib/arel/nodes/unqualified_column.rb +22 -0
  305. data/lib/arel/nodes/update_statement.rb +41 -0
  306. data/lib/arel/nodes/values_list.rb +9 -0
  307. data/lib/arel/nodes/window.rb +126 -0
  308. data/lib/arel/nodes/with.rb +11 -0
  309. data/lib/arel/order_predications.rb +13 -0
  310. data/lib/arel/predications.rb +257 -0
  311. data/lib/arel/select_manager.rb +271 -0
  312. data/lib/arel/table.rb +110 -0
  313. data/lib/arel/tree_manager.rb +72 -0
  314. data/lib/arel/update_manager.rb +34 -0
  315. data/lib/arel/visitors.rb +20 -0
  316. data/lib/arel/visitors/depth_first.rb +204 -0
  317. data/lib/arel/visitors/dot.rb +297 -0
  318. data/lib/arel/visitors/ibm_db.rb +34 -0
  319. data/lib/arel/visitors/informix.rb +62 -0
  320. data/lib/arel/visitors/mssql.rb +157 -0
  321. data/lib/arel/visitors/mysql.rb +83 -0
  322. data/lib/arel/visitors/oracle.rb +159 -0
  323. data/lib/arel/visitors/oracle12.rb +66 -0
  324. data/lib/arel/visitors/postgresql.rb +110 -0
  325. data/lib/arel/visitors/sqlite.rb +39 -0
  326. data/lib/arel/visitors/to_sql.rb +889 -0
  327. data/lib/arel/visitors/visitor.rb +46 -0
  328. data/lib/arel/visitors/where_sql.rb +23 -0
  329. data/lib/arel/window_predications.rb +9 -0
  330. data/lib/rails/generators/active_record.rb +19 -0
  331. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  332. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  333. data/lib/rails/generators/active_record/migration.rb +48 -0
  334. data/lib/rails/generators/active_record/migration/migration_generator.rb +75 -0
  335. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  336. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +48 -0
  337. data/lib/rails/generators/active_record/model/model_generator.rb +49 -0
  338. data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
  339. data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
  340. metadata +418 -0
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # = Active Record \Timestamp
5
+ #
6
+ # Active Record automatically timestamps create and update operations if the
7
+ # table has fields named <tt>created_at/created_on</tt> or
8
+ # <tt>updated_at/updated_on</tt>.
9
+ #
10
+ # Timestamping can be turned off by setting:
11
+ #
12
+ # config.active_record.record_timestamps = false
13
+ #
14
+ # Timestamps are in UTC by default but you can use the local timezone by setting:
15
+ #
16
+ # config.active_record.default_timezone = :local
17
+ #
18
+ # == Time Zone aware attributes
19
+ #
20
+ # Active Record keeps all the <tt>datetime</tt> and <tt>time</tt> columns
21
+ # timezone aware. By default, these values are stored in the database as UTC
22
+ # and converted back to the current <tt>Time.zone</tt> when pulled from the database.
23
+ #
24
+ # This feature can be turned off completely by setting:
25
+ #
26
+ # config.active_record.time_zone_aware_attributes = false
27
+ #
28
+ # You can also specify that only <tt>datetime</tt> columns should be time-zone
29
+ # aware (while <tt>time</tt> should not) by setting:
30
+ #
31
+ # ActiveRecord::Base.time_zone_aware_types = [:datetime]
32
+ #
33
+ # You can also add database specific timezone aware types. For example, for PostgreSQL:
34
+ #
35
+ # ActiveRecord::Base.time_zone_aware_types += [:tsrange, :tstzrange]
36
+ #
37
+ # Finally, you can indicate specific attributes of a model for which time zone
38
+ # conversion should not applied, for instance by setting:
39
+ #
40
+ # class Topic < ActiveRecord::Base
41
+ # self.skip_time_zone_conversion_for_attributes = [:written_on]
42
+ # end
43
+ module Timestamp
44
+ extend ActiveSupport::Concern
45
+
46
+ included do
47
+ class_attribute :record_timestamps, default: true
48
+ end
49
+
50
+ def initialize_dup(other) # :nodoc:
51
+ super
52
+ clear_timestamp_attributes
53
+ end
54
+
55
+ module ClassMethods # :nodoc:
56
+ def touch_attributes_with_time(*names, time: nil)
57
+ attribute_names = timestamp_attributes_for_update_in_model
58
+ attribute_names |= names.map(&:to_s)
59
+ attribute_names.index_with(time || current_time_from_proper_timezone)
60
+ end
61
+
62
+ def timestamp_attributes_for_create_in_model
63
+ @timestamp_attributes_for_create_in_model ||=
64
+ (timestamp_attributes_for_create & column_names).freeze
65
+ end
66
+
67
+ def timestamp_attributes_for_update_in_model
68
+ @timestamp_attributes_for_update_in_model ||=
69
+ (timestamp_attributes_for_update & column_names).freeze
70
+ end
71
+
72
+ def all_timestamp_attributes_in_model
73
+ @all_timestamp_attributes_in_model ||=
74
+ (timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model).freeze
75
+ end
76
+
77
+ def current_time_from_proper_timezone
78
+ default_timezone == :utc ? Time.now.utc : Time.now
79
+ end
80
+
81
+ private
82
+ def timestamp_attributes_for_create
83
+ ["created_at", "created_on"]
84
+ end
85
+
86
+ def timestamp_attributes_for_update
87
+ ["updated_at", "updated_on"]
88
+ end
89
+
90
+ def reload_schema_from_cache
91
+ @timestamp_attributes_for_create_in_model = nil
92
+ @timestamp_attributes_for_update_in_model = nil
93
+ @all_timestamp_attributes_in_model = nil
94
+ super
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def _create_record
101
+ if record_timestamps
102
+ current_time = current_time_from_proper_timezone
103
+
104
+ all_timestamp_attributes_in_model.each do |column|
105
+ if !attribute_present?(column)
106
+ _write_attribute(column, current_time)
107
+ end
108
+ end
109
+ end
110
+
111
+ super
112
+ end
113
+
114
+ def _update_record
115
+ if @_touch_record && should_record_timestamps?
116
+ current_time = current_time_from_proper_timezone
117
+
118
+ timestamp_attributes_for_update_in_model.each do |column|
119
+ next if will_save_change_to_attribute?(column)
120
+ _write_attribute(column, current_time)
121
+ end
122
+ end
123
+
124
+ super
125
+ end
126
+
127
+ def create_or_update(touch: true, **)
128
+ @_touch_record = touch
129
+ super
130
+ end
131
+
132
+ def should_record_timestamps?
133
+ record_timestamps && (!partial_writes? || has_changes_to_save?)
134
+ end
135
+
136
+ def timestamp_attributes_for_create_in_model
137
+ self.class.timestamp_attributes_for_create_in_model
138
+ end
139
+
140
+ def timestamp_attributes_for_update_in_model
141
+ self.class.timestamp_attributes_for_update_in_model
142
+ end
143
+
144
+ def all_timestamp_attributes_in_model
145
+ self.class.all_timestamp_attributes_in_model
146
+ end
147
+
148
+ def current_time_from_proper_timezone
149
+ self.class.current_time_from_proper_timezone
150
+ end
151
+
152
+ def max_updated_column_timestamp
153
+ timestamp_attributes_for_update_in_model
154
+ .map { |attr| self[attr]&.to_time }
155
+ .compact
156
+ .max
157
+ end
158
+
159
+ # Clear attributes and changed_attributes
160
+ def clear_timestamp_attributes
161
+ all_timestamp_attributes_in_model.each do |attribute_name|
162
+ self[attribute_name] = nil
163
+ clear_attribute_changes([attribute_name])
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # = Active Record Touch Later
5
+ module TouchLater # :nodoc:
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ before_commit_without_transaction_enrollment :touch_deferred_attributes
10
+ end
11
+
12
+ def touch_later(*names) # :nodoc:
13
+ unless persisted?
14
+ raise ActiveRecordError, <<-MSG.squish
15
+ cannot touch on a new or destroyed record object. Consider using
16
+ persisted?, new_record?, or destroyed? before touching
17
+ MSG
18
+ end
19
+
20
+ @_defer_touch_attrs ||= timestamp_attributes_for_update_in_model
21
+ @_defer_touch_attrs |= names
22
+ @_touch_time = current_time_from_proper_timezone
23
+
24
+ surreptitiously_touch @_defer_touch_attrs
25
+ add_to_transaction
26
+ @_new_record_before_last_commit ||= false
27
+
28
+ # touch the parents as we are not calling the after_save callbacks
29
+ self.class.reflect_on_all_associations(:belongs_to).each do |r|
30
+ if touch = r.options[:touch]
31
+ ActiveRecord::Associations::Builder::BelongsTo.touch_record(self, changes_to_save, r.foreign_key, r.name, touch, :touch_later)
32
+ end
33
+ end
34
+ end
35
+
36
+ def touch(*names, time: nil) # :nodoc:
37
+ if has_defer_touch_attrs?
38
+ names |= @_defer_touch_attrs
39
+ end
40
+ super(*names, time: time)
41
+ end
42
+
43
+ private
44
+
45
+ def surreptitiously_touch(attrs)
46
+ attrs.each { |attr| write_attribute attr, @_touch_time }
47
+ clear_attribute_changes attrs
48
+ end
49
+
50
+ def touch_deferred_attributes
51
+ if has_defer_touch_attrs? && persisted?
52
+ @_skip_dirty_tracking = true
53
+ touch(*@_defer_touch_attrs, time: @_touch_time)
54
+ @_defer_touch_attrs, @_touch_time = nil, nil
55
+ end
56
+ end
57
+
58
+ def has_defer_touch_attrs?
59
+ defined?(@_defer_touch_attrs) && @_defer_touch_attrs.present?
60
+ end
61
+
62
+ def belongs_to_touch_method
63
+ :touch_later
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,493 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # See ActiveRecord::Transactions::ClassMethods for documentation.
5
+ module Transactions
6
+ extend ActiveSupport::Concern
7
+ #:nodoc:
8
+ ACTIONS = [:create, :destroy, :update]
9
+
10
+ included do
11
+ define_callbacks :commit, :rollback,
12
+ :before_commit,
13
+ :before_commit_without_transaction_enrollment,
14
+ :commit_without_transaction_enrollment,
15
+ :rollback_without_transaction_enrollment,
16
+ scope: [:kind, :name]
17
+ end
18
+
19
+ # = Active Record Transactions
20
+ #
21
+ # \Transactions are protective blocks where SQL statements are only permanent
22
+ # if they can all succeed as one atomic action. The classic example is a
23
+ # transfer between two accounts where you can only have a deposit if the
24
+ # withdrawal succeeded and vice versa. \Transactions enforce the integrity of
25
+ # the database and guard the data against program errors or database
26
+ # break-downs. So basically you should use transaction blocks whenever you
27
+ # have a number of statements that must be executed together or not at all.
28
+ #
29
+ # For example:
30
+ #
31
+ # ActiveRecord::Base.transaction do
32
+ # david.withdrawal(100)
33
+ # mary.deposit(100)
34
+ # end
35
+ #
36
+ # This example will only take money from David and give it to Mary if neither
37
+ # +withdrawal+ nor +deposit+ raise an exception. Exceptions will force a
38
+ # ROLLBACK that returns the database to the state before the transaction
39
+ # began. Be aware, though, that the objects will _not_ have their instance
40
+ # data returned to their pre-transactional state.
41
+ #
42
+ # == Different Active Record classes in a single transaction
43
+ #
44
+ # Though the #transaction class method is called on some Active Record class,
45
+ # the objects within the transaction block need not all be instances of
46
+ # that class. This is because transactions are per-database connection, not
47
+ # per-model.
48
+ #
49
+ # In this example a +balance+ record is transactionally saved even
50
+ # though #transaction is called on the +Account+ class:
51
+ #
52
+ # Account.transaction do
53
+ # balance.save!
54
+ # account.save!
55
+ # end
56
+ #
57
+ # The #transaction method is also available as a model instance method.
58
+ # For example, you can also do this:
59
+ #
60
+ # balance.transaction do
61
+ # balance.save!
62
+ # account.save!
63
+ # end
64
+ #
65
+ # == Transactions are not distributed across database connections
66
+ #
67
+ # A transaction acts on a single database connection. If you have
68
+ # multiple class-specific databases, the transaction will not protect
69
+ # interaction among them. One workaround is to begin a transaction
70
+ # on each class whose models you alter:
71
+ #
72
+ # Student.transaction do
73
+ # Course.transaction do
74
+ # course.enroll(student)
75
+ # student.units += course.units
76
+ # end
77
+ # end
78
+ #
79
+ # This is a poor solution, but fully distributed transactions are beyond
80
+ # the scope of Active Record.
81
+ #
82
+ # == +save+ and +destroy+ are automatically wrapped in a transaction
83
+ #
84
+ # Both {#save}[rdoc-ref:Persistence#save] and
85
+ # {#destroy}[rdoc-ref:Persistence#destroy] come wrapped in a transaction that ensures
86
+ # that whatever you do in validations or callbacks will happen under its
87
+ # protected cover. So you can use validations to check for values that
88
+ # the transaction depends on or you can raise exceptions in the callbacks
89
+ # to rollback, including <tt>after_*</tt> callbacks.
90
+ #
91
+ # As a consequence changes to the database are not seen outside your connection
92
+ # until the operation is complete. For example, if you try to update the index
93
+ # of a search engine in +after_save+ the indexer won't see the updated record.
94
+ # The #after_commit callback is the only one that is triggered once the update
95
+ # is committed. See below.
96
+ #
97
+ # == Exception handling and rolling back
98
+ #
99
+ # Also have in mind that exceptions thrown within a transaction block will
100
+ # be propagated (after triggering the ROLLBACK), so you should be ready to
101
+ # catch those in your application code.
102
+ #
103
+ # One exception is the ActiveRecord::Rollback exception, which will trigger
104
+ # a ROLLBACK when raised, but not be re-raised by the transaction block.
105
+ #
106
+ # *Warning*: one should not catch ActiveRecord::StatementInvalid exceptions
107
+ # inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an
108
+ # error occurred at the database level, for example when a unique constraint
109
+ # is violated. On some database systems, such as PostgreSQL, database errors
110
+ # inside a transaction cause the entire transaction to become unusable
111
+ # until it's restarted from the beginning. Here is an example which
112
+ # demonstrates the problem:
113
+ #
114
+ # # Suppose that we have a Number model with a unique column called 'i'.
115
+ # Number.transaction do
116
+ # Number.create(i: 0)
117
+ # begin
118
+ # # This will raise a unique constraint error...
119
+ # Number.create(i: 0)
120
+ # rescue ActiveRecord::StatementInvalid
121
+ # # ...which we ignore.
122
+ # end
123
+ #
124
+ # # On PostgreSQL, the transaction is now unusable. The following
125
+ # # statement will cause a PostgreSQL error, even though the unique
126
+ # # constraint is no longer violated:
127
+ # Number.create(i: 1)
128
+ # # => "PG::Error: ERROR: current transaction is aborted, commands
129
+ # # ignored until end of transaction block"
130
+ # end
131
+ #
132
+ # One should restart the entire transaction if an
133
+ # ActiveRecord::StatementInvalid occurred.
134
+ #
135
+ # == Nested transactions
136
+ #
137
+ # #transaction calls can be nested. By default, this makes all database
138
+ # statements in the nested transaction block become part of the parent
139
+ # transaction. For example, the following behavior may be surprising:
140
+ #
141
+ # User.transaction do
142
+ # User.create(username: 'Kotori')
143
+ # User.transaction do
144
+ # User.create(username: 'Nemu')
145
+ # raise ActiveRecord::Rollback
146
+ # end
147
+ # end
148
+ #
149
+ # creates both "Kotori" and "Nemu". Reason is the ActiveRecord::Rollback
150
+ # exception in the nested block does not issue a ROLLBACK. Since these exceptions
151
+ # are captured in transaction blocks, the parent block does not see it and the
152
+ # real transaction is committed.
153
+ #
154
+ # In order to get a ROLLBACK for the nested transaction you may ask for a real
155
+ # sub-transaction by passing <tt>requires_new: true</tt>. If anything goes wrong,
156
+ # the database rolls back to the beginning of the sub-transaction without rolling
157
+ # back the parent transaction. If we add it to the previous example:
158
+ #
159
+ # User.transaction do
160
+ # User.create(username: 'Kotori')
161
+ # User.transaction(requires_new: true) do
162
+ # User.create(username: 'Nemu')
163
+ # raise ActiveRecord::Rollback
164
+ # end
165
+ # end
166
+ #
167
+ # only "Kotori" is created. This works on MySQL and PostgreSQL. SQLite3 version >= '3.6.8' also supports it.
168
+ #
169
+ # Most databases don't support true nested transactions. At the time of
170
+ # writing, the only database that we're aware of that supports true nested
171
+ # transactions, is MS-SQL. Because of this, Active Record emulates nested
172
+ # transactions by using savepoints on MySQL and PostgreSQL. See
173
+ # https://dev.mysql.com/doc/refman/5.7/en/savepoint.html
174
+ # for more information about savepoints.
175
+ #
176
+ # === \Callbacks
177
+ #
178
+ # There are two types of callbacks associated with committing and rolling back transactions:
179
+ # #after_commit and #after_rollback.
180
+ #
181
+ # #after_commit callbacks are called on every record saved or destroyed within a
182
+ # transaction immediately after the transaction is committed. #after_rollback callbacks
183
+ # are called on every record saved or destroyed within a transaction immediately after the
184
+ # transaction or savepoint is rolled back.
185
+ #
186
+ # These callbacks are useful for interacting with other systems since you will be guaranteed
187
+ # that the callback is only executed when the database is in a permanent state. For example,
188
+ # #after_commit is a good spot to put in a hook to clearing a cache since clearing it from
189
+ # within a transaction could trigger the cache to be regenerated before the database is updated.
190
+ #
191
+ # === Caveats
192
+ #
193
+ # If you're on MySQL, then do not use Data Definition Language (DDL) operations in nested
194
+ # transactions blocks that are emulated with savepoints. That is, do not execute statements
195
+ # like 'CREATE TABLE' inside such blocks. This is because MySQL automatically
196
+ # releases all savepoints upon executing a DDL operation. When +transaction+
197
+ # is finished and tries to release the savepoint it created earlier, a
198
+ # database error will occur because the savepoint has already been
199
+ # automatically released. The following example demonstrates the problem:
200
+ #
201
+ # Model.connection.transaction do # BEGIN
202
+ # Model.connection.transaction(requires_new: true) do # CREATE SAVEPOINT active_record_1
203
+ # Model.connection.create_table(...) # active_record_1 now automatically released
204
+ # end # RELEASE SAVEPOINT active_record_1
205
+ # # ^^^^ BOOM! database error!
206
+ # end
207
+ #
208
+ # Note that "TRUNCATE" is also a MySQL DDL statement!
209
+ module ClassMethods
210
+ # See the ConnectionAdapters::DatabaseStatements#transaction API docs.
211
+ def transaction(options = {}, &block)
212
+ connection.transaction(options, &block)
213
+ end
214
+
215
+ def before_commit(*args, &block) # :nodoc:
216
+ set_options_for_callbacks!(args)
217
+ set_callback(:before_commit, :before, *args, &block)
218
+ end
219
+
220
+ # This callback is called after a record has been created, updated, or destroyed.
221
+ #
222
+ # You can specify that the callback should only be fired by a certain action with
223
+ # the +:on+ option:
224
+ #
225
+ # after_commit :do_foo, on: :create
226
+ # after_commit :do_bar, on: :update
227
+ # after_commit :do_baz, on: :destroy
228
+ #
229
+ # after_commit :do_foo_bar, on: [:create, :update]
230
+ # after_commit :do_bar_baz, on: [:update, :destroy]
231
+ #
232
+ def after_commit(*args, &block)
233
+ set_options_for_callbacks!(args)
234
+ set_callback(:commit, :after, *args, &block)
235
+ end
236
+
237
+ # Shortcut for <tt>after_commit :hook, on: [ :create, :update ]</tt>.
238
+ def after_save_commit(*args, &block)
239
+ set_options_for_callbacks!(args, on: [ :create, :update ])
240
+ set_callback(:commit, :after, *args, &block)
241
+ end
242
+
243
+ # Shortcut for <tt>after_commit :hook, on: :create</tt>.
244
+ def after_create_commit(*args, &block)
245
+ set_options_for_callbacks!(args, on: :create)
246
+ set_callback(:commit, :after, *args, &block)
247
+ end
248
+
249
+ # Shortcut for <tt>after_commit :hook, on: :update</tt>.
250
+ def after_update_commit(*args, &block)
251
+ set_options_for_callbacks!(args, on: :update)
252
+ set_callback(:commit, :after, *args, &block)
253
+ end
254
+
255
+ # Shortcut for <tt>after_commit :hook, on: :destroy</tt>.
256
+ def after_destroy_commit(*args, &block)
257
+ set_options_for_callbacks!(args, on: :destroy)
258
+ set_callback(:commit, :after, *args, &block)
259
+ end
260
+
261
+ # This callback is called after a create, update, or destroy are rolled back.
262
+ #
263
+ # Please check the documentation of #after_commit for options.
264
+ def after_rollback(*args, &block)
265
+ set_options_for_callbacks!(args)
266
+ set_callback(:rollback, :after, *args, &block)
267
+ end
268
+
269
+ def before_commit_without_transaction_enrollment(*args, &block) # :nodoc:
270
+ set_options_for_callbacks!(args)
271
+ set_callback(:before_commit_without_transaction_enrollment, :before, *args, &block)
272
+ end
273
+
274
+ def after_commit_without_transaction_enrollment(*args, &block) # :nodoc:
275
+ set_options_for_callbacks!(args)
276
+ set_callback(:commit_without_transaction_enrollment, :after, *args, &block)
277
+ end
278
+
279
+ def after_rollback_without_transaction_enrollment(*args, &block) # :nodoc:
280
+ set_options_for_callbacks!(args)
281
+ set_callback(:rollback_without_transaction_enrollment, :after, *args, &block)
282
+ end
283
+
284
+ private
285
+
286
+ def set_options_for_callbacks!(args, enforced_options = {})
287
+ options = args.extract_options!.merge!(enforced_options)
288
+ args << options
289
+
290
+ if options[:on]
291
+ fire_on = Array(options[:on])
292
+ assert_valid_transaction_action(fire_on)
293
+ options[:if] = Array(options[:if])
294
+ options[:if].unshift(-> { transaction_include_any_action?(fire_on) })
295
+ end
296
+ end
297
+
298
+ def assert_valid_transaction_action(actions)
299
+ if (actions - ACTIONS).any?
300
+ raise ArgumentError, ":on conditions for after_commit and after_rollback callbacks have to be one of #{ACTIONS}"
301
+ end
302
+ end
303
+ end
304
+
305
+ # See ActiveRecord::Transactions::ClassMethods for detailed documentation.
306
+ def transaction(options = {}, &block)
307
+ self.class.transaction(options, &block)
308
+ end
309
+
310
+ def destroy #:nodoc:
311
+ with_transaction_returning_status { super }
312
+ end
313
+
314
+ def save(*) #:nodoc:
315
+ with_transaction_returning_status { super }
316
+ end
317
+
318
+ def save!(*) #:nodoc:
319
+ with_transaction_returning_status { super }
320
+ end
321
+
322
+ def touch(*) #:nodoc:
323
+ with_transaction_returning_status { super }
324
+ end
325
+
326
+ def before_committed! # :nodoc:
327
+ _run_before_commit_without_transaction_enrollment_callbacks
328
+ _run_before_commit_callbacks
329
+ end
330
+
331
+ # Call the #after_commit callbacks.
332
+ #
333
+ # Ensure that it is not called if the object was never persisted (failed create),
334
+ # but call it after the commit of a destroyed object.
335
+ def committed!(should_run_callbacks: true) #:nodoc:
336
+ force_clear_transaction_record_state
337
+ if should_run_callbacks
338
+ @_committed_already_called = true
339
+ _run_commit_without_transaction_enrollment_callbacks
340
+ _run_commit_callbacks
341
+ end
342
+ ensure
343
+ @_committed_already_called = false
344
+ end
345
+
346
+ # Call the #after_rollback callbacks. The +force_restore_state+ argument indicates if the record
347
+ # state should be rolled back to the beginning or just to the last savepoint.
348
+ def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
349
+ if should_run_callbacks
350
+ _run_rollback_callbacks
351
+ _run_rollback_without_transaction_enrollment_callbacks
352
+ end
353
+ ensure
354
+ restore_transaction_record_state(force_restore_state)
355
+ clear_transaction_record_state
356
+ end
357
+
358
+ # Executes +method+ within a transaction and captures its return value as a
359
+ # status flag. If the status is true the transaction is committed, otherwise
360
+ # a ROLLBACK is issued. In any case the status flag is returned.
361
+ #
362
+ # This method is available within the context of an ActiveRecord::Base
363
+ # instance.
364
+ def with_transaction_returning_status
365
+ status = nil
366
+ self.class.transaction do
367
+ if has_transactional_callbacks?
368
+ add_to_transaction
369
+ else
370
+ sync_with_transaction_state if @transaction_state&.finalized?
371
+ @transaction_state = self.class.connection.transaction_state
372
+ end
373
+ remember_transaction_record_state
374
+
375
+ status = yield
376
+ raise ActiveRecord::Rollback unless status
377
+ end
378
+ status
379
+ end
380
+
381
+ def trigger_transactional_callbacks? # :nodoc:
382
+ (@_new_record_before_last_commit || _trigger_update_callback) && persisted? ||
383
+ _trigger_destroy_callback && destroyed?
384
+ end
385
+
386
+ private
387
+ attr_reader :_committed_already_called, :_trigger_update_callback, :_trigger_destroy_callback
388
+
389
+ # Save the new record state and id of a record so it can be restored later if a transaction fails.
390
+ def remember_transaction_record_state
391
+ @_start_transaction_state ||= {
392
+ id: id,
393
+ new_record: @new_record,
394
+ destroyed: @destroyed,
395
+ attributes: @attributes,
396
+ frozen?: frozen?,
397
+ level: 0
398
+ }
399
+ @_start_transaction_state[:level] += 1
400
+
401
+ if _committed_already_called
402
+ @_new_record_before_last_commit = false
403
+ else
404
+ @_new_record_before_last_commit = @_start_transaction_state[:new_record]
405
+ end
406
+ end
407
+
408
+ # Clear the new record state and id of a record.
409
+ def clear_transaction_record_state
410
+ return unless @_start_transaction_state
411
+ @_start_transaction_state[:level] -= 1
412
+ force_clear_transaction_record_state if @_start_transaction_state[:level] < 1
413
+ end
414
+
415
+ # Force to clear the transaction record state.
416
+ def force_clear_transaction_record_state
417
+ @_start_transaction_state = nil
418
+ @transaction_state = nil
419
+ end
420
+
421
+ # Restore the new record state and id of a record that was previously saved by a call to save_record_state.
422
+ def restore_transaction_record_state(force_restore_state = false)
423
+ if restore_state = @_start_transaction_state
424
+ if force_restore_state || restore_state[:level] <= 1
425
+ @new_record = restore_state[:new_record]
426
+ @destroyed = restore_state[:destroyed]
427
+ @attributes = restore_state[:attributes].map do |attr|
428
+ value = @attributes.fetch_value(attr.name)
429
+ attr = attr.with_value_from_user(value) if attr.value != value
430
+ attr
431
+ end
432
+ @mutations_from_database = nil
433
+ @mutations_before_last_save = nil
434
+ if @attributes.fetch_value(@primary_key) != restore_state[:id]
435
+ @attributes.write_from_user(@primary_key, restore_state[:id])
436
+ end
437
+ freeze if restore_state[:frozen?]
438
+ end
439
+ end
440
+ end
441
+
442
+ # Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
443
+ def transaction_include_any_action?(actions)
444
+ actions.any? do |action|
445
+ case action
446
+ when :create
447
+ persisted? && @_new_record_before_last_commit
448
+ when :update
449
+ !(@_new_record_before_last_commit || destroyed?) && _trigger_update_callback
450
+ when :destroy
451
+ _trigger_destroy_callback
452
+ end
453
+ end
454
+ end
455
+
456
+ # Add the record to the current transaction so that the #after_rollback and #after_commit
457
+ # callbacks can be called.
458
+ def add_to_transaction
459
+ self.class.connection.add_transaction_record(self)
460
+ end
461
+
462
+ def has_transactional_callbacks?
463
+ !_rollback_callbacks.empty? || !_commit_callbacks.empty? || !_before_commit_callbacks.empty?
464
+ end
465
+
466
+ # Updates the attributes on this particular Active Record object so that
467
+ # if it's associated with a transaction, then the state of the Active Record
468
+ # object will be updated to reflect the current state of the transaction.
469
+ #
470
+ # The <tt>@transaction_state</tt> variable stores the states of the associated
471
+ # transaction. This relies on the fact that a transaction can only be in
472
+ # one rollback or commit (otherwise a list of states would be required).
473
+ # Each Active Record object inside of a transaction carries that transaction's
474
+ # TransactionState.
475
+ #
476
+ # This method checks to see if the ActiveRecord object's state reflects
477
+ # the TransactionState, and rolls back or commits the Active Record object
478
+ # as appropriate.
479
+ def sync_with_transaction_state
480
+ if transaction_state = @transaction_state
481
+ if transaction_state.fully_committed?
482
+ force_clear_transaction_record_state
483
+ elsif transaction_state.committed?
484
+ clear_transaction_record_state
485
+ elsif transaction_state.rolledback?
486
+ force_restore_state = transaction_state.fully_rolledback?
487
+ restore_transaction_record_state(force_restore_state)
488
+ clear_transaction_record_state
489
+ end
490
+ end
491
+ end
492
+ end
493
+ end