aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
* Deprecate mismatched collation comparison for uniquness validatorRyuta Kamizono2019-03-043-2/+16
| | | | | | | | | | | | | | | | | | | | | In MySQL, the default collation is case insensitive. Since the uniqueness validator enforces case sensitive comparison by default, it frequently causes mismatched collation issues (performance, weird behavior, etc) to MySQL users. https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/ https://github.com/rails/rails/issues/1399 https://github.com/rails/rails/pull/13465 https://github.com/gitlabhq/gitlabhq/commit/c1dddf8c7d947691729f6d64a8ea768b5c915855 https://github.com/huginn/huginn/pull/1330#discussion_r55152573 I'd like to deprecate the implicit default enforcing since I frequently experienced the problems in code reviews. Note that this change has no effect to sqlite3, postgresql, and oracle-enhanced adapters which are implemented as case sensitive by default, only affect to mysql2 adapter (I can take a work if sqlserver adapter will support Rails 6.0).
* Fixed reselect throwing NoMethodError on ActiveRecord.Abhay Nikam2019-03-031-1/+2
|
* Merge pull request #35441 from kamipo/allow_from_with_index_hintRyuta Kamizono2019-03-011-2/+5
|\ | | | | Relax table name detection in `from` to allow any extension like INDEX hint
| * Relax table name detection in `from` to allow any extension like INDEX hintRyuta Kamizono2019-03-011-2/+5
| | | | | | | | | | | | | | | | | | | | #35360 allows table name qualified if `from` has original table name. But that is still too strict. We have a valid use case that `from` with INDEX hint (e.g. `from("comments USE INDEX (PRIMARY)")`). So I've relaxed the table name detection in `from` to allow any extension like INDEX hint. Fixes #35359.
* | Merge pull request #35431 from kamipo/enable_sql_cache_on_findRyuta Kamizono2019-03-011-1/+1
|\ \ | |/ |/| Enable SQL statement cache for `find` on base class as with `find_by`
| * Enable SQL statement cache for `find` on base class as with `find_by`Ryuta Kamizono2019-03-011-1/+1
| | | | | | | | Related and follows d333d85254d27cd572e6ecce8ee850c107a4f340.
* | Merge pull request #33611 from willianveiga/feature/reselect-methodAndrew White2019-03-011-0/+21
|\ \ | |/ |/| Add reselect method
| * Avoid creating an extra relation instanceWillian Gustavo Veiga2018-10-241-2/+9
| |
| * Merge branch 'master' into feature/reselect-methodWillian Gustavo Veiga2018-10-224-19/+15
| |\
| * \ Merge branch 'master' into feature/reselect-methodWillian Gustavo Veiga2018-10-1714-118/+82
| |\ \
| * \ \ Merge branch 'master' into feature/reselect-methodWillian Gustavo Veiga2018-10-1129-692/+944
| |\ \ \
| * \ \ \ Merge branch 'master' into feature/reselect-methodWillian Gustavo Veiga2018-10-02105-714/+1276
| |\ \ \ \
| * | | | | Add reselect methodWillian Gustavo Veiga2018-08-131-0/+14
| | | | | |
* | | | | | [ci skip] The `find` method coerces the given arguments to integer if the ↵Mehmet Emin INAC2019-02-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | `primary key` is integer
* | | | | | [ci skip] Fix the documentation of ActiveRecord::FinderMethods#findMehmet Emin INAC2019-02-281-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It's mentioned everywhere as `ActiveRecord::RecordNotFound` so to be coherent with the rest of the documentation I've applied it here. Also doc was saying if the parameter is integer it coerces it which is other way around.
* | | | | | Refactor `generated_relation_methods` to remove duplicated code on ↵Ryuta Kamizono2019-02-281-31/+20
| | | | | | | | | | | | | | | | | | | | | | | | `ClassSpecificRelation`
* | | | | | Allow returning nil for `default_scope`Ryuta Kamizono2019-02-281-5/+4
| | | | | |
* | | | | | Fix preload with nested associationsRafael Mendonça França2019-02-261-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When the middle association doesn't have any records and the inner association is not an empty scope the owner will be `nil` so we can't try to reset the inverse association.
* | | | | | Add negative scopes for all enum values (#35381)David Heinemeier Hansson2019-02-261-0/+6
| | | | | | | | | | | | | | | | | | Add negative scopes for all enum values
* | | | | | Don't cache `find_by` statements on STI subclassesRyuta Kamizono2019-02-271-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Caching `find_by` statements on STI subclasses is unsafe, since `type IN (?,?,?,?)` part is dynamic, and we don't have SQL statements cache invalidation when a STI subclass is created or removed for now.
* | | | | | fixes different `count` calculation when using `size` manual `select` with ↵jvillarejo2019-02-261-3/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | DISTINCT When using `select` with `'DISTINCT( ... )'` if you use method `size` on a non loaded relation it overrides the column selected by passing `:all` so it returns different value than count. This fixes #35214
* | | | | | Preparing for 6.0.0.beta2 releaseRafael Mendonça França2019-02-251-1/+1
| | | | | |
* | | | | | Merge pull request #35402 from alimi/update-ar-read-regexAaron Patterson2019-02-251-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | Support read queries with leading characters while preventing writes
| * | | | | | Update READ_QUERY regexAli Ibrahim2019-02-251-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * The READ_QUERY regex would consider reads to be writes if they started with spaces or parens. For example, a UNION query might have parens around each SELECT - (SELECT ...) UNION (SELECT ...). * It will now correctly treat these queries as reads.
* | | | | | | Refactor `type_to_sql` to handle converting `limit` to `size` in itselfRyuta Kamizono2019-02-264-67/+60
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Also, improving an argument error message for `limit`, extracting around `type_to_sql` code into schema statements, and more exercise tests.
* | | | | | | Fix prepared statements caching to be enabled even when query caching is enabledRyuta Kamizono2019-02-263-13/+20
|/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Related cbcdecd, 2a56b2d. This is a regression caused by cbcdecd. If query caching is enabled, prepared statement handles are never re-used, since we missed that a query is preprocessed when query caching is enabled, but doesn't keep the `preparable` flag. We should care about that case.
* | | | | | Merge pull request #35352 from kamipo/update_all_doesnt_care_optimistic_lockingRyuta Kamizono2019-02-251-10/+15
|\ \ \ \ \ \ | | | | | | | | | | | | | | Ensure `update_all` series doesn't care optimistic locking
| * | | | | | Ensure `update_all` series cares about optimistic lockingRyuta Kamizono2019-02-251-10/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Incrementing the lock version invalidates any other process's optimistic lock, which is the desired outcome: the record no longer looks the same as it did when they loaded it.
* | | | | | | minor grammar fix [ci skip]Shivam Jain2019-02-241-1/+1
| | | | | | |
* | | | | | | Fix `pluck` and `select` with `from` if `from` has original table nameRyuta Kamizono2019-02-221-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is caused by 0ee96d1. Since #18744, `select` columns doesn't be qualified by table name if using `from`. 0ee96d1 follows that for `pluck` as well. But people depends that `pluck` columns are qualified even if using `from`. So I've fixed that to be qualified if `from` has the original table name to keep the behavior as much as before. Fixes #35359.
* | | | | | | Should not pass extra args to `_update_record`Ryuta Kamizono2019-02-213-7/+13
|/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The argument of `_update_record` and `_create_record` is `attribute_names`, that is reserved for overriding by partial writes attribute names. https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/persistence.rb#L719 https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/persistence.rb#L737 https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/attribute_methods/dirty.rb#L171 https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/attribute_methods/dirty.rb#L177 The reason that no failing with extra args is that `Timestamp` module which is most outside module of the `_update_record` discards the extra args. https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/timestamp.rb#L104 But that looks odd dependency. It should not be passed extra args, should only be passed attribute names.
* | | | | | Address "warning: in `column': the last argument was passed as a single Hash"Ryuta Kamizono2019-02-212-4/+4
| | | | | |
* | | | | | Extract `default_uniqueness_comparison` to ease to handle mismatched ↵Ryuta Kamizono2019-02-212-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | collation issues In MySQL, the default collation is case insensitive. Since the uniqueness validator enforces case sensitive comparison by default, it frequently causes mismatched collation issues (performance, weird behavior, etc) to MySQL users. https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/ https://github.com/rails/rails/issues/1399 https://github.com/rails/rails/pull/13465 https://github.com/gitlabhq/gitlabhq/commit/c1dddf8c7d947691729f6d64a8ea768b5c915855 https://github.com/huginn/huginn/pull/1330#discussion_r55152573 This extracts `default_uniqueness_comparison` to ease to handle the mismatched collation issues on the connection.
* | | | | | Fix lint `ShadowingOuterLocalVariable`soartec-lab2019-02-211-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Revert "Fix lint `ShadowingOuterLocalVariable`" This reverts commit 38bd45a48992b500478a82d56d31468a322937a8. Change of variable name Fix lint `ShadowingOuterLocalVariable`
* | | | | | Merge pull request #35263 from hatch-carl/reduce-postgres-uuid-allocationsRyuta Kamizono2019-02-211-3/+6
|\ \ \ \ \ \ | | | | | | | | | | | | | | Reduce unused allocations when casting UUIDs for Postgres
| * | | | | | Reduce unused allocations when casting UUIDs for PostgresCarl Thuringer2019-02-201-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using the subscript method `#[]` on a string has several overloads and rather complex implementation. One of the overloads is the capability to accept a regular expression and then run a match, then return the receiver (if it matched) or one of the groups from the MatchData. The function of the `UUID#cast` method is to cast a UUID to a type and format acceptable by postgres. Naturally UUIDs are supposed to be string and of a certain format, but it had been determined that it was not ideal for the framework to send just any old string to Postgres and allow the engine to complain when "foobar" or "" was sent, being obviously of the wrong format for a valid UUID. Therefore this code was written to facilitate the checking, and if it were not of the correct format, a `nil` would be returned as is conventional in Rails. Now, the subscript method will allocate one or more strings on a match and return one of them, based on the index parameter. However, there is no need for a new string, as a UUID of the correct format is already such, and so long as the format was verified then the string supplied is adequate for consumption by the database. The subscript method also creates a MatchData object which will never be used, and so must eventually be garbage collected. Garbage collection indeed. This innocuous method tends to be called quite a lot, for example if the primary key of a table is a uuid, then this method will be called. If the foreign key of a relation is a UUID, once again this method is called. If that foreign key is belonging to a has_many relationship with dozens of objects, then again dozens of UUIDs shall be cast to a dup of themselves, and spawn dozens of MatchData objects, and so on. So, for users that: * Use UUIDs as primary keys * Use Postgres * Operate on collections of objects This accomplishes a significant savings in total allocations, and may save many garbage collections.
* | | | | | | Merge pull request #35327 from abhaynikam/use-delete-by-and-destroy-by-methodRyuta Kamizono2019-02-203-3/+3
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Replaced usage of where.delete/destroy_all with delete/destroy_by
| * | | | | | | Replaced usage of where.delete/destroy_all with delete/destroy_byAbhay Nikam2019-02-203-3/+3
| |/ / / / / /
* | | | | | | Merge pull request #35247 from bogdan/fix-source-reflection-reset-codeRyuta Kamizono2019-02-202-21/+10
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Fix reset of the source association when through association is loaded
| * | | | | | | Fix reset of the source association when through association is loadedBogdan Gusiev2019-02-202-21/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The special case happens when through association has a custom scope that is applied to the source association when loading. In this case, the soucre association would need to be reset after main association is loaded. See tests. The special case exists when a through association has
* | | | | | | | PostgreSQL: Support endless range values for range typesRyuta Kamizono2019-02-202-2/+2
| |/ / / / / / |/| | | | | |
* | | | | | | Merge pull request #35316 from abhaynikam/35304-add-delete_by_and_destroy_byRyuta Kamizono2019-02-192-1/+27
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | Introduce delete_by and destroy_by methods to ActiveRecord::Relation
| * | | | | | | Introduce delete_by and destroy_by methods to ActiveRecord::RelationAbhay Nikam2019-02-192-1/+27
|/ / / / / / /
* | | | | | | Don't allow `where` with invalid value matches to nil valuesRyuta Kamizono2019-02-181-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | That is considered as silently leaking information. If type casting doesn't return any actual value, it should not be matched to any record. Fixes #33624. Closes #33946.
* | | | | | | Use placeholder for `type_condition` predicateRyuta Kamizono2019-02-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before: ``` SELECT "comments".* FROM "comments" WHERE "comments"."type" IN ('VerySpecialComment') AND "comments"."post_id" = ? LIMIT ? [["post_id", 4], ["LIMIT", 1]] ``` After: ``` SELECT "comments".* FROM "comments" WHERE "comments"."type" = ? AND "comments"."post_id" = ? LIMIT ? [["type", "VerySpecialComment"], ["post_id", 4], ["LIMIT", 1]] ```
* | | | | | | Fix eager loading polymorphic association with mixed table conditionsRyuta Kamizono2019-02-184-25/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes a bug that the `foreign_key` and the `foreign_type` are separated as different table conditions if a polymorphic association has a scope that joins another tables.
* | | | | | | Merge pull request #35274 from AlexBrodianoi/fix_does_not_support_reverseRyuta Kamizono2019-02-171-1/+1
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a single ordering argument.
| * | | | | | | Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a ↵Finn Young2019-02-171-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | single ordering argument.
* | | | | | | | Fix `order` with custom attributesRyuta Kamizono2019-02-171-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This follows up 0ee96d13de29680e148ccb8e5b68025f29fd091c.
* | | | | | | | Fix the regex that extract mismatched foreign key informationRyuta Kamizono2019-02-172-20/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The CI failure for `test_errors_for_bigint_fks_on_integer_pk_table` is due to the poor regex that extract all ``` `(\w+)` ``` like parts from the message (`:foreign_key` should be `"old_car_id"`, but `"engines"`): https://travis-ci.org/rails/rails/jobs/494123455#L1703 I've improved the regex more strictly and have more exercised mismatched foreign key tests. Fixes #35294