aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
Commit message (Collapse)AuthorAgeFilesLines
* handle passing in primary key to unique_by, and handle primary keys missing ↵Lachlan Sylvester2019-08-052-2/+22
| | | | indexes
* Merge pull request #36845 from eileencodes/fix-pg-closed-connectionEileen M. Uchitelle2019-08-041-6/+11
|\ | | | | Fix `PG::ConnectionBad` error when running fixtures
| * Fix `PG::ConnectionBad` error when running fixtureseileencodes2019-08-021-6/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | At first this appeared to be a multi-db bug but after some invesitgation it was clear that this can occur just by calling `establish_connection` from ApplicationRecord. After some investigation we found that this only occurred when using fixtures. The console boots fine, the server runs fine, and the tests even run fine if we used paralellization or eager loading in the tests. I tracked the issue down to the line that calls `self.connection_specification_name = name` in the SchemaMigration changes for Rails 6.0. But how can this be? That is not that major of a change? How could `connection_specification_name` be a problem? First `connection_specification_name` caches the name of the connection specificatio. Second, fixtures were incorrectly holding onto a reference to that connection. So when you went to run the tests the models wouldn't be connected and when the fixtures tried to load the data it would choke on that unconnected database. The changes here move the connection into a lambda so we can call it when we need it rather than blowing up before the model is connected. Fixes #36743 Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
* | Address `DEPRECATED: Use assert_nil if expecting nil`Yasuo Honda2019-08-031-2/+2
|/ | | | | | | | | | | | | | | | | ```ruby $ cd activerecord $ bin/test test/cases/dirty_test.rb:494 ... snip ... DEPRECATED: Use assert_nil if expecting nil from /home/yahonda/git/rails/activerecord/test/cases/dirty_test.rb:494. This will fail in Minitest 6. DEPRECATED: Use assert_nil if expecting nil from /home/yahonda/git/rails/activerecord/test/cases/dirty_test.rb:511. This will fail in Minitest 6. . Finished in 0.061593s, 16.2356 runs/s, 795.5428 assertions/s. 1 runs, 49 assertions, 0 failures, 0 errors, 0 skips $ ``` Refer seattlerb/minitest#666 rails/rails#27712
* Add ability to unset preventing writeseileencodes2019-08-023-7/+43
| | | | | | | | | | | Previously if an app attempts to do a write inside a read request it will be impossilbe to switch back to writing to the primary. This PR adds an argument to the `while_preventing_writes` so that we can make sure to turn it off if we're doing a write on a primary. Fixes #36830 Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
* Merge pull request #36814 from eileencodes/introduce-invalid-configuration-errorEileen M. Uchitelle2019-08-022-3/+23
|\ | | | | Introduce InvalidConfigurationError
| * Introduce InvalidConfigurationErroreileencodes2019-08-012-3/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In our app at work we had a faked config like this: ``` { "foo" => :bar, "bar" => { "adapter" => "memory" } } ``` This config is invalid. You can't say for foo env just have a symbol, nor would this work if you had fa foo env with just a string. A configuration must be a url or an adapter or a database. Otherwise it's invalid and we can't parse it. When this was just yaml turned into hashes you could get away with passing whatever. It wouldn't work but it wouldn't blow up either. Now that we're using objects we were returning `nil` for these but that just means we either blow up on `for_current_env` or compact the `nil`'s. I think it's a better user experience to not build the configs and raise an appropriate error. This is also an invalid config because if you do pass a string here it should be a URL. ``` { "foo" => "bar", "bar" => { "adapter" => "memory" } } ```
* | Revert "MethodCallAssertions is a regular player of the team ↵Rafael Mendonça França2019-08-021-0/+2
| | | | | | | | | | | | ActiveSupport::TestCase now" This reverts commit 98d0f7ebd34b858f12a12dcf37ae54fdbb5cab64.
* | Add *_previously_was attribute methods when dirty tracking (#36836)David Heinemeier Hansson2019-08-011-0/+6
| |
* | Merge pull request #36821 from rails/virtual-attribute-typeRyuta Kamizono2019-08-021-0/+84
|\ \ | | | | | | Add tests for selecting aggregrates
| * | Add tests for selecting aggregatesGodfrey Chan2019-08-011-0/+84
| | | | | | | | | | | | | | | | | | These tests verifies that aggregates like `AVG` can be selected as "virtual attributes" on Active Record models and have the correct column type.
* | | MethodCallAssertions is a regular player of the team ActiveSupport::TestCase nowAkira Matsuda2019-08-021-2/+0
| |/ |/| | | | | It's used everywhere, clean and mature enough
* | Deduplicate joins valuesRyuta Kamizono2019-08-022-3/+13
|/ | | | | | | | | #36805 have one possible regression that failing deduplication if `joins_values` have complex order (e.g. `joins_values = [join_node_a, :comments, :tags, join_node_a]`). This fixes the deduplication to take it in the first phase before grouping.
* No private defRafael Mendonça França2019-08-011-1/+2
|
* It may be better to explicitly require 'object/try' where we call `try`Akira Matsuda2019-08-013-0/+4
| | | | | | In most cases it works now without explicit require because it's accidentally required through active_support/core_ext/date_and_time/calculations.rb where we still call `try`, but that would stop working if we changed the Calculations implementation and remove the require call there.
* Use `try` only when we're unsure if the receiver would respond_to the methodAkira Matsuda2019-08-016-6/+6
|
* Merge pull request #36827 from akshaymohite/fix-documentation-typo-in-exampleVipul A M2019-08-011-1/+1
|\ | | | | Fixed a typo in documentation example of activerecord database configuration. [ci skip]
| * Fixed a typo in documentation example of activerecord database ↵Akshay Mohite2019-08-011-1/+1
| | | | | | | | | | | | configuration. [ci skip] - The example has sqlite3 adpater in database.yml, whereas configuration object had incorrectly specified mysql2 in documentation.
* | Fix db:seedeileencodes2019-07-311-0/+2
|/ | | | | | | | | | | | | | | | | | | The `rake db:seed` command was broken for the primary environment if the application is using multiple databases. We never implemented `rake db:seed` for other databases (coming soon), but that shouldn't break the default case. The reason this was broken was because `abort_if_pending_migrations` would loop through the configs for all databases and check for migrations but would leave the last established connection. So `db:seed` was looking in the wrong database for the table to seed. This PR doesn't fix the fact that `db:seed` doesn't work for multiple databases but does fix the default case. Fixes #36817 Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
* Address to rubocop offencesRyuta Kamizono2019-07-311-1/+1
|
* Reduce Array allocationsAkira Matsuda2019-07-311-2/+2
|
* Merge pull request #36708 from ↵Kasper Timm Hansen2019-07-315-12/+37
|\ | | | | | | | | rails/has-one-polymorphic-touch-dont-cache-association-result-inside-create-transaction Polymorphic has_one touch: Don't cache association result inside crea…
| * Polymorphic has_one touch: Reset association cache result after create ↵Kasper Timm Hansen2019-07-315-12/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | transaction In case of a polymorphic association there's no automatic inverse_of to assign the inverse record. So to get the record there needs to be a query executed, however, if the query fires within the transaction that's trying to create the associated record, no record can be found. And worse, the nil result is cached on the association so after the transaction commits the record can't be found. That's what happens if touch is enabled on a polymorphic has_one association. Consider a Comment with a commentable association that needs to be touched. For `Comment.create(commentable: Post.new)`, the existing code essentially does `commentable.send(:comment)` within the create transaction for the comment and thus not finding the comment. Now we're purposefully clearing the cache in case we've tried accessing the association within the transaction and found no object. Before: ``` kaspth-imac 2.6.3 ~/code/rails/activerecord master *= ARCONN=postgresql bin/test test/cases/associations/has_one_associations_test.rb -n /commit/ Using postgresql Run options: -n /commit/ --seed 46022 D, [2019-07-19T03:30:37.864537 #96022] DEBUG -- : Chef Load (0.2ms) SELECT "chefs".* FROM "chefs" WHERE "chefs"."employable_id" = $1 AND "chefs"."employable_type" = $2 LIMIT $3 [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"], ["LIMIT", 1]] D, [2019-07-19T03:30:37.865013 #96022] DEBUG -- : Chef Create (0.2ms) INSERT INTO "chefs" ("employable_id", "employable_type") VALUES ($1, $2) RETURNING "id" [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"]] D, [2019-07-19T03:30:37.865201 #96022] DEBUG -- : TRANSACTION (0.1ms) RELEASE SAVEPOINT active_record_1 D, [2019-07-19T03:30:37.874136 #96022] DEBUG -- : TRANSACTION (0.1ms) ROLLBACK D, [2019-07-19T03:30:37.874323 #96022] DEBUG -- : TRANSACTION (0.1ms) ROLLBACK F Failure: HasOneAssociationsTest#test_polymorphic_has_one_with_touch_option_on_create_wont_cache_assocation_so_fetching_after_transaction_commit_works [/Users/kaspth/code/rails/activerecord/test/cases/associations/has_one_associations_test.rb:716]: --- expected +++ actual @@ -1 +1 @@ -#<Chef id: 1, employable_id: 1, employable_type: "DrinkDesignerWithPolymorphicTouchChef", department_id: nil, employable_list_type: nil, employable_list_id: nil> +nil ``` After: ``` kaspth-imac 2.6.3 ~/code/rails/activerecord master *= ARCONN=postgresql bin/test test/cases/associations/has_one_associations_test.rb -n /commit/ Using postgresql Run options: -n /commit/ --seed 46022 D, [2019-07-19T03:30:22.479387 #95973] DEBUG -- : Chef Create (0.3ms) INSERT INTO "chefs" ("employable_id", "employable_type") VALUES ($1, $2) RETURNING "id" [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"]] D, [2019-07-19T03:30:22.479574 #95973] DEBUG -- : TRANSACTION (0.1ms) RELEASE SAVEPOINT active_record_1 D, [2019-07-19T03:30:22.482051 #95973] DEBUG -- : Chef Load (0.1ms) SELECT "chefs".* FROM "chefs" WHERE "chefs"."employable_id" = $1 AND "chefs"."employable_type" = $2 LIMIT $3 [["employable_id", 1], ["employable_type", "DrinkDesignerWithPolymorphicTouchChef"], ["LIMIT", 1]] D, [2019-07-19T03:30:22.482317 #95973] DEBUG -- : TRANSACTION (0.1ms) ROLLBACK D, [2019-07-19T03:30:22.482437 #95973] DEBUG -- : TRANSACTION (0.1ms) ROLLBACK . Finished in 0.088498s, 11.2997 runs/s, 22.5994 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips ``` Notice the select now fires after the commit.
* | fix a typoyamato-payforward2019-07-311-1/+1
| |
* | Accessing ivar with Symbols might be just a very little bit better than with ↵Akira Matsuda2019-07-311-5/+5
| | | | | | | | fstrings
* | Merge pull request #36805 from ↵Ryuta Kamizono2019-07-303-24/+65
|\ \ | | | | | | | | | | | | kamipo/user_supplied_joins_order_should_be_preserved Preserve user supplied joins order as much as possible
| * | Preserve user supplied joins order as much as possibleRyuta Kamizono2019-07-303-24/+65
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, string joins are always applied as last joins part, and Arel join nodes are always applied as leading joins part (since #36304), it makes people struggled to preserve user supplied joins order. To mitigate this problem, preserve the order of string joins and Arel join nodes either before or after of association joins. Fixes #36761. Fixes #34328. Fixes #24281. Fixes #12953.
* | | Add `silence_warnings` for defining 'not_' prefix enum elementsRyuta Kamizono2019-07-301-1/+3
|/ / | | | | | | | | | | | | | | | | To suppress the following warnings in tests. ``` ~/rails/activerecord/lib/active_record/scoping/named.rb:190: warning: method redefined; discarding old not_sent ~/rails/activerecord/lib/active_record/scoping/named.rb:190: warning: previous definition of not_sent was here ```
* | Merge pull request #36782 from jhawthorn/move_database_exists_to_adapterJohn Hawthorn2019-07-298-18/+13
|\ \ | | | | | | Move DatabaseAlreadyExists detection to DB adapter
| * | Move DatabaseAlreadyExists detection to DB adapterJohn Hawthorn2019-07-298-18/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously it was the responsibility of the database tasks to translate the invalid statement from creating a duplicate database into an ActiveRecord::Tasks::DatabaseAlreadyExists error. It's actually easier for us to do this detection inside of the adapter, where we already do a case statement on the return code to translate the error. This commit introduces ActiveRecord::DatabaseAlreadyExists, a subclass of StatementInvalid, and updates both AbstractMysqlAdapter and PostgresqlAdapter to return this more specific exception in that case. Because this is a subclass of the old exception, StatementInvalid, it should be backwards compatible with any code expecting that from create_database. This works for both create_database and exectute("CREATE DATABASE")
* | | Expand CHANGELOG for #36800 [ci skip]Ryuta Kamizono2019-07-301-2/+2
|/ /
* | Enabled matches_regex for MySqlJames Pearson2019-07-293-0/+58
| | | | | | | | | | | | | | | | | | | | | | | | | | Previously matches_regex was only availble on PostgreSql, this will enable it for MySql Usage example: users = User.arel_table; users = User.arel_table; User.where(users[:email].matches_regexp('(.*)\@gmail.com')) Update activerecord/test/cases/arel/visitors/mysql_test.rb Co-Authored-By: Ryuta Kamizono <kamipo@gmail.com>
* | Use match? where we don't need MatchDataAkira Matsuda2019-07-293-3/+3
| |
* | Merge pull request #36787 from st0012/refactor-sql-testsRyuta Kamizono2019-07-282-8/+2
|\ \ | | | | | | Use capture_sql helper method in tests
| * | Use capture_sql helper method in testsst00122019-07-282-8/+2
| | |
* | | Merge pull request #36303 from gaotongfei/feature/ignore-specified-fixturesRafael França2019-07-276-5/+111
|\ \ \ | | | | | | | | Allow specifying fixtures to be ignored in "_fixture" section
| * | | Allow specify fixtures to be ignoredTongfei Gao2019-07-276-5/+111
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Allow specifying what fixtures can be ignored by setting `ignore` in fixtures YAML file: # users.yml _fixture: ignore: - base base: &base admin: false introduction: "This is a default description" admin: <<: *base admin: true visitor: <<: *base In the above example, "base" fixture will be ignored when creating users fixture. This is helpful when you want to inherit attributes and it makes your fixtures more "DRY".
* | | Update links to new MySQL server errors reference page [ci skip]Carlos Antonio da Silva2019-07-272-2/+2
| | |
* | | Avoid to use a method that acts on the hashyuuji.yaginuma2019-07-271-1/+1
|/ / | | | | | | To avoid a deprecation warning.
* | Use match? where we don't need MatchDataAkira Matsuda2019-07-276-6/+6
| | | | | | | | We're already running Performance/RegexpMatch cop, but it seems like the cop is not always =~ justice
* | Merge pull request #36773 from seejohnrun/db-configuration-separate-env-varsEileen M. Uchitelle2019-07-263-7/+45
|\ \ | | | | | | Allow separate database env variables per-connection
| * | Allow separate database env variables per-connectionJohn Crepezzi2019-07-263-7/+45
| |/ | | | | | | | | | | | | | | | | | | | | This commit adds a feature which allows separate database ENV variables to be defined for each spec in a 3-tier config. The names for the environment variables will be `#{name.upcase}_DATABASE_URL` This commit also introduces a change in behavior around handling of `DATABASE_URL`. Instead of using `DATABASE_URL` to change _all_ specs in a multi-database configuration, it will now only affect the `primary` connection.
* | Merge pull request #36776 from ↵Rafael França2019-07-262-3/+10
|\ \ | | | | | | | | | | | | giraffate/fix_join_middle_table_alias_when_using_HABTM Fix join middle table alias when using HABTM
| * | Fix join middle table alias when using HABTMTakayuki Nakata2019-07-262-3/+10
| | | | | | | | | | | | | | | | | | | | | | | | In using HABTM, join middle table alias is combined with the associated models name without sort, while middle table name is combined with those models name with sort. Fixes #36742.
* | | Merge pull request #36372 from instructure-bridge/6-0-stableRafael França2019-07-262-8/+21
| |/ |/| | | Don't break configurations.each, .first before the deprecation period
* | Only merge DATABASE_URL settings into the current envJohn Crepezzi2019-07-252-18/+36
| | | | | | | | | | | | | | This commit fixes a regression where when the `DATABASE_URL` environment variable was set and the current Rails environment had a valid configuration defined in the database config, settings from the environment variable would affect _all_ environments (not just the current one).
* | Merge pull request #36766 from jhawthorn/call_connection_error_numberJohn Hawthorn2019-07-252-4/+9
|\ \ | | | | | | Use connection.error_number in MySQLDatabaseTasks
| * | Use connection.error_number in MySQLDatabaseTasksJohn Hawthorn2019-07-252-4/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | MySQLDatabaseTasks, like AbstractMysqlAdapter, should be able to operate on any mysql adapter, not just mysql2. Errors having a .error_number attribute is a mysql2 specific API, which we (Rails) don't control, so we should instead use connection.error_number(err), which we do. This also updates tests to better test how this really works, previously it stubbed create_database to raise Tasks::DatabaseAlreadyExists, which can never happen.
* | | Merge pull request #36380 from ↵Rafael Mendonça França2019-07-253-0/+39
|\ \ \ | |/ / |/| | | | | | | | | | | edudepetris/ed/36272-better-negative-scope-warning Add a warning for enum elements with 'not_' prefix.
| * | Add a warning for enum elements with 'not_' prefix.Edu Depetris2019-06-303-0/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When a enum element contains the prefix 'not_'. I warns to users to be aware of this new feature. Example code: class Foo < ActiveRecord::Base enum status: [:sent, :not_sent] end