rails/activerecord/test/cases
Godfrey Chan 7386ffc781 Restore ActiveRecord states after a rollback for models w/o callbacks
This fixes a regression (#13744) that was caused by 67d8bb9.

In 67d8bb9, we introduced lazy rollback for records, such that the
record's internal states and attributes are not restored immediately
after a transaction rollback, but deferred until they are first
accessed.

This optimization is only performed when the model does not have any
transactional callbacks (e.g. `after_commit` and `after_create`).

Unfortunately, the models used to test the affected codepaths all
comes with some sort of transactional callbacks. Therefore this
codepath remains largely untested until now and as a result there are
a few issues in the implementation that remains hidden until now.

First, the `sync_with_transaction_state` (or more accurately,
`update_attributes_from_transaction_state`) would perform the
synchronization prematurely before a transaction is finalized (i.e.
comitted or rolled back). As a result, when the actuall rollback
happens, the record will incorrectly assumes that its internal states
match the transaction state, and neglect to perform the restore.

Second, `update_attributes_from_transaction_state` calls `committed!`
in some cases. This in turns checks for the `destroyed?` state which
also requires synchronization with the transaction stae, which causes
an infnite recurrsion.

This fix works by deferring the synchronization until the transaction
has been finalized (addressing the first point), and also unrolled
the `committed!` and `rolledback!` logic in-place (addressing the
second point).

It should be noted that the primary purpose of the `committed!` and
`rolledback!` methods are to trigger the relevant transactional
callbacks. Since this code path is only entered when there are no
transactional callbacks on the model, this shouldn't be necessary. By
unrolling the method calls, the intention here (to restore the states
when necessary) becomes more clear.
2014-01-18 11:16:52 -08:00
..
adapters test to ensure bad prepared statements raise a StatementInvalid exception 2014-01-17 14:08:00 -08:00
associations Extract a method to simplify setup code 2014-01-16 18:52:43 -02:00
attribute_methods initialize generated modules on inclusion and on inheritence 2013-07-02 17:11:35 -07:00
coders Remove all error masking when decoding serialized data fails? 2013-04-02 11:45:14 -04:00
connection_adapters Restore DATABASE_URL even if it's nil in connection_handler test 2014-01-10 10:06:19 +05:30
connection_specification Allow "url" sub key in database.yml configuration 2013-12-30 12:21:14 -05:00
fixture_set Introduce a context for rendering fixtures ERB. 2013-12-03 02:52:26 -05:00
migration make change_column_null reversible. Closes #13576. 2014-01-08 15:51:06 +01:00
relation raise ArgumentError exception if Model.where.not is called with nil argument 2013-12-30 01:53:02 +05:30
scoping Fix bad usage of #select with hash 2013-12-03 10:45:40 -02:00
tasks create/drop test and development databases only if RAILS_ENV is nil 2014-01-08 14:37:07 +01:00
validations Add CHANGELOG entry for #13557 [ci skip] 2014-01-01 19:08:28 -02:00
adapter_test.rb using symbol instead of string in establish_connection 2013-12-25 15:24:25 +05:30
aggregations_test.rb Removed unnecessary class 2013-04-06 02:17:38 +05:30
ar_schema_test.rb Call assume_migrated_upto_version on connection 2013-05-11 22:39:39 -04:00
associations_test.rb do is_a? tests on assignment so runtime is faster 2013-07-31 18:19:36 -07:00
attribute_methods_test.rb Fix AR#method_missing re-dispatching into overwritten attribute methods. 2013-09-29 18:58:50 +02:00
autosave_association_test.rb tests without transactional fixtures need to cleanup afterwards. 2014-01-16 09:33:45 +01:00
base_test.rb reset column information after fiddling with Encoding.default_internal 2014-01-16 09:34:49 +01:00
batches_test.rb Merge pull request #11161 from dmitry/find_in_batches_works_without_logger 2013-06-28 06:00:20 -07:00
binary_test.rb Use Encoding::UTF_8 constant 🚯 2013-01-28 17:06:02 +09:00
bind_parameter_test.rb log bind variables after they were type casted. 2013-11-09 11:23:34 +01:00
calculations_test.rb Fix type cast on group sum with custom expression 2013-12-10 14:30:12 +02:00
callbacks_test.rb callbacks are wrapped with lambdas 2013-05-10 10:13:21 -07:00
clone_test.rb Fix freeze applying to cloned objects 2013-04-15 19:57:28 -04:00
column_alias_test.rb please use ruby -I lib:test path/to/test.rb, or export RUBY_OPT 2011-06-06 15:47:13 -07:00
column_definition_test.rb decouple column definition from the database connection 2013-03-22 16:21:59 -07:00
column_test.rb prevent global timezone state from leaking out of test cases. 2013-10-25 14:40:57 +02:00
connection_management_test.rb Don't skip tests if we don't need to. 2013-11-08 13:59:07 -02:00
connection_pool_test.rb if we remove the connection from the pool, we should close it 2013-07-08 16:21:47 -07:00
core_test.rb Extract AR::Core#inspect and AR::Core.inspect's test code from base_test.rb 2013-05-22 02:24:37 +09:00
counter_cache_test.rb Refactor AR's counter_cache_test.rb test 2013-05-23 01:28:04 +09:00
custom_locking_test.rb Deprecate ActiveRecord::Base.scoped. 2012-07-27 17:27:47 +01:00
database_statements_test.rb please use ruby -I lib:test path/to/test.rb, or export RUBY_OPT 2011-06-06 15:47:13 -07:00
date_time_test.rb prevent global timezone state from leaking out of test cases. 2013-10-25 14:40:57 +02:00
defaults_test.rb Handle single quotes in PostgreSQL default column values 2013-06-19 11:03:40 -05:00
dirty_test.rb Add integration test for #12459 2013-12-05 21:32:51 -02:00
disconnected_test.rb Don't skip tests if we don't need to. 2013-11-08 13:59:07 -02:00
dup_test.rb Don't need to check if the scope respond to call 2013-08-31 14:53:28 -03:00
enum_test.rb Enum mappings are now exposed via class methods instead of constants. 2014-01-14 04:00:34 -08:00
explain_subscriber_test.rb Don't try to EXPLAIN select_db calls 2013-05-10 16:21:59 +02:00
explain_test.rb Remove unused return value, because collecting_queries_for_explain isn't public API. 2013-03-06 00:35:55 +09:00
finder_respond_to_test.rb Avoid compiling regexs in AR::Base.respond_to? 2013-08-21 18:18:26 -07:00
finder_test.rb we have with_env_tz as global test helper. Remove duplicate. 2014-01-16 09:34:49 +01:00
fixtures_test.rb Fix bad test order dependency between LoadAllFixturesTest#test_all_there and FoxyFixturesTest#test_ignores_belongs_to_symbols_if_association_and_foreign_key_are_named_the_same 2013-11-16 00:22:05 -08:00
forbidden_attributes_protection_test.rb Add regression test to #12343 2013-09-24 20:59:26 -03:00
habtm_destroy_order_test.rb Destroy association habtm record before destroying the record itself. Fixes issue #402. 2011-07-08 22:26:54 +01:00
helper.rb Disable available locales checks to avoid warnings running the tests 2013-12-17 09:05:41 -02:00
hot_compatibility_test.rb recover from test runs that leave the database in a bad state 2014-01-13 15:02:28 -08:00
i18n_test.rb please use ruby -I lib:test path/to/test.rb, or export RUBY_OPT 2011-06-06 15:47:13 -07:00
inheritance_test.rb Don't try to get the subclass if the inheritance column doesn't exist 2014-01-14 18:53:45 +05:30
integration_test.rb Fix test for cache_key + touched 2014-01-16 09:05:55 -02:00
invalid_connection_test.rb Changed message for Model.inspect 2013-11-09 20:57:09 +01:00
invalid_date_test.rb Remove unused assignments from activerecord tests 2012-04-29 10:02:58 -07:00
invertible_migration_test.rb fix failing test caused by 3771e4d511 2013-05-06 16:49:06 -04:00
json_serialization_test.rb rewrite tests to have no side-effects on include_root_in_json. 2013-03-04 21:18:40 +01:00
locking_test.rb Deprecate unused quoted_locking_column method. 2013-09-24 21:56:52 +09:00
log_subscriber_test.rb Don't skip tests if we don't need to. 2013-11-08 13:59:07 -02:00
migration_test.rb we have with_env_tz as global test helper. Remove duplicate. 2014-01-16 09:34:49 +01:00
migrator_test.rb Revert "Used Yield instead of block.call" -- this causes all of atom_feed_helper_test.rb to fail with "SystemStackError: stack level too deep". 2013-11-14 15:31:27 -08:00
mixin_test.rb replace custom Time stubbing with travel and travel_to. 2013-11-21 17:29:53 +01:00
modules_test.rb add missing require 2013-10-02 17:04:02 -07:00
multiparameter_attributes_test.rb Don't skip tests if we don't need to. 2013-11-08 13:59:07 -02:00
multiple_db_test.rb Deprecate use of string in establish_connection as connection lookup 2013-12-24 10:18:54 +01:00
nested_attributes_test.rb This test does not test anything that happens in the real world. If you 2013-06-13 15:36:50 -07:00
nested_attributes_with_callbacks_test.rb Use assert_not rather than refute 2013-08-12 15:21:21 +02:00
persistence_test.rb Copy changed_attributes across to newly become'd records 2013-12-27 15:54:06 +00:00
pooled_connections_test.rb remove dead code 2013-07-08 16:30:10 -07:00
primary_keys_test.rb Remove more skip 2013-11-08 14:18:54 -02:00
query_cache_test.rb use teardown for cleanup, not setup. 2014-01-16 09:33:45 +01:00
quoting_test.rb prevent global timezone state from leaking out of test cases. 2013-10-25 14:40:57 +02:00
readonly_test.rb implicit_readonly is being removed in favor of calling readonly explicitly 2013-05-27 21:07:39 +02:00
reaper_test.rb ConnectionPool wait_timeout no longer used for different types of timeouts. #6441 2012-05-23 12:08:11 -04:00
reflection_test.rb scope_chain should not be mutated for other reflections 2013-10-14 11:59:59 -04:00
relation_test.rb Improve tests to use only public API 2013-12-31 21:04:11 -02:00
relations_test.rb rm dup code 2014-01-14 17:47:10 -08:00
reload_models_test.rb Revert "Merge pull request #8989 from robertomiranda/use-rails-4-find-by" 2013-01-18 09:15:19 -05:00
result_test.rb Merge pull request #10993 from Empact/result-each-enumerator 2013-06-25 20:07:59 -03:00
sanitize_test.rb Process sub-query relation's binding values 2013-10-20 21:07:07 +03:00
schema_dumper_test.rb sqlite >= 3.8.0 supports partial indexes 2014-01-14 08:53:32 -07:00
serialization_test.rb ActiveRecord::Base.include_root_in_json is false by default. 2013-03-04 21:18:40 +01:00
serialized_attribute_test.rb prevent global timezone state from leaking out of test cases. 2013-10-25 14:40:57 +02:00
statement_cache_test.rb cleanup statement cache test 2013-04-11 18:08:46 +05:30
store_test.rb Fix: ActiveRecord::Store TypeError conversion when using YAML coder 2014-01-06 11:36:19 -02:00
test_case.rb Do not count query for all_constraints and all_tab_cols in Oracle 2013-11-18 11:59:36 +09:00
timestamp_test.rb polymorphic belongs_to association with touch: true updates old record correctly 2013-12-05 16:03:07 +01:00
transaction_callbacks_test.rb Do not set up a variable used only in two tests 2014-01-16 18:40:18 -02:00
transaction_isolation_test.rb Deprecate use of string in establish_connection as connection lookup 2013-12-24 10:18:54 +01:00
transactions_test.rb Restore ActiveRecord states after a rollback for models w/o callbacks 2014-01-18 11:16:52 -08:00
unconnected_test.rb Remove ActiveRecord::Model 2012-10-26 15:51:02 +01:00
validations_repair_helper.rb The repair_validations helper was not working correctly before because 2013-03-19 12:23:20 -04:00
validations_test.rb Prefer assert_raise instead of flunk + rescue to test for exceptions 2013-12-19 09:20:51 -02:00
xml_serialization_test.rb prevent global timezone state from leaking out of test cases. 2013-10-25 14:40:57 +02:00
yaml_serialization_test.rb Add test for ActiveRecord::Relation YAML serialization, for #5303 issue 2013-11-03 21:35:44 +07:00