aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
Commit message (Collapse)AuthorAgeFilesLines
* Eager load the association classes on bootRafael Mendonça França2017-07-271-7/+7
| | | | | | | | | | | We were expecting those classes to be loaded when an association is defined but they are not. If you add a debugger on the first line of any of those classes and try to define the corresponding association you will see that the classes are not loaded. Fixes #26273
* Use `_relation_for_itself` wherever possibleSean Griffin2017-07-261-3/+2
| | | | | | | Anywhere that we're doing `self.class.unscoped.where(primary_key => id)` is somewhere that someone may want to extend. Even if this method isn't public API yet, this will make it easier for us to eventually expose an API around this. Plus, duplicated code makes me sad.
* Merge pull request #29941 from kamipo/remove_single_element_array_preprocessSean Griffin2017-07-252-2/+2
|\ | | | | Remove single element array preprocess
| * Remove single element array preprocessRyuta Kamizono2017-07-262-2/+2
| | | | | | | | | | Since 213796f, array predicate handler supports making binds, so the preprocess is no longer needed.
* | Clarify add_column limit documentationLisa Ugray2017-07-251-0/+1
| | | | | | | | | | The limit option is ignored by PostgreSQL and may be ignored by 3rd party backends. Make this clear in the docs. Fixes #29922.
* | Allow `Relation#or` to accept a relation with different `references`Sean Griffin2017-07-251-1/+2
|/ | | | | | | | | Note that the two relations must still have the same `includes` values (which is the only time `references` actually does anything). It makes sense for us to allow this, as `references` is called implicitly when passing a hash to `where`. Fixes #29411
* Merge pull request #29934 from kamipo/remove_join_informationSean Griffin2017-07-253-10/+5
|\ | | | | Remove useless `JoinInformation`
| * Remove useless `JoinInformation`Ryuta Kamizono2017-07-253-10/+5
| | | | | | | | | | Since 213796f removed `binds`, `JoinInformation` only contain `joins`. So it is enough to return `joins` simply.
* | Remove unused `queries_predicates`Ryuta Kamizono2017-07-251-12/+0
|/ | | | Since 213796f, `queries_predicates` is no longer used.
* Merge pull request #29848 from kamipo/fix_distinct_count_with_order_and_limitRafael França2017-07-243-11/+24
|\ | | | | Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`
| * Should keep the table name qualified `*` for distinct subqueryRyuta Kamizono2017-07-221-1/+1
| |
| * Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`Ryuta Kamizono2017-07-223-11/+24
| | | | | | | | | | | | | | Since #26972, `ORDER BY` is kept if `LIMIT` is presented for performance. But in most SQL servers (e.g. PostgreSQL, SQL Server, etc), `ORDER BY` expressions must appear in select list for `SELECT DISTINCT`. We should not replace existing select list in that case.
* | Merge pull request #29765 from lugray/fix_counter_cacheRafael França2017-07-242-4/+1
|\ \ | | | | | | Fix `counter_cache` double increment
| * | Add test for fixed `counter_cache` double incrementLisa Ugray2017-07-192-4/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When an `after_create` callback did `update_attributes` on a record with multiple `belongs_to` associations with counter caches, even numbered associations would have their counters double-incremented. Fixes to `ActiveModel::Dirty` in 020abad fixed this. This adds regression tests for this bug fixed incidentally in the other commit, which also removed the need for the workaround using @_after_create_counter_called.
* | | Fix test failures when prepared statements are disabledSean Griffin2017-07-243-14/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | This also reverts the change to enable prepared statements by default on MySQL (though I suspect we could enable them and it'd be great). This change brings back a collector closer to the old `Bind` collector in Arel. However, this one lives in AR, since this is an AR specific need. Additionally, we only use it for statement caching, since the new substitute collector in Arel is higher performance for most cases.
* | | Fix build failures on PGSean Griffin2017-07-241-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Honestly I don't think the tests that are fixed by this change should have been merged. Passing a range or an array to `where` has a special meaning. We need to solve the problem more concretely without overriding the behavior that is present for *every* other type. However, the damage has been done. These changes were in 5.1, so we need a deprecation cycle to remove it.
* | | Fix build failures on MySQLSean Griffin2017-07-242-2/+2
| | | | | | | | | | | | | | | | | | There's an actual bug in 213796fb4936dce1da2f0c097a054e1af5c25c2c around prepared statements being disabled. I'm looking into it, but in the mean time this gets the build green so it doesn't block other PRs
* | | Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-2426-329/+225
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* | | Avoid modifying frozen string in check_schema_fileEugene Kenny2017-07-231-1/+1
| | | | | | | | | | | | | | | | | | This was missed when the frozen string literal pragma was added to this file because the string is only modified when running in the context of a full Rails app, which wasn't covered by the test suite.
* | | Merge pull request #29768 from janpieper/sort-enabled-extensions-in-dumpEileen M. Uchitelle2017-07-221-1/+1
|\ \ \ | | | | | | | | Sort enabled adapter extensions in schema dump
| * | | Sort enabled adapter extensions in schema dumpJan Pieper2017-07-121-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The list of enabled adapter extensions in the schema dump isn't sorted by default, so it may happen that the sorting changes over time. If you're using a VCS, a change to the sorting results in a diff without any real change. Sorting the list should solve this problem.
* | | | Merge pull request #29870 from kamipo/use_true_false_literalsSean Griffin2017-07-222-14/+4
|\ \ \ \ | |_|_|/ |/| | | Use `TRUE` and `FALSE` boolean literals for MySQL
| * | | Use `TRUE` and `FALSE` boolean literals for MySQLRyuta Kamizono2017-07-202-14/+6
| | | | | | | | | | | | | | | | | | | | | | | | Since #29699, abstract boolean serialization has been changed to use `TRUE` and `FALSE` literals. MySQL also support the literals. So we can use the abstract boolean serialization even for MySQL.
* | | | Merge pull request #29869 from kamipo/make_type_map_to_privateRafael França2017-07-214-17/+16
|\ \ \ \ | | | | | | | | | | Make `type_map` to private because it is only used in the connection adapter
| * | | | Make `type_map` to private because it is only used in the connection adapterRyuta Kamizono2017-07-204-17/+16
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | `type_map` is an internal API and it is only used in the connection adapter. And also, some type map initializer methods requires passed `type_map`, but those instances already has `type_map` in itself. So we don't need explicit passing `type_map` to the initializers.
* | | | Merge pull request #29855 from lugray/has_one_destroyed_by_associationRafael França2017-07-211-0/+2
|\ \ \ \ | | | | | | | | | | Match destroyed_by_association for has_one to has_many
| * | | | Match destroyed_by_association for has_one to has_manyLisa Ugray2017-07-211-0/+2
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | When a has_many association is destroyed by `dependent: destroy`, destroyed_by_association is set to the reflection, and this can be checked in callbacks. This matches that behaviour for has_one associations.
* / / / Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-19235-0/+470
|/ / /
* | | Merge pull request #29033 from kamipo/make_preload_query_to_prepared_statementsSean Griffin2017-07-181-1/+1
|\ \ \ | |_|/ |/| | Make preload query to preparable
| * | Make preload query to preparableRyuta Kamizono2017-07-071-1/+1
| | | | | | | | | | | | | | | | | | | | | Currently preload query cannot be prepared statements even if `prepared_statements: true` due to array handler in predicate builder doesn't support making bind params. This makes preload query to preparable by don't passing array value if possible.
* | | Fix type casting a time for MariaDBRyuta Kamizono2017-07-191-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Context #24542. Since 8ebe1f2, it has lost stripping date part for a time value. But I confirmed it is still needed even if MariaDB 10.2.6 GA. MariaDB 10.2.6, `prepared_statements: true`: ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/time_precision_test.rb -n test_formatting_time_according_to_precision Using mysql2 Run options: -n test_formatting_time_according_to_precision --seed 37614 F Failure: TimePrecisionTest#test_formatting_time_according_to_precision [test/cases/time_precision_test.rb:53]: Failed assertion, no message given. bin/rails test test/cases/time_precision_test.rb:46 Finished in 0.040279s, 24.8268 runs/s, 24.8268 assertions/s. 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips ```
* | | Merge branch 'master' into fix_unscope_where_column_with_orSean Griffin2017-07-1827-180/+95
|\ \ \
| * \ \ Merge pull request #29785 from ↵Sean Griffin2017-07-181-0/+1
| |\ \ \ | | | | | | | | | | | | | | | | | | | | cswilliams/rescue_postgres_connection_errors_on_dealloc Catch postgres connection errors when trying to dealloc
| | * | | Catch postgres connection errors when trying to dealloc the statement poolChris Williams2017-07-131-0/+1
| | | |/ | | |/| | | | | | | | | | | | | | | | | connection_active? will sometimes return true when the connection is actually dead/disconnected (see #3392 for a discussion of why this is). When this happens, a query is run on the dead connection which causes various postgres connection errors to be raised. This fix catches any such errors and ignores them. Closes #29760
| * | | Merge pull request #29834 from ↵Sean Griffin2017-07-183-3/+19
| |\ \ \ | | | | | | | | | | | | | | | | | | | | kamipo/fix_unscoping_default_scope_with_sti_association Fix unscoping `default_scope` in STI associations
| | * | | Fix unscoping `default_scope` for `Preloader`Ryuta Kamizono2017-07-191-1/+11
| | | | |
| | * | | Fix unscoping `default_scope` in STI associationsRyuta Kamizono2017-07-192-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since 5c71000, it has lost to be able to unscope `default_scope` in STI associations. This change will use `.empty_scope?` instead of `.values.empty?` to regard as an empty scope if only have `type_condition`.
| * | | | Don't convert dates to strings when using prepared statements in mysqlSean Griffin2017-07-181-0/+8
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | Dates are able to be natively handled by the mysql2 gem. libmysql (and the wire protocol) represent each portion of the date as an integer, which is significantly faster to encode and decode. By passing the Ruby date objects through directly, we can save a good bit of time and memory.
| * | | Remove deprecated code concerning non-attributes and `*_will_change!`Sean Griffin2017-07-182-18/+3
| | | |
| * | | Remove deprecated code concerning dirty methods in after callbacksSean Griffin2017-07-181-79/+8
| | | |
| * | | Remove incorrect commentSean Griffin2017-07-181-2/+0
| | | | | | | | | | | | | | | | This method needs to be protected.
| * | | Fix `JoinDependency` with using a custom tableRyuta Kamizono2017-07-185-13/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, `JoinDependency` doesn't use a custom table alias: ``` % ARCONN=sqlite3 be ruby -w -Itest test/cases/relations_test.rb -n test_using_a_custom_table_with_joins_affects_the_wheres Using sqlite3 Run options: -n test_using_a_custom_table_with_joins_affects_the_wheres --seed 14531 E Error:RelationTest#test_using_a_custom_table_with_joins_affects_the_wheres: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.author_id: SELECT "omg_posts".* FROM "posts" "omg_posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" WHERE "omg_posts"."title" = ? LIMIT ? ```
| * | | Merge pull request #29796 from kamipo/fix_where_with_custom_tableSean Griffin2017-07-171-1/+1
| |\ \ \ | | | | | | | | | | Fix `where` with a custom table
| | * | | Fix `where` with a custom tableRyuta Kamizono2017-07-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, SELECT clause doesn't use a custom table alias name: ``` % ARCONN=sqlite3 be ruby -w -Itest test/cases/relations_test.rb -n test_using_a_custom_table_affects_the_wheres Using sqlite3 Run options: -n test_using_a_custom_table_affects_the_wheres --seed 31818 E Error: RelationTest#test_using_a_custom_table_affects_the_wheres: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: posts: SELECT "posts".* FROM "posts" "omg_posts" WHERE "omg_posts"."title" = ? LIMIT ? ```
| * | | | `Persistence#delete` should not be affected by scopingRyuta Kamizono2017-07-181-1/+5
| |/ / / | | | | | | | | | | | | | | | | `self.class.delete` is delegated to `all` and `all` is affected by scoping. It should use `unscoped` to not be affected by that.
| * | | Allow multiparameter assigned attributes to be used with `text_field`Sean Griffin2017-07-171-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Between 4.2 and 5.0 the behavior of how multiparameter attributes interact with `_before_type_cast` changed. In 4.2 it returns the post-type-cast value. After 5.0, it returns the hash that gets sent to the type. This behavior is correct, but will cause an issue if you then tried to render that value in an input like `text_field` or `hidden_field`. In this case, we want those fields to use the post-type-cast form, instead of the `_before_type_cast` (the main reason it uses `_before_type_cast` at all is to avoid losing data when casting a non-numeric string to integer). I've opted to modify `came_from_user?` rather than introduce a new method for this as I want to avoid complicating that contract further, and technically the multiparameter hash didn't come from assignment, it was constructed internally by AR. Close #27888.
| * | | Merge pull request #29825 from kamipo/remove_useless_arel_engineSean Griffin2017-07-173-12/+1
| |\ \ \ | | | | | | | | | | Remove useless `arel_engine`
| | * | | Remove useless `arel_engine`Ryuta Kamizono2017-07-173-12/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `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.
| * | | | Post.joins(:users) should not be affected by `User.current_scope`Sean Griffin2017-07-171-4/+2
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This change was introduced by #18109. The intent of that change was to specifically apply `unscoped`, not to allow all changes to `current_scope` to affect the join. The idea of allowing `current_scope` to affect joins is interesting and potentially more consistent, but has sever problems associated with it. The fact that we're specifically stripping out joins indicates one such problem (and potentially leads to invalid queries). Ultimately it's difficult to reason about what `Posts.joins(:users)` actually means if it's affected by `User.current_scope`, and it's difficult to specifically control what does or doesn't get added. If we were starting from scratch, I don't think I'd have `joins` be affected by `default_scope` either, but that's too big of a breaking change to make at this point. With this change, we no longer apply `current_scope` when bringing in joins, with the singular exception of the motivating use case which introduced this bug, which is providing a way to *opt-out* of having the default scope apply to joins. Fixes #29338.
| * | | Merge pull request #29812 from kamipo/remove_unused_requiresKasper Timm Hansen2017-07-162-5/+0
| |\ \ \ | | | | | | | | | | Remove unused requires