activerecord 4.2.11.3 → 5.0.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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1281 -1204
- data/MIT-LICENSE +2 -2
- data/README.rdoc +7 -8
- data/examples/performance.rb +2 -3
- data/examples/simple.rb +0 -1
- data/lib/active_record/aggregations.rb +35 -24
- data/lib/active_record/association_relation.rb +3 -3
- data/lib/active_record/associations/alias_tracker.rb +19 -16
- data/lib/active_record/associations/association.rb +11 -9
- data/lib/active_record/associations/association_scope.rb +73 -102
- data/lib/active_record/associations/belongs_to_association.rb +21 -32
- data/lib/active_record/associations/builder/association.rb +28 -34
- data/lib/active_record/associations/builder/belongs_to.rb +43 -18
- data/lib/active_record/associations/builder/collection_association.rb +7 -19
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +14 -11
- data/lib/active_record/associations/builder/has_many.rb +4 -4
- data/lib/active_record/associations/builder/has_one.rb +11 -6
- data/lib/active_record/associations/builder/singular_association.rb +3 -10
- data/lib/active_record/associations/collection_association.rb +49 -41
- data/lib/active_record/associations/collection_proxy.rb +67 -27
- data/lib/active_record/associations/foreign_association.rb +1 -1
- data/lib/active_record/associations/has_many_association.rb +20 -71
- data/lib/active_record/associations/has_many_through_association.rb +8 -47
- data/lib/active_record/associations/has_one_association.rb +12 -5
- data/lib/active_record/associations/join_dependency/join_association.rb +16 -10
- data/lib/active_record/associations/join_dependency.rb +29 -19
- data/lib/active_record/associations/preloader/association.rb +46 -52
- data/lib/active_record/associations/preloader/collection_association.rb +0 -6
- data/lib/active_record/associations/preloader/has_many_through.rb +1 -1
- data/lib/active_record/associations/preloader/has_one.rb +0 -8
- data/lib/active_record/associations/preloader/through_association.rb +27 -14
- data/lib/active_record/associations/preloader.rb +14 -4
- data/lib/active_record/associations/singular_association.rb +7 -1
- data/lib/active_record/associations/through_association.rb +11 -3
- data/lib/active_record/associations.rb +317 -209
- data/lib/active_record/attribute/user_provided_default.rb +28 -0
- data/lib/active_record/attribute.rb +68 -18
- data/lib/active_record/attribute_assignment.rb +19 -140
- data/lib/active_record/attribute_decorators.rb +6 -5
- data/lib/active_record/attribute_methods/before_type_cast.rb +1 -1
- data/lib/active_record/attribute_methods/dirty.rb +46 -86
- data/lib/active_record/attribute_methods/primary_key.rb +2 -2
- data/lib/active_record/attribute_methods/query.rb +2 -2
- data/lib/active_record/attribute_methods/read.rb +31 -59
- data/lib/active_record/attribute_methods/serialization.rb +13 -16
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +61 -14
- data/lib/active_record/attribute_methods/write.rb +13 -37
- data/lib/active_record/attribute_methods.rb +76 -47
- data/lib/active_record/attribute_mutation_tracker.rb +70 -0
- data/lib/active_record/attribute_set/builder.rb +6 -4
- data/lib/active_record/attribute_set.rb +30 -3
- data/lib/active_record/attributes.rb +199 -81
- data/lib/active_record/autosave_association.rb +49 -16
- data/lib/active_record/base.rb +32 -23
- data/lib/active_record/callbacks.rb +39 -43
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +20 -8
- data/lib/active_record/collection_cache_key.rb +40 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +452 -182
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +65 -61
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +2 -2
- data/lib/active_record/connection_adapters/abstract/quoting.rb +74 -10
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +61 -39
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +236 -185
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +72 -17
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +380 -141
- data/lib/active_record/connection_adapters/abstract/transaction.rb +51 -34
- data/lib/active_record/connection_adapters/abstract_adapter.rb +141 -59
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +401 -370
- data/lib/active_record/connection_adapters/column.rb +28 -43
- data/lib/active_record/connection_adapters/connection_specification.rb +15 -27
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +22 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +50 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +125 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +70 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +51 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +67 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +93 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +54 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +32 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +29 -166
- data/lib/active_record/connection_adapters/postgresql/column.rb +5 -10
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +10 -72
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +42 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +27 -57
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +7 -22
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +1 -26
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +0 -4
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +4 -4
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +31 -17
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +0 -4
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid.rb +1 -6
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +26 -18
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +29 -10
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -79
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +47 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +234 -148
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +248 -160
- data/lib/active_record/connection_adapters/schema_cache.rb +36 -23
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +32 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +48 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +149 -192
- data/lib/active_record/connection_adapters/statement_pool.rb +31 -12
- data/lib/active_record/connection_handling.rb +37 -14
- data/lib/active_record/core.rb +89 -107
- data/lib/active_record/counter_cache.rb +13 -24
- data/lib/active_record/dynamic_matchers.rb +1 -20
- data/lib/active_record/enum.rb +113 -76
- data/lib/active_record/errors.rb +87 -48
- data/lib/active_record/explain_registry.rb +1 -1
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +26 -5
- data/lib/active_record/fixtures.rb +76 -40
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +32 -40
- data/lib/active_record/integration.rb +4 -4
- data/lib/active_record/internal_metadata.rb +56 -0
- data/lib/active_record/legacy_yaml_adapter.rb +18 -2
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +15 -15
- data/lib/active_record/locking/pessimistic.rb +1 -1
- data/lib/active_record/log_subscriber.rb +43 -21
- data/lib/active_record/migration/command_recorder.rb +59 -18
- data/lib/active_record/migration/compatibility.rb +126 -0
- data/lib/active_record/migration.rb +363 -133
- data/lib/active_record/model_schema.rb +129 -41
- data/lib/active_record/nested_attributes.rb +58 -29
- data/lib/active_record/null_relation.rb +16 -8
- data/lib/active_record/persistence.rb +121 -80
- data/lib/active_record/query_cache.rb +15 -18
- data/lib/active_record/querying.rb +10 -9
- data/lib/active_record/railtie.rb +23 -16
- data/lib/active_record/railties/controller_runtime.rb +1 -1
- data/lib/active_record/railties/databases.rake +69 -46
- data/lib/active_record/readonly_attributes.rb +1 -1
- data/lib/active_record/reflection.rb +282 -115
- data/lib/active_record/relation/batches/batch_enumerator.rb +67 -0
- data/lib/active_record/relation/batches.rb +139 -34
- data/lib/active_record/relation/calculations.rb +79 -108
- data/lib/active_record/relation/delegation.rb +7 -20
- data/lib/active_record/relation/finder_methods.rb +163 -81
- data/lib/active_record/relation/from_clause.rb +32 -0
- data/lib/active_record/relation/merger.rb +16 -42
- data/lib/active_record/relation/predicate_builder/array_handler.rb +11 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +88 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +17 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +17 -0
- data/lib/active_record/relation/predicate_builder/class_handler.rb +27 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +57 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +33 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
- data/lib/active_record/relation/predicate_builder.rb +120 -107
- data/lib/active_record/relation/query_attribute.rb +19 -0
- data/lib/active_record/relation/query_methods.rb +308 -244
- data/lib/active_record/relation/record_fetch_warning.rb +49 -0
- data/lib/active_record/relation/spawn_methods.rb +4 -7
- data/lib/active_record/relation/where_clause.rb +174 -0
- data/lib/active_record/relation/where_clause_factory.rb +38 -0
- data/lib/active_record/relation.rb +176 -116
- data/lib/active_record/result.rb +4 -3
- data/lib/active_record/runtime_registry.rb +1 -1
- data/lib/active_record/sanitization.rb +95 -66
- data/lib/active_record/schema.rb +26 -22
- data/lib/active_record/schema_dumper.rb +62 -38
- data/lib/active_record/schema_migration.rb +11 -14
- data/lib/active_record/scoping/default.rb +23 -9
- data/lib/active_record/scoping/named.rb +49 -28
- data/lib/active_record/scoping.rb +32 -15
- data/lib/active_record/secure_token.rb +38 -0
- data/lib/active_record/serialization.rb +2 -4
- data/lib/active_record/statement_cache.rb +16 -14
- data/lib/active_record/store.rb +8 -3
- data/lib/active_record/suppressor.rb +58 -0
- data/lib/active_record/table_metadata.rb +68 -0
- data/lib/active_record/tasks/database_tasks.rb +57 -43
- data/lib/active_record/tasks/mysql_database_tasks.rb +6 -14
- data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -2
- data/lib/active_record/tasks/sqlite_database_tasks.rb +5 -1
- data/lib/active_record/timestamp.rb +20 -9
- data/lib/active_record/touch_later.rb +58 -0
- data/lib/active_record/transactions.rb +138 -56
- data/lib/active_record/type/adapter_specific_registry.rb +130 -0
- data/lib/active_record/type/date.rb +2 -45
- data/lib/active_record/type/date_time.rb +2 -49
- data/lib/active_record/type/internal/abstract_json.rb +29 -0
- data/lib/active_record/type/internal/timezone.rb +15 -0
- data/lib/active_record/type/serialized.rb +15 -14
- data/lib/active_record/type/time.rb +10 -16
- data/lib/active_record/type/type_map.rb +4 -4
- data/lib/active_record/type.rb +66 -17
- data/lib/active_record/type_caster/connection.rb +29 -0
- data/lib/active_record/type_caster/map.rb +19 -0
- data/lib/active_record/type_caster.rb +7 -0
- data/lib/active_record/validations/absence.rb +23 -0
- data/lib/active_record/validations/associated.rb +10 -3
- data/lib/active_record/validations/length.rb +24 -0
- data/lib/active_record/validations/presence.rb +11 -12
- data/lib/active_record/validations/uniqueness.rb +30 -29
- data/lib/active_record/validations.rb +33 -32
- data/lib/active_record.rb +8 -4
- data/lib/rails/generators/active_record/migration/migration_generator.rb +7 -4
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +8 -3
- data/lib/rails/generators/active_record/migration/templates/migration.rb +8 -1
- data/lib/rails/generators/active_record/migration.rb +7 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +32 -15
- data/lib/rails/generators/active_record/model/templates/application_record.rb +5 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +3 -0
- metadata +60 -34
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -498
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +0 -11
- 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/integer.rb +0 -11
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- 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 -31
- data/lib/active_record/type/decimal.rb +0 -64
- data/lib/active_record/type/decimal_without_scale.rb +0 -11
- 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 -59
- 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 -40
- data/lib/active_record/type/text.rb +0 -11
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/unsigned_integer.rb +0 -15
- data/lib/active_record/type/value.rb +0 -110
@@ -4,32 +4,23 @@ module ActiveRecord
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
#:nodoc:
|
6
6
|
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
7
|
|
18
8
|
included do
|
19
9
|
define_callbacks :commit, :rollback,
|
20
|
-
|
10
|
+
:before_commit,
|
11
|
+
:before_commit_without_transaction_enrollment,
|
12
|
+
:commit_without_transaction_enrollment,
|
13
|
+
:rollback_without_transaction_enrollment,
|
14
|
+
terminator: deprecated_false_terminator,
|
21
15
|
scope: [:kind, :name]
|
22
|
-
|
23
|
-
mattr_accessor :raise_in_transactional_callbacks, instance_writer: false
|
24
|
-
self.raise_in_transactional_callbacks = false
|
25
16
|
end
|
26
17
|
|
27
18
|
# = Active Record Transactions
|
28
19
|
#
|
29
|
-
# Transactions are protective blocks where SQL statements are only permanent
|
20
|
+
# \Transactions are protective blocks where SQL statements are only permanent
|
30
21
|
# if they can all succeed as one atomic action. The classic example is a
|
31
22
|
# transfer between two accounts where you can only have a deposit if the
|
32
|
-
# withdrawal succeeded and vice versa. Transactions enforce the integrity of
|
23
|
+
# withdrawal succeeded and vice versa. \Transactions enforce the integrity of
|
33
24
|
# the database and guard the data against program errors or database
|
34
25
|
# break-downs. So basically you should use transaction blocks whenever you
|
35
26
|
# have a number of statements that must be executed together or not at all.
|
@@ -49,20 +40,20 @@ module ActiveRecord
|
|
49
40
|
#
|
50
41
|
# == Different Active Record classes in a single transaction
|
51
42
|
#
|
52
|
-
# Though the transaction class method is called on some Active Record class,
|
43
|
+
# Though the #transaction class method is called on some Active Record class,
|
53
44
|
# the objects within the transaction block need not all be instances of
|
54
45
|
# that class. This is because transactions are per-database connection, not
|
55
46
|
# per-model.
|
56
47
|
#
|
57
48
|
# In this example a +balance+ record is transactionally saved even
|
58
|
-
# though
|
49
|
+
# though #transaction is called on the +Account+ class:
|
59
50
|
#
|
60
51
|
# Account.transaction do
|
61
52
|
# balance.save!
|
62
53
|
# account.save!
|
63
54
|
# end
|
64
55
|
#
|
65
|
-
# The
|
56
|
+
# The #transaction method is also available as a model instance method.
|
66
57
|
# For example, you can also do this:
|
67
58
|
#
|
68
59
|
# balance.transaction do
|
@@ -89,7 +80,8 @@ module ActiveRecord
|
|
89
80
|
#
|
90
81
|
# == +save+ and +destroy+ are automatically wrapped in a transaction
|
91
82
|
#
|
92
|
-
# Both
|
83
|
+
# Both {#save}[rdoc-ref:Persistence#save] and
|
84
|
+
# {#destroy}[rdoc-ref:Persistence#destroy] come wrapped in a transaction that ensures
|
93
85
|
# that whatever you do in validations or callbacks will happen under its
|
94
86
|
# protected cover. So you can use validations to check for values that
|
95
87
|
# the transaction depends on or you can raise exceptions in the callbacks
|
@@ -98,7 +90,7 @@ module ActiveRecord
|
|
98
90
|
# As a consequence changes to the database are not seen outside your connection
|
99
91
|
# until the operation is complete. For example, if you try to update the index
|
100
92
|
# of a search engine in +after_save+ the indexer won't see the updated record.
|
101
|
-
# The
|
93
|
+
# The #after_commit callback is the only one that is triggered once the update
|
102
94
|
# is committed. See below.
|
103
95
|
#
|
104
96
|
# == Exception handling and rolling back
|
@@ -107,11 +99,11 @@ module ActiveRecord
|
|
107
99
|
# be propagated (after triggering the ROLLBACK), so you should be ready to
|
108
100
|
# catch those in your application code.
|
109
101
|
#
|
110
|
-
# One exception is the
|
102
|
+
# One exception is the ActiveRecord::Rollback exception, which will trigger
|
111
103
|
# a ROLLBACK when raised, but not be re-raised by the transaction block.
|
112
104
|
#
|
113
|
-
# *Warning*: one should not catch
|
114
|
-
# inside a transaction block.
|
105
|
+
# *Warning*: one should not catch ActiveRecord::StatementInvalid exceptions
|
106
|
+
# inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an
|
115
107
|
# error occurred at the database level, for example when a unique constraint
|
116
108
|
# is violated. On some database systems, such as PostgreSQL, database errors
|
117
109
|
# inside a transaction cause the entire transaction to become unusable
|
@@ -137,11 +129,11 @@ module ActiveRecord
|
|
137
129
|
# end
|
138
130
|
#
|
139
131
|
# One should restart the entire transaction if an
|
140
|
-
#
|
132
|
+
# ActiveRecord::StatementInvalid occurred.
|
141
133
|
#
|
142
134
|
# == Nested transactions
|
143
135
|
#
|
144
|
-
#
|
136
|
+
# #transaction calls can be nested. By default, this makes all database
|
145
137
|
# statements in the nested transaction block become part of the parent
|
146
138
|
# transaction. For example, the following behavior may be surprising:
|
147
139
|
#
|
@@ -153,7 +145,7 @@ module ActiveRecord
|
|
153
145
|
# end
|
154
146
|
# end
|
155
147
|
#
|
156
|
-
# creates both "Kotori" and "Nemu". Reason is the
|
148
|
+
# creates both "Kotori" and "Nemu". Reason is the ActiveRecord::Rollback
|
157
149
|
# exception in the nested block does not issue a ROLLBACK. Since these exceptions
|
158
150
|
# are captured in transaction blocks, the parent block does not see it and the
|
159
151
|
# real transaction is committed.
|
@@ -177,22 +169,22 @@ module ActiveRecord
|
|
177
169
|
# writing, the only database that we're aware of that supports true nested
|
178
170
|
# transactions, is MS-SQL. Because of this, Active Record emulates nested
|
179
171
|
# transactions by using savepoints on MySQL and PostgreSQL. See
|
180
|
-
# http://dev.mysql.com/doc/refman/5.
|
172
|
+
# http://dev.mysql.com/doc/refman/5.7/en/savepoint.html
|
181
173
|
# for more information about savepoints.
|
182
174
|
#
|
183
|
-
# === Callbacks
|
175
|
+
# === \Callbacks
|
184
176
|
#
|
185
177
|
# There are two types of callbacks associated with committing and rolling back transactions:
|
186
|
-
#
|
178
|
+
# #after_commit and #after_rollback.
|
187
179
|
#
|
188
|
-
#
|
189
|
-
# transaction immediately after the transaction is committed.
|
180
|
+
# #after_commit callbacks are called on every record saved or destroyed within a
|
181
|
+
# transaction immediately after the transaction is committed. #after_rollback callbacks
|
190
182
|
# are called on every record saved or destroyed within a transaction immediately after the
|
191
183
|
# transaction or savepoint is rolled back.
|
192
184
|
#
|
193
185
|
# These callbacks are useful for interacting with other systems since you will be guaranteed
|
194
186
|
# that the callback is only executed when the database is in a permanent state. For example,
|
195
|
-
#
|
187
|
+
# #after_commit is a good spot to put in a hook to clearing a cache since clearing it from
|
196
188
|
# within a transaction could trigger the cache to be regenerated before the database is updated.
|
197
189
|
#
|
198
190
|
# === Caveats
|
@@ -206,20 +198,24 @@ module ActiveRecord
|
|
206
198
|
# automatically released. The following example demonstrates the problem:
|
207
199
|
#
|
208
200
|
# Model.connection.transaction do # BEGIN
|
209
|
-
# Model.connection.transaction(requires_new: true) do
|
201
|
+
# Model.connection.transaction(requires_new: true) do # CREATE SAVEPOINT active_record_1
|
210
202
|
# Model.connection.create_table(...) # active_record_1 now automatically released
|
211
|
-
# end # RELEASE
|
203
|
+
# end # RELEASE SAVEPOINT active_record_1
|
212
204
|
# # ^^^^ BOOM! database error!
|
213
205
|
# end
|
214
206
|
#
|
215
207
|
# Note that "TRUNCATE" is also a MySQL DDL statement!
|
216
208
|
module ClassMethods
|
217
|
-
# See
|
209
|
+
# See the ConnectionAdapters::DatabaseStatements#transaction API docs.
|
218
210
|
def transaction(options = {}, &block)
|
219
|
-
# See the ConnectionAdapters::DatabaseStatements#transaction API docs.
|
220
211
|
connection.transaction(options, &block)
|
221
212
|
end
|
222
213
|
|
214
|
+
def before_commit(*args, &block) # :nodoc:
|
215
|
+
set_options_for_callbacks!(args)
|
216
|
+
set_callback(:before_commit, :before, *args, &block)
|
217
|
+
end
|
218
|
+
|
223
219
|
# This callback is called after a record has been created, updated, or destroyed.
|
224
220
|
#
|
225
221
|
# You can specify that the callback should only be fired by a certain action with
|
@@ -232,32 +228,69 @@ module ActiveRecord
|
|
232
228
|
# after_commit :do_foo_bar, on: [:create, :update]
|
233
229
|
# after_commit :do_bar_baz, on: [:update, :destroy]
|
234
230
|
#
|
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
231
|
def after_commit(*args, &block)
|
238
232
|
set_options_for_callbacks!(args)
|
239
233
|
set_callback(:commit, :after, *args, &block)
|
240
|
-
|
241
|
-
|
242
|
-
|
234
|
+
end
|
235
|
+
|
236
|
+
# Shortcut for <tt>after_commit :hook, on: :create</tt>.
|
237
|
+
def after_create_commit(*args, &block)
|
238
|
+
set_options_for_callbacks!(args, on: :create)
|
239
|
+
set_callback(:commit, :after, *args, &block)
|
240
|
+
end
|
241
|
+
|
242
|
+
# Shortcut for <tt>after_commit :hook, on: :update</tt>.
|
243
|
+
def after_update_commit(*args, &block)
|
244
|
+
set_options_for_callbacks!(args, on: :update)
|
245
|
+
set_callback(:commit, :after, *args, &block)
|
246
|
+
end
|
247
|
+
|
248
|
+
# Shortcut for <tt>after_commit :hook, on: :destroy</tt>.
|
249
|
+
def after_destroy_commit(*args, &block)
|
250
|
+
set_options_for_callbacks!(args, on: :destroy)
|
251
|
+
set_callback(:commit, :after, *args, &block)
|
243
252
|
end
|
244
253
|
|
245
254
|
# This callback is called after a create, update, or destroy are rolled back.
|
246
255
|
#
|
247
|
-
# Please check the documentation of
|
256
|
+
# Please check the documentation of #after_commit for options.
|
248
257
|
def after_rollback(*args, &block)
|
249
258
|
set_options_for_callbacks!(args)
|
250
259
|
set_callback(:rollback, :after, *args, &block)
|
251
|
-
|
252
|
-
|
253
|
-
|
260
|
+
end
|
261
|
+
|
262
|
+
def before_commit_without_transaction_enrollment(*args, &block) # :nodoc:
|
263
|
+
set_options_for_callbacks!(args)
|
264
|
+
set_callback(:before_commit_without_transaction_enrollment, :before, *args, &block)
|
265
|
+
end
|
266
|
+
|
267
|
+
def after_commit_without_transaction_enrollment(*args, &block) # :nodoc:
|
268
|
+
set_options_for_callbacks!(args)
|
269
|
+
set_callback(:commit_without_transaction_enrollment, :after, *args, &block)
|
270
|
+
end
|
271
|
+
|
272
|
+
def after_rollback_without_transaction_enrollment(*args, &block) # :nodoc:
|
273
|
+
set_options_for_callbacks!(args)
|
274
|
+
set_callback(:rollback_without_transaction_enrollment, :after, *args, &block)
|
275
|
+
end
|
276
|
+
|
277
|
+
def raise_in_transactional_callbacks
|
278
|
+
ActiveSupport::Deprecation.warn('ActiveRecord::Base.raise_in_transactional_callbacks is deprecated and will be removed without replacement.')
|
279
|
+
true
|
280
|
+
end
|
281
|
+
|
282
|
+
def raise_in_transactional_callbacks=(value)
|
283
|
+
ActiveSupport::Deprecation.warn('ActiveRecord::Base.raise_in_transactional_callbacks= is deprecated, has no effect and will be removed without replacement.')
|
284
|
+
value
|
254
285
|
end
|
255
286
|
|
256
287
|
private
|
257
288
|
|
258
|
-
def set_options_for_callbacks!(args)
|
259
|
-
options = args.
|
260
|
-
|
289
|
+
def set_options_for_callbacks!(args, enforced_options = {})
|
290
|
+
options = args.extract_options!.merge!(enforced_options)
|
291
|
+
args << options
|
292
|
+
|
293
|
+
if options[:on]
|
261
294
|
fire_on = Array(options[:on])
|
262
295
|
assert_valid_transaction_action(fire_on)
|
263
296
|
options[:if] = Array(options[:if])
|
@@ -306,26 +339,37 @@ module ActiveRecord
|
|
306
339
|
clear_transaction_record_state
|
307
340
|
end
|
308
341
|
|
309
|
-
|
342
|
+
def before_committed! # :nodoc:
|
343
|
+
_run_before_commit_without_transaction_enrollment_callbacks
|
344
|
+
_run_before_commit_callbacks
|
345
|
+
end
|
346
|
+
|
347
|
+
# Call the #after_commit callbacks.
|
310
348
|
#
|
311
349
|
# Ensure that it is not called if the object was never persisted (failed create),
|
312
350
|
# but call it after the commit of a destroyed object.
|
313
|
-
def committed!(should_run_callbacks
|
314
|
-
|
351
|
+
def committed!(should_run_callbacks: true) #:nodoc:
|
352
|
+
if should_run_callbacks && destroyed? || persisted?
|
353
|
+
_run_commit_without_transaction_enrollment_callbacks
|
354
|
+
_run_commit_callbacks
|
355
|
+
end
|
315
356
|
ensure
|
316
357
|
force_clear_transaction_record_state
|
317
358
|
end
|
318
359
|
|
319
|
-
# Call the
|
360
|
+
# Call the #after_rollback callbacks. The +force_restore_state+ argument indicates if the record
|
320
361
|
# state should be rolled back to the beginning or just to the last savepoint.
|
321
|
-
def rolledback!(force_restore_state
|
322
|
-
|
362
|
+
def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
|
363
|
+
if should_run_callbacks
|
364
|
+
_run_rollback_callbacks
|
365
|
+
_run_rollback_without_transaction_enrollment_callbacks
|
366
|
+
end
|
323
367
|
ensure
|
324
368
|
restore_transaction_record_state(force_restore_state)
|
325
369
|
clear_transaction_record_state
|
326
370
|
end
|
327
371
|
|
328
|
-
# Add the record to the current transaction so that the
|
372
|
+
# Add the record to the current transaction so that the #after_rollback and #after_commit callbacks
|
329
373
|
# can be called.
|
330
374
|
def add_to_transaction
|
331
375
|
if has_transactional_callbacks?
|
@@ -423,5 +467,43 @@ module ActiveRecord
|
|
423
467
|
end
|
424
468
|
end
|
425
469
|
end
|
470
|
+
|
471
|
+
private
|
472
|
+
|
473
|
+
def set_transaction_state(state) # :nodoc:
|
474
|
+
@transaction_state = state
|
475
|
+
end
|
476
|
+
|
477
|
+
def has_transactional_callbacks? # :nodoc:
|
478
|
+
!_rollback_callbacks.empty? || !_commit_callbacks.empty? || !_before_commit_callbacks.empty?
|
479
|
+
end
|
480
|
+
|
481
|
+
# Updates the attributes on this particular Active Record object so that
|
482
|
+
# if it's associated with a transaction, then the state of the Active Record
|
483
|
+
# object will be updated to reflect the current state of the transaction
|
484
|
+
#
|
485
|
+
# The +@transaction_state+ variable stores the states of the associated
|
486
|
+
# transaction. This relies on the fact that a transaction can only be in
|
487
|
+
# one rollback or commit (otherwise a list of states would be required)
|
488
|
+
# Each Active Record object inside of a transaction carries that transaction's
|
489
|
+
# TransactionState.
|
490
|
+
#
|
491
|
+
# This method checks to see if the ActiveRecord object's state reflects
|
492
|
+
# the TransactionState, and rolls back or commits the Active Record object
|
493
|
+
# as appropriate.
|
494
|
+
#
|
495
|
+
# Since Active Record objects can be inside multiple transactions, this
|
496
|
+
# method recursively goes through the parent of the TransactionState and
|
497
|
+
# checks if the Active Record object reflects the state of the object.
|
498
|
+
def sync_with_transaction_state
|
499
|
+
update_attributes_from_transaction_state(@transaction_state)
|
500
|
+
end
|
501
|
+
|
502
|
+
def update_attributes_from_transaction_state(transaction_state)
|
503
|
+
if transaction_state && transaction_state.finalized?
|
504
|
+
restore_transaction_record_state if transaction_state.rolledback?
|
505
|
+
clear_transaction_record_state
|
506
|
+
end
|
507
|
+
end
|
426
508
|
end
|
427
509
|
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'active_model/type/registry'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# :stopdoc:
|
5
|
+
module Type
|
6
|
+
class AdapterSpecificRegistry < ActiveModel::Type::Registry
|
7
|
+
def add_modifier(options, klass, **args)
|
8
|
+
registrations << DecorationRegistration.new(options, klass, **args)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def registration_klass
|
14
|
+
Registration
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_registration(symbol, *args)
|
18
|
+
registrations
|
19
|
+
.select { |registration| registration.matches?(symbol, *args) }
|
20
|
+
.max
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Registration
|
25
|
+
def initialize(name, block, adapter: nil, override: nil)
|
26
|
+
@name = name
|
27
|
+
@block = block
|
28
|
+
@adapter = adapter
|
29
|
+
@override = override
|
30
|
+
end
|
31
|
+
|
32
|
+
def call(_registry, *args, adapter: nil, **kwargs)
|
33
|
+
if kwargs.any? # https://bugs.ruby-lang.org/issues/10856
|
34
|
+
block.call(*args, **kwargs)
|
35
|
+
else
|
36
|
+
block.call(*args)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def matches?(type_name, *args, **kwargs)
|
41
|
+
type_name == name && matches_adapter?(**kwargs)
|
42
|
+
end
|
43
|
+
|
44
|
+
def <=>(other)
|
45
|
+
if conflicts_with?(other)
|
46
|
+
raise TypeConflictError.new("Type #{name} was registered for all
|
47
|
+
adapters, but shadows a native type with
|
48
|
+
the same name for #{other.adapter}".squish)
|
49
|
+
end
|
50
|
+
priority <=> other.priority
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
attr_reader :name, :block, :adapter, :override
|
56
|
+
|
57
|
+
def priority
|
58
|
+
result = 0
|
59
|
+
if adapter
|
60
|
+
result |= 1
|
61
|
+
end
|
62
|
+
if override
|
63
|
+
result |= 2
|
64
|
+
end
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
def priority_except_adapter
|
69
|
+
priority & 0b111111100
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def matches_adapter?(adapter: nil, **)
|
75
|
+
(self.adapter.nil? || adapter == self.adapter)
|
76
|
+
end
|
77
|
+
|
78
|
+
def conflicts_with?(other)
|
79
|
+
same_priority_except_adapter?(other) &&
|
80
|
+
has_adapter_conflict?(other)
|
81
|
+
end
|
82
|
+
|
83
|
+
def same_priority_except_adapter?(other)
|
84
|
+
priority_except_adapter == other.priority_except_adapter
|
85
|
+
end
|
86
|
+
|
87
|
+
def has_adapter_conflict?(other)
|
88
|
+
(override.nil? && other.adapter) ||
|
89
|
+
(adapter && other.override.nil?)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class DecorationRegistration < Registration
|
94
|
+
def initialize(options, klass, adapter: nil)
|
95
|
+
@options = options
|
96
|
+
@klass = klass
|
97
|
+
@adapter = adapter
|
98
|
+
end
|
99
|
+
|
100
|
+
def call(registry, *args, **kwargs)
|
101
|
+
subtype = registry.lookup(*args, **kwargs.except(*options.keys))
|
102
|
+
klass.new(subtype)
|
103
|
+
end
|
104
|
+
|
105
|
+
def matches?(*args, **kwargs)
|
106
|
+
matches_adapter?(**kwargs) && matches_options?(**kwargs)
|
107
|
+
end
|
108
|
+
|
109
|
+
def priority
|
110
|
+
super | 4
|
111
|
+
end
|
112
|
+
|
113
|
+
protected
|
114
|
+
|
115
|
+
attr_reader :options, :klass
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def matches_options?(**kwargs)
|
120
|
+
options.all? do |key, value|
|
121
|
+
kwargs[key] == value
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
class TypeConflictError < StandardError
|
128
|
+
end
|
129
|
+
# :startdoc:
|
130
|
+
end
|
@@ -1,50 +1,7 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Type
|
3
|
-
class Date <
|
4
|
-
|
5
|
-
:date
|
6
|
-
end
|
7
|
-
|
8
|
-
def klass
|
9
|
-
::Date
|
10
|
-
end
|
11
|
-
|
12
|
-
def type_cast_for_database(value)
|
13
|
-
type_cast(value)
|
14
|
-
end
|
15
|
-
|
16
|
-
def type_cast_for_schema(value)
|
17
|
-
"'#{value.to_s(:db)}'"
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def cast_value(value)
|
23
|
-
if value.is_a?(::String)
|
24
|
-
return if value.empty?
|
25
|
-
fast_string_to_date(value) || fallback_string_to_date(value)
|
26
|
-
elsif value.respond_to?(:to_date)
|
27
|
-
value.to_date
|
28
|
-
else
|
29
|
-
value
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def fast_string_to_date(string)
|
34
|
-
if string =~ ConnectionAdapters::Column::Format::ISO_DATE
|
35
|
-
new_date $1.to_i, $2.to_i, $3.to_i
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def fallback_string_to_date(string)
|
40
|
-
new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
|
41
|
-
end
|
42
|
-
|
43
|
-
def new_date(year, mon, mday)
|
44
|
-
if year && year != 0
|
45
|
-
::Date.new(year, mon, mday) rescue nil
|
46
|
-
end
|
47
|
-
end
|
3
|
+
class Date < ActiveModel::Type::Date
|
4
|
+
include Internal::Timezone
|
48
5
|
end
|
49
6
|
end
|
50
7
|
end
|
@@ -1,54 +1,7 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Type
|
3
|
-
class DateTime <
|
4
|
-
include
|
5
|
-
|
6
|
-
def type
|
7
|
-
:datetime
|
8
|
-
end
|
9
|
-
|
10
|
-
def type_cast_for_database(value)
|
11
|
-
return super unless value.acts_like?(:time)
|
12
|
-
|
13
|
-
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
|
14
|
-
|
15
|
-
if value.respond_to?(zone_conversion_method)
|
16
|
-
value = value.send(zone_conversion_method)
|
17
|
-
end
|
18
|
-
|
19
|
-
return value unless has_precision?
|
20
|
-
|
21
|
-
result = value.to_s(:db)
|
22
|
-
if value.respond_to?(:usec) && (1..6).cover?(precision)
|
23
|
-
"#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}"
|
24
|
-
else
|
25
|
-
result
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
alias has_precision? precision
|
32
|
-
|
33
|
-
def cast_value(string)
|
34
|
-
return string unless string.is_a?(::String)
|
35
|
-
return if string.empty?
|
36
|
-
|
37
|
-
fast_string_to_time(string) || fallback_string_to_time(string)
|
38
|
-
end
|
39
|
-
|
40
|
-
# '0.123456' -> 123456
|
41
|
-
# '1.123456' -> 123456
|
42
|
-
def microseconds(time)
|
43
|
-
time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
|
44
|
-
end
|
45
|
-
|
46
|
-
def fallback_string_to_time(string)
|
47
|
-
time_hash = ::Date._parse(string)
|
48
|
-
time_hash[:sec_fraction] = microseconds(time_hash)
|
49
|
-
|
50
|
-
new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset))
|
51
|
-
end
|
3
|
+
class DateTime < ActiveModel::Type::DateTime
|
4
|
+
include Internal::Timezone
|
52
5
|
end
|
53
6
|
end
|
54
7
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
module Internal # :nodoc:
|
4
|
+
class AbstractJson < ActiveModel::Type::Value # :nodoc:
|
5
|
+
include ActiveModel::Type::Helpers::Mutable
|
6
|
+
|
7
|
+
def type
|
8
|
+
:json
|
9
|
+
end
|
10
|
+
|
11
|
+
def deserialize(value)
|
12
|
+
if value.is_a?(::String)
|
13
|
+
::ActiveSupport::JSON.decode(value) rescue nil
|
14
|
+
else
|
15
|
+
value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def serialize(value)
|
20
|
+
::ActiveSupport::JSON.encode(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
def accessor
|
24
|
+
ActiveRecord::Store::StringKeyedHashAccessor
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|