aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
Commit message (Collapse)AuthorAgeFilesLines
* Clear the transaction state when AR object is dupedYuriy Ustushenko2018-01-251-2/+4
|
* Avoid passing unnecessary arguments to relationDaniel Colson2018-01-241-1/+1
| | | | | | | | | | | | Most of the time the table and predicate_builder passed to Relation.new are exactly the arel_table and predicate builder of the given klass. This uses klass.arel_table and klass.predicate_builder as the defaults, so we don't have to pass them in most cases. This does change the signaure of both Relation and AssocationRelation. Are we ok with that?
* Log call site for all queriesOlivier Lacan2017-12-131-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This new ActiveRecord configuration option allows you to easily pinpoint what line of application code is triggering SQL queries in the development log by appending below each SQL statement log the line of Ruby code that triggered it. It’s useful with N+1 issues, and to locate stray queries. By default this new option ignores Rails and Ruby code in order to surface only callers from your application Ruby code or your gems. It is enabled on newly generated Rails 5.2 applications and can be enabled on existing Rails applications: ```ruby Rails.application.configure do # ... config.active_record.verbose_query_logs = true end ``` The `rails app:upgrade` task will also add it to `config/development.rb`. This feature purposely avoids coupling with ActiveSupport::BacktraceCleaner since ActiveRecord can be used without ActiveRecord. This decision can be reverted in the future to allow more configurable backtraces (the exclusion of gem callers for example).
* Prevent extra `spawn` to make `klass.all` faster (#29009)Ryuta Kamizono2017-11-201-1/+2
| | | | | | | | | | | | | | | | | | | | These extra `spawn` are called via `klass.all` and `klass.all` is called everywhere in the internal. Avoiding the extra `spawn` makes` klass.all` 30% faster for STI classes. https://gist.github.com/kamipo/684d03817a8115848cec8e8b079560b7 ``` Warming up -------------------------------------- fast relation 4.410k i/100ms slow relation 3.334k i/100ms Calculating ------------------------------------- fast relation 47.373k (± 5.2%) i/s - 238.140k in 5.041836s slow relation 35.757k (±15.9%) i/s - 176.702k in 5.104625s Comparison: fast relation: 47373.2 i/s slow relation: 35756.7 i/s - 1.32x slower ```
* remove :enabled optionBen Toews2017-11-091-3/+2
|
* add config to check arguments to unsafe AR methodsBen Toews2017-11-091-0/+9
|
* Remove deprecated configuration `.error_on_ignored_order_or_limit`Rafael Mendonça França2017-10-231-20/+0
|
* Update links to use https instead of http [ci skip]Yoshiyuki Hirano2017-08-221-1/+1
|
* Fix outdated comment for `Core::ClassMethods#===` [ci skip] (#30146)Ryuta Kamizono2017-08-121-2/+2
| | | | | | | | | | | | | This comment was added at 97849de, but `AssociationProxy` and `test_triple_equality` was removed at 1644663. Currently the `===` is used for `test_decorated_polymorphic_where` that added at #11945. So I updated "association proxies" to "decorated models". And also, currently `Core::ClassMethods` appears in the doc. http://api.rubyonrails.org/classes/ActiveRecord/Core/ClassMethods.html But it looks like that the methods in the module is not public API. So I also added `# :nodoc:` to the module.
* Use `Concurrent::Map` than `Mutex` and `Mutex_m` for statement cachesRyuta Kamizono2017-08-041-4/+3
| | | | | Statement caches are used as a concurrent map. It will more clarify to using `Concurrent::Map`.
* Passing `klass` to `StatementCache.new`Ryuta Kamizono2017-08-041-2/+2
| | | | | | Actually `StatementCache#execute` is always passed the same klass that the owner klass of the connection when the statement cache is created. So passing `klass` to `StatementCache.new` will make more DRY.
* Merge pull request #29842 from kamipo/fix_find_by_with_rangeMatthew Draper2017-08-021-3/+2
|\ | | | | Fix `find_by` with range conditions
| * Fix `find_by` with range conditionsRyuta Kamizono2017-07-201-3/+2
| | | | | | | | | | `StatementCache` doesn't support range conditions. So we need to through the args to `FinderMethods#find_by` if range value is passed.
* | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|/
* Remove useless `arel_engine`Ryuta Kamizono2017-07-171-10/+0
| | | | | | | | | | | `arel_engine` is only used in `raise_record_not_found_exception!` to use `engine.connection` (and `connection.visitor`) in `arel.where_sql`. https://github.com/rails/arel/blob/v8.0.0/lib/arel/select_manager.rb#L183 But `klass.connection` will work as expected even if not using `arel_engine` (described by `test_connection`). So `arel_engine` is no longer needed.
* Remove unused requiresRyuta Kamizono2017-07-161-2/+0
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Use mattr_accessor default: option throughout the projectGenadi Samokovarov2017-06-031-13/+6
|
* Remove unused `@txn` variableEugene Kenny2017-03-151-1/+0
| | | | | | This was added in c24c885209ac2334dc6f798c394a821ee270bec6, removed in b89ffe7f0047eb614e42232a21201b317b880755, and then (unintentionally?) reintroduced in 2d7ae1b08ee2a10b12cbfeef3a6cc6da55b57df6.
* `type_condition` should be overwritten by `create_with_value` in ↵Ryuta Kamizono2017-01-151-1/+1
| | | | | | | | | | `scope_for_create` `type_condition` should be overwritten by `create_with_value`. So `type` in `create_with_value` should be a string because `where_values_hash` keys are converted to string. Fixes #27600.
* `self.` is not needed when calling its own instance methodAkira Matsuda2017-01-051-2/+2
| | | | Actually, private methods cannot be called with `self.`, so it's not just redundant, it's a bad habit in Ruby
* Fix `find_by` and `where` consistencyRyuta Kamizono2017-01-041-3/+3
| | | | | | | | | | 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.
* Raise ArgumentError when a instance of ActiveRecord::Base is passed toRafael Mendonça França2017-01-031-9/+5
| | | | find and exists?
* Mark :GeneratedAssociationMethods also as private_constantPrathamesh Sonpatki2016-12-311-0/+2
| | | | | | | | | | | | - After https://github.com/rails/rails/commit/64e5b897ac944a05a33275e3828a3d4047a6b457, only :GeneratedAssociationMethods was remaining to be marked as private constant, so marked it as well. - Before: >> User.constants(false) => [:GeneratedAssociationMethods] - After: >> User.constants(false) => []
* No need to nodoc private methodsAkira Matsuda2016-12-241-3/+3
|
* Translate numeric value out of range to the specific exceptionRyuta Kamizono2016-12-061-2/+2
| | | | Raise `ActiveRecord::RangeError` when values that executed are out of range.
* Don't assign default attributes until after loading schemaSean Griffin2016-11-031-1/+1
| | | | | | | | If the call to `.define_attribute_methods` actually ends up loading the schema (*very* hard to do, as it requires the object being created without `allocate` having been called, but it can be done by manually calling `initialize` from inside `marshal_load` if you're crazy), the value of `_default_attributes` will change from that call.
* allow ActiveRecord::Core#slice to use array argCohen Carlisle2016-10-311-1/+1
|
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-1/+1
|
* Use xor to avoid allocations in `AR::Core#hash`Sean Griffin2016-09-261-1/+1
| | | | | | | | This is not as good a solution as actually hashing both values, but Ruby doesn't expose that capability other than allocating the array. Unless we were to do something silly like have a thread local array that is re-used, I don't see any other way to do this without allocation. This solution may not be perfect, but it should reasonably avoid collisions to the extent that we need.
* fixes remaining RuboCop issues [Vipul A M, Xavier Noria]Xavier Noria2016-09-011-1/+1
|
* Include user defined attributes in inspectSean Griffin2016-08-311-1/+1
| | | | The fact that this only includes column names is an oversight.
* Ensure that inverse associations are set before running callbacksSean Griffin2016-08-311-0/+2
| | | | | | | | | | | | | | | | | If a parent association was accessed in an `after_find` or `after_initialize` callback, it would always end up loading the association, and then immediately overwriting the association we just loaded. If this occurred in a way that the parent's `current_scope` was set to eager load the child, this would result in an infinite loop and eventually overflow the stack. For records that are created with `.new`, we have a mechanism to perform an action before the callbacks are run. I've introduced the same code path for records created with `instantiate`, and updated all code which sets inverse instances on newly loaded associations to use this block instead. Fixes #26320.
* Remove "Under Ruby 1.9" [ci skip]Ryuta Kamizono2016-08-281-9/+9
| | | | Rails dropped Ruby 1.9 support, but this comment still true.
* revises more Lint/EndAlignment offensesXavier Noria2016-08-081-3/+3
|
* code gardening: removes redundant selfsXavier Noria2016-08-081-2/+2
| | | | | | | | | A few have been left for aesthetic reasons, but have made a pass and removed most of them. Note that if the method `foo` returns an array, `foo << 1` is a regular push, nothing to do with assignments, so no self required.
* revises most Lint/EndAlignment offensesXavier Noria2016-08-071-5/+6
| | | | Some case expressions remain, need to think about those ones.
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-43/+43
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-13/+13
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* deprecates the error_on_ignored_order_or_limit instance readerXavier Noria2016-07-181-0/+4
| | | | | | | | | Albeit the previous existence of this method is not seen in the patch itself, the declaration mattr_accessor :error_on_ignored_order_or_limit, instance_writer: false was present before. It was removed recently in 210012f.
* Remove mattr_accessor for deprecated config `error_on_ignored_order_or_limit`.Prathamesh Sonpatki2016-07-181-1/+8
| | | | | - It adds lot of warnings while running the tests. After https://github.com/rails/rails/commit/451437c6f57e66cc7586ec966e530493927098c7 it is not needed. - Added reader method with deprecation warning for backward compatibility.
* adds support for limits in batch processingXavier Noria2016-07-131-3/+12
|
* Make Active Record emit significantly smaller YAMLSean Griffin2016-05-311-5/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reduces the size of a YAML encoded Active Record object by ~80% depending on the number of columns. There were a number of wasteful things that occurred when we encoded the objects before that have resulted in numerous wins - We were emitting the result of `attributes_before_type_cast` as a hack to work around some laziness issues - The name of an attribute was emitted multiple times, since the attribute objects were in a hash keyed by the name. We now store them in an array instead, and reconstruct the hash using the name - The types were included for every attribute. This would use backrefs if multiple objects were encoded, but really we don't need to include it at all unless it differs from the type at the class level. (The only time that will occur is if the field is the result of a custom select clause) - `original_attribute:` was included over and over and over again since the ivar is almost always `nil`. We've added a custom implementation of `encode_with` on the attribute objects to ensure we don't write the key when the field is `nil`. This isn't without a cost though. Since we're no longer including the types, an object can find itself in an invalid state if the type changes on the class after serialization. This is the same as 4.1 and earlier, but I think it's worth noting. I was worried that I'd introduce some new state bugs as a result of doing this, so I've added an additional test that asserts mutation not being lost as the result of YAML round tripping. Fixes #25145
* `ActiveRecord::Base#hash` should differ between classesSean Griffin2016-05-311-1/+1
| | | | | | | | Prior to this change, we would get collisions if Active Record objects of different classes with the same ID were used as keys of the same hash. It bothers me slightly that we have to allocate inside of this method, but Ruby doesn't provide any way to hash multiple values without allocation
* s/specification_name/connection_specification_nameArthur Neves2016-05-061-1/+1
|
* s/specification_id/specification_nameArthur Neves2016-05-051-1/+1
|
* Refactor connection handlerArthur Neves2016-05-051-1/+1
| | | | | | | | | | | | | | | ConnectionHandler will not have any knowlodge of AR models now, it will only know about the specs. Like that we can decouple the two, and allow the same model to use more than one connection. Historically, folks used to create abstract AR classes on the fly in order to have multiple connections for the same model, and override the connection methods. With this, now we can override the `specificiation_id` method in the model, to return a key, that will be used to find the connection_pool from the handler.
* Pass over all Rails 5 warnings, to make sure:Vipul A M2016-04-121-1/+1
| | | | | | | | | | - we are ending sentences properly - fixing of space issues - fixed continuity issues in some sentences. Reverts https://github.com/rails/rails/commit/8fc97d198ef31c1d7a4b9b849b96fc08a667fb02 . This change reverts making sure we add '.' at end of deprecation sentences. This is to keep sentences within Rails itself consistent and with a '.' at the end.
* Prepared statements shouldn't share a cache with unprepared statementsSean Griffin2016-03-311-3/+4
| | | | | | | | | When prepared statements are enabled, the statement cache caches the SQL directly, including the bind parameters. If a similar query is run later with prepared statements disabled, we need to use a separate cache instead of trying to share the same one. Fixes #24351