aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | | | Merge pull request #22170 from ↵Matthew Draper2016-03-023-14/+47
|\ \ \ \ \ \ | |_|/ / / / |/| | | | | | | | | | | | | | | | | samphilipd/sam/properly_deallocate_prepared_statements_outside_of_transaction Correctly deallocate prepared statements if we fail inside a transaction
| * | | | | Correctly deallocate prepared statements if we fail inside a transactionSam Davies2015-11-053-14/+47
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Addresses issue #12330 Overview ======== Cached postgres prepared statements become invalidated if the schema changes in a way that it affects the returned result. Examples: - adding or removing a column then doing a 'SELECT *' - removing the foo column then doing a 'SELECT bar.foo' In normal operation this isn't a problem, we can rescue the error, deallocate the prepared statement and re-issue the command. However in PostgreSQL transactions, once any command fails, the transaction becomes 'poisoned' and any subsequent commands will raise InFailedSQLTransaction. This includes DEALLOCATE statements, so the default deallocation strategy instead of removing the cached prepared statement instead raises InFailedSQLTransaction. Why this is bad =============== 1. InFailedSQLTransaction is a fairly cryptic error and doesn't communicate any useful information about what has actually gone wrong. 2. In the naive implementation the prepared statement never gets deallocated - it stays alive for the length of the session taking up memory on the postgres server. 3. It is unsafe to retry the transaction because the bad prepared statement is still in the cache and we would see the exact same failure repeated. Solution ======== If we are outside a transaction we can continue to handle these failures gracefully in the usual way. Inside a transaction instead of issuing a DEALLOCATE command that will certainly fail, we now raise ActiveRecord::PreparedStatementCacheExpired. This can be handled further up the stack, notably inside TransactionManager#within_new_transaction. Here we can make sure to first rollback the transaction, then safely issue DEALLOCATE statements to invalidate the rest of the cached prepared statements. This also allows the user (or some gem) the opportunity to catch this error and voluntarily retry the transaction if a schema change causes the prepared statement cache to become invalidated. Because the outdated statement has been deallocated, we can expect the transaction to succeed on the second try.
* | | | | | Respect through association scopes when used with polymorphicSean Griffin2016-02-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When the `source_type` option is passed to a has_many through, the resulting `Reflection` will be an instance of `PolymorphicReflection` and not `ThroughReflection`, meaning that it will ignore the scopes that it needs to apply from the through reflections. This adds an additional delegation call to remedy this. I've been finding the reflection code completely impenetrable lately, it could use some major love. Fixes #22726
* | | | | | Merge pull request #23948 from ctm/remove_pathological_regexpRafael França2016-02-291-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | Removes potentially quadratic Regexp from ActiveRecord::LogSubscriber…
| * | | | | | Removes potentially quadratic Regexp from ActiveRecord::LogSubscriber#sql_colorClifford T. Matthews2016-02-231-1/+1
| | | | | | |
* | | | | | | Merge pull request #18766 from yasyf/issue_17864Sean Griffin2016-02-291-8/+15
|\ \ \ \ \ \ \ | |_|_|/ / / / |/| | | | | | | | | | | | | | | | | | | | Honour joining model order in `has_many :through` associations when eager loading
| * | | | | | Honour the order of the joining model in a `has_many :through`Yasyf Mohamedali2015-03-022-9/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | association when eager loading. Previously, eager loading a `has_many :through` association with no defined order would return the records in the natural order of the database. Now, these records will be returned in the order that the joining record is returned, in case there is a defined order there.
* | | | | | | Merge pull request #23846 from ↵Rafael França2016-02-252-11/+17
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | y-yagi/correctly_check_ApplicationRecord_in_mountable_engine correctly check `ApplicationRecord` is exist in moutable engine
| * | | | | | | correctly check `ApplicationRecord` is exist in moutable engineyuuji.yaginuma2016-02-252-11/+17
| | |/ / / / / | |/| | | | | | | | | | | | | | | | | | | Follow up to 1813b29fc7632959800252f36e4b2e6ed4ac7266
* | | | | | | Fix uniqueness validation with an after_create hook.Joe Rafaniello2016-02-251-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | record.id_was is nil in after_create/after_save, so we should use id in these cases. While this logic feels incomplete, the existing update_record uses the same logic: https://github.com/rails/rails/blob/2fda4e0874a97a76107ab9e88305169f2c625933/activerecord/lib/active_record/relation.rb#L83 This logic was originally added for a similar problem: updates not working with after_create hook. See: 482f8c15b1d699c95bfbc3d836f674a09c0d9031 Followup to #23581 Fixes #23844
* | | | | | | Ensure suppressor runs before validationseileencodes2016-02-241-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I ran into an issue where validations on a suppressed record were causing validation errors to be thrown on a record that was never going to be saved. There isn't a reason to run the validations on a record that doesn't matter. This change moves the suppressor up the chain to be run on the `save` or `save!` in the validations rather than in persistence. The issue with running it when we hit persistence is that the validations are run first, then we hit persistance, and then we hit the suppressor. The suppressor comes first. The change to the test was required since I added the `validates_presence_of` validations. Adding this alone was enough to demonstrate the issue. I added a new test to demonstrate the new behavior is explict.
* | | | | | | Prep release for Rails 5 beta3eileencodes2016-02-241-1/+1
| | | | | | |
* | | | | | | Merge pull request #23840 from yui-knk/improve_the_readability_of_ar_docJon Moss2016-02-231-6/+14
|\ \ \ \ \ \ \ | |/ / / / / / |/| | | | | | [ci skip] Improve the readability of documents of nested_attributes
| * | | | | | [ci skip] Improve the readability of documents of nested_attributesyui-knk2016-02-241-6/+14
| | | | | | |
* | | | | | | Revert changes to validations from PR #18612eileencodes2016-02-234-15/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In order to fix issue #17621 we added a check to validations that determined if a record should be validated. Based on the existing tests and behavior we wanted we determined the best way to do that was by checking if `!record.peristed? || record.changed? || record.marked_for_destruction?` This change didn't make it into a release until now. When #23790 was opened we realized that `valid?` and `invalid?` were broken and did not work on persisted records because of the `!record.persisted?`. While there is still a bug that #17621 brought up, this change was too drastic and should not be a RC blocker. I will work on fixing this so that we don't break `valid?` but also aren't validating parent records through child records if that parent record is validate false. This change removes the code changes to validate and the corresponding tests. It adds tests for two of the bugs found since Rails 5 beta2 release. Fixes #17621
* | | | | | | Merge pull request #23628 from maclover7/fix-23625Sean Griffin2016-02-231-1/+1
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Fix issue #23625
| * | | | | | | Fix issue #23625Jon Moss2016-02-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This resolves a bug where if the primary key used is not `id` (ex: `uuid`), and has a `validates_uniqueness_of` in the model, a uniqueness error would be raised. This is a partial revert of commit `119b9181ece399c67213543fb5227b82688b536f`, which introduced this behavior.
* | | | | | | | reestablish previous connection after creating all databasesAaron Patterson2016-02-231-0/+4
| |/ / / / / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | creating all databases mutates the connection pool. This patch restores the connection pool to the connection spec established before creating all databases. Fixes #23279
* | | | | | | Merge pull request #23419 from ↵Matthew Draper2016-02-231-1/+1
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | prathamesh-sonpatki/fix-showing-of-deprecation-warning-for-legacy-migrations Correctly show deprecation warning for incompatible migrations
| * | | | | | | Correctly show deprecation warning for incompatible migrationsPrathamesh Sonpatki2016-02-121-1/+1
| | | | | | | |
* | | | | | | | Add notes for future selvesJon Moss2016-02-221-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Once RubyGems 2.5.0 is required, then the duplicated files can be removed, and symlinks can be used instead. [ci skip]
* | | | | | | | Generate ApplicationRecord if it does not already existJon Moss2016-02-222-0/+11
| | | | | | | |
* | | | | | | | Merge pull request #22748 from Azzurrio/masterMatthew Draper2016-02-221-1/+1
|\ \ \ \ \ \ \ \ | |_|_|_|_|_|_|/ |/| | | | | | | Fix NoMethodError preparable for Arel::Visitors::PostgreSQL
| * | | | | | | Fix NoMethodError preparable for Arel::Visitors in case prepared statements ↵Azzurrio2016-02-221-1/+1
| |/ / / / / / | | | | | | | | | | | | | | | | | | | | | is falsy
* | | | | | | Merge pull request #23769 from kamipo/remove_alias_exec_without_stmt_exec_querySantiago Pastorino2016-02-211-2/+0
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Remove `alias exec_without_stmt exec_query`
| * | | | | | | Remove `alias exec_without_stmt exec_query`Ryuta Kamizono2016-02-191-2/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This alias was for compatibility with legacy mysql adapter. But the return value of both methods is already inconsistent. `exec_query` returns `ActiveRecord::Result` instance. But `exec_without_stmt` returns `[result_set, affected_rows]` https://github.com/rails/rails/blob/v4.2.5.1/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb#L335-L364 Legacy mysql adapter was already removed in Rails 5.0. I think we can remove this inconsistent alias.
* | | | | | | | Reduce `attribute.to_s`Ryuta Kamizono2016-02-221-6/+5
| | | | | | | |
* | | | | | | | Merge pull request #23794 from matthewd/mutation-safe-to_aMatthew Draper2016-02-2110-24/+32
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | Mutating the result of Relation#to_a should not affect the relation
| * | | | | | | | Mutating the result of Relation#to_a should not affect the relationMatthew Draper2016-02-2110-24/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Clarifying this separation and enforcing relation immutability is the culmination of the previous efforts to remove the mutator method delegations.
* | | | | | | | | Always validate record if validating a virtual attributeeileencodes2016-02-203-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #23645 When you're using an `attr_accessor` for a record instead of an attribute in the database there's no way for the record to know if it has `changed?` unless you tell it `attribute_will_change!("attribute")`. The change made in 27aa4dd updated validations to check if a record was `changed?` or `marked_for_destruction?` or not `persisted?`. It did not take into account virtual attributes that do not affect the model's dirty status. The only way to fix this is to always validate the record if the attribute does not belong to the set of attributes the record expects (in `record.attributes`) because virtual attributes will not be in that hash. I think we should consider deprecating this particular behavior in the future and requiring that the user mark the record dirty by noting that the virtual attribute will change. Unfortunately this isn't easy because we have no way of knowing that you did the "right thing" in your application by marking it dirty and will get the deprecation warning even if you are doing the correct thing. For now this restores expected behavior when using a virtual attribute by always validating the record, as well as adds tests for this case. I was going to add the `!record.attributes.include?(attribute)` to the `should_validate?` method but `uniqueness` cannot validate a virtual attribute with nothing to hold on to the attribute. Because of this `should_validate?` was about to become a very messy method so I decided to split them up so we can handle it specifically for each case.
* | | | | | | | | eliminate warnings about multiple primary keys on habtm join tablesAaron Patterson2016-02-192-1/+4
|/ / / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | habtm join tables commonly have two id columns and it's OK to make those two id columns a primary key. This commit eliminates the warnings for join tables that have this setup. ManageIQ/manageiq#6713
* | | | | | | | Add methods to array delegation from `Relation`Kevin Dougherty2016-02-191-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Delegation of some `Array` methods was removed in commit 9d79334. That change did add explicit delegation of a few methods that `Array` has but which aren't on `Enumerable`. However, a few non-mutation methods were omitted. This adds `Array` delegation of `#in_groups`, `#in_groups_of`, `#shuffle` and `#split`. This allows things like `MyThing.all.in_groups_of(3) { ... }` to continue working as they did before commit 9d79334.
* | | | | | | | Merge pull request #23525 from kamipo/remove_unused_requireSean Griffin2016-02-181-1/+0
|\ \ \ \ \ \ \ \ | |/ / / / / / / |/| | | | | | | Remove unused require
| * | | | | | | Remove unused requireRyuta Kamizono2016-02-061-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `require 'active_support/core_ext/benchmark'` was added by 4ecdf24. But currently unused anymore.
* | | | | | | | Merge pull request #23736 from ↵Sean Griffin2016-02-172-7/+4
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | kamipo/remove_needless_case_insensitive_comparison_in_mysql2_adapter Remove needless `case_insensitive_comparison` in mysql2 adapter
| * | | | | | | | Remove needless `case_insensitive_comparison` in mysql2 adapterRyuta Kamizono2016-02-172-7/+4
| | |_|/ / / / / | |/| | | | | | | | | | | | | | | | | | | | | | Simply it is sufficient to override `can_perform_case_insensitive_comparison_for?`.
* | | | | | | | Merge pull request #22365 from ↵Sean Griffin2016-02-173-4/+70
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | phuibonhoa/phuibonhoa/polymorphic_where_multiple_types Fixed `where` for polymorphic associations when passed an array containing different types.
| * | | | | | | | Fixed `where` for polymorphic associations when passed an array containing ↵Philippe Huibonhoa2016-02-163-4/+70
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | different types. When passing in an array of different types of objects to `where`, it would only take into account the class of the first object in the array. PriceEstimate.where(estimate_of: [Treasure.find(1), Car.find(2)]) # => SELECT "price_estimates".* FROM "price_estimates" WHERE ("price_estimates"."estimate_of_type" = 'Treasure' AND "price_estimates"."estimate_of_id" IN (1, 2)) This is fixed to properly look for any records matching both type and id: PriceEstimate.where(estimate_of: [Treasure.find(1), Car.find(2)]) # => SELECT "price_estimates".* FROM "price_estimates" WHERE (("price_estimates"."estimate_of_type" = 'Treasure' AND "price_estimates"."estimate_of_id" = 1) OR ("price_estimates"."estimate_of_type" = 'Car' AND "price_estimates"."estimate_of_id" = 2))
* | | | | | | | | Merge pull request #23570 from rthbound/addresses-23568Matthew Draper2016-02-181-8/+0
|\ \ \ \ \ \ \ \ \ | |_|/ / / / / / / |/| | | | | | | | Addresses #23568, Incorrect error message with accepts_nested_attributes_for / has_many & has_one
| * | | | | | | | Addresses #23568Ryan T. Hosford2016-02-091-8/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Corrects an incorrect exception message when using accepts_nested_attributes_for - Removes rescue/reraise behavior introduced in #19077 - Adds has_many & has_one, nested_attributes test case specifying the message that should be conveyed with an exception raised because one of the nested attributes provided is unknown
* | | | | | | | | Show proper error message when a non-relation object is passed to ↵Prathamesh Sonpatki2016-02-171-0/+4
| |/ / / / / / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | AR::Relation#or - Previously it used to show error message <"undefined method `limit_value' for {:title=>\"Rails\"}:Hash"> - Now it shows following error message. >> Post.where.not(name: 'DHH').or(name: 'Tenderlove') ArgumentError: You have passed Hash object to #or. Pass an ActiveRecord::Relation object instead. - Fixes #23714.
* | | | | | | | Let t.foreign_key use the same `to_table` twiceGeorge Millo2016-02-151-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously if you used `t.foreign_key` twice within the same `create_table` block using the same `to_table`, all statements except the final one would fail silently. For example, the following code: def change create_table :flights do |t| t.integer :from_id, index: true, null: false t.integer :to_id, index: true, null: false t.foreign_key :airports, column: :from_id t.foreign_key :airports, column: :to_id end end Would only create one foreign key, on the column `from_id`. This commit allows multiple foreign keys to the same table to be created within one `create_table` block.
* | | | | | | | Remove the method already overridden by private methodMehmet Emin İNAÇ2016-02-141-13/+0
| | | | | | | |
* | | | | | | | Merge pull request #23377 from bogdan/last-with-sqlEileen M. Uchitelle2016-02-132-9/+21
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | Fix AR::Relation#last bugs instroduced in 7705fc
| * | | | | | | | Make ActiveRecord::Relation#last to reverse SQL orderBogdan Gusiev2016-02-132-9/+21
| | |_|/ / / / / | |/| | | | | | | | | | | | | | | | | | | | | | instead of loading the relation into memory
* / | | | | | | Fix grammar `a` to `an` [ci skip]Ryuta Kamizono2016-02-134-4/+4
|/ / / / / / /
* | | | | | | Merge pull request #18109 from k0kubun/unscoped-joinsSean Griffin2016-02-111-6/+12
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Allow `joins` to be unscoped Fixes #13775
| * | | | | | | Allow `joins` to be unscopedTakashi Kokubun2016-01-311-6/+12
| | | | | | | |
* | | | | | | | Merge pull request #23581 from diego-silva/uniqueness-validator-pk-fixSean Griffin2016-02-111-1/+1
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | UniquenessValidator exclude itself when PK changed
| * | | | | | | | UniquenessValidator exclude itself when PK changedDiego Silva2016-02-091-1/+1
| | |/ / / / / / | |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When changing the PK for a record which has a uniqueness validation on some other attribute, Active Record should exclude itself from the validation based on the PK value stored on the DB (id_was) instead of its new value (id).