Commit Graph

116 Commits

Author SHA1 Message Date
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
Sean Griffin
4f39d897b3 Fix failing tests caused by #21350 2015-08-24 07:59:40 -06:00
Shane Hender
95bdb95ad5 Cause ActiveRecord::Base::reload to also ignore the QueryCache. 2015-04-28 18:00:09 +01:00
Kuldeep Aggarwal
bf6a33fd82 use Model.reset_column_information to clear table cache connection wide.
`widgets` table is being created in `primary_keys_test.rb` for PostgreSQLAdapter, MysqlAdapter, Mysql2Adapter
    and it makes test to fail earlier.

    Before:
      `bundle exec rake mysql2:test`

    ```
    Finished in 127.287669s, 35.5258 runs/s, 97.8885 assertions/s.

      1) Error:
    PersistenceTest::SaveTest#test_save_touch_false:
    ActiveModel::UnknownAttributeError: unknown attribute 'name' for #<Class:0x0000000a7d6ef0>.
        /home/kd/projects/kd-rails/activerecord/lib/active_record/attribute_assignment.rb:36:in `rescue in _assign_attribute'
        /home/kd/projects/kd-rails/activerecord/lib/active_record/attribute_assignment.rb:34:in `_assign_attribute'
        /home/kd/projects/kd-rails/activemodel/lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
        /home/kd/projects/kd-rails/activemodel/lib/active_model/attribute_assignment.rb:39:in `each'
        /home/kd/projects/kd-rails/activemodel/lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
        /home/kd/projects/kd-rails/activerecord/lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
        /home/kd/projects/kd-rails/activemodel/lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
        /home/kd/projects/kd-rails/activerecord/lib/active_record/core.rb:293:in `initialize'
        /home/kd/projects/kd-rails/activerecord/lib/active_record/inheritance.rb:61:in `new'
        /home/kd/projects/kd-rails/activerecord/lib/active_record/inheritance.rb:61:in `new'
        /home/kd/projects/kd-rails/activerecord/lib/active_record/persistence.rb:50:in `create!'
        /home/kd/projects/kd-rails/activerecord/test/cases/persistence_test.rb:913:in `test_save_touch_false'

    4522 runs, 12460 assertions, 0 failures, 1 errors, 4 skips
    ```

    After:
      `bundle exec rake mysql2:test`

    ```
       Finished in 135.785086s, 33.3026 runs/s, 91.7774 assertions/s.

       4522 runs, 12462 assertions, 0 failures, 0 errors, 4 skips
    ```
2015-03-31 12:14:03 +05:30
Brandon Weiss
0965863564 Closes rails/rails#18864: Renaming transactional fixtures to transactional tests
I’m renaming all instances of `use_transcational_fixtures` to
`use_transactional_tests` and “transactional fixtures” to
“transactional tests”.

I’m deprecating `use_transactional_fixtures=`. So anyone who is
explicitly setting this will get a warning telling them to use
`use_transactional_tests=` instead.

I’m maintaining backwards compatibility—both forms will work.
`use_transactional_tests` will check to see if
`use_transactional_fixtures` is set and use that, otherwise it will use
itself. But because `use_transactional_tests` is a class attribute
(created with `class_attribute`) this requires a little bit of hoop
jumping. The writer method that `class_attribute` generates defines a
new reader method that return the value being set. Which means we can’t
set the default of `true` using `use_transactional_tests=` as was done
previously because that won’t take into account anyone using
`use_transactional_fixtures`. Instead I defined the reader method
manually and it checks `use_transactional_fixtures`. If it was set then
it should be used, otherwise it should return the default, which is
`true`. If someone uses `use_transactional_tests=` then it will
overwrite the backwards-compatible method with whatever they set.
2015-03-16 11:35:44 -07:00
Yves Senn
dea2aafbfa add regression test. Closes #18400. 2015-03-05 11:39:29 +01:00
Prathamesh Sonpatki
0fcd4cf5c2 Run SQL only if attribute changed for update_attribute method
- This is based on https://github.com/rails/rails/issues/18400 but
   tackling same issue with update_attribute method instead of update method.
2015-01-18 20:00:57 +05:30
Rafael Mendonça França
9598c9655f Merge pull request #11898 from prathamesh-sonpatki/patch-update
Changed ActiveRecord::Relation#update behavior so that it will work on Relation objects without giving id

Conflicts:
	activerecord/CHANGELOG.md
2015-01-02 10:48:48 -03:00
Dan Olson
e780e2fda3 Provide :touch option to save() to accommodate saving without updating timestamps. [#18202] 2014-12-27 22:22:37 -06:00
Prathamesh Sonpatki
5ef713c53c Allow ActiveRecord::Relation#update to run on result of a relation with callbacks and validations
- Right now, there is no method to update multiple records with
  validations and callbacks.
- Changed the behavior of existing `update` method so that when `id`
  attribute is not given and the method is called on an `Relation`
  object, it will execute update for every record of the `Relation` and
  will run validations and callbacks for every record.
- Added test case for validating that the callbacks run when `update` is
  called on a `Relation`.
- Changed test_create_columns_not_equal_attributes test from
  persistence_test to include author_name column on topics table as it
  it used in before_update callback.
- This change introduces performance issues when a large number of
  records are to be updated because it runs UPDATE query for every
  record of the result. The `update_all` method can be used in that case
  if callbacks are not required because it will only run single UPDATE
  for all the records.
2014-12-20 15:33:18 +05:30
Erik Michaels-Ober
d1374f99bf Pass symbol as an argument instead of a block 2014-11-29 11:53:24 +01:00
Arun Agrawal
9ae210ba09 Build fix when running in isolation
`Computer` class needs to be require

See #17217 for more details
2014-11-14 10:24:11 +01:00
Godfrey Chan
c031016558 Fix mysql/mysql2 failing with FK constraint errors
One of the author fixture we have ("david") references an author address by ID.
Since we disable FK checks when inserting fixtures, this is all fine until we
try to update it, at which point MySQL would complain about the missing row
referenced by the `author_address_id`.

[Godfrey Chan, Matthew Draper]
2014-07-05 13:57:16 -07:00
Matthew Draper
de03c29778 After find-via-reload, the record is not new 2014-07-05 00:21:24 +09:30
Sean Griffin
3529bbd8ca reload should fully reload attributes
`reload` is meant to put a record in the same state it would be if you
were to do `Post.find(post.id)`. This means we should fully replace the
attributes hash.
2014-06-22 14:34:31 -06:00
Rafael Mendonça França
e61e1b30a5 Merge pull request #15593 from sgrif/sg-attribute
Introduce an Attribute object to handle the type casting dance
2014-06-13 15:45:49 -03:00
Sean Griffin
6f08db05c0 Introduce an Attribute object to handle the type casting dance
There's a lot more that can be moved to these, but this felt like a good
place to introduce the object. Plans are:

- Remove all knowledge of type casting from the columns, beyond a
  reference to the cast_type
- Move type_cast_for_database to these objects
- Potentially make them mutable, introduce a state machine, and have
  dirty checking handled here as well
- Move `attribute`, `decorate_attribute`, and anything else that
  modifies types to mess with this object, not the columns hash
- Introduce a collection object to manage these, reduce allocations, and
  not require serializing the types
2014-06-13 10:20:54 -06:00
Sean Griffin
8019ca7a0f Update test data which doesn't reflect expected usage
Topics call `serialize :content`, which means that the values in the
database should be YAML encoded, and we would only expect to receive
YAML strings to `update_column` and `update_columns`.
2014-06-12 15:33:25 -06:00
Yves Senn
485dab41c4 Merge pull request #15503 from sgrif/sg-json-hstore-storage
Bring type casting behavior of hstore/json in line with serialized
2014-06-06 14:11:54 +02:00
Yves Senn
01777079cd Merge pull request #14971 from versioncontrol/#14785
Baseclass becomes! subclass
2014-06-06 08:56:29 +02:00