aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
* Use separate Relation subclasses for each AR classJon Leighton2012-11-303-35/+103
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | At present, ActiveRecord::Delegation compiles delegation methods on a global basis. The compiled methods apply to all subsequent Relation instances. This creates several problems: 1) After Post.all.recent has been called, User.all.respond_to?(:recent) will be true, even if User.all.recent will actually raise an error due to no User.recent method existing. (See #8080.) 2) Depending on the AR class, the delegation should do different things. For example, if a Post.zip method exists, then Post.all.zip should call it. But this will then result in User.zip being called by a subsequent User.all.zip, even if User.zip does not exist, when in fact User.all.zip should call User.all.to_a.zip. (There are various variants of this problem.) We are creating these compiled delegations in order to avoid method missing and to avoid repeating logic on each invocation. One way of handling these issues is to add additional checks in various places to ensure we're doing the "right thing". However, this makes the compiled methods signficantly slower. In which case, there's almost no point in avoiding method_missing at all. (See #8127 for a proposed solution which takes this approach.) This is an alternative approach which involves creating a subclass of ActiveRecord::Relation for each AR class represented. So, with this patch, Post.all.class != User.all.class. This means that the delegations are compiled for and only apply to a single AR class. A compiled method for Post.all will not be invoked from User.all. This solves the above issues without incurring significant performance penalties. It's designed to be relatively seamless, however the downside is a bit of complexity and potentially confusion for a user who thinks that Post.all and User.all should be instances of the same class. Benchmark --------- require 'active_record' require 'benchmark/ips' class Post < ActiveRecord::Base establish_connection adapter: 'sqlite3', database: ':memory:' connection.create_table :posts def self.omg :omg end end relation = Post.all Benchmark.ips do |r| r.report('delegation') { relation.omg } r.report('constructing') { Post.all } end Before ------ Calculating ------------------------------------- delegation 4392 i/100ms constructing 4780 i/100ms ------------------------------------------------- delegation 144235.9 (±27.7%) i/s - 663192 in 5.038075s constructing 182015.5 (±21.2%) i/s - 850840 in 5.005364s After ----- Calculating ------------------------------------- delegation 6677 i/100ms constructing 6260 i/100ms ------------------------------------------------- delegation 166828.2 (±34.2%) i/s - 754501 in 5.001430s constructing 116575.5 (±18.6%) i/s - 563400 in 5.036690s Comments -------- Bear in mind that the standard deviations in the above are huge, so we can't compare the numbers too directly. However, we can conclude that Relation construction has become a little slower (as we'd expect), but not by a huge huge amount, and we can still construct a large number of Relations quite quickly.
* Fix memory leak in development modeJon Leighton2012-11-301-8/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Keying these hashes by klass causes reloadable classes to never get freed. Thanks to @thedarkone for pointing this out in the comments on 221571beb6b4bb7437989bdefaf421f993ab6002. This doesn't seem to make a massive difference to performance. Benchmark --------- require 'active_record' require 'benchmark/ips' class Post < ActiveRecord::Base establish_connection adapter: 'sqlite3', database: ':memory:' end GC.disable Benchmark.ips(20) do |r| r.report { Post.connection } end Before ------ Calculating ------------------------------------- 5632 i/100ms ------------------------------------------------- 218671.0 (±1.9%) i/s - 4364800 in 19.969401s After ----- Calculating ------------------------------------- 8743 i/100ms ------------------------------------------------- 206525.9 (±17.8%) i/s - 4039266 in 19.992590s
* Mark Relation mutators as :nodoc:Jon Leighton2012-11-302-48/+23
| | | | | These are for internal use only and cannot be relied on as part of the public API. See discussion on 8c2c60511beaad05a218e73c4918ab89fb1804f0.
* Move instantiation responsibilities from Inheritance to Persistence. Have ↵Jeremy Kemper2012-11-292-24/+50
| | | | Inheritance#discriminate_class_for_record handle STI lookup duties.
* Added STI support to init and building associationsJason Rush2012-11-293-1/+28
| | | | | | | | Allows you to do BaseClass.new(:type => "SubClass") as well as parent.children.build(:type => "SubClass") or parent.build_child to initialize an STI subclass. Ensures that the class name is a valid class and that it is in the ancestors of the super class that the association is expecting.
* Fix typo in the Observers deprecation message. [ci skip]Lucas Mazza2012-11-281-1/+1
|
* Gracefully handle upgrading apps with observersRafael Mendonça França2012-11-281-0/+12
| | | | | | Rather than just raising a NoMethodError when copying the config, this commit adds a warning message until either the rails-observers gem is installed or the relevant config options are removed.
* Remove observers and sweepersRafael Mendonça França2012-11-288-165/+4
| | | | | | | | They was extracted from a plugin. See https://github.com/rails/rails-observers [Rafael Mendonça França + Steve Klabnik]
* Ensure that associations have a symbol argument.Steve Klabnik2012-11-281-0/+2
| | | | Fixes #7418.
* Remove useless check of AR being defined from teardown fixturesCarlos Antonio da Silva2012-11-271-5/+4
| | | | | | | We are already in the AR namespace, there's no way for it to be undefined. See the cousin commit 13e72db77063f57c3028a906690d42fb068845bb Refactor a bit teardown fixtures to avoid two conditionals.
* Don't run explain on slow queries for database adapters that don't support itBlake Smith2012-11-262-6/+14
|
* schema cache already has the columns as a hash, so use thatAaron Patterson2012-11-252-2/+12
|
* speed up fixture loading by querying the schema cache for column namesAaron Patterson2012-11-252-2/+11
|
* Don't call will_change! for datetime nil->"".Alisdair McDiarmid2012-11-251-0/+1
| | | | | | | Setting a nil datetime attribute to a blank string should not cause the attribute to be dirty. Fix #8310
* Move initialize_copy method around to let new method / build alias closerCarlos Antonio da Silva2012-11-241-8/+8
|
* Merge pull request #8291 from senny/8265_build_with_polymorphic_associationRafael Mendonça França2012-11-221-1/+2
|\ | | | | | | | | | | | | prevent mass assignment of polymorphic type when using `build` Conflicts: activerecord/CHANGELOG.md
| * prevent mass assignment of polymorphic type when using `build`Yves Senn2012-11-221-1/+2
| | | | | | | | Closes #8265
* | Remove the #sum method from CollectionAssociationCarlos Antonio da Silva2012-11-211-9/+0
| | | | | | | | | | | | | | Since edd94cee9af1688dd036fc58fd405adb30a5e0da, CollectionProxy delegates all calculation methods - except count - to the scope, which does basically what this method was doing, but since we're delegating from the proxy, the association method was never called.
* | Deprecate Relation#sum with a block.Carlos Antonio da Silva2012-11-212-1/+7
| | | | | | | | | | | | | | To perform a sum calculation over the array of elements, use to_a.sum(&block). Please check the discussion in f9cb645dfcb5cc89f59d2f8b58a019486c828c73 for more context.
* | Revert "Yield only one argument instead of splatting."Carlos Antonio da Silva2012-11-211-14/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit f9cb645dfcb5cc89f59d2f8b58a019486c828c73. Conflicts: activerecord/CHANGELOG.md Revert "Allow blocks for count with ActiveRecord::Relation. Document and test that sum allows blocks" This reverts commit 9cc2bf69ce296b7351dc612a8366193390a305f3. Conflicts: activerecord/lib/active_record/relation/calculations.rb
* | Don't allocate new strings in compiled attribute methodsJon Leighton2012-11-213-21/+38
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This improves memory and performance without having to use symbols which present DoS problems. Thanks @headius and @tenderlove for the suggestion. This was originally committed in f1765019ce9b6292f2264b4601dad5daaffe3a89, and then reverted in d3494903719682abc0948bef290af0d3d7b5a440 due to it causing problems in a real application. This second attempt should solve that. Benchmark --------- require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') class Post < ActiveRecord::Base connection.create_table :posts, force: true do |t| t.string :name end end post = Post.create name: 'omg' Benchmark.ips do |r| r.report('Post.new') { Post.new name: 'omg' } r.report('post.name') { post.name } r.report('post.name=') { post.name = 'omg' } r.report('Post.find(1).name') { Post.find(1).name } end Before ------ Calculating ------------------------------------- Post.new 1419 i/100ms post.name 7538 i/100ms post.name= 3024 i/100ms Post.find(1).name 243 i/100ms ------------------------------------------------- Post.new 20637.6 (±12.7%) i/s - 102168 in 5.039578s post.name 1167897.7 (±18.2%) i/s - 5186144 in 4.983077s post.name= 64305.6 (±9.6%) i/s - 317520 in 4.998720s Post.find(1).name 2678.8 (±10.8%) i/s - 13365 in 5.051265s After ----- Calculating ------------------------------------- Post.new 1431 i/100ms post.name 7790 i/100ms post.name= 3181 i/100ms Post.find(1).name 245 i/100ms ------------------------------------------------- Post.new 21308.8 (±12.2%) i/s - 105894 in 5.053879s post.name 1534103.8 (±2.1%) i/s - 7634200 in 4.979405s post.name= 67441.0 (±7.5%) i/s - 337186 in 5.037871s Post.find(1).name 2681.9 (±10.6%) i/s - 13475 in 5.084511s
* Merge pull request #7716 from steveklabnik/issue_7715Rafael Mendonça França2012-11-211-1/+1
|\ | | | | Coerce strings in create_join_table.
| * Coerce strings in create_join_table.Steve Klabnik2012-11-211-1/+1
| | | | | | | | | | | | | | If you accidentally pass a string and a symbol, this breaks. So we coerce them both to strings. Fixes #7715
* | TypoChris Patuzzo2012-11-211-1/+1
| |
* | Merge pull request #6245 from bogdan/bc_timestampRafael Mendonça França2012-11-212-3/+9
|\ \ | | | | | | Postgresql adapter: fix handling of BC timestamps
| * | Fix postgresql adapter to handle bc timestamps correctlyBogdan Gusiev2012-11-212-3/+9
| | |
* | | Refactoring, testing and documenting pg_connection.distinctSemyon Perepelitsa2012-11-211-13/+10
|/ /
* | Remove return guard and use code convetionsRafael Mendonça França2012-11-201-7/+11
| |
* | Postgresql doesn't accepts limits on text columns.Victor Costan2012-11-201-0/+7
| |
* | Be a bit less conservative with mysql in adapterCarlos Antonio da Silva2012-11-191-1/+1
| |
* | Remove not needed begin..end from AR#saveCarlos Antonio da Silva2012-11-191-5/+3
| |
* | Merge pull request #3023 from Tho85/preserve_sti_typeRafael Mendonça França2012-11-181-1/+12
|\ \ | | | | | | | | | | | | | | | | | | AR::Base.becomes should not change the STI type Conflicts: activerecord/CHANGELOG.md
| * | AR::Base.becomes should not change the STI typeThomas Hollstegge2012-11-171-1/+12
| | | | | | | | | | | | If you want to change the STI type too, use AR::Base.becomes! instead
* | | Add rename_index to change_table.Jarek Radosz2012-11-191-0/+8
| | |
* | | Remove not used require and some useless test commentsCarlos Antonio da Silva2012-11-171-2/+0
| | |
* | | Bump mysql gem version to the newly 2.9.0, fix build.Carlos Antonio da Silva2012-11-171-1/+1
| | |
* | | Remove return guardCarlos Antonio da Silva2012-11-171-2/+1
|/ /
* | Add a deprecation before removing find_table_nameAlexey Muranov2012-11-171-0/+6
| | | | | | The `ActiveRecord::Fixtures::find_table_name` method was removed from "master" almost a year ago, but it was never deprecated. Here it comes back, more dead than alive.
* | Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-11-1722-204/+204
|\ \ | | | | | | | | | | | | Conflicts: actionpack/lib/action_dispatch/routing/redirection.rb
| * | 1.9 Syntax related changesAvnerCohen2012-11-1015-93/+93
| | |
| * | 1.9 hash syntax changesAvnerCohen2012-11-087-111/+111
| | |
* | | arel columns can be used for grouping so that "weird" column names are usableAaron Patterson2012-11-151-0/+4
| | |
* | | stop hardcoding FrontBase adapter conditionalsAaron Patterson2012-11-151-1/+1
| | |
* | | stop passing *args to generate aliasesAaron Patterson2012-11-151-4/+2
| | |
* | | create fewer relation objectsAaron Patterson2012-11-151-3/+6
| | |
* | | Merge pull request #8176 from senny/7551_pluck_with_selectRafael Mendonça França2012-11-131-1/+3
|\ \ \ | | | | | | | | `#pluck` can be used on a relation with `select` clause (#7551)
| * | | `#pluck` can be used on a relation with `select` clause.Yves Senn2012-11-121-1/+3
| | | | | | | | | | | | | | | | Closes #7551
* | | | only clear caches if we are actually connected to the databaseAaron Patterson2012-11-121-2/+4
|/ / /
* | | Remove duplicated ConnectionHandling extension in AR::BaseCarlos Antonio da Silva2012-11-111-1/+0
| | | | | | | | | | | | | | | Thanks @toretore. https://github.com/rails/rails/commit/9e4c41c903e8e58721f2c41776a8c60ddba7a0a9#commitcomment-2134706
* | | Make ActiveRecord::Delegation#method_missing threadsafeJon Leighton2012-11-101-3/+21
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Two threads may be in method_missing at the same time. If so, they might both try to define the same delegator method. Such a situation probably wouldn't result in a particularly spectacular bug as one method would probably just be overridden by an identical method, but it could cause warnings to pop up. (It could be worse if method definition is non-atomic in a particular implementation.) (We will also need this mutex shortly anyway, see #8127.)