Commit Graph

135 Commits

Author SHA1 Message Date
lulalala
23dd7c0285 Assert deprecation 2019-03-31 22:59:12 +08:00
Ryuta Kamizono
4c05434973 Restore an ability that class level update without giving ids
That ability was introduced at #11898 as `Relation#update` without
giving ids, so the ability on the class level is not documented and not
tested.

c83e30d which fixes #33470 has lost two undocumented abilities.

One has fixed at 5c65688, but I missed the ability on the class level.

Removing any feature should not be suddenly happened in a stable version
even if that is not documented.

I've restored the ability and added test case to avoid any regression in
the future.

Fixes #34743.
2019-01-02 06:23:52 +09:00
Ryuta Kamizono
811be47778 Use assert_no_queries not to ignore BEGIN/COMMIT queries
`test_update_does_not_run_sql_if_record_has_not_changed` would pass
without #18501 since `assert_queries` ignores BEGIN/COMMIT unless
`ignore_none: true` is given.

Since #32647, empty BEGIN/COMMIT is ommited. So we no longer need to use
`assert_queries(0)` to ignore BEGIN/COMMIT in the queries.
2018-10-05 04:11:13 +09:00
Ryuta Kamizono
609b1ec9b6 Persistence#increment! requires an attribute argument which is incremented 2018-09-24 03:03:04 +09:00
Ryuta Kamizono
68d6c1353a Extract {update,delete}_all_test.rb from persistence_test.rb and relations_test.rb
`persistence_test.rb` and `relations_test.rb` have too many lines, so
I'd like to extract relation around tests to dedicated files before
newly test added.
2018-09-16 08:41:08 +09:00
Ryuta Kamizono
cad0b7d91d Fix touch option to behave consistently with Persistence#touch method
`touch` option was added to `increment!` (#27660) and `update_counters`
(#26995). But that option behaves inconsistently with
`Persistence#touch` method.

If `touch` option is passed attribute names, it won't update
update_at/on attributes unlike `Persistence#touch` method.

Due to changed from `Persistence#touch` to `increment!` with `touch`
option, #31405 has a regression that `counter_cache` with `touch` option
which is passed attribute names won't update update_at/on attributes.

I think that the inconsistency is not intended. To get back consistency,
ensure that `touch` option updates update_at/on attributes.
2018-06-18 19:08:41 +09:00
Ryuta Kamizono
1dc17e7b2e Fix CustomCops/AssertNot to allow it to have failure message
Follow up of #32605.
2018-05-13 11:32:47 +09:00
Ryuta Kamizono
ab3ad6a9ad becomes should clear the mutation tracker which is created in after_initialize
`becomes` creates new object and copies attributes from the receiver. If
new object has mutation tracker which is created in `after_initialize`,
it should be cleared since it is for discarded attributes.

But if the receiver doesn't have mutation tracker yet, it will not be
cleared properly.

It should be cleared regardless of whether the receiver has mutation
tracker or not.

Fixes #32867.
2018-05-11 19:57:22 +09:00
Daniel Colson
a1ac18671a Replace assert ! with assert_not
This autocorrects the violations after adding a custom cop in
3305c78dcd.
2018-04-19 08:11:33 -04:00
Eddie Lebow
5645149d3a
Deprecate update_attributes and update_attributes!
Closes #31998
2018-02-17 13:58:54 -08:00
Ryuta Kamizono
06d04bc237 Invoke load_schema in _default_attributes
Currently `_default_attributes` doesn't work unless `load_schema` is
called before.

The `MissingAttributeError` is caused by `reload_schema_from_cache` is
invoked by `serialize`.

I added `load_schema` in `_default_attributes` to `_default_attributes`
works without any dependency like `attribute_types` etc.

Closes #31905.
2018-02-06 22:32:03 +09:00
Daniel Colson
fda1863e1a Remove extra whitespace 2018-01-25 23:32:59 -05:00
Daniel Colson
82c39e1a0b Use assert_empty and assert_not_empty 2018-01-25 23:32:59 -05:00
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Leonel Galan
3562331669 Ignores a default subclass when becomes(Parent)
Fixes issue described in #30399: A default value on the
inheritance column prevented `child.becomes(Parent)` to return
an instance of `Parent` as expected, instead it returns an instance
of the default subclass.

The change was introduced by #17169 and it was meant to affect
initialization, alone. Where `Parent.new` is expected to return
an instance of the default subclass.
2018-01-22 10:04:47 -05:00
Ryuta Kamizono
562dd0494a Don't allow destroyed object mutation after save or save! is called
Currently `object.save` will unfreeze the object, due to
`changes_applied` replaces frozen `@attributes` to new `@attributes`.

Since originally destroyed objects are not allowed to be mutated, `save`
and `save!` should not return success in that case.

Fixes #28563.
2018-01-15 04:52:01 +09:00
Mike Busch
c53287b237 save attributes changed by callbacks after update_attribute
update_attribute previously stopped execution, before saving and before
running callbacks, if the record's attributes hadn't changed. [The
documentation](http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute)
says that "Callbacks are invoked", which was not happening if the
persisted attributes hadn't changed.
2017-12-22 07:41:33 -05:00
Ryuta Kamizono
9e7260da1b Using subselect for delete_all with limit or offset
Arel doesn't support subselect generation for DELETE unlike UPDATE yet,
but we already have that generation in connection adapters. We can
simply use the subselect generated by that one.
2017-12-19 20:27:10 +09:00
Ryuta Kamizono
1118e2c289 Using subselect generated by the connection adapter for update_all with offset
Most RDBMS (except SQLite) requires subselect for UPDATE with OFFSET,
but Arel doesn't support executable subselect generation for MySQL's
UPDATE yet. We need to use the subselect generated by the connection
adapter for now, it works well.

Fixes #30148.
2017-12-19 18:58:25 +09:00
Chris Salzberg
4aee5fcbfb
Add failing test 2017-12-17 11:58:26 +09:00
Chris Salzberg
c91a44d540
Modify test to correctly pass attributes hash 2017-12-15 23:53:49 +09:00
Ryuta Kamizono
05d1e9e413 Remove unnecessary scoping 2017-12-01 18:28:31 +09:00
Ryuta Kamizono
695ec1fef0
Class level update and destroy checks all the records exist before making changes (#31306)
It makes more sense than ignoring invalid IDs.
2017-12-01 18:21:21 +09:00
Ryuta Kamizono
0dc3b6535d Maintain raising RecordNotFound for class level update and destroy
In 35836019, class level `update` and `destroy` suppressed
`RecordNotFound` to ensure returning affected objects. But
`RecordNotFound` is a common exception caught by a `rescue_from`
handler. So changing the behavior when a typical `params[:id]` is passed
has a compatibility problem. The previous behavior should not be
changed.

Fixes #31301.
2017-12-01 12:49:46 +09:00
Ryuta Kamizono
8a2ee3d8c6 Ensure apply_join_dependency for update_all and delete_all if eager-loading is needed
If a relation has eager-loading values, `count` and `exists?` works
properly, but `update_all` and `delete_all` doesn't work due to missing
`apply_join_dependency`. It should be applied to work consistently.

Fixes #28863.
2017-11-06 04:04:54 +09:00
Ryuta Kamizono
358360198e Ensure returning affected objects for class level update and destroy
Class level `update` and `destroy` are using `find` in the internal, so
it will raise `RecordNotFound` if given ids cannot find an object even
though the method already affect (update or destroy) to any objects.
These methods should return affected objects even in that case.
2017-09-18 08:12:59 +09:00
Ryuta Kamizono
7612675fb7 Add test cases that class level destroy, delete, and update are affected by scoping (#29997)
I tried to change the expectation in #29976, but it is expected behavior
at least for now. So I added the test cases to prevent anyone change the
expectation.
2017-08-18 14:01:48 +09:00
Rafael Mendonça França
feb1ddae02 Merge remote-tracking branch 'origin/master' into unlock-minitest 2017-08-01 17:34:14 -04:00
Kir Shatrov
831be98f9a Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
Ryuta Kamizono
972f0d6cb6 Persistence#delete should not be affected by scoping
`self.class.delete` is delegated to `all` and `all` is affected by
scoping. It should use `unscoped` to not be affected by that.
2017-07-18 03:27:55 +09:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Alex Kitchens
550eadf436 Use existing class in PersistenceTest::SaveTest
Creating a new class for widgets was causing failing tests because it
clashed with other widget classes. This test does not need to create its
own class, so I changed it to an existing class.

```
ARCONN=mysql2 bin/test --seed 25364 test/cases/*test.rb -n \
"/^(?:PrimaryKeyIntegerTest#(?:test_primary_key_with_serial_integer_are_automatically_numbered)|PersistenceTest::SaveTest#(?:test_save_touch_false))$/"
```
2017-06-01 17:05:54 -05:00
Ryuta Kamizono
0d17168289 Replace \Z to \z
\Z was a mistake of \z. Replace \Z to \z to prevent newly \Z added.
2017-04-24 21:22:12 +09:00
akihiro17
bad9bfbea6 Add the touch option to ActiveRecord#increment! and decrement!
Supports the `touch` option from update_counters.
The default behavior is not to update timestamp columns.
2017-01-14 01:00:33 +09:00
Diego Plentz
8bd7735a2c fixing update_all and delete_all when chained with left_joins. fixes #27192 2016-11-27 19:47:51 -02:00
Rafael Mendonça França
fe1f4b2ad5
Add more rubocop rules about whitespaces 2016-10-29 01:17:49 -02:00
Prathamesh Sonpatki
730e99affb
Return true if attribute is not changed for update_attribute
- If the attribute is not changed, then update_attribute does not run
  SQL query, this effectively means that no change was made to the
  attribute.
- This change was made in https://github.com/rails/rails/commit/0fcd4cf5
  to avoid a SQL call.
- But the change resulted into `nil` being returned when there was no
  change in the attribute value.
- This commit corrects the behavior to return true if there is no change
  in attribute value. This is same as previous behavior of Rails 4.2
  plus benefit of no additional SQL call.
- Fixes #26593.
2016-09-23 15:24:55 +05:30
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Ryuta Kamizono
f006de5dc5 Fix broken alignments caused by auto-correct commit 411ccbd
Hash syntax auto-correcting breaks alignments. 411ccbdab2608c62aabdb320d52cb02d446bb39c
2016-08-10 06:36:39 +09:00
Xavier Noria
411ccbdab2 remove redundant curlies from hash arguments 2016-08-06 19:44:11 +02:00
Xavier Noria
d22e522179 modernizes hash syntax in activerecord 2016-08-06 19:37:57 +02:00
Xavier Noria
9617db2078 applies new string literal convention in activerecord/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:26:53 +02:00
Akira Matsuda
887eaa8689 Typos in AR tests 2016-02-03 22:08:13 +09:00
Vokhmin Alexey V
61e5081404 ActiveRecord::Base#becomes should copy the errors 2015-12-14 19:02:20 +03:00
Sean Griffin
986f79a446 Fix test failures
The previous commit changes the state of the class, and while we are
cleaning up the database, I forgot to clean up the class
2015-11-07 08:44:29 -07:00
Sean Griffin
9deb6ababe Ensure #reset_column_information clears child classes as well
I've added a redundant test for this under the attributes API as well,
as that also causes this bug to manifest through public API (and
demonstrates that calling `reset_column_information` on the child
classes would be insufficient)

Since children of a class should always share a table with their parent,
just reloading the schema from the cache should be sufficient here.
`reload_schema_from_cache` should probably become public and
`# :nodoc:`, but I'd rather avoid the git churn here.

Fixes #22057
2015-11-07 08:20:34 -07:00
Yuki Nishijima
266455cf25 Deprecate exception#original_exception in favor of exception#cause 2015-11-03 06:54:34 -08:00
Bogdan Gusiev
85a1c02508 Make #increment! and #decrement! methods concurency safe 2015-10-05 15:13:04 +03:00
Wojciech Wnętrzak
c82c5f8ffd Deprecate passing conditions to AR::Relation destroy_all and delete_all methods 2015-09-06 16:14:16 +02:00