activerecord 4.2.0 → 5.2.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +640 -928
- data/MIT-LICENSE +2 -2
- data/README.rdoc +10 -11
- data/examples/performance.rb +32 -31
- data/examples/simple.rb +5 -4
- data/lib/active_record/aggregations.rb +264 -247
- data/lib/active_record/association_relation.rb +24 -6
- data/lib/active_record/associations/alias_tracker.rb +29 -35
- data/lib/active_record/associations/association.rb +87 -41
- data/lib/active_record/associations/association_scope.rb +106 -132
- data/lib/active_record/associations/belongs_to_association.rb +55 -36
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
- data/lib/active_record/associations/builder/association.rb +29 -38
- data/lib/active_record/associations/builder/belongs_to.rb +77 -30
- data/lib/active_record/associations/builder/collection_association.rb +14 -23
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +50 -39
- data/lib/active_record/associations/builder/has_many.rb +6 -4
- data/lib/active_record/associations/builder/has_one.rb +13 -6
- data/lib/active_record/associations/builder/singular_association.rb +15 -11
- data/lib/active_record/associations/collection_association.rb +145 -266
- data/lib/active_record/associations/collection_proxy.rb +242 -138
- data/lib/active_record/associations/foreign_association.rb +13 -0
- data/lib/active_record/associations/has_many_association.rb +35 -75
- data/lib/active_record/associations/has_many_through_association.rb +51 -69
- data/lib/active_record/associations/has_one_association.rb +39 -24
- data/lib/active_record/associations/has_one_through_association.rb +18 -9
- data/lib/active_record/associations/join_dependency/join_association.rb +40 -81
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
- data/lib/active_record/associations/join_dependency.rb +134 -154
- data/lib/active_record/associations/preloader/association.rb +85 -116
- data/lib/active_record/associations/preloader/through_association.rb +85 -74
- data/lib/active_record/associations/preloader.rb +83 -93
- data/lib/active_record/associations/singular_association.rb +27 -40
- data/lib/active_record/associations/through_association.rb +48 -23
- data/lib/active_record/associations.rb +1732 -1596
- data/lib/active_record/attribute_assignment.rb +58 -182
- data/lib/active_record/attribute_decorators.rb +39 -15
- data/lib/active_record/attribute_methods/before_type_cast.rb +12 -5
- data/lib/active_record/attribute_methods/dirty.rb +94 -125
- data/lib/active_record/attribute_methods/primary_key.rb +86 -71
- data/lib/active_record/attribute_methods/query.rb +4 -2
- data/lib/active_record/attribute_methods/read.rb +45 -63
- data/lib/active_record/attribute_methods/serialization.rb +40 -20
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +62 -36
- data/lib/active_record/attribute_methods/write.rb +31 -46
- data/lib/active_record/attribute_methods.rb +170 -117
- data/lib/active_record/attributes.rb +201 -74
- data/lib/active_record/autosave_association.rb +118 -45
- data/lib/active_record/base.rb +60 -48
- data/lib/active_record/callbacks.rb +97 -57
- data/lib/active_record/coders/json.rb +3 -1
- data/lib/active_record/coders/yaml_column.rb +37 -13
- data/lib/active_record/collection_cache_key.rb +53 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +712 -284
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +10 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +254 -87
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +72 -22
- data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -52
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +6 -4
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +67 -46
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +328 -217
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +81 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +617 -212
- data/lib/active_record/connection_adapters/abstract/transaction.rb +139 -75
- data/lib/active_record/connection_adapters/abstract_adapter.rb +332 -191
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +567 -563
- data/lib/active_record/connection_adapters/column.rb +50 -41
- data/lib/active_record/connection_adapters/connection_specification.rb +147 -135
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +42 -195
- data/lib/active_record/connection_adapters/postgresql/column.rb +35 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +46 -115
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +50 -57
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +10 -6
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +5 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +5 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +9 -13
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +7 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -19
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -9
- data/lib/active_record/connection_adapters/postgresql/oid/{integer.rb → oid.rb} +6 -2
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +33 -11
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +52 -34
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +65 -51
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid.rb +23 -25
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +107 -47
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +144 -90
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +466 -280
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +12 -8
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +439 -330
- data/lib/active_record/connection_adapters/schema_cache.rb +48 -24
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +269 -324
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +40 -27
- data/lib/active_record/core.rb +205 -202
- data/lib/active_record/counter_cache.rb +80 -37
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -105
- data/lib/active_record/enum.rb +136 -90
- data/lib/active_record/errors.rb +180 -52
- data/lib/active_record/explain.rb +23 -11
- data/lib/active_record/explain_registry.rb +4 -2
- data/lib/active_record/explain_subscriber.rb +11 -6
- data/lib/active_record/fixture_set/file.rb +35 -9
- data/lib/active_record/fixtures.rb +193 -135
- data/lib/active_record/gem_version.rb +5 -3
- data/lib/active_record/inheritance.rb +148 -112
- data/lib/active_record/integration.rb +70 -28
- data/lib/active_record/internal_metadata.rb +45 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +92 -98
- data/lib/active_record/locking/pessimistic.rb +15 -3
- data/lib/active_record/log_subscriber.rb +95 -33
- data/lib/active_record/migration/command_recorder.rb +133 -90
- data/lib/active_record/migration/compatibility.rb +217 -0
- data/lib/active_record/migration/join_table.rb +8 -6
- data/lib/active_record/migration.rb +594 -267
- data/lib/active_record/model_schema.rb +292 -111
- data/lib/active_record/nested_attributes.rb +266 -214
- data/lib/active_record/no_touching.rb +8 -2
- data/lib/active_record/null_relation.rb +24 -37
- data/lib/active_record/persistence.rb +350 -119
- data/lib/active_record/query_cache.rb +13 -24
- data/lib/active_record/querying.rb +19 -17
- data/lib/active_record/railtie.rb +117 -35
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +9 -3
- data/lib/active_record/railties/databases.rake +160 -174
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +447 -288
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +204 -55
- data/lib/active_record/relation/calculations.rb +259 -244
- data/lib/active_record/relation/delegation.rb +67 -60
- data/lib/active_record/relation/finder_methods.rb +290 -253
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +91 -68
- data/lib/active_record/relation/predicate_builder/array_handler.rb +24 -23
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/predicate_builder.rb +118 -92
- data/lib/active_record/relation/query_attribute.rb +45 -0
- data/lib/active_record/relation/query_methods.rb +446 -389
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +18 -16
- data/lib/active_record/relation/where_clause.rb +186 -0
- data/lib/active_record/relation/where_clause_factory.rb +34 -0
- data/lib/active_record/relation.rb +287 -339
- data/lib/active_record/result.rb +54 -36
- data/lib/active_record/runtime_registry.rb +6 -4
- data/lib/active_record/sanitization.rb +155 -124
- data/lib/active_record/schema.rb +30 -24
- data/lib/active_record/schema_dumper.rb +91 -87
- data/lib/active_record/schema_migration.rb +19 -19
- data/lib/active_record/scoping/default.rb +102 -84
- data/lib/active_record/scoping/named.rb +81 -32
- data/lib/active_record/scoping.rb +45 -26
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +5 -5
- data/lib/active_record/statement_cache.rb +45 -35
- data/lib/active_record/store.rb +42 -36
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +82 -0
- data/lib/active_record/tasks/database_tasks.rb +136 -95
- data/lib/active_record/tasks/mysql_database_tasks.rb +59 -89
- data/lib/active_record/tasks/postgresql_database_tasks.rb +84 -31
- data/lib/active_record/tasks/sqlite_database_tasks.rb +44 -16
- data/lib/active_record/timestamp.rb +70 -38
- data/lib/active_record/touch_later.rb +64 -0
- data/lib/active_record/transactions.rb +208 -123
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type/adapter_specific_registry.rb +136 -0
- data/lib/active_record/type/date.rb +4 -41
- data/lib/active_record/type/date_time.rb +4 -38
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +13 -5
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +30 -15
- data/lib/active_record/type/text.rb +2 -2
- data/lib/active_record/type/time.rb +11 -16
- data/lib/active_record/type/type_map.rb +15 -17
- data/lib/active_record/type/unsigned_integer.rb +9 -7
- data/lib/active_record/type.rb +79 -23
- data/lib/active_record/type_caster/connection.rb +33 -0
- data/lib/active_record/type_caster/map.rb +23 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +13 -4
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +14 -13
- data/lib/active_record/validations/uniqueness.rb +41 -32
- data/lib/active_record/validations.rb +38 -35
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +36 -21
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +43 -35
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +8 -6
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +8 -7
- data/lib/rails/generators/active_record/migration.rb +18 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +18 -22
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +3 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +77 -53
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute.rb +0 -149
- data/lib/active_record/attribute_set/builder.rb +0 -86
- data/lib/active_record/attribute_set.rb +0 -77
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +0 -13
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -30
- data/lib/active_record/type/decimal.rb +0 -40
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -55
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -36
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -101
- /data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# = Active Record Touch Later
|
5
|
+
module TouchLater
|
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
|
+
self.class.connection.add_transaction_record self
|
26
|
+
|
27
|
+
# touch the parents as we are not calling the after_save callbacks
|
28
|
+
self.class.reflect_on_all_associations(:belongs_to).each do |r|
|
29
|
+
if touch = r.options[:touch]
|
30
|
+
ActiveRecord::Associations::Builder::BelongsTo.touch_record(self, changes_to_save, r.foreign_key, r.name, touch, :touch_later)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def touch(*names, time: nil) # :nodoc:
|
36
|
+
if has_defer_touch_attrs?
|
37
|
+
names |= @_defer_touch_attrs
|
38
|
+
end
|
39
|
+
super(*names, time: time)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def surreptitiously_touch(attrs)
|
45
|
+
attrs.each { |attr| write_attribute attr, @_touch_time }
|
46
|
+
clear_attribute_changes attrs
|
47
|
+
end
|
48
|
+
|
49
|
+
def touch_deferred_attributes
|
50
|
+
if has_defer_touch_attrs? && persisted?
|
51
|
+
touch(*@_defer_touch_attrs, time: @_touch_time)
|
52
|
+
@_defer_touch_attrs, @_touch_time = nil, nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_defer_touch_attrs?
|
57
|
+
defined?(@_defer_touch_attrs) && @_defer_touch_attrs.present?
|
58
|
+
end
|
59
|
+
|
60
|
+
def belongs_to_touch_method
|
61
|
+
:touch_later
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -1,35 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
# See ActiveRecord::Transactions::ClassMethods for documentation.
|
3
5
|
module Transactions
|
4
6
|
extend ActiveSupport::Concern
|
5
7
|
#:nodoc:
|
6
8
|
ACTIONS = [:create, :destroy, :update]
|
7
|
-
#:nodoc:
|
8
|
-
CALLBACK_WARN_MESSAGE = "Currently, Active Record suppresses errors raised " \
|
9
|
-
"within `after_rollback`/`after_commit` callbacks and only print them to " \
|
10
|
-
"the logs. In the next version, these errors will no longer be suppressed. " \
|
11
|
-
"Instead, the errors will propagate normally just like in other Active " \
|
12
|
-
"Record callbacks.\n" \
|
13
|
-
"\n" \
|
14
|
-
"You can opt into the new behavior and remove this warning by setting:\n" \
|
15
|
-
"\n" \
|
16
|
-
" config.active_record.raise_in_transactional_callbacks = true\n\n"
|
17
9
|
|
18
10
|
included do
|
19
11
|
define_callbacks :commit, :rollback,
|
20
|
-
|
12
|
+
:before_commit,
|
13
|
+
:before_commit_without_transaction_enrollment,
|
14
|
+
:commit_without_transaction_enrollment,
|
15
|
+
:rollback_without_transaction_enrollment,
|
21
16
|
scope: [:kind, :name]
|
22
|
-
|
23
|
-
mattr_accessor :raise_in_transactional_callbacks, instance_writer: false
|
24
|
-
self.raise_in_transactional_callbacks = false
|
25
17
|
end
|
26
18
|
|
27
19
|
# = Active Record Transactions
|
28
20
|
#
|
29
|
-
# Transactions are protective blocks where SQL statements are only permanent
|
21
|
+
# \Transactions are protective blocks where SQL statements are only permanent
|
30
22
|
# if they can all succeed as one atomic action. The classic example is a
|
31
23
|
# transfer between two accounts where you can only have a deposit if the
|
32
|
-
# withdrawal succeeded and vice versa. Transactions enforce the integrity of
|
24
|
+
# withdrawal succeeded and vice versa. \Transactions enforce the integrity of
|
33
25
|
# the database and guard the data against program errors or database
|
34
26
|
# break-downs. So basically you should use transaction blocks whenever you
|
35
27
|
# have a number of statements that must be executed together or not at all.
|
@@ -49,20 +41,20 @@ module ActiveRecord
|
|
49
41
|
#
|
50
42
|
# == Different Active Record classes in a single transaction
|
51
43
|
#
|
52
|
-
# Though the transaction class method is called on some Active Record class,
|
44
|
+
# Though the #transaction class method is called on some Active Record class,
|
53
45
|
# the objects within the transaction block need not all be instances of
|
54
46
|
# that class. This is because transactions are per-database connection, not
|
55
47
|
# per-model.
|
56
48
|
#
|
57
49
|
# In this example a +balance+ record is transactionally saved even
|
58
|
-
# though
|
50
|
+
# though #transaction is called on the +Account+ class:
|
59
51
|
#
|
60
52
|
# Account.transaction do
|
61
53
|
# balance.save!
|
62
54
|
# account.save!
|
63
55
|
# end
|
64
56
|
#
|
65
|
-
# The
|
57
|
+
# The #transaction method is also available as a model instance method.
|
66
58
|
# For example, you can also do this:
|
67
59
|
#
|
68
60
|
# balance.transaction do
|
@@ -89,7 +81,8 @@ module ActiveRecord
|
|
89
81
|
#
|
90
82
|
# == +save+ and +destroy+ are automatically wrapped in a transaction
|
91
83
|
#
|
92
|
-
# Both
|
84
|
+
# Both {#save}[rdoc-ref:Persistence#save] and
|
85
|
+
# {#destroy}[rdoc-ref:Persistence#destroy] come wrapped in a transaction that ensures
|
93
86
|
# that whatever you do in validations or callbacks will happen under its
|
94
87
|
# protected cover. So you can use validations to check for values that
|
95
88
|
# the transaction depends on or you can raise exceptions in the callbacks
|
@@ -98,7 +91,7 @@ module ActiveRecord
|
|
98
91
|
# As a consequence changes to the database are not seen outside your connection
|
99
92
|
# until the operation is complete. For example, if you try to update the index
|
100
93
|
# of a search engine in +after_save+ the indexer won't see the updated record.
|
101
|
-
# The
|
94
|
+
# The #after_commit callback is the only one that is triggered once the update
|
102
95
|
# is committed. See below.
|
103
96
|
#
|
104
97
|
# == Exception handling and rolling back
|
@@ -107,11 +100,11 @@ module ActiveRecord
|
|
107
100
|
# be propagated (after triggering the ROLLBACK), so you should be ready to
|
108
101
|
# catch those in your application code.
|
109
102
|
#
|
110
|
-
# One exception is the
|
103
|
+
# One exception is the ActiveRecord::Rollback exception, which will trigger
|
111
104
|
# a ROLLBACK when raised, but not be re-raised by the transaction block.
|
112
105
|
#
|
113
|
-
# *Warning*: one should not catch
|
114
|
-
# inside a transaction block.
|
106
|
+
# *Warning*: one should not catch ActiveRecord::StatementInvalid exceptions
|
107
|
+
# inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an
|
115
108
|
# error occurred at the database level, for example when a unique constraint
|
116
109
|
# is violated. On some database systems, such as PostgreSQL, database errors
|
117
110
|
# inside a transaction cause the entire transaction to become unusable
|
@@ -132,16 +125,16 @@ module ActiveRecord
|
|
132
125
|
# # statement will cause a PostgreSQL error, even though the unique
|
133
126
|
# # constraint is no longer violated:
|
134
127
|
# Number.create(i: 1)
|
135
|
-
# # => "
|
128
|
+
# # => "PG::Error: ERROR: current transaction is aborted, commands
|
136
129
|
# # ignored until end of transaction block"
|
137
130
|
# end
|
138
131
|
#
|
139
132
|
# One should restart the entire transaction if an
|
140
|
-
#
|
133
|
+
# ActiveRecord::StatementInvalid occurred.
|
141
134
|
#
|
142
135
|
# == Nested transactions
|
143
136
|
#
|
144
|
-
#
|
137
|
+
# #transaction calls can be nested. By default, this makes all database
|
145
138
|
# statements in the nested transaction block become part of the parent
|
146
139
|
# transaction. For example, the following behavior may be surprising:
|
147
140
|
#
|
@@ -153,7 +146,7 @@ module ActiveRecord
|
|
153
146
|
# end
|
154
147
|
# end
|
155
148
|
#
|
156
|
-
# creates both "Kotori" and "Nemu". Reason is the
|
149
|
+
# creates both "Kotori" and "Nemu". Reason is the ActiveRecord::Rollback
|
157
150
|
# exception in the nested block does not issue a ROLLBACK. Since these exceptions
|
158
151
|
# are captured in transaction blocks, the parent block does not see it and the
|
159
152
|
# real transaction is committed.
|
@@ -177,28 +170,28 @@ module ActiveRecord
|
|
177
170
|
# writing, the only database that we're aware of that supports true nested
|
178
171
|
# transactions, is MS-SQL. Because of this, Active Record emulates nested
|
179
172
|
# transactions by using savepoints on MySQL and PostgreSQL. See
|
180
|
-
#
|
173
|
+
# https://dev.mysql.com/doc/refman/5.7/en/savepoint.html
|
181
174
|
# for more information about savepoints.
|
182
175
|
#
|
183
|
-
# === Callbacks
|
176
|
+
# === \Callbacks
|
184
177
|
#
|
185
178
|
# There are two types of callbacks associated with committing and rolling back transactions:
|
186
|
-
#
|
179
|
+
# #after_commit and #after_rollback.
|
187
180
|
#
|
188
|
-
#
|
189
|
-
# transaction immediately after the transaction is committed.
|
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
|
190
183
|
# are called on every record saved or destroyed within a transaction immediately after the
|
191
184
|
# transaction or savepoint is rolled back.
|
192
185
|
#
|
193
186
|
# These callbacks are useful for interacting with other systems since you will be guaranteed
|
194
187
|
# that the callback is only executed when the database is in a permanent state. For example,
|
195
|
-
#
|
188
|
+
# #after_commit is a good spot to put in a hook to clearing a cache since clearing it from
|
196
189
|
# within a transaction could trigger the cache to be regenerated before the database is updated.
|
197
190
|
#
|
198
191
|
# === Caveats
|
199
192
|
#
|
200
|
-
# If you're on MySQL, then do not use DDL operations in nested
|
201
|
-
# blocks that are emulated with savepoints. That is, do not execute statements
|
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
|
202
195
|
# like 'CREATE TABLE' inside such blocks. This is because MySQL automatically
|
203
196
|
# releases all savepoints upon executing a DDL operation. When +transaction+
|
204
197
|
# is finished and tries to release the savepoint it created earlier, a
|
@@ -206,20 +199,24 @@ module ActiveRecord
|
|
206
199
|
# automatically released. The following example demonstrates the problem:
|
207
200
|
#
|
208
201
|
# Model.connection.transaction do # BEGIN
|
209
|
-
# Model.connection.transaction(requires_new: true) do
|
202
|
+
# Model.connection.transaction(requires_new: true) do # CREATE SAVEPOINT active_record_1
|
210
203
|
# Model.connection.create_table(...) # active_record_1 now automatically released
|
211
|
-
# end # RELEASE
|
204
|
+
# end # RELEASE SAVEPOINT active_record_1
|
212
205
|
# # ^^^^ BOOM! database error!
|
213
206
|
# end
|
214
207
|
#
|
215
208
|
# Note that "TRUNCATE" is also a MySQL DDL statement!
|
216
209
|
module ClassMethods
|
217
|
-
# See
|
210
|
+
# See the ConnectionAdapters::DatabaseStatements#transaction API docs.
|
218
211
|
def transaction(options = {}, &block)
|
219
|
-
# See the ConnectionAdapters::DatabaseStatements#transaction API docs.
|
220
212
|
connection.transaction(options, &block)
|
221
213
|
end
|
222
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
|
+
|
223
220
|
# This callback is called after a record has been created, updated, or destroyed.
|
224
221
|
#
|
225
222
|
# You can specify that the callback should only be fired by a certain action with
|
@@ -232,44 +229,71 @@ module ActiveRecord
|
|
232
229
|
# after_commit :do_foo_bar, on: [:create, :update]
|
233
230
|
# after_commit :do_bar_baz, on: [:update, :destroy]
|
234
231
|
#
|
235
|
-
# Note that transactional fixtures do not play well with this feature. Please
|
236
|
-
# use the +test_after_commit+ gem to have these hooks fired in tests.
|
237
232
|
def after_commit(*args, &block)
|
238
233
|
set_options_for_callbacks!(args)
|
239
234
|
set_callback(:commit, :after, *args, &block)
|
240
|
-
|
241
|
-
|
242
|
-
|
235
|
+
end
|
236
|
+
|
237
|
+
# Shortcut for <tt>after_commit :hook, on: :create</tt>.
|
238
|
+
def after_create_commit(*args, &block)
|
239
|
+
set_options_for_callbacks!(args, on: :create)
|
240
|
+
set_callback(:commit, :after, *args, &block)
|
241
|
+
end
|
242
|
+
|
243
|
+
# Shortcut for <tt>after_commit :hook, on: :update</tt>.
|
244
|
+
def after_update_commit(*args, &block)
|
245
|
+
set_options_for_callbacks!(args, on: :update)
|
246
|
+
set_callback(:commit, :after, *args, &block)
|
247
|
+
end
|
248
|
+
|
249
|
+
# Shortcut for <tt>after_commit :hook, on: :destroy</tt>.
|
250
|
+
def after_destroy_commit(*args, &block)
|
251
|
+
set_options_for_callbacks!(args, on: :destroy)
|
252
|
+
set_callback(:commit, :after, *args, &block)
|
243
253
|
end
|
244
254
|
|
245
255
|
# This callback is called after a create, update, or destroy are rolled back.
|
246
256
|
#
|
247
|
-
# Please check the documentation of
|
257
|
+
# Please check the documentation of #after_commit for options.
|
248
258
|
def after_rollback(*args, &block)
|
249
259
|
set_options_for_callbacks!(args)
|
250
260
|
set_callback(:rollback, :after, *args, &block)
|
251
|
-
|
252
|
-
|
253
|
-
|
261
|
+
end
|
262
|
+
|
263
|
+
def before_commit_without_transaction_enrollment(*args, &block) # :nodoc:
|
264
|
+
set_options_for_callbacks!(args)
|
265
|
+
set_callback(:before_commit_without_transaction_enrollment, :before, *args, &block)
|
266
|
+
end
|
267
|
+
|
268
|
+
def after_commit_without_transaction_enrollment(*args, &block) # :nodoc:
|
269
|
+
set_options_for_callbacks!(args)
|
270
|
+
set_callback(:commit_without_transaction_enrollment, :after, *args, &block)
|
271
|
+
end
|
272
|
+
|
273
|
+
def after_rollback_without_transaction_enrollment(*args, &block) # :nodoc:
|
274
|
+
set_options_for_callbacks!(args)
|
275
|
+
set_callback(:rollback_without_transaction_enrollment, :after, *args, &block)
|
254
276
|
end
|
255
277
|
|
256
278
|
private
|
257
279
|
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
280
|
+
def set_options_for_callbacks!(args, enforced_options = {})
|
281
|
+
options = args.extract_options!.merge!(enforced_options)
|
282
|
+
args << options
|
283
|
+
|
284
|
+
if options[:on]
|
285
|
+
fire_on = Array(options[:on])
|
286
|
+
assert_valid_transaction_action(fire_on)
|
287
|
+
options[:if] = Array(options[:if])
|
288
|
+
options[:if].unshift(-> { transaction_include_any_action?(fire_on) })
|
289
|
+
end
|
265
290
|
end
|
266
|
-
end
|
267
291
|
|
268
|
-
|
269
|
-
|
270
|
-
|
292
|
+
def assert_valid_transaction_action(actions)
|
293
|
+
if (actions - ACTIONS).any?
|
294
|
+
raise ArgumentError, ":on conditions for after_commit and after_rollback callbacks have to be one of #{ACTIONS}"
|
295
|
+
end
|
271
296
|
end
|
272
|
-
end
|
273
297
|
end
|
274
298
|
|
275
299
|
# See ActiveRecord::Transactions::ClassMethods for detailed documentation.
|
@@ -306,31 +330,48 @@ module ActiveRecord
|
|
306
330
|
clear_transaction_record_state
|
307
331
|
end
|
308
332
|
|
309
|
-
|
333
|
+
def before_committed! # :nodoc:
|
334
|
+
_run_before_commit_without_transaction_enrollment_callbacks
|
335
|
+
_run_before_commit_callbacks
|
336
|
+
end
|
337
|
+
|
338
|
+
# Call the #after_commit callbacks.
|
310
339
|
#
|
311
340
|
# Ensure that it is not called if the object was never persisted (failed create),
|
312
341
|
# but call it after the commit of a destroyed object.
|
313
|
-
def committed!(should_run_callbacks
|
314
|
-
_run_commit_callbacks if should_run_callbacks && destroyed? || persisted?
|
315
|
-
ensure
|
342
|
+
def committed!(should_run_callbacks: true) #:nodoc:
|
316
343
|
force_clear_transaction_record_state
|
344
|
+
if should_run_callbacks && (destroyed? || persisted?)
|
345
|
+
@_committed_already_called = true
|
346
|
+
_run_commit_without_transaction_enrollment_callbacks
|
347
|
+
_run_commit_callbacks
|
348
|
+
end
|
349
|
+
ensure
|
350
|
+
@_committed_already_called = false
|
317
351
|
end
|
318
352
|
|
319
|
-
# Call the
|
353
|
+
# Call the #after_rollback callbacks. The +force_restore_state+ argument indicates if the record
|
320
354
|
# state should be rolled back to the beginning or just to the last savepoint.
|
321
|
-
def rolledback!(force_restore_state
|
322
|
-
|
355
|
+
def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
|
356
|
+
if should_run_callbacks
|
357
|
+
_run_rollback_callbacks
|
358
|
+
_run_rollback_without_transaction_enrollment_callbacks
|
359
|
+
end
|
323
360
|
ensure
|
324
361
|
restore_transaction_record_state(force_restore_state)
|
325
362
|
clear_transaction_record_state
|
326
363
|
end
|
327
364
|
|
328
|
-
# Add the record to the current transaction so that the
|
365
|
+
# Add the record to the current transaction so that the #after_rollback and #after_commit callbacks
|
329
366
|
# can be called.
|
330
367
|
def add_to_transaction
|
331
|
-
if
|
332
|
-
|
368
|
+
if has_transactional_callbacks?
|
369
|
+
self.class.connection.add_transaction_record(self)
|
370
|
+
else
|
371
|
+
sync_with_transaction_state
|
372
|
+
set_transaction_state(self.class.connection.transaction_state)
|
333
373
|
end
|
374
|
+
remember_transaction_record_state
|
334
375
|
end
|
335
376
|
|
336
377
|
# Executes +method+ within a transaction and captures its return value as a
|
@@ -343,75 +384,119 @@ module ActiveRecord
|
|
343
384
|
status = nil
|
344
385
|
self.class.transaction do
|
345
386
|
add_to_transaction
|
346
|
-
|
347
|
-
status = yield
|
348
|
-
rescue ActiveRecord::Rollback
|
349
|
-
clear_transaction_record_state
|
350
|
-
status = nil
|
351
|
-
end
|
352
|
-
|
387
|
+
status = yield
|
353
388
|
raise ActiveRecord::Rollback unless status
|
354
389
|
end
|
355
390
|
status
|
391
|
+
ensure
|
392
|
+
if @transaction_state && @transaction_state.committed?
|
393
|
+
clear_transaction_record_state
|
394
|
+
end
|
356
395
|
end
|
357
396
|
|
358
397
|
protected
|
398
|
+
attr_reader :_committed_already_called, :_trigger_update_callback, :_trigger_destroy_callback
|
399
|
+
|
400
|
+
private
|
359
401
|
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
402
|
+
# Save the new record state and id of a record so it can be restored later if a transaction fails.
|
403
|
+
def remember_transaction_record_state
|
404
|
+
@_start_transaction_state.reverse_merge!(
|
405
|
+
id: id,
|
406
|
+
new_record: @new_record,
|
407
|
+
destroyed: @destroyed,
|
408
|
+
frozen?: frozen?,
|
409
|
+
)
|
410
|
+
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
|
411
|
+
remember_new_record_before_last_commit
|
365
412
|
end
|
366
|
-
|
367
|
-
|
413
|
+
|
414
|
+
def remember_new_record_before_last_commit
|
415
|
+
if _committed_already_called
|
416
|
+
@_new_record_before_last_commit = false
|
417
|
+
else
|
418
|
+
@_new_record_before_last_commit = @_start_transaction_state[:new_record]
|
419
|
+
end
|
368
420
|
end
|
369
|
-
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
|
370
|
-
@_start_transaction_state[:frozen?] = frozen?
|
371
|
-
end
|
372
421
|
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
422
|
+
# Clear the new record state and id of a record.
|
423
|
+
def clear_transaction_record_state
|
424
|
+
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
|
425
|
+
force_clear_transaction_record_state if @_start_transaction_state[:level] < 1
|
426
|
+
end
|
378
427
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
428
|
+
# Force to clear the transaction record state.
|
429
|
+
def force_clear_transaction_record_state
|
430
|
+
@_start_transaction_state.clear
|
431
|
+
end
|
383
432
|
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
433
|
+
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
|
434
|
+
def restore_transaction_record_state(force = false)
|
435
|
+
unless @_start_transaction_state.empty?
|
436
|
+
transaction_level = (@_start_transaction_state[:level] || 0) - 1
|
437
|
+
if transaction_level < 1 || force
|
438
|
+
restore_state = @_start_transaction_state
|
439
|
+
thaw
|
440
|
+
@new_record = restore_state[:new_record]
|
441
|
+
@destroyed = restore_state[:destroyed]
|
442
|
+
pk = self.class.primary_key
|
443
|
+
if pk && _read_attribute(pk) != restore_state[:id]
|
444
|
+
_write_attribute(pk, restore_state[:id])
|
445
|
+
end
|
446
|
+
freeze if restore_state[:frozen?]
|
447
|
+
end
|
394
448
|
end
|
395
449
|
end
|
396
|
-
end
|
397
450
|
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
451
|
+
# Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
|
452
|
+
def transaction_include_any_action?(actions)
|
453
|
+
actions.any? do |action|
|
454
|
+
case action
|
455
|
+
when :create
|
456
|
+
persisted? && @_new_record_before_last_commit
|
457
|
+
when :update
|
458
|
+
!(@_new_record_before_last_commit || destroyed?) && _trigger_update_callback
|
459
|
+
when :destroy
|
460
|
+
_trigger_destroy_callback
|
461
|
+
end
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
def set_transaction_state(state)
|
466
|
+
@transaction_state = state
|
467
|
+
end
|
402
468
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
469
|
+
def has_transactional_callbacks?
|
470
|
+
!_rollback_callbacks.empty? || !_commit_callbacks.empty? || !_before_commit_callbacks.empty?
|
471
|
+
end
|
472
|
+
|
473
|
+
# Updates the attributes on this particular Active Record object so that
|
474
|
+
# if it's associated with a transaction, then the state of the Active Record
|
475
|
+
# object will be updated to reflect the current state of the transaction.
|
476
|
+
#
|
477
|
+
# The <tt>@transaction_state</tt> variable stores the states of the associated
|
478
|
+
# transaction. This relies on the fact that a transaction can only be in
|
479
|
+
# one rollback or commit (otherwise a list of states would be required).
|
480
|
+
# Each Active Record object inside of a transaction carries that transaction's
|
481
|
+
# TransactionState.
|
482
|
+
#
|
483
|
+
# This method checks to see if the ActiveRecord object's state reflects
|
484
|
+
# the TransactionState, and rolls back or commits the Active Record object
|
485
|
+
# as appropriate.
|
486
|
+
#
|
487
|
+
# Since Active Record objects can be inside multiple transactions, this
|
488
|
+
# method recursively goes through the parent of the TransactionState and
|
489
|
+
# checks if the Active Record object reflects the state of the object.
|
490
|
+
def sync_with_transaction_state
|
491
|
+
update_attributes_from_transaction_state(@transaction_state)
|
492
|
+
end
|
493
|
+
|
494
|
+
def update_attributes_from_transaction_state(transaction_state)
|
495
|
+
if transaction_state && transaction_state.finalized?
|
496
|
+
restore_transaction_record_state(transaction_state.fully_rolledback?) if transaction_state.rolledback?
|
497
|
+
force_clear_transaction_record_state if transaction_state.fully_committed?
|
498
|
+
clear_transaction_record_state if transaction_state.fully_completed?
|
413
499
|
end
|
414
500
|
end
|
415
|
-
end
|
416
501
|
end
|
417
502
|
end
|