aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
...
| * | | Recommend use of rails over bin/railsAlberto Almagro2018-07-062-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As discussed in #33203 rails command already looks for, and runs, bin/rails if it is present. We were mixing recommendations within guides and USAGE guidelines, in some files we recommended using rails, in others bin/rails and in some cases we even had both options mixed together.
* | | | Rails guides are now served over httpsPaul McMahon2018-07-241-1/+1
| |/ / |/| | | | | | | | | | | http links will be redirected to the https version, but still better to just directly link to the https version.
* | | Merge pull request #33415 from orhantoy/feature/docs-consistent-hash-syntaxGeorge Claghorn2018-07-221-8/+8
|\ \ \ | | | | | | | | [ci skip] Use consistent hash syntax in AR docs
| * | | [ci skip] Use consistent hash syntax in AR docsOrhan Toy2018-07-221-8/+8
| | | | | | | | | | | | | | | | | | | | The examples with `.where` uses hash w/ symbol keys so it would be more consistent to also do this with `.new`. Also from my experience the hash w/ symbol keys is more widely used with `where/new/create` etc. in ActiveRecord.
* | | | [ci skip] Tidy up formatting of examplesOrhan Toy2018-07-221-12/+3
|/ / / | | | | | | | | | The consecutive verbatim blocks were being merged making the output look weird.
* | | don't impose primary key order if limit() is definedBrian Christian2018-07-191-1/+1
| | |
* | | Avoid extra scoping in delegating to klass methods in the `scope` blockRyuta Kamizono2018-07-191-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since #29301, delegating to klass methods in the `scope` block would cause extra scoping by the receiver itself. The extra scoping would always override intermediate scoping like `unscoped` and caused the regression #33387. To keep the original scoping behavior, should avoid the extra scoping in the `scope` block. Fixes #33387.
* | | Fix `insert_fixtures_set` to be restored original connection flagsRyuta Kamizono2018-07-192-29/+36
| | | | | | | | | | | | | | | | | | #33363 has two regressions. First one is that `insert_fixtures_set` is failed if flags is an array. Second one is that connection flags are not restored if `set_server_option` is not supported.
* | | Revert "Short circuit the scoping delegation for `relation.all`"Ryuta Kamizono2018-07-191-1/+0
| | | | | | | | | | | | | | | | | | | | | This reverts commit eb807384c81a6e086b17a576755e992e6c4c685e. If the current scope is affected by the `unscoped` block, `all` won't be the same with `spawn`.
* | | Merge pull request #33363 from ahorek/transaction_bugRafael França2018-07-181-5/+23
|\ \ \ | | | | | | | | use set_server_option if possible
| * | | use set_server_option if possiblepavel2018-07-151-5/+23
| | | |
* | | | Implement change() to convert to "2001-01-01" firstSean Prashad2018-07-171-0/+1
|/ / / | | | | | | | | | (cherry picked from commit da34d4766c33a042aeb92778a492fa810ec23001)
* | | Merge pull request #28327 from rngtng/add-seed-dependencyRafael França2018-07-111-1/+1
|\ \ \ | | | | | | | | Add missing env & config dependency to `rake db:seed`
| * | | Add missing load config dependency to `rake db:seed`Tobias Bielohlawek2018-07-101-1/+1
| | | |
* | | | Avoid unneeded expanded column aliases array cachingRyuta Kamizono2018-07-102-8/+5
|/ / /
* | | SQLite: Don't leak internal schema objectsRyuta Kamizono2018-07-092-4/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Related #31201. If creating custom primary key (like a string) in SQLite, it would also create an internal index implicitly which named begin with "sqlite_". It need to be hidden since the internal object names are reserved and prohibited for public use. See https://www.sqlite.org/fileformat2.html#intschema Fixes #33320.
* | | Fix default value for mysql time types with specified precisionNikolay Kondratyev2018-07-041-2/+2
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The TIME, DATETIME, and TIMESTAMP types [have supported](https://mariadb.com/kb/en/library/microseconds-in-mariadb/) a fractional seconds precision from 0 to 6. Default values from time columns with specified precision is read as `current_timestamp(n)` from information schema. rake `db:schema:dump` produces `schema.rb` **without** default values for time columns with the specified precision: t.datetime "last_message_at", precision: 6, null: false rake `db:schema:dump` produces `schema.rb` **with** default values for time columns with the specified precision: t.datetime "last_message_at", precision: 6, default: -> { "current_timestamp(6)" }, null: false
* | Use `construct_join_dependency` in all placesRyuta Kamizono2018-07-033-13/+6
| |
* | Don't extract `readonly_value` each timeRyuta Kamizono2018-07-032-5/+7
| |
* | Don't share seen object cache between different join nodes in eager loadingRyuta Kamizono2018-07-031-2/+2
| | | | | | | | | | | | | | | | | | | | | | Currently, the seen object cache is shared if join nodes have the same target class. But it is a wrong assumption, we can't share the seen object cache between different join nodes (e.g. `:readonly_account` and `:accounts` have the same target class `Account`, but the instances have the different state `readonly`). Fixes #26805. Closes #27737.
* | Merge pull request #33242 from brasic/sqlite-readonlyRyuta Kamizono2018-07-031-1/+3
|\ \ | | | | | | | | | Support readonly option in SQLite3Adapter
| * | Support readonly option in SQLite3AdapterCarl Brasic2018-07-021-1/+3
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Readonly sqlite database files are very useful as a data format for storing configuration/lookup data that is too complicated for YAML files. But since such files would typically be committed to a source control repository, it's important to ensure that they are truly safe from being inadvertently modified. Unfortunately using unix permissions isn't enough, as sqlite will "helpfully" add the write bit to a database file whenever it's written to. sqlite3-ruby has supported a `:readonly` option since version 1.3.2 (see https://github.com/sparklemotion/sqlite3-ruby/commit/c20c9f5dd2990042) This simply passes that option through to the adapter if present in the config hash. I think this is best considered an adapter-specific option since no other supported database has an identical concept.
* | Fix documentation based on feedbackAaron Patterson2018-06-262-3/+7
| |
* | define attribute methods in `init_from_db`Aaron Patterson2018-06-261-0/+2
| | | | | | | | | | Now that `allocate` is removed, we need to define attribute methods in all "init" methods.
* | Merge branch 'master' into homogeneous-allocationAaron Patterson2018-06-263-7/+7
|\ \ | | | | | | | | | | | | | | | | | | | | | * master: Call initialize after allocate Remove `ActiveSupport::Concern` from `ActiveRecord::Aggregations` Add example for no_touching? in active_record/no_touching for api docs [ci skip] Generate a new key for each service test
| * | Call initialize after allocateAaron Patterson2018-06-261-5/+0
| | | | | | | | | | | | | | | | | | | | | | | | If someone calls allocate on the object, they'd better also call an initialization routine too (you can't expect allocate to do any initialization work). Before this commit, AR objects that are instantiated from the database would call `define_attribute_methods` twice.
| * | Remove `ActiveSupport::Concern` from `ActiveRecord::Aggregations`Ryuta Kamizono2018-06-261-2/+0
| | | | | | | | | | | | | | | `include Aggregations` no longer needs to invoke `extend Aggregations::ClassMethods` since 657060b.
| * | Add example for no_touching? in active_record/no_touching for api docs [ci skip]Michael Munroe2018-06-251-0/+7
| | | | | | | | | | | | | | | There was no example code for ActiveRecord::NoTouching#no_touching?. This PR adds an example for the API docs.
* | | Speed up homogeneous AR lists / reduce allocationsAaron Patterson2018-06-254-2/+38
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit speeds up allocating homogeneous lists of AR objects. We can know if the result set contains an STI column before initializing every AR object, so this change pulls the "does this result set contain an STI column?" test up, then uses a specialized instantiation function. This way we only have to check for an STI column once rather than N times. This change also introduces a new initialization function that is meant for use when allocating AR objects that come from the database. Doing this allows us to eliminate one hash allocation per AR instance. Here is a benchmark: ```ruby require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name t.timestamps null: false end end class User < ActiveRecord::Base; end 2000.times do User.create!(name: "Gorby") end Benchmark.ips do |x| x.report("find") do User.limit(2000).to_a end end ``` Results: Before: ``` [aaron@TC activerecord (master)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb Warming up -------------------------------------- find 5.000 i/100ms Calculating ------------------------------------- find 56.192 (± 3.6%) i/s - 285.000 in 5.080940s ``` After: ``` [aaron@TC activerecord (homogeneous-allocation)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb Warming up -------------------------------------- find 7.000 i/100ms Calculating ------------------------------------- find 72.204 (± 2.8%) i/s - 364.000 in 5.044592s ```
* | Lazily add `Aggregations` to `composed_of` modelsAaron Patterson2018-06-252-1/+5
| | | | | | | | | | | | | | | | `composed_of` is a fairly rare method to call on models. This commit adds the `Aggregations` module to models that call `composed_of` so that models that *don't* call `composed_of` don't need to instantiate the `aggregation_cache` hash. This saves one hash allocation per model instance that doesn't use `composed_of`
* | Use `reflection.alias_candidate` in `table_alias_for`Ryuta Kamizono2018-06-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The "join" affix in `table_alias_for` was added 12 years ago at 02d3444 to address poor alias tracking. `AssociationScope` no longer uses the "join" suffixed alias since 0408e21 and had been removed at a1ec8b5. `table_alias_for` is the last place that using the useless legacy suffixed alias, but we can't remove the suffix since some test cases directly refers the alias name by `where` with string literal, so at least removing the suffix would break our test cases. (e.g. https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb#L699-L731).
* | Fix formatting of `primary_key` [ci skip]yuuji.yaginuma2018-06-241-2/+2
| |
* | Remove all `aliases` passing from `JoinDependency`Ryuta Kamizono2018-06-201-2/+2
| | | | | | | | Follow up of 15367a2c674bf19eeefa12ccb64391bdd50d883d.
* | Remove useless `aliases` argument from `JoinDependency#construct`Ryuta Kamizono2018-06-201-5/+5
| | | | | | | | | | Since `aliases` is a part of `JoinDependency` and already cached at 1a723c65bbe91ad969b67416233d20eff6d2a46a.
* | Remove unused `rs` argument from `JoinDependency#construct`Ryuta Kamizono2018-06-201-5/+5
| | | | | | | | It was no longer used since d7ddaa530fd1b94e22d745cbaf2e8a5a34ee9734.
* | Merge pull request #33074 from lsylvester/optimize-pluckRafael França2018-06-191-5/+14
|\ \ | | | | | | Reduce Memory Allocation when using .pluck
| * | Avoid creating temporary arrays in ActiveRecord::Result#cast_values in order ↵Lachlan Sylvester2018-06-191-5/+14
| | | | | | | | | | | | to speed up pluck
* | | Merge pull request #33158 from bogdan/cleanup-habtm-builderRyuta Kamizono2018-06-191-37/+16
|\ \ \ | | | | | | | | Removed useless utility classes inside HABTM association builder
| * | | Removed useless utility classes inside HABTM association builderBogdan Gusiev2018-06-191-37/+16
| |/ /
* / / Ensure to calculate column aliases after all table aliases are constructedRyuta Kamizono2018-06-197-85/+80
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, column aliases which is used for eager loading are calculated before constructing all table aliases in FROM clause. `JoinDependency#join_constraints` constructs table aliases for `joins` first, and then always re-constructs table aliases for eager loading. If both `joins` and eager loading are given a same table association, the re-construction would cause the discrepancy between column aliases and table aliases. To avoid the discrepancy, the column aliases should be calculated after all table aliases are constructed. Fixes #30603.
* | Merge pull request #33147 from nsgc/add-api-document-about-migration-methodsRyuta Kamizono2018-06-181-0/+5
|\ \ | | | | | | Add docs for ActiveRecord::Migration#say, #say_with_time, #suppress_messages [ci skip]
| * | Add docs for ActiveRecord::Migration#say, #say_with_time, #suppress_messages ↵Naoki Nishiguchi2018-06-181-0/+5
| | | | | | | | | | | | [ci skip]
* | | Fix `touch` option to behave consistently with `Persistence#touch` methodRyuta Kamizono2018-06-183-24/+25
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `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.
* | Merge pull request #29373 from untidy-hair/store_accessor_enhanceRyuta Kamizono2018-06-131-5/+21
|\ \ | | | | | | | | | Allow prefix/suffix options for store accessors
| * | Update prefix and allow suffix options for store accessorsYukio Mizuta2018-06-121-5/+21
| | |
* | | Add ability to configure cache notifications infoEileen Uchitelle2018-06-121-6/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This may seem like an unnecessary refactoring but some apps want / need to configure the information passed to the query cache logger. In order to do that we can add a method here that can be easily overridden by the app itself, rather than hacking the query cache logger to include that information. To override apps can call ``` def cache_notifications_info super.merge(connected_host: "hostname") end ``` This will take what's already in the query cache logger and add `@something="yea"` to the object. At GitHub we use this to log the number of queries that are cached, the connection host and the connection url.
* | | Don't use `target=`Rafael Mendonça França2018-06-111-1/+1
| | | | | | | | | | | | | | | It mark the association as loaded and this can cause the object to be in an stale state.
* | | Use `-=` to change the update the collection on the associationRafael Mendonça França2018-06-111-2/+1
| | | | | | | | | | | | | | | This also mark the association as loaded given we changed it in memory and avoid the next access to the reader to make a query to the databse.
* | | Merge pull request #29939 from arthurchui/activerecord-delete-associations-loopRafael Mendonça França2018-06-111-1/+2
|\ \ \ | | | | | | | | | | | | Use hash lookup for deleting existing associations from `target`
| * | | Use hash lookup for deleting existing associations from `target`Arthur Chui2017-07-251-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `Array#delete` searches for all occurrences in the `target` array. When performing `dependent: :destroy` in active_record/associations/collection_association, the loop becomes O(N^2). It is particularly slow when destroying large amount of associations (e.g. 10K records). Either `Hash` or `Set` can optimize the loop to O(N). `Hash` is slightly faster in this simple usage. ```ruby class Dummy; end num = 10_000 test1a = num.times.map { Dummy.new }; nil test1b = test1a.dup test2a = num.times.map { Dummy.new }; nil test2b = test2a.dup Benchmark.ips do |x| x.config(stats: :bootstrap, confidence: 95) x.report("hash") do hash = test1a.group_by { |r| r } test1b.select! { |r| !hash[r] } end x.report("array") do test2a.each { |r| test2b.delete(r) } end x.compare! end ``` ``` Calculating ------------------------------------- hash 11.000 i/100ms array 1.000 i/100ms ------------------------------------------------- hash 114.586 (±16.6%) i/s - 561.000 array 1.710k (±10.3%) i/s - 8.377k Comparison: array: 1710.4 i/s hash: 114.6 i/s - 14.93x slower ```