aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
Commit message (Collapse)AuthorAgeFilesLines
* Do not silently fail to generate a cache_versionschneems2018-10-171-0/+8
| | | | | | When an `updated_at` column exists on the model, but is not available on the instance (likely due to a select), we should raise an error rather than silently not generating a cache_version. Without this behavior it's likely that cache entries will not be able to be invalidated and this will happen without notice. This behavior was reported and described by @lsylvester in https://github.com/rails/rails/pull/34197#issuecomment-429668759.
* Use raw time string from DB to generate ActiveRecord#cache_versionschneems2018-10-171-2/+72
| | | | | | | | | | | | | | | | | | | | Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
* 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.
* Fix Collection cache key with limit and custom select (PG:AmbigousColumn: Error)Federico Martinez2018-10-151-0/+14
| | | | 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`.
* Basic API for connection switchingEileen Uchitelle2018-10-101-0/+193
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* Raise on invalid definition valuesAlberto Almagro2018-10-101-0/+11
| | | | | | | | | When defining a Hash enum it can be easy to use [] instead of {}. This commit checks that only valid definition values are provided, those can be a Hash, an array of Symbols or an array of Strings. Otherwise it raises an ArgumentError. Fixes #33961
* Merge pull request #34122 from kamipo/generate_relation_methodsRyuta Kamizono2018-10-104-5/+27
|\ | | | | Generate delegation methods to named scope in the definition time
| * Generate delegation methods to named scope in the definition timeRyuta Kamizono2018-10-094-5/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The delegation methods to named scope are defined when `method_missing` is invoked on the relation. Since #29301, the receiver in the named scope is changed to the relation like others (e.g. `default_scope`, etc) for consistency. Most named scopes would be delegated from relation by `method_missing`, since we don't allow scopes to be defined which conflict with instance methods on `Relation` (#31179). But if a named scope is defined with the same name as any method on the `superclass` (e.g. `Kernel.open`), the `method_missing` on the relation is not invoked. To address the issue, make the delegation methods to named scope is generated in the definition time. Fixes #34098.
* | Merge pull request #34094 from ↵Ryuta Kamizono2018-10-103-0/+89
|\ \ | | | | | | | | | | | | christophemaximin/fix-activerecord-clearing-of-query-cache Fix inconsistent behavior by clearing QueryCache when reloading associations
| * | Clear QueryCache when reloading associationsChristophe Maximin2018-10-103-0/+89
| | |
* | | Call `load_schema` before `assert_no_queries`Ryuta Kamizono2018-10-101-0/+1
| | | | | | | | | | | | | | | | | | | | | Follow up 45be690f8e6db019aac6198ba49d608a2e14824b. `predicate_builder.build` in `where` requires `load_schema` for `type_for_attribute`.
* | | Merge pull request #34081 from gmcgibbon/db_migrate_status_moveEileen M. Uchitelle2018-10-091-1/+23
|\ \ \ | |_|/ |/| | Move db:migrate:status to DatabaseTasks method
| * | Move db:migrate:status to DatabaseTasks methodGannon McGibbon2018-10-081-1/+23
| | |
* | | Call `define_attribute_methods` before `assert_no_queries` to address CI ↵Ryuta Kamizono2018-10-095-9/+80
|/ / | | | | | | | | | | | | | | | | | | | | | | flakiness Follow up 45be690f8e6db019aac6198ba49d608a2e14824b. Somehow calling `define_attribute_methods` in `build`/`new` sometimes causes the `table_exists?` query. To address CI flakiness due to `assert_no_queries` failure, ensure `define_attribute_methods` before `assert_no_queries`.
* | Fix test name to add missing "set"Ryuta Kamizono2018-10-081-1/+1
| |
* | Fix `AssociationRelation` not to set inverse instance key just like beforeRyuta Kamizono2018-10-071-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since #31575, `set_inverse_instance` replaces the foreign key by the current owner immediately to make it happen when a record is added to collection association. But `set_inverse_instance` is not only called when a record is added, but also when a record is loaded from queries. And also, that loaded records are not always associated records for some reason (using `or`, `unscope`, `rewhere`, etc). It is hard to distinguish whether or not we should invoke `set_inverse_instance`, but at least we should avoid the undesired side-effect which was brought from #31575. Fixes #34108.
* | add test for cache_version precisionLachlan Sylvester2018-10-071-31/+55
| |
* | Exercise stringify of database configurationsbogdanvlviv2018-10-051-0/+5
|/ | | | | | | | | | | | | Since #33968 we stringify keys of database configuration This commit adds more assertions in order to ensure that and prevent any regression in the future. Currently, if remove `to_s` added in #33968 from `env_name.to_s` on the line (activerecord/lib/active_record/database_configurations.rb:107), there is no test that would fail. One of the added assertions should emphasize why we need this `to_s`. Follow up #33968
* Remove `ignore_none: false` to assert no queries more strictlyRyuta Kamizono2018-10-057-31/+31
| | | | Follow up 811be477786455d144819a5e9fbb7f9f54b8da69.
* Use `assert_no_queries` not to ignore BEGIN/COMMIT queriesRyuta Kamizono2018-10-0513-38/+37
| | | | | | | | | `test_update_does_not_run_sql_if_record_has_not_changed` would pass without #18501 since `assert_queries` ignores BEGIN/COMMIT unless `ignore_none: true` is given. Since #32647, empty BEGIN/COMMIT is ommited. So we no longer need to use `assert_queries(0)` to ignore BEGIN/COMMIT in the queries.
* failing test for eager loadingMatt Jones2018-10-041-0/+5
| | | | cherry-picked from https://github.com/rails/rails/pull/33763
* Merge pull request #33938 from faucct/bugfix/preload_through_no_recordsEileen M. Uchitelle2018-10-041-0/+5
|\ | | | | ActiveRecord::Associations::Preloader should not fail to preload through missing records
| * ActiveRecord::Associations::Preloader should not fail to preload through ↵Nikita Sokolov2018-10-021-0/+5
| | | | | | | | missing records
* | Escape table name so that it can be used in regular expressionAidan Haran2018-10-041-1/+1
|/
* Merge pull request #23593 from meinac/add_index_option_for_change_tableRyuta Kamizono2018-10-011-0/+8
|\ | | | | | | index option added for change_table migrations
| * Index option added for change_table migrationsMehmet Emin INAC2018-09-221-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In case if we want to add a column into the existing table with index on it, we have to add column and index in two seperate lines. With this feature we don't need to write an extra line to add index for column. We can just use `index` option. Old behaviour in action: ``` change_table(:languages) do |t| t.string :country_code t.index: :country_code end ``` New behaviour in action: ``` change_table(:languages) do |t| t.string :country_code, index: true end ``` Exactly same behaviour is already exist for `create_table` migrations.
* | Handle DELETE with LIMIT in ArelRyuta Kamizono2018-09-301-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | MySQL supports DELETE with LIMIT and ORDER BY. https://dev.mysql.com/doc/refman/8.0/en/delete.html Before: ``` Post Destroy (1.0ms) DELETE FROM `posts` WHERE `posts`.`id` IN (SELECT `id` FROM (SELECT `posts`.`id` FROM `posts` WHERE `posts`.`author_id` = ? ORDER BY `posts`.`id` ASC LIMIT ?) __active_record_temp) [["author_id", 1], ["LIMIT", 1]] ``` After: ``` Post Destroy (0.4ms) DELETE FROM `posts` WHERE `posts`.`author_id` = ? ORDER BY `posts`.`id` ASC LIMIT ? [["author_id", 1], ["LIMIT", 1]] ```
* | `SQLString#compile` is no longer used since ↵Ryuta Kamizono2018-09-301-11/+5
| | | | | | | | 53521a9e39b9d8af4165d7703c36dc905f1f8f67
* | Remove `visit_Fixnum` and `visit_Bignum`Ryuta Kamizono2018-09-301-1/+1
| | | | | | | | Follow up ae406cd633dab2cafbc0d1bb5922d1ca40056ea0.
* | Make `test_initialize_with_invalid_attribute` work correctlyyuuji.yaginuma2018-09-301-4/+6
| | | | | | | | | | | | | | | | Originally specified attributes were only normal values, and `ActiveRecord::MultiparameterAssignmentErrors` did not occur. In addition, an assertion is performed only on rescue, even if an exception does not occur, the test passes. To avoid this use `assert_raise`.
* | Bugfix ActiveRecord::Relation#merge special case of from clauseBogdan Gusiev2018-09-281-0/+4
| | | | | | | | | | When one relation is merged into another that has a different base class merging `from_clause` causes invalid SQL to be generated
* | Merge pull request #31604 from fatkodima/reverting-transactionEileen M. Uchitelle2018-09-273-0/+40
|\ \ | | | | | | Fix `transaction` reverting for migrations
| * | Fix `transaction` reverting for migrationsfatkodima2018-09-263-0/+40
| | | | | | | | | | | | [fatkodima & David Verhasselt]
* | | Avoid extra touch queries when counter cache is updatedRyuta Kamizono2018-09-271-4/+8
| | | | | | | | | | | | Since counter cache handles touch option too.
* | | Use -X when loading structure.sql via psqlJ Smith2018-09-271-3/+3
| | |
* | | Removed invalid -X flag for pg_dumpMatthias Winkelmann2018-09-271-7/+7
|/ /
* | Merge pull request #33983 from gmcgibbon/raise_if_fixture_path_blankRafael França2018-09-261-0/+16
|\ \ | | | | | | Raise an error when loading all fixtures from nil fixture_path
| * | Raise an error when loading all fixtures from nil fixture_pathGannon McGibbon2018-09-261-0/+16
| | | | | | | | | | | | [Gannon McGibbon + Max Albrecht]
* | | Merge pull request #31819 from bpohoriletz/masterEileen M. Uchitelle2018-09-261-0/+26
|\ \ \ | | | | | | | | If association is a hash-like object preloading fails
| * | | If association is a hash-like object preloading failsBohdan Pohorilets2018-09-261-0/+26
| | | | | | | | | | | | | | | | | | | | | | | | If you pass a hash-like object to preload associations (for example ActionController::Parameters) preloader will fail with the ArgumentError. This change allows passing objects that may be converted to a Hash or String into a preloader
* | | | Remove force parent loading when counter cache child is created/destroyedRyuta Kamizono2018-09-261-1/+5
| |/ / |/| | | | | | | | | | | | | | | | | `association.increment_counters` and `association.decrement_counters` works regardless of parent target is loaded or not. Related 52e11e462f6114a4d12225c639c5f501f0ffec7a.
* | | Revert "Remove `counter_cache_target` which is no longer called"Ryuta Kamizono2018-09-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 376ffe0ea2e59dc51461122210729c05a10fb443. Since 38fae1f, `association.increment_counters` is called without inflated parent target if inverse_of is disabled. In that case, that commit would cause extra queries to inflate parent.
* | | Update counter cache in memory if parent target is existedRyuta Kamizono2018-09-261-0/+32
|/ / | | | | | | Fixes #19550.
* | Fix "warning: shadowing outer local variable - config"yuuji.yaginuma2018-09-261-2/+2
| |
* | Merge pull request #33968 from gmcgibbon/stringify_db_configurationsRafael França2018-09-251-0/+24
|\ \ | | | | | | Stringify database configurations
| * | Stringify database configurationsGannon McGibbon2018-09-241-0/+24
| | |
* | | Change the empty block style to have space inside of the blockRafael Mendonça França2018-09-2520-43/+43
| | |
* | | Abandon TOP support.Vladimir Kochnev2018-09-253-4/+2
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Initially, `TOP` was introduced to support `limit` for MSSQL database. Unlike PostgreSQL/MySQL/SQLite, MSSQL does not have native `LIMIT`/`OFFSET` support. The commit adding `TOP` is 1a246f71616cf246a75ef6cbdb56032e43d4e643. However, it figured out that `TOP` implementation was weak and it's not sufficient to also support `OFFSET`, then `TOP` was substituted with `ROW_NUMBER()` subquery in be48ed3071fd6524d0145c4ad3faeb4aafe3eda3. This is a well known trick in MSSQL - https://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server. So now we don't need this `visit_Arel_Nodes_Top` at all. It does nothing useful but also adds an extra space after `SELECT` when `LIMIT` is being used for **any** database.
* | `Persistence#increment!` requires an attribute argument which is incrementedRyuta Kamizono2018-09-241-0/+5
| |