Commit Graph

7766 Commits

Author SHA1 Message Date
Kir Shatrov
8312a0d222 Deprecate reflection class name to accept a class
The idea of `class_name` as an option of reflection is that passing a
string would allow us to lazy autoload the class.

Using `belongs_to :client, class_name: Customer` is eagerloading models more than necessary
and creating possible circular dependencies.
2017-01-09 13:08:33 -05:00
Santiago Pastorino
80bf338415 Merge pull request #27603 from prathamesh-sonpatki/fix-random-failure
Fix random failure related to migration environment
2017-01-08 00:35:36 -03:00
Santiago Pastorino
533da1d5a9 Merge pull request #27602 from prathamesh-sonpatki/followup-27596
Followup of #27596, test output of "run" method as well
2017-01-08 00:35:22 -03:00
Sean Griffin
11a50f4811 Merge pull request #27598 from NickLaMuro/fix-deep-nesting-where-clauses-with-joins
Fix bug with symbolized keys in .where with nested join
2017-01-07 17:30:34 -05:00
Prathamesh Sonpatki
33191e2079
Fix random failure related to migration environment
- Reference: https://travis-ci.org/rails/rails/jobs/189764676

- Reproduction command:

    MTB_VERBOSE=2 bundle exec minitest_bisect --seed 33328 -Itest "test/cases/migration_test.rb" "test/cases/tasks/database_tasks_test.rb"

- You need to also add minitest_bisect gem to the Gemfile to reproduce
  this failure.
2017-01-07 17:45:33 +05:30
Prathamesh Sonpatki
cb28aee503
Followup of #27596, test output of "run" method as well
- #27596 Updated "run" and "migrate" method for returning correct
   value but only added test for "migrate" method. This commit adds a
   test for "run" as well.
2017-01-07 16:39:34 +05:30
Nick LaMuro
b42e594a43 Add failing test for where with joins
This will cause a failure with the changes from 8e2e5f9:

8e2e5f9e3d

With the `singularize` call that is being done in that method when there
is multiple nestings of associations (JOIN calling a JOIN) and the
`stringify_keys!` is only called once here:

https://github.com/rails/rails/blob/21e5fd4/activerecord/lib/active_record/relation/where_clause_factory.rb#L16

And not in the subsequent recursion in `.predicate_builder`
2017-01-06 17:49:19 -06:00
schneems
93ee268ef4 Preserve up and down return type
In Rails 4.2 calling `ActiveRecord::Migrator.migrate` would return an array of results. Without realizing that this return type was expected I accidentally introduced a change in 4d60e93174

This PR preserves the previous behavior and adds a test on the return type. This will need a backport to 5.0 branch.
2017-01-06 13:30:42 -06:00
Akira Matsuda
5f03172f54 Privatize unneededly protected methods in Active Record 2017-01-05 20:01:02 +09:00
Akira Matsuda
5473e390d3 self. is not needed when calling its own instance method
Actually, private methods cannot be called with `self.`, so it's not just redundant, it's a bad habit in Ruby
2017-01-05 19:58:52 +09:00
Rafael Mendonça França
209bfc0a5c
Fix style guide violations 2017-01-05 00:40:24 -05:00
Yasuo Honda
cc3b114b42 Require "models/post" before "models/comment"
Refer a similar pull request #26370
2017-01-04 21:13:05 +00:00
Chris Holmes
f8ab3ae18f Raise error when has_many through is defined before through association
https://github.com/rails/rails/issues/26834

This change raises an error if a has_many through association
is defined before the through association.
2017-01-04 11:56:08 +00:00
Ryuta Kamizono
b334aa0ea4 Fix find_by and where consistency
The alternative of #26213.

Currently `find_by` and `where` with AR object return inconsistent
result. This is caused by statement cache does not support AR object.
Passing to finder method to fix the issue.

Fixes #26210.
2017-01-04 12:27:44 +09:00
Rafael Mendonça França
127509c071
Revert "Merge pull request #21233 from mtsmfm/disable-referential-integrity-without-superuser-privileges"
This reverts commit eeac6151a55cb7d5f799e1ae33aa64a839cbc3aa, reversing
changes made to 5c40239d3104543e70508360d27584a3e4dc5baf.

Reason: Broke the isolated tests.
https://travis-ci.org/rails/rails/builds/188721346
2017-01-03 22:11:16 -05:00
Rafael França
c1b8d0d94e Merge pull request #27563 from kamipo/fix_typo
Fix typo s/permited/permitted/
2017-01-03 20:47:05 -05:00
Ryuta Kamizono
a7cb2f9638 Fix typo s/permited/permitted/
```
% git grep -n permited
actionview/test/template/url_helper_test.rb:238:  def test_button_to_with_permited_strong_params
actionview/test/template/url_helper_test.rb:245:  def test_button_to_with_unpermited_strong_params
activerecord/test/cases/relations_test.rb:1620:  def test_update_on_relation_passing_active_record_object_is_not_permited
```
2017-01-04 10:41:12 +09:00
Rafael França
eeac6151a5 Merge pull request #21233 from mtsmfm/disable-referential-integrity-without-superuser-privileges
Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser privileges
2017-01-03 20:38:50 -05:00
Rafael Mendonça França
fdc219e0f8
Merge pull request #26689 from kamipo/deprecate_passing_name_to_indexes
Deprecate passing `name` to `indexes` like `tables`
2017-01-03 19:25:14 -05:00
Rafael Mendonça França
0df8649f58
Remove deprecation of using ActiveRecord::Base instance in .update 2017-01-03 19:22:52 -05:00
Ryuta Kamizono
52ba03438e
Fix test name deprecated to not_permitted
Follow up to 4b6709e818177792735e99a70ec03210c0ac38dc.
2017-01-03 19:04:30 -05:00
Ryuta Kamizono
457e6c77d4 Deprecate passing name to indexes like tables
Passing `name` to `tables` is already deprecated at #21601.
Passing `name` to `indexes` is also unused.
2017-01-04 08:48:59 +09:00
Sean Griffin
032fe34803 Merge pull request #25460 from maclover7/jm-uncached
Add explicit testing for `uncached` vs. `cached`
2017-01-03 18:43:16 -05:00
Jon Moss
8ded825bc2 Compare deserialized values for PostgreSQL::OID::Hstore types
Per the regression commit below, the commit changes the behavior of
`#changed?`to consult the `#changed_in_place?` method on `Type::Value` classes.
Per this change, `PostgreSQL::OID::Hstore` needs to override this method
in order to compare the deserialized forms of the two arguments. In
Ruby, two hashes are considered equal even if their key order is
different. This commit helps to bring that behavior to `Hstore` values.

Fixes regression introduced by 8e633e505880755e7e366ccec2210bbe2b5436e7

Fixes #27502
2017-01-03 16:07:47 -05:00
Rafael Mendonça França
4b6709e818
Raise ArgumentError when a instance of ActiveRecord::Base is passed to
find and exists?
2017-01-03 15:53:03 -05:00
Sean Griffin
929ded40fc Merge pull request #27335 from kamipo/remove_duplicated_model_definition
Remove duplicated model class definitions in `test/cases/base_test.rb`
2017-01-03 12:33:31 -05:00
Sean Griffin
0f1d0b1b52 Consistently apply adapter behavior when serializing arrays
In f1a0fa9 we moved backend specific timestamp behavior out of the type
and into the adapter. This was in line with our general attempt to
reduce the number of adapter specific type subclasses. However, on PG,
the array type performs all serialization, including database encoding
in its serialize method.

This means that we have converted the value into a string before
reaching the database, so no adapter specific logic can be applied (and
this also means that timestamp arrays were using the default `.to_s`
method on the given object, which likely meant timestamps were being
ignored in certain cases as well)

Ultimately I want to do a more in depth refactoring which separates
database serializer objects from the active model type objects, to give
us a less awkward API for introducing the attributes API onto Active
Model.

However, in the short term, we follow the solution we've applied
elsewhere for this. Move behavior off of the type and into the adapter,
and use a data object to allow the type to communicate information up
the stack.

Fixes #27514.
2017-01-03 11:15:16 -05:00
Konstantin Lazarev
cdf8a2b493 Cache results of computing model type
We faced a significant performance decrease when we started using STI
without storing full namespaced class name in type column (because of PostgreSQL
length limit for ENUM types).
We realized that the cause of it is the slow STI model instantiation. Problematic
method appears to be `ActiveRecord::Base.compute_type`, which is used to find
the right class for STI model on every instantiation.
It builds an array of candidate types and then iterates through it calling
`safe_constantize` on every type until it finds appropriate constant. So if
desired type isn't the first element in this array there will be at least one
unsuccessful call to `safe_constantize`, which is very expensive, since it's
defined in terms of `begin; rescue; end`.

This commit is an attempt to speed up `compute_type` method simply by caching
results of previous calls.

```ruby
class MyCompany::MyApp::Business::Accounts::Base < ApplicationRecord
  self.table_name = 'accounts'
  self.store_full_sti_class = false
end

class MyCompany::MyApp::Business::Accounts::Free < Base
end

class MyCompany::MyApp::Business::Accounts::Standard < Base
  # patch .compute_type there
end

puts '======================= .compute_type ======================='
Benchmark.ips do |x|
  x.report("original method") do
    MyCompany::MyApp::Business::Accounts::Free.send :compute_type, 'Free'
  end
  x.report("with types cached") do
    MyCompany::MyApp::Business::Accounts::Standard.send :compute_type, 'Standard'
  end
  x.compare!
end
```

```
======================= .compute_type =======================
  with types cached:  1529019.4 i/s
    original method:     2850.2 i/s - 536.46x  slower
```

```ruby
5_000.times do |i|
  MyCompany::MyApp::Business::Accounts::Standard.create!(name: "standard_#{i}")
end

5_000.times do |i|
  MyCompany::MyApp::Business::Accounts::Free.create!(name: "free_#{i}")
end

puts '====================== .limit(100).to_a ======================='
Benchmark.ips do |x|
  x.report("without .compute_type patch") do
    MyCompany::MyApp::Business::Accounts::Free.limit(100).to_a
  end
  x.report("with .compute_type patch") do
    MyCompany::MyApp::Business::Accounts::Standard.limit(100).to_a
  end
  x.compare!
end
```

```
====================== .limit(100).to_a =======================
     with .compute_type patch:      360.5 i/s
  without .compute_type patch:       24.7 i/s - 14.59x  slower
```
2017-01-03 19:02:38 +05:00
Kasper Timm Hansen
33e60514ae Merge pull request #27543 from kamipo/fix_update_counters_of_multiple_records_with_touch
Fix update counters of multiple records with touch: true
2017-01-02 21:10:32 +01:00
Ryuta Kamizono
77ff9a0adb Push current_time_from_proper_timezone and timestamp attributes methods up to class method
Actually these methods don't need instantiation.
2017-01-03 04:34:43 +09:00
Ryuta Kamizono
b177427d97 Fix update counters of multiple records with touch: true
Currently does not work the following example in the doc:

```ruby
  # For the Posts with id of 10 and 15, increment the comment_count by 1
  # and update the updated_at value for each counter.
  Post.update_counters [10, 15], comment_count: 1, touch: true
  # Executes the following SQL:
  # UPDATE posts
  #    SET comment_count = COALESCE(comment_count, 0) + 1,
  #    `updated_at` = '2016-10-13T09:59:23-05:00'
  #  WHERE id IN (10, 15)
```
2017-01-03 01:40:04 +09:00
utilum
b63c2d6bfb Remove duplicated test
b8f74860b61782e3b949ade3bb51bff40899e89b provided a nicer version of
`#test_structure_load` but the old version was not removed.
2017-01-02 16:30:56 +01:00
Rafael França
2e621f6c91 Merge pull request #27526 from kirs/database-tasks-schema-cache
Dump schema cache for custom connection
2017-01-01 22:55:49 -05:00
Kir Shatrov
6cd757963d Dump schema cache for custom connection
Today `rake db:schema:cache:dump` only supports dumping cache for a
single connection (`ActiveRecord::Base.connection`). This doesn't work
for apps with multiple databases.

This PR makes `DatabaseTasks` to provide an API for dumping schema cache
for any connection.
2017-01-01 19:23:23 -05:00
Matthew Draper
dae525eec8 Replace sleep with synchronization 2017-01-02 05:04:17 +10:30
Kasper Timm Hansen
19deeb08df Fix tests with counter cache touching and more.
* Refactor to use `touch_updates`

  Ensures we only call `current_time_from_proper_timezone` from one place.

* Clarify touch default in tests.

  Not interested in what happens when passed false but that
  nothing passed means no touching.

* Backdate touched columns in tests.

  We can't be sure a test progresses through time, so our
  touching code may be working correctly but the test
  itself is brittle.

  Fix by backdating that's further in the past akin to
  what the timestamps tests do:
  d753645d40/activerecord/test/cases/timestamp_test.rb (L17)

* Expand changelog entry.

  Elaborate and show examples.

Closes #26995.

[ Jarred Trost & Kasper Timm Hansen ]
2017-01-01 18:34:50 +01:00
Jarred Trost
bf77e641ce Added option to ActiveRecord::CounterCache methods. 2017-01-01 18:33:50 +01:00
Ryuta Kamizono
2b5dacb43d Change timestamp_attributes_for_{create,update} from symbol to string
`timestamp_attributes_for_{create,update}` is defined as symbol but
always used as string with `to_s`. This allocates extra strings. To
avoid extra allocation, change the definitions from symbol to string.

```ruby
pp ObjectSpace::AllocationTracer.trace {
  1_000.times { |i|
    Post.create!
  }
}
```

Before:

```
["~/rails/activerecord/lib/active_record/timestamp.rb", 121]=>[1002, 0, 750, 0, 1, 18528],
["~/rails/activerecord/lib/active_record/timestamp.rb", 105]=>[1002, 0, 750, 0, 1, 7720],
["~/rails/activerecord/lib/active_record/timestamp.rb", 101]=>[1002, 0, 750, 0, 1, 7720],
["~/rails/activerecord/lib/active_record/timestamp.rb", 109]=>[1002, 0, 750, 0, 1, 13896],
["~/rails/activerecord/lib/active_record/timestamp.rb", 61]=>[4008, 0, 3000, 0, 1, 30880],
```

After:

```
["~/rails/activerecord/lib/active_record/timestamp.rb", 120]=>[1000, 0, 756, 0, 1, 17184],
["~/rails/activerecord/lib/active_record/timestamp.rb", 104]=>[1000, 0, 756, 0, 1, 7160],
["~/rails/activerecord/lib/active_record/timestamp.rb", 100]=>[1000, 0, 756, 0, 1, 7160],
["~/rails/activerecord/lib/active_record/timestamp.rb", 108]=>[1000, 0, 756, 0, 1, 12888],
```
2017-01-01 07:07:45 +09:00
Ryuta Kamizono
b43d017397 Dump array subtype options correctly
Currently schema dumper does not dump array subtype `precision` and
`scale` options. This commit fixes the issue.
2017-01-01 02:56:13 +09:00
Ryuta Kamizono
c6dcc37bee Remove deprecated #uniq, #uniq!, and #uniq_value 2016-12-30 21:29:18 +09:00
Ryuta Kamizono
74ac546640 Remove deprecated #insert_sql, #update_sql, and #delete_sql 2016-12-30 21:14:59 +09:00
Rafael Mendonça França
3955218dc1
Remove deprecated #use_transactional_fixtures configuration 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
e7381d289e
Remove deprecated conditions parameter from #delete_all 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
d31a6d1384
Remove deprecated conditions parameter from #destroy_all 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
4fc3366d9d
Remove deprecated support to passing arguments to #select when a block is provided. 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
fc3e679647
Remove deprecated support to query using commas on LIMIT 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
b4664864c9
Remove deprecated support to passing a class as a value in a query 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
931b4b4d64
Raises IrreversibleOrderError when using last with an irreversible order 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
0944182ad7
Raise when a through association has an ambiguous reflection name 2016-12-29 17:53:04 -05:00
Rafael Mendonça França
249f71a22a
Raises when ActiveRecord::Migration is inherited directly. 2016-12-29 17:53:04 -05:00