aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | | Amend CVE note and security guide section wordingsGannon McGibbon2018-11-061-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Reword first sentence of dep management and CVE section of security guide. Also, reword and move gemspec notes above deps. [ci skip]
* | | | | Add CVE note to security guide and gemspecsGannon McGibbon2018-11-061-0/+3
|/ / / / | | | | | | | | | | | | [ci skip]
* | | | Fix inspect with non-primary key id attributeEugene Kenny2018-11-065-3/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `read_attribute` method always returns the primary key when asked to read the `id` attribute, even if the primary key isn't named `id`, and even if another attribute named `id` exists. For the `inspect`, `attribute_for_inspect` and `pretty_print` methods, this behaviour is undesirable, as they're used to examine the internal state of the record. By using `_read_attribute` instead, we'll get the real value of the `id` attribute.
* | | | Move `resolve_sti_reflections` which is table row related code into `TableRow`Ryuta Kamizono2018-11-032-89/+78
| | | |
* | | | Don't pass useless `table_name` to `ModelMetadata.new`Ryuta Kamizono2018-11-033-13/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | The `model_metadata` is only used if `model_class` is given. If `model_class` is given, the `table_name` is always `model_class.table_name`.
* | | | Don't pass unused `connection` to `FixtureSet.new`Ryuta Kamizono2018-11-032-14/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `@connection` is no longer used since ee5ab22. Originally the `@connection` was useless because it is only used in `timestamp_column_names`, which is only used if `model_class` is given. If `model_class` is given, the `@connection` is always `model_class.connection`.
* | | | Checking boundable not only `IN` clause but also `NOT IN` clauseRyuta Kamizono2018-11-034-8/+24
| | | |
* | | | Updating sample code on ActiveRecord#before_destroy callback [ci skip]Espartaco Palma2018-10-311-1/+1
| | | | | | | | | | | | | | | | It was executing a delete_all method with wrong parameter
* | | | Fix "warning: shadowing outer local variable - role"Ryuta Kamizono2018-10-311-9/+13
| | | | | | | | | | | | | | | | Caused at #34196.
* | | | Merge pull request #34353 from gmcgibbon/fix_lock_docs_locking_clauseRafael França2018-10-301-3/+3
|\ \ \ \ | | | | | | | | | | Fix example for database-specific locking clause
| * | | | Fix example for database-specific locking clauseGannon McGibbon2018-10-301-3/+3
| | | | | | | | | | | | | | | | | | | | [ci skip]
* | | | | Fix failing testSean Griffin2018-10-301-0/+1
|/ / / / | | | | | | | | | | | | | | | | b63701e moved the assignment before the query, but we need to capture our old id before assignment in case we are assigning the id.
* | | | `update_columns` raises if the column is unknownSean Griffin2018-10-303-4/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previosly, `update_columns` would just take whatever keys you gave it and tried to run the update query. Most likely this would result in an error from the database. However, if the column actually did exist, but was in `ignored_columns`, this would result in the method returning successfully when it should have raised, and an attribute that should not exist written to `@attributes`.
* | | | Merge pull request #19388 from yakara-ltd/fix-habtm-fixture-orderRyuta Kamizono2018-10-302-0/+7
|\ \ \ \ | | | | | | | | | | Avoid violating key constraints in fixture HABTM associations
| * | | | Avoid violating key constraints in fixture HABTM associationsJames Le Cuirot2018-10-292-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When loading fixtures, Ruby 1.9's hash key ordering means that HABTM join table rows are always loaded before the parent table rows, violating foreign key constraints that may be in place. This very simple change ensures that the parent table's key appears first in the hash. Violations may still occur if fixtures are loaded in the wrong order but those instances can be avoided unlike this one.
* | | | | Merge pull request #34196 from gmcgibbon/connection_switch_string_nameEileen M. Uchitelle2018-10-303-6/+63
|\ \ \ \ \ | | | | | | | | | | | | Add support for hash and url configs to be used in connected_to
| * | | | | Add support for hash and url configs in connected_toGannon McGibbon2018-10-263-6/+63
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add support for hash and url configs in database hash of `ActiveRecord::Base.connected_to`.
* | | | | | Exercise HABTM fixtures with foreign key constraintsRyuta Kamizono2018-10-302-45/+52
| | | | | |
* | | | | | Document exception from restrict_with_exception [ci skip]Malcolm Locke2018-10-291-2/+2
| |/ / / / |/| | | |
* | | | | `exists?` with string argument is not invalid typeRyuta Kamizono2018-10-271-5/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Any type can be a primary key, so blank string is also valid value. Closes #26356.
* | | | | `create_table` with `:primary_key` option has no effect if `id: false` is givenRyuta Kamizono2018-10-273-8/+6
| | | | | | | | | | | | | | | | | | | | Use column definition with `primary_key: true` instead.
* | | | | Ignore empty condition on #construct_relation_for_existsr7kamura2018-10-272-1/+5
|/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | At https://github.com/rails/rails/commit/fc0e3354af7e7878bdd905a95ce4c1491113af9a, ```rb relation = relation.where(conditions) ``` was rewritten to: ```rb relation.where!(condition) ``` This change accidentally changed the result of `Topic.exists?({})` from true to false. To fix this regression, first I moved the blank check logic (`opts.blank?`) from `#where` to `#where!`, because I thought `#where!` should be identical to `#where`, except that instead of returning a new relation, it adds the condition to the existing relation. But on second thought after some discussion on https://github.com/rails/rails/pull/34329, I started to think that just fixing `#construct_relation_for_exists` is more preferable than changing `#where` and `#where!`.
* | | | Merge pull request #34208 from yskkin/inspect_with_parameter_filterRyuta Kamizono2018-10-264-32/+90
|\ \ \ \ | | | | | | | | | | Implement AR#inspect using ParameterFilter
| * | | | Implement AR#inspect using ParamterFilter.Yoshiyuki Kinjo2018-10-194-32/+90
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | AR instance support `filter_parameters` since #33756. Though Regex or Proc is valid as `filter_parameters`, they are not supported as AR#inspect. I also add :mask option and #filter_params to `ActiveSupport::ParameterFilter#new` to implement this.
* | | | | Merge pull request #34307 from kamipo/mysql_expression_supportRyuta Kamizono2018-10-268-11/+83
|\ \ \ \ \ | | | | | | | | | | | | Support default expression and expression indexes for MySQL
| * | | | | Support default expression for MySQLRyuta Kamizono2018-10-255-4/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | MySQL 8.0.13 and higher supports default value to be a function or expression. https://dev.mysql.com/doc/refman/8.0/en/create-table.html
| * | | | | Support expression indexes for MySQLRyuta Kamizono2018-10-255-7/+49
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | MySQL 8.0.13 and higher supports functional key parts that index expression values rather than column or column prefix values. https://dev.mysql.com/doc/refman/8.0/en/create-index.html
* | | | | | Partly revert #31819bogdanvlviv2018-10-261-26/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The PR#31819 changed `#preloaders_on` and added some test, then #33938 reverted changes that were added to the method in #31819. Since changes in the method were reverted and as mentioned in the comment https://github.com/rails/rails/pull/31819#discussion_r221847481 that titles of the tests added in #31819 don't reflect implementation I think we can remove those test for now.
* | | | | | `assert_called_with` should require `args` argumentbogdanvlviv2018-10-251-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There are two main reasons why `assert_called_with` should require `args` argument: 1) If we want to assert that some method should be called and we don't need to check with which arguments it should be called then we should use `assert_called`. 2) `assert_called_with` without `args` argument doesn't assert anything! ```ruby assert_called_with(@object, :increment) do @object.decrement end ``` It causes false assertions in tests that could cause regressions in the project. I found this bug by working on [minitest-mock_expectations](https://github.com/bogdanvlviv/minitest-mock_expectations) gem. This gem is an extension for minitest that provides almost the same method call assertions. I was wondering whether you would consider adding "minitest-mock_expectations" to `rails/rails` instead of private `ActiveSupport::Testing::MethodCallAssertions` module. If yes, I'll send a patch - https://github.com/bogdanvlviv/rails/commit/a970ecc42c3a9637947599f2c13e3762e4b59208
* | | | | | Deterministic connection collation in testsRyuta Kamizono2018-10-251-0/+1
|/ / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The default collation for utf8mb4 is changed to utf8mb4_0900_ai_ci in MySQL 8.0, so `test_mysql_connection_collation_is_configured` is affected by MySQL version and/or default_collation_for_utf8mb4 system variable. https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_collation_for_utf8mb4 ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/adapters/mysql2/connection_test.rb -n test_mysql_connection_collation_is_configured Using mysql2 Run options: -n test_mysql_connection_collation_is_configured --seed 10421 # Running: F Failure: Mysql2ConnectionTest#test_mysql_connection_collation_is_configured [test/cases/adapters/mysql2/connection_test.rb:108]: Expected: "utf8mb4_general_ci" Actual: "utf8mb4_0900_ai_ci" rails test test/cases/adapters/mysql2/connection_test.rb:106 Finished in 0.013842s, 72.2450 runs/s, 144.4900 assertions/s. 1 runs, 2 assertions, 1 failures, 0 errors, 0 skips ```
* | | | | Merge pull request #34303 from kamipo/lazy_checking_boundableRafael França2018-10-246-9/+18
|\ \ \ \ \ | |_|_|/ / |/| | | | Lazy checking whether or not values in IN clause are boundable
| * | | | Lazy checking whether or not values in IN clause are boundableRyuta Kamizono2018-10-246-9/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since #33844, eager loading/preloading with too many and/or too large ids won't be broken by pre-checking whether the value is constructable or not. But the pre-checking caused the type to be evaluated at relation build time instead of at the query execution time, that is breaking an expectation for some apps. I've made the pre-cheking lazy as much as possible, that is no longer happend at relation build time.
* | | | | Fix typo of duplicated `the` [ci skip]ohbarye2018-10-241-1/+1
| | | | |
* | | | | Hide PG::Connection from API docs [ci skip]Francesco Rodríguez2018-10-231-1/+1
| | | | |
* | | | | MySQL 8.0.13 raises `ER_NO_REFERENCED_ROW` and `ER_ROW_IS_REFERENCED`Yasuo Honda2018-10-231-1/+3
|/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | when user has no parent table access privileges Refer https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-errors >> * Previously, the ER_NO_REFERENCED_ROW_2 and ER_ROW_IS_REFERENCED_2 error messages for foreign key operations were displayed and revealed information about parent tables, even when the user had no parent table access privileges. Error handling for this situation has been revised: * If the user does have table-level privileges for all parent tables, ER_NO_REFERENCED_ROW_2 and ER_ROW_IS_REFERENCED_2 are displayed, the same as before. * If the user does not have table-level privileges for all parent tables, more generic error messages are displayed instead (ER_NO_REFERENCED_ROW and ER_ROW_IS_REFERENCED). << This pull request addresses these 3 failures: ```ruby $ ARCONN=mysql2 bundle exec ruby -w -Itest test/cases/adapter_test.rb -n /foreign/ Using mysql2 Run options: -n /foreign/ --seed 14251 F Failure: ActiveRecord::AdapterForeignKeyTest#test_foreign_key_violations_are_translated_to_specific_exception_with_validate_false [test/cases/adapter_test.rb:348]: [ActiveRecord::InvalidForeignKey] exception expected, not Class: <ActiveRecord::StatementInvalid> Message: <"Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails: INSERT INTO `fk_test_has_fk` (`fk_id`) VALUES (1231231231)"> ... snip ... rails test test/cases/adapter_test.rb:343 F Failure: ActiveRecord::AdapterForeignKeyTest#test_foreign_key_violations_on_delete_are_translated_to_specific_exception [test/cases/adapter_test.rb:368]: [ActiveRecord::InvalidForeignKey] exception expected, not Class: <ActiveRecord::StatementInvalid> Message: <"Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails: DELETE FROM fk_test_has_pk WHERE pk_id = 1"> ... snip ... rails test test/cases/adapter_test.rb:365 F Failure: ActiveRecord::AdapterForeignKeyTest#test_foreign_key_violations_on_insert_are_translated_to_specific_exception [test/cases/adapter_test.rb:358]: [ActiveRecord::InvalidForeignKey] exception expected, not Class: <ActiveRecord::StatementInvalid> Message: <"Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails: INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"> ... snip ... rails test test/cases/adapter_test.rb:357 Finished in 0.087370s, 34.3366 runs/s, 34.3366 assertions/s. 3 runs, 3 assertions, 3 failures, 0 errors, 0 skips $ ```
* | | | Reduce string allocations in read/write_attributeEugene Kenny2018-10-212-8/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When `attr_name` is passed as a symbol, it's currently converted to a string by `attribute_alias?`, and potentially also `attribute_alias`, as well as by the `read_attribute`/`write_attribute` method itself. By converting `attr_name` to a string up front, the extra allocations related to attribute aliases can be avoided.
* | | | Don't expose internal `get_value`/`set_value` methodsRyuta Kamizono2018-10-182-11/+9
| |/ / |/| |
* | | Merge pull request #34197 from schneems/schneems/symbol-hash-respond_toRichard Schneeman2018-10-173-12/+11
|\ \ \ | | | | | | | | ActiveRecord#respond_to? No longer allocates strings
| * | | ActiveRecord#respond_to? No longer allocates stringsschneems2018-10-153-12/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is an alternative to https://github.com/rails/rails/pull/34195 The active record `respond_to?` method needs to do two things if `super` does not say that the method exists. It has to see if the "name" being passed in represents a column in the table. If it does then it needs to pass it to `has_attribute?` to see if the key exists in the current object. The reason why this is slow is that `has_attribute?` needs a string and most (almost all) objects passed in are symbols. The only time we need to allocate a string in this method is if the column does exist in the database, and since these are a limited number of strings (since column names are a finite set) then we can pre-generate all of them and use the same string. We generate a list hash of column names and convert them to symbols, and store the value as the string name. This allows us to both check if the "name" exists as a column, but also provides us with a string object we can use for the `has_attribute?` call. I then ran the test suite and found there was only one case where we're intentionally passing in a string and changed it to a symbol. (However there are tests where we are using a string key, but they don't ship with rails). As re-written this method should never allocate unless the user passes in a string key, which is fairly uncommon with `respond_to?`. This also eliminates the need to special case every common item that might come through the method via the `case` that was originally added in https://github.com/rails/rails/commit/f80aa5994603e684e3fecd3f53bfbf242c73a107 (by me) and then with an attempt to extend in https://github.com/rails/rails/pull/34195. As a bonus this reduces 6,300 comparisons (in the CodeTriage app homepage) to 450 as we also no longer need to loop through the column array to check for an `include?`.
* | | | Remove and flip `index: true` for `references` in the doc [ci skip]Ryuta Kamizono2018-10-172-8/+8
| | | | | | | | | | | | | | | | Follow up #32146.
* | | | Consolidate duplicated code that initializing an empty model objectRyuta Kamizono2018-10-172-20/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `init_with` and `init_from_db` are almost the same code except decode `coder`. And also, named `init_from_db` is a little misreading, a raw values hash from the database is already converted to an attributes object by `attributes_builder.build_from_database`, so passed `attributes` in that method is just an attributes object. I renamed that method to `init_with_attributes` since the method is shared with `init_with` to initialize an empty model object.
* | | | Consistently extract checking version for all adaptersRyuta Kamizono2018-10-174-27/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | I don't prefer to extract it for one adapter even though all adapters also does. Related to #34227.
* | | | Merge pull request #34227 from bkuhlmann/master-lazy_mysql_version_check_supportAaron Patterson2018-10-161-4/+11
|\ \ \ \ | | | | | | | | | | Refactored abstract MySQL adapter to support lazy version check.
| * | | | Refactored abstract MySQL adapter to support lazy version check.Brooke Kuhlmann2018-10-161-4/+11
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Will allow sub classes to override the protected `#check_version` method hook if desired. For example, this will be most helpful in sub classes that wish to support lazy initialization because the version check can be postponed until the connection is ready to be initialized.
* | | | Add regression test against habtm memoized singular_idsAlberto Almagro2018-10-161-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Starting in Rails 5.0.0 and still present in Rails 5.2.1, `singular_ids` got memoized and didn't reload after more items were added to the relation. Although 19c8071 happens to fix the issue, it only adds tests for `has_many` relations while this bug only affected `has_and_belongs_to_many` relations. This commit adds a regression test to ensure it never happens again with `habtm` relations. Ensures #34179 never gets reproduced.
* | | | Merge pull request #33075 from ↵Ryuta Kamizono2018-10-163-2/+22
|\ \ \ \ | |/ / / |/| | | | | | | | | | | | | | | fedxgibson/pg_ambigous_column_cache_key_limit_custom_select Fix Collection cache key with limit and custom select
| * | | Fix Collection cache key with limit and custom select (PG:AmbigousColumn: Error)Federico Martinez2018-10-153-2/+22
|/ / / | | | | | | | | | Change query to use alias name for timestamp_column to avoid ambiguity problems when using timestamp from subquery.
* / / Ensure to test that `project.developers` is ordered by `developers.name desc`Ryuta Kamizono2018-10-151-1/+1
|/ / | | | | | | | | | | | | | | | | `developers.name desc` was added at d59f3a7, but any test case isn't failed even if the `developers.name desc` is removed since all tested developers are consistently ordered on both `name` and `id`. I changed one developers creation ordering to ensure to test that `project.developers` is ordered by `developers.name desc`.
* | Improve model attribute accessor method names for backtracesDylan Thacker-Smith2018-10-123-53/+21
| | | | | | | | | | | | | | | | Ruby uses the original method name, so will show the __temp__ method name in the backtrace. However, in the common case the method name is compatible with the `def` keyword, so we can avoid the __temp__ method name in that case to improve the name shown in backtraces or TracePoint#method_id.
* | Basic API for connection switchingEileen Uchitelle2018-10-104-1/+328
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This PR adds the ability to 1) connect to multiple databases in a model, and 2) switch between those connections using a block. To connect a model to a set of databases for writing and reading use the following API. This API supercedes `establish_connection`. The `writing` and `reading` keys represent handler / role names and `animals` and `animals_replica` represents the database key to look up the configuration hash from. ``` class AnimalsBase < ApplicationRecord connects_to database: { writing: :animals, reading: :animals_replica } end ``` Inside the application - outside the model declaration - we can switch connections with a block call to `connected_to`. If we want to connect to a db that isn't default (ie readonly_slow) we can connect like this: Outside the model we may want to connect to a new database (one that is not in the default writing/reading set) - for example a slow replica for making slow queries. To do this we have the `connected_to` method that takes a `database` hash that matches the signature of `connects_to`. The `connected_to` method also takes a block. ``` AcitveRecord::Base.connected_to(database: { slow_readonly: :primary_replica_slow }) do ModelInPrimary.do_something_thats_slow end ``` For models that are already loaded and connections that are already connected, `connected_to` doesn't need to pass in a `database` because you may want to run queries against multiple databases using a specific role/handler. In this case `connected_to` can take a `role` and use that to swap on the connection passed. This simplies queries - and matches how we do it in GitHub. Once you're connected to the database you don't need to re-connect, we assume the connection is in the pool and simply pass the handler we'd like to swap on. ``` ActiveRecord::Base.connected_to(role: :reading) do Dog.read_something_from_dog ModelInPrimary.do_something_from_model_in_primary end ```