activerecord 1.0.0 → 4.0.0
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 +7 -0
- data/CHANGELOG.md +2102 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +213 -0
- data/examples/performance.rb +172 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +180 -84
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +248 -0
- data/lib/active_record/associations/association_scope.rb +135 -0
- data/lib/active_record/associations/belongs_to_association.rb +92 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +35 -0
- data/lib/active_record/associations/builder/association.rb +108 -0
- data/lib/active_record/associations/builder/belongs_to.rb +98 -0
- data/lib/active_record/associations/builder/collection_association.rb +89 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
- data/lib/active_record/associations/builder/has_many.rb +15 -0
- data/lib/active_record/associations/builder/has_one.rb +25 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +608 -0
- data/lib/active_record/associations/collection_proxy.rb +986 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +58 -39
- data/lib/active_record/associations/has_many_association.rb +116 -85
- data/lib/active_record/associations/has_many_through_association.rb +197 -0
- data/lib/active_record/associations/has_one_association.rb +102 -0
- data/lib/active_record/associations/has_one_through_association.rb +36 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +235 -0
- data/lib/active_record/associations/join_helper.rb +45 -0
- data/lib/active_record/associations/preloader/association.rb +121 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +63 -0
- data/lib/active_record/associations/preloader.rb +178 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +1437 -431
- data/lib/active_record/attribute_assignment.rb +201 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +70 -0
- data/lib/active_record/attribute_methods/dirty.rb +118 -0
- data/lib/active_record/attribute_methods/primary_key.rb +122 -0
- data/lib/active_record/attribute_methods/query.rb +40 -0
- data/lib/active_record/attribute_methods/read.rb +107 -0
- data/lib/active_record/attribute_methods/serialization.rb +162 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +59 -0
- data/lib/active_record/attribute_methods/write.rb +63 -0
- data/lib/active_record/attribute_methods.rb +393 -0
- data/lib/active_record/autosave_association.rb +426 -0
- data/lib/active_record/base.rb +268 -930
- data/lib/active_record/callbacks.rb +203 -230
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +638 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +390 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +129 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +501 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +873 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +389 -275
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
- data/lib/active_record/connection_adapters/column.rb +318 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +517 -90
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +911 -138
- data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +624 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +463 -0
- data/lib/active_record/counter_cache.rb +122 -0
- data/lib/active_record/dynamic_matchers.rb +131 -0
- data/lib/active_record/errors.rb +213 -0
- data/lib/active_record/explain.rb +38 -0
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +29 -0
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +892 -138
- data/lib/active_record/inheritance.rb +200 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +47 -0
- data/lib/active_record/locking/optimistic.rb +181 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/log_subscriber.rb +82 -0
- data/lib/active_record/migration/command_recorder.rb +164 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +1015 -0
- data/lib/active_record/model_schema.rb +345 -0
- data/lib/active_record/nested_attributes.rb +546 -0
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +509 -0
- data/lib/active_record/query_cache.rb +56 -0
- data/lib/active_record/querying.rb +62 -0
- data/lib/active_record/railtie.rb +205 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +50 -0
- data/lib/active_record/railties/databases.rake +402 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +30 -0
- data/lib/active_record/reflection.rb +544 -87
- data/lib/active_record/relation/batches.rb +93 -0
- data/lib/active_record/relation/calculations.rb +399 -0
- data/lib/active_record/relation/delegation.rb +125 -0
- data/lib/active_record/relation/finder_methods.rb +349 -0
- data/lib/active_record/relation/merger.rb +161 -0
- data/lib/active_record/relation/predicate_builder.rb +106 -0
- data/lib/active_record/relation/query_methods.rb +1044 -0
- data/lib/active_record/relation/spawn_methods.rb +73 -0
- data/lib/active_record/relation.rb +655 -0
- data/lib/active_record/result.rb +67 -0
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +168 -0
- data/lib/active_record/schema.rb +65 -0
- data/lib/active_record/schema_dumper.rb +204 -0
- data/lib/active_record/schema_migration.rb +39 -0
- data/lib/active_record/scoping/default.rb +146 -0
- data/lib/active_record/scoping/named.rb +175 -0
- data/lib/active_record/scoping.rb +82 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/serializers/xml_serializer.rb +197 -0
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +156 -0
- data/lib/active_record/tasks/database_tasks.rb +203 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
- data/lib/active_record/test_case.rb +96 -0
- data/lib/active_record/timestamp.rb +119 -0
- data/lib/active_record/transactions.rb +366 -69
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +49 -0
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +225 -0
- data/lib/active_record/validations.rb +64 -185
- data/lib/active_record/version.rb +11 -0
- data/lib/active_record.rb +149 -24
- data/lib/rails/generators/active_record/migration/migration_generator.rb +62 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +39 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record.rb +23 -0
- metadata +261 -161
- data/CHANGELOG +0 -581
- data/README +0 -361
- data/RUNNING_UNIT_TESTS +0 -36
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.png +0 -0
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/install.rb +0 -60
- data/lib/active_record/associations/association_collection.rb +0 -70
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -107
- data/lib/active_record/deprecated_associations.rb +0 -70
- data/lib/active_record/observer.rb +0 -71
- data/lib/active_record/support/class_attribute_accessors.rb +0 -43
- data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
- data/lib/active_record/support/clean_logger.rb +0 -10
- data/lib/active_record/support/inflector.rb +0 -70
- data/lib/active_record/vendor/mysql.rb +0 -1117
- data/lib/active_record/vendor/simple.rb +0 -702
- data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
- data/lib/active_record/wrappings.rb +0 -59
- data/rakefile +0 -122
- data/test/abstract_unit.rb +0 -16
- data/test/aggregations_test.rb +0 -34
- data/test/all.sh +0 -8
- data/test/associations_test.rb +0 -477
- data/test/base_test.rb +0 -513
- data/test/class_inheritable_attributes_test.rb +0 -33
- data/test/connections/native_mysql/connection.rb +0 -24
- data/test/connections/native_postgresql/connection.rb +0 -24
- data/test/connections/native_sqlite/connection.rb +0 -24
- data/test/deprecated_associations_test.rb +0 -336
- data/test/finder_test.rb +0 -67
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- data/test/fixtures/auto_id.rb +0 -4
- data/test/fixtures/column_name.rb +0 -3
- data/test/fixtures/companies/first_client +0 -6
- data/test/fixtures/companies/first_firm +0 -4
- data/test/fixtures/companies/second_client +0 -6
- data/test/fixtures/company.rb +0 -37
- data/test/fixtures/company_in_module.rb +0 -33
- data/test/fixtures/course.rb +0 -3
- data/test/fixtures/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- data/test/fixtures/customer.rb +0 -30
- data/test/fixtures/customers/david +0 -6
- data/test/fixtures/db_definitions/mysql.sql +0 -96
- data/test/fixtures/db_definitions/mysql2.sql +0 -4
- data/test/fixtures/db_definitions/postgresql.sql +0 -113
- data/test/fixtures/db_definitions/postgresql2.sql +0 -4
- data/test/fixtures/db_definitions/sqlite.sql +0 -85
- data/test/fixtures/db_definitions/sqlite2.sql +0 -4
- data/test/fixtures/default.rb +0 -2
- data/test/fixtures/developer.rb +0 -8
- data/test/fixtures/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- data/test/fixtures/developers_projects/david_action_controller +0 -2
- data/test/fixtures/developers_projects/david_active_record +0 -2
- data/test/fixtures/developers_projects/jamis_active_record +0 -2
- data/test/fixtures/entrant.rb +0 -3
- data/test/fixtures/entrants/first +0 -3
- data/test/fixtures/entrants/second +0 -3
- data/test/fixtures/entrants/third +0 -3
- data/test/fixtures/fixture_database.sqlite +0 -0
- data/test/fixtures/fixture_database_2.sqlite +0 -0
- data/test/fixtures/movie.rb +0 -5
- data/test/fixtures/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/project.rb +0 -3
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/reply.rb +0 -21
- data/test/fixtures/subscriber.rb +0 -5
- data/test/fixtures/subscribers/first +0 -2
- data/test/fixtures/subscribers/second +0 -2
- data/test/fixtures/topic.rb +0 -20
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/fixtures_test.rb +0 -20
- data/test/inflector_test.rb +0 -104
- data/test/inheritance_test.rb +0 -125
- data/test/lifecycle_test.rb +0 -110
- data/test/modules_test.rb +0 -21
- data/test/multiple_db_test.rb +0 -46
- data/test/pk_test.rb +0 -57
- data/test/reflection_test.rb +0 -78
- data/test/thread_safety_test.rb +0 -33
- data/test/transactions_test.rb +0 -83
- data/test/unconnected_test.rb +0 -24
- data/test/validations_test.rb +0 -126
@@ -1,102 +1,399 @@
|
|
1
|
-
require 'active_record/vendor/simple.rb'
|
2
1
|
require 'thread'
|
3
2
|
|
4
3
|
module ActiveRecord
|
5
|
-
|
6
|
-
|
4
|
+
# See ActiveRecord::Transactions::ClassMethods for documentation.
|
5
|
+
module Transactions
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
ACTIONS = [:create, :destroy, :update]
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
base.extend(ClassMethods)
|
11
|
-
|
12
|
-
base.class_eval do
|
13
|
-
alias_method :destroy_without_transactions, :destroy
|
14
|
-
alias_method :destroy, :destroy_with_transactions
|
9
|
+
class TransactionError < ActiveRecordError # :nodoc:
|
10
|
+
end
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
12
|
+
included do
|
13
|
+
define_callbacks :commit, :rollback, :terminator => "result == false", :scope => [:kind, :name]
|
19
14
|
end
|
20
15
|
|
21
|
-
#
|
22
|
-
# The classic example is a transfer between two accounts where you can only have a deposit if the withdrawal succedded and
|
23
|
-
# vice versa. Transaction enforce the integrity of the database and guards the data against program errors or database break-downs.
|
24
|
-
# So basically you should use transaction blocks whenever you have a number of statements that must be executed together or
|
25
|
-
# not at all. Example:
|
16
|
+
# = Active Record Transactions
|
26
17
|
#
|
27
|
-
#
|
18
|
+
# Transactions are protective blocks where SQL statements are only permanent
|
19
|
+
# if they can all succeed as one atomic action. The classic example is a
|
20
|
+
# transfer between two accounts where you can only have a deposit if the
|
21
|
+
# withdrawal succeeded and vice versa. Transactions enforce the integrity of
|
22
|
+
# the database and guard the data against program errors or database
|
23
|
+
# break-downs. So basically you should use transaction blocks whenever you
|
24
|
+
# have a number of statements that must be executed together or not at all.
|
25
|
+
#
|
26
|
+
# For example:
|
27
|
+
#
|
28
|
+
# ActiveRecord::Base.transaction do
|
28
29
|
# david.withdrawal(100)
|
29
30
|
# mary.deposit(100)
|
30
31
|
# end
|
31
32
|
#
|
32
|
-
# This example will only take money from David and give to Mary if neither
|
33
|
-
#
|
34
|
-
# that the
|
33
|
+
# This example will only take money from David and give it to Mary if neither
|
34
|
+
# +withdrawal+ nor +deposit+ raise an exception. Exceptions will force a
|
35
|
+
# ROLLBACK that returns the database to the state before the transaction
|
36
|
+
# began. Be aware, though, that the objects will _not_ have their instance
|
37
|
+
# data returned to their pre-transactional state.
|
35
38
|
#
|
36
|
-
# ==
|
39
|
+
# == Different Active Record classes in a single transaction
|
37
40
|
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
+
# Though the transaction class method is called on some Active Record class,
|
42
|
+
# the objects within the transaction block need not all be instances of
|
43
|
+
# that class. This is because transactions are per-database connection, not
|
44
|
+
# per-model.
|
41
45
|
#
|
42
|
-
#
|
46
|
+
# In this example a +balance+ record is transactionally saved even
|
47
|
+
# though +transaction+ is called on the +Account+ class:
|
43
48
|
#
|
44
|
-
#
|
45
|
-
#
|
49
|
+
# Account.transaction do
|
50
|
+
# balance.save!
|
51
|
+
# account.save!
|
52
|
+
# end
|
46
53
|
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
54
|
+
# The +transaction+ method is also available as a model instance method.
|
55
|
+
# For example, you can also do this:
|
56
|
+
#
|
57
|
+
# balance.transaction do
|
58
|
+
# balance.save!
|
59
|
+
# account.save!
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# == Transactions are not distributed across database connections
|
63
|
+
#
|
64
|
+
# A transaction acts on a single database connection. If you have
|
65
|
+
# multiple class-specific databases, the transaction will not protect
|
66
|
+
# interaction among them. One workaround is to begin a transaction
|
67
|
+
# on each class whose models you alter:
|
68
|
+
#
|
69
|
+
# Student.transaction do
|
70
|
+
# Course.transaction do
|
71
|
+
# course.enroll(student)
|
72
|
+
# student.units += course.units
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# This is a poor solution, but fully distributed transactions are beyond
|
77
|
+
# the scope of Active Record.
|
78
|
+
#
|
79
|
+
# == +save+ and +destroy+ are automatically wrapped in a transaction
|
80
|
+
#
|
81
|
+
# Both +save+ and +destroy+ come wrapped in a transaction that ensures
|
82
|
+
# that whatever you do in validations or callbacks will happen under its
|
83
|
+
# protected cover. So you can use validations to check for values that
|
84
|
+
# the transaction depends on or you can raise exceptions in the callbacks
|
85
|
+
# to rollback, including <tt>after_*</tt> callbacks.
|
86
|
+
#
|
87
|
+
# As a consequence changes to the database are not seen outside your connection
|
88
|
+
# until the operation is complete. For example, if you try to update the index
|
89
|
+
# of a search engine in +after_save+ the indexer won't see the updated record.
|
90
|
+
# The +after_commit+ callback is the only one that is triggered once the update
|
91
|
+
# is committed. See below.
|
92
|
+
#
|
93
|
+
# == Exception handling and rolling back
|
94
|
+
#
|
95
|
+
# Also have in mind that exceptions thrown within a transaction block will
|
96
|
+
# be propagated (after triggering the ROLLBACK), so you should be ready to
|
97
|
+
# catch those in your application code.
|
98
|
+
#
|
99
|
+
# One exception is the <tt>ActiveRecord::Rollback</tt> exception, which will trigger
|
100
|
+
# a ROLLBACK when raised, but not be re-raised by the transaction block.
|
101
|
+
#
|
102
|
+
# *Warning*: one should not catch <tt>ActiveRecord::StatementInvalid</tt> exceptions
|
103
|
+
# inside a transaction block. <tt>ActiveRecord::StatementInvalid</tt> exceptions indicate that an
|
104
|
+
# error occurred at the database level, for example when a unique constraint
|
105
|
+
# is violated. On some database systems, such as PostgreSQL, database errors
|
106
|
+
# inside a transaction cause the entire transaction to become unusable
|
107
|
+
# until it's restarted from the beginning. Here is an example which
|
108
|
+
# demonstrates the problem:
|
109
|
+
#
|
110
|
+
# # Suppose that we have a Number model with a unique column called 'i'.
|
111
|
+
# Number.transaction do
|
112
|
+
# Number.create(i: 0)
|
113
|
+
# begin
|
114
|
+
# # This will raise a unique constraint error...
|
115
|
+
# Number.create(i: 0)
|
116
|
+
# rescue ActiveRecord::StatementInvalid
|
117
|
+
# # ...which we ignore.
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# # On PostgreSQL, the transaction is now unusable. The following
|
121
|
+
# # statement will cause a PostgreSQL error, even though the unique
|
122
|
+
# # constraint is no longer violated:
|
123
|
+
# Number.create(i: 1)
|
124
|
+
# # => "PGError: ERROR: current transaction is aborted, commands
|
125
|
+
# # ignored until end of transaction block"
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
# One should restart the entire transaction if an
|
129
|
+
# <tt>ActiveRecord::StatementInvalid</tt> occurred.
|
130
|
+
#
|
131
|
+
# == Nested transactions
|
132
|
+
#
|
133
|
+
# +transaction+ calls can be nested. By default, this makes all database
|
134
|
+
# statements in the nested transaction block become part of the parent
|
135
|
+
# transaction. For example, the following behavior may be surprising:
|
136
|
+
#
|
137
|
+
# User.transaction do
|
138
|
+
# User.create(username: 'Kotori')
|
139
|
+
# User.transaction do
|
140
|
+
# User.create(username: 'Nemu')
|
141
|
+
# raise ActiveRecord::Rollback
|
142
|
+
# end
|
143
|
+
# end
|
144
|
+
#
|
145
|
+
# creates both "Kotori" and "Nemu". Reason is the <tt>ActiveRecord::Rollback</tt>
|
146
|
+
# exception in the nested block does not issue a ROLLBACK. Since these exceptions
|
147
|
+
# are captured in transaction blocks, the parent block does not see it and the
|
148
|
+
# real transaction is committed.
|
149
|
+
#
|
150
|
+
# In order to get a ROLLBACK for the nested transaction you may ask for a real
|
151
|
+
# sub-transaction by passing <tt>requires_new: true</tt>. If anything goes wrong,
|
152
|
+
# the database rolls back to the beginning of the sub-transaction without rolling
|
153
|
+
# back the parent transaction. If we add it to the previous example:
|
154
|
+
#
|
155
|
+
# User.transaction do
|
156
|
+
# User.create(username: 'Kotori')
|
157
|
+
# User.transaction(requires_new: true) do
|
158
|
+
# User.create(username: 'Nemu')
|
159
|
+
# raise ActiveRecord::Rollback
|
160
|
+
# end
|
50
161
|
# end
|
51
162
|
#
|
52
|
-
#
|
53
|
-
#
|
163
|
+
# only "Kotori" is created. This works on MySQL and PostgreSQL. SQLite3 version >= '3.6.8' also supports it.
|
164
|
+
#
|
165
|
+
# Most databases don't support true nested transactions. At the time of
|
166
|
+
# writing, the only database that we're aware of that supports true nested
|
167
|
+
# transactions, is MS-SQL. Because of this, Active Record emulates nested
|
168
|
+
# transactions by using savepoints on MySQL and PostgreSQL. See
|
169
|
+
# http://dev.mysql.com/doc/refman/5.6/en/savepoint.html
|
170
|
+
# for more information about savepoints.
|
171
|
+
#
|
172
|
+
# === Callbacks
|
173
|
+
#
|
174
|
+
# There are two types of callbacks associated with committing and rolling back transactions:
|
175
|
+
# +after_commit+ and +after_rollback+.
|
176
|
+
#
|
177
|
+
# +after_commit+ callbacks are called on every record saved or destroyed within a
|
178
|
+
# transaction immediately after the transaction is committed. +after_rollback+ callbacks
|
179
|
+
# are called on every record saved or destroyed within a transaction immediately after the
|
180
|
+
# transaction or savepoint is rolled back.
|
54
181
|
#
|
55
|
-
#
|
182
|
+
# These callbacks are useful for interacting with other systems since you will be guaranteed
|
183
|
+
# that the callback is only executed when the database is in a permanent state. For example,
|
184
|
+
# +after_commit+ is a good spot to put in a hook to clearing a cache since clearing it from
|
185
|
+
# within a transaction could trigger the cache to be regenerated before the database is updated.
|
56
186
|
#
|
57
|
-
#
|
58
|
-
#
|
187
|
+
# === Caveats
|
188
|
+
#
|
189
|
+
# If you're on MySQL, then do not use DDL operations in nested transactions
|
190
|
+
# blocks that are emulated with savepoints. That is, do not execute statements
|
191
|
+
# like 'CREATE TABLE' inside such blocks. This is because MySQL automatically
|
192
|
+
# releases all savepoints upon executing a DDL operation. When +transaction+
|
193
|
+
# is finished and tries to release the savepoint it created earlier, a
|
194
|
+
# database error will occur because the savepoint has already been
|
195
|
+
# automatically released. The following example demonstrates the problem:
|
196
|
+
#
|
197
|
+
# Model.connection.transaction do # BEGIN
|
198
|
+
# Model.connection.transaction(requires_new: true) do # CREATE SAVEPOINT active_record_1
|
199
|
+
# Model.connection.create_table(...) # active_record_1 now automatically released
|
200
|
+
# end # RELEASE savepoint active_record_1
|
201
|
+
# # ^^^^ BOOM! database error!
|
202
|
+
# end
|
203
|
+
#
|
204
|
+
# Note that "TRUNCATE" is also a MySQL DDL statement!
|
205
|
+
module ClassMethods
|
206
|
+
# See ActiveRecord::Transactions::ClassMethods for detailed documentation.
|
207
|
+
def transaction(options = {}, &block)
|
208
|
+
# See the ConnectionAdapters::DatabaseStatements#transaction API docs.
|
209
|
+
connection.transaction(options, &block)
|
210
|
+
end
|
211
|
+
|
212
|
+
# This callback is called after a record has been created, updated, or destroyed.
|
213
|
+
#
|
214
|
+
# You can specify that the callback should only be fired by a certain action with
|
215
|
+
# the +:on+ option:
|
216
|
+
#
|
217
|
+
# after_commit :do_foo, on: :create
|
218
|
+
# after_commit :do_bar, on: :update
|
219
|
+
# after_commit :do_baz, on: :destroy
|
220
|
+
#
|
221
|
+
# after_commit :do_foo_bar, :on [:create, :update]
|
222
|
+
# after_commit :do_bar_baz, :on [:update, :destroy]
|
223
|
+
#
|
224
|
+
# Note that transactional fixtures do not play well with this feature. Please
|
225
|
+
# use the +test_after_commit+ gem to have these hooks fired in tests.
|
226
|
+
def after_commit(*args, &block)
|
227
|
+
set_options_for_callbacks!(args)
|
228
|
+
set_callback(:commit, :after, *args, &block)
|
229
|
+
end
|
230
|
+
|
231
|
+
# This callback is called after a create, update, or destroy are rolled back.
|
232
|
+
#
|
233
|
+
# Please check the documentation of +after_commit+ for options.
|
234
|
+
def after_rollback(*args, &block)
|
235
|
+
set_options_for_callbacks!(args)
|
236
|
+
set_callback(:rollback, :after, *args, &block)
|
237
|
+
end
|
238
|
+
|
239
|
+
private
|
240
|
+
|
241
|
+
def set_options_for_callbacks!(args)
|
242
|
+
options = args.last
|
243
|
+
if options.is_a?(Hash) && options[:on]
|
244
|
+
assert_valid_transaction_action(options[:on])
|
245
|
+
options[:if] = Array(options[:if])
|
246
|
+
fire_on = Array(options[:on]).map(&:to_sym)
|
247
|
+
options[:if] << "transaction_include_any_action?(#{fire_on})"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def assert_valid_transaction_action(actions)
|
252
|
+
actions = Array(actions)
|
253
|
+
if (actions - ACTIONS).any?
|
254
|
+
raise ArgumentError, ":on conditions for after_commit and after_rollback callbacks have to be one of #{ACTIONS.join(",")}"
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# See ActiveRecord::Transactions::ClassMethods for detailed documentation.
|
260
|
+
def transaction(options = {}, &block)
|
261
|
+
self.class.transaction(options, &block)
|
262
|
+
end
|
263
|
+
|
264
|
+
def destroy #:nodoc:
|
265
|
+
with_transaction_returning_status { super }
|
266
|
+
end
|
267
|
+
|
268
|
+
def save(*) #:nodoc:
|
269
|
+
rollback_active_record_state! do
|
270
|
+
with_transaction_returning_status { super }
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
def save!(*) #:nodoc:
|
275
|
+
with_transaction_returning_status { super }
|
276
|
+
end
|
277
|
+
|
278
|
+
# Reset id and @new_record if the transaction rolls back.
|
279
|
+
def rollback_active_record_state!
|
280
|
+
remember_transaction_record_state
|
281
|
+
yield
|
282
|
+
rescue Exception
|
283
|
+
restore_transaction_record_state
|
284
|
+
raise
|
285
|
+
ensure
|
286
|
+
clear_transaction_record_state
|
287
|
+
end
|
288
|
+
|
289
|
+
# Call the after_commit callbacks
|
59
290
|
#
|
60
|
-
#
|
61
|
-
|
62
|
-
|
63
|
-
|
291
|
+
# Ensure that it is not called if the object was never persisted (failed create),
|
292
|
+
# but call it after the commit of a destroyed object
|
293
|
+
def committed! #:nodoc:
|
294
|
+
run_callbacks :commit if destroyed? || persisted?
|
295
|
+
ensure
|
296
|
+
clear_transaction_record_state
|
297
|
+
end
|
64
298
|
|
299
|
+
# Call the after rollback callbacks. The restore_state argument indicates if the record
|
300
|
+
# state should be rolled back to the beginning or just to the last savepoint.
|
301
|
+
def rolledback!(force_restore_state = false) #:nodoc:
|
302
|
+
run_callbacks :rollback
|
303
|
+
ensure
|
304
|
+
restore_transaction_record_state(force_restore_state)
|
305
|
+
end
|
306
|
+
|
307
|
+
# Add the record to the current transaction so that the :after_rollback and :after_commit callbacks
|
308
|
+
# can be called.
|
309
|
+
def add_to_transaction
|
310
|
+
if self.class.connection.add_transaction_record(self)
|
311
|
+
remember_transaction_record_state
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
# Executes +method+ within a transaction and captures its return value as a
|
316
|
+
# status flag. If the status is true the transaction is committed, otherwise
|
317
|
+
# a ROLLBACK is issued. In any case the status flag is returned.
|
318
|
+
#
|
319
|
+
# This method is available within the context of an ActiveRecord::Base
|
320
|
+
# instance.
|
321
|
+
def with_transaction_returning_status
|
322
|
+
status = nil
|
323
|
+
self.class.transaction do
|
324
|
+
add_to_transaction
|
65
325
|
begin
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
block.call
|
71
|
-
|
72
|
-
connection.commit_db_transaction
|
73
|
-
objects.each { |o| o.commit_transaction }
|
74
|
-
rescue Exception => exception
|
75
|
-
connection.rollback_db_transaction
|
76
|
-
objects.each { |o| o.abort_transaction }
|
77
|
-
raise exception
|
78
|
-
ensure
|
79
|
-
TRANSACTION_MUTEX.unlock
|
326
|
+
status = yield
|
327
|
+
rescue ActiveRecord::Rollback
|
328
|
+
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
|
329
|
+
status = nil
|
80
330
|
end
|
331
|
+
|
332
|
+
raise ActiveRecord::Rollback unless status
|
333
|
+
end
|
334
|
+
status
|
335
|
+
end
|
336
|
+
|
337
|
+
protected
|
338
|
+
|
339
|
+
# Save the new record state and id of a record so it can be restored later if a transaction fails.
|
340
|
+
def remember_transaction_record_state #:nodoc:
|
341
|
+
@_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
|
342
|
+
unless @_start_transaction_state.include?(:new_record)
|
343
|
+
@_start_transaction_state[:new_record] = @new_record
|
81
344
|
end
|
345
|
+
unless @_start_transaction_state.include?(:destroyed)
|
346
|
+
@_start_transaction_state[:destroyed] = @destroyed
|
347
|
+
end
|
348
|
+
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
|
349
|
+
@_start_transaction_state[:frozen?] = @attributes.frozen?
|
350
|
+
end
|
351
|
+
|
352
|
+
# Clear the new record state and id of a record.
|
353
|
+
def clear_transaction_record_state #:nodoc:
|
354
|
+
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
|
355
|
+
@_start_transaction_state.clear if @_start_transaction_state[:level] < 1
|
82
356
|
end
|
83
357
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
358
|
+
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
|
359
|
+
def restore_transaction_record_state(force = false) #:nodoc:
|
360
|
+
unless @_start_transaction_state.empty?
|
361
|
+
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
|
362
|
+
if @_start_transaction_state[:level] < 1 || force
|
363
|
+
restore_state = @_start_transaction_state
|
364
|
+
was_frozen = restore_state[:frozen?]
|
365
|
+
@attributes = @attributes.dup if @attributes.frozen?
|
366
|
+
@new_record = restore_state[:new_record]
|
367
|
+
@destroyed = restore_state[:destroyed]
|
368
|
+
if restore_state.has_key?(:id)
|
369
|
+
self.id = restore_state[:id]
|
370
|
+
else
|
371
|
+
@attributes.delete(self.class.primary_key)
|
372
|
+
@attributes_cache.delete(self.class.primary_key)
|
373
|
+
end
|
374
|
+
@attributes.freeze if was_frozen
|
375
|
+
@_start_transaction_state.clear
|
376
|
+
end
|
89
377
|
end
|
90
378
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
379
|
+
|
380
|
+
# Determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed.
|
381
|
+
def transaction_record_state(state) #:nodoc:
|
382
|
+
@_start_transaction_state[state]
|
383
|
+
end
|
384
|
+
|
385
|
+
# Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
|
386
|
+
def transaction_include_any_action?(actions) #:nodoc:
|
387
|
+
actions.any? do |action|
|
388
|
+
case action
|
389
|
+
when :create
|
390
|
+
transaction_record_state(:new_record)
|
391
|
+
when :destroy
|
392
|
+
destroyed?
|
393
|
+
when :update
|
394
|
+
!(transaction_record_state(:new_record) || destroyed?)
|
395
|
+
end
|
98
396
|
end
|
99
|
-
return result
|
100
397
|
end
|
101
398
|
end
|
102
|
-
end
|
399
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Translation
|
3
|
+
include ActiveModel::Translation
|
4
|
+
|
5
|
+
# Set the lookup ancestors for ActiveModel.
|
6
|
+
def lookup_ancestors #:nodoc:
|
7
|
+
klass = self
|
8
|
+
classes = [klass]
|
9
|
+
return classes if klass == ActiveRecord::Base
|
10
|
+
|
11
|
+
while klass != klass.base_class
|
12
|
+
classes << klass = klass.superclass
|
13
|
+
end
|
14
|
+
classes
|
15
|
+
end
|
16
|
+
|
17
|
+
# Set the i18n scope to overwrite ActiveModel.
|
18
|
+
def i18n_scope #:nodoc:
|
19
|
+
:activerecord
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
class AssociatedValidator < ActiveModel::EachValidator #:nodoc:
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
|
6
|
+
record.errors.add(attribute, :invalid, options.merge(:value => value))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
# Validates whether the associated object or objects are all valid.
|
13
|
+
# Works with any kind of association.
|
14
|
+
#
|
15
|
+
# class Book < ActiveRecord::Base
|
16
|
+
# has_many :pages
|
17
|
+
# belongs_to :library
|
18
|
+
#
|
19
|
+
# validates_associated :pages, :library
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# WARNING: This validation must not be used on both ends of an association.
|
23
|
+
# Doing so will lead to a circular dependency and cause infinite recursion.
|
24
|
+
#
|
25
|
+
# NOTE: This validation will not fail if the association hasn't been
|
26
|
+
# assigned. If you want to ensure that the association is both present and
|
27
|
+
# guaranteed to be valid, you also need to use +validates_presence_of+.
|
28
|
+
#
|
29
|
+
# Configuration options:
|
30
|
+
#
|
31
|
+
# * <tt>:message</tt> - A custom error message (default is: "is invalid").
|
32
|
+
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
|
33
|
+
# validation contexts by default (+nil+), other options are <tt>:create</tt>
|
34
|
+
# and <tt>:update</tt>.
|
35
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
36
|
+
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
37
|
+
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
38
|
+
# proc or string should return or evaluate to a +true+ or +false+ value.
|
39
|
+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
40
|
+
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
41
|
+
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
42
|
+
# method, proc or string should return or evaluate to a +true+ or +false+
|
43
|
+
# value.
|
44
|
+
def validates_associated(*attr_names)
|
45
|
+
validates_with AssociatedValidator, _merge_attributes(attr_names)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
class PresenceValidator < ActiveModel::Validations::PresenceValidator # :nodoc:
|
4
|
+
def validate(record)
|
5
|
+
super
|
6
|
+
attributes.each do |attribute|
|
7
|
+
next unless record.class.reflect_on_association(attribute)
|
8
|
+
associated_records = Array(record.send(attribute))
|
9
|
+
|
10
|
+
# Superclass validates presence. Ensure present records aren't about to be destroyed.
|
11
|
+
if associated_records.present? && associated_records.all? { |r| r.marked_for_destruction? }
|
12
|
+
record.errors.add(attribute, :blank, options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
# Validates that the specified attributes are not blank (as defined by
|
20
|
+
# Object#blank?), and, if the attribute is an association, that the
|
21
|
+
# associated object is not marked for destruction. Happens by default
|
22
|
+
# on save.
|
23
|
+
#
|
24
|
+
# class Person < ActiveRecord::Base
|
25
|
+
# has_one :face
|
26
|
+
# validates_presence_of :face
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# The face attribute must be in the object and it cannot be blank or marked
|
30
|
+
# for destruction.
|
31
|
+
#
|
32
|
+
# If you want to validate the presence of a boolean field (where the real values
|
33
|
+
# are true and false), you will want to use
|
34
|
+
# <tt>validates_inclusion_of :field_name, in: [true, false]</tt>.
|
35
|
+
#
|
36
|
+
# This is due to the way Object#blank? handles boolean values:
|
37
|
+
# <tt>false.blank? # => true</tt>.
|
38
|
+
#
|
39
|
+
# This validator defers to the ActiveModel validation for presence, adding the
|
40
|
+
# check to see that an associated object is not marked for destruction. This
|
41
|
+
# prevents the parent object from validating successfully and saving, which then
|
42
|
+
# deletes the associated object, thus putting the parent object into an invalid
|
43
|
+
# state.
|
44
|
+
#
|
45
|
+
# Configuration options:
|
46
|
+
# * <tt>:message</tt> - A custom error message (default is: "can't be blank").
|
47
|
+
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
|
48
|
+
# validation contexts by default (+nil+), other options are <tt>:create</tt>
|
49
|
+
# and <tt>:update</tt>.
|
50
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
|
51
|
+
# the validation should occur (e.g. <tt>if: :allow_validation</tt>, or
|
52
|
+
# <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
|
53
|
+
# or string should return or evaluate to a +true+ or +false+ value.
|
54
|
+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
|
55
|
+
# if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
56
|
+
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
|
57
|
+
# proc or string should return or evaluate to a +true+ or +false+ value.
|
58
|
+
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
59
|
+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
|
60
|
+
def validates_presence_of(*attr_names)
|
61
|
+
validates_with PresenceValidator, _merge_attributes(attr_names)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|