aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
Commit message (Collapse)AuthorAgeFilesLines
* Don't call after_commit callbacks despite a record isn't savedRyuta Kamizono2019-04-122-4/+26
| | | | | | | | | | | Regardless of a record isn't saved (e.g. validation is failed), `after_commit` / `after_rollback` callbacks are invoked for now. To fix the issue, this adds a record to the current transaction only when a record is actually saved. Fixes #29747. Closes #29833.
* Merge pull request #28155 from lcreid/belongs_toRyuta Kamizono2019-04-105-0/+52
|\ | | | | | | Fix "autosave: true" on belongs_to of join model causes invalid records to be saved
| * Fix circular `autosave: true`Larry Reid2018-07-235-0/+52
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Use a variable local to the `save_collection_association` method in `activerecord/lib/active_record/autosave_association.rb`, instead of an instance variable. Prior to this PR, when there was a circular series of `autosave: true` associations, the callback for a `has_many` association was run while another instance of the same callback on the same association hadn't finished running. When control returned to the first instance of the callback, the instance variable had changed, and subsequent associated records weren't saved correctly. Specifically, the ID field for the `belongs_to` corresponding to the `has_many` was `nil`. Remove unnecessary test and comments. Fixes #28080.
* | Add assertions for lazy sync transaction stateRyuta Kamizono2019-04-101-36/+49
| |
* | Remove duplicated attribute alias resolution in `_select!`Ryuta Kamizono2019-04-091-0/+1
| | | | | | | | This is also resolved in `arel_column`.
* | Merge pull request #34800 from mqchau/mysqlCountDeleteRowInLockMatthew Draper2019-04-091-0/+28
|\ \ | | | | | | Wrap Mysql count of deleted rows in lock block to avoid conflict in test
| * | Wrap Mysql count of deleted rows in lock block to avoid conflict in testQuan Chau2019-04-081-0/+28
| | |
* | | Merge pull request #35887 from kamipo/argument_errorRyuta Kamizono2019-04-096-12/+12
|\ \ \ | | | | | | | | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options
| * | | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other ↵Ryuta Kamizono2019-04-076-12/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | options When I've added new `:size` option in #35071, I've found that invalid `:limit` and `:precision` raises `ActiveRecordError` unlike other invalid options. I think that is hard to distinguish argument errors and statement invalid errors since the `StatementInvalid` is a subclass of the `ActiveRecordError`. https://github.com/rails/rails/blob/c9e4c848eeeb8999b778fa1ae52185ca5537fffe/activerecord/lib/active_record/errors.rb#L103 ```ruby begin # execute any migration rescue ActiveRecord::StatementInvalid # statement invalid rescue ActiveRecord::ActiveRecordError, ArgumentError # `ActiveRecordError` except `StatementInvalid` is maybe an argument error end ``` I'd say this is the inconsistency worth fixing. Before: ```ruby add_column :items, :attr1, :binary, size: 10 # => ArgumentError add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError ``` After: ```ruby add_column :items, :attr1, :binary, size: 10 # => ArgumentError add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError add_column :items, :attr3, :integer, limit: 10 # => ArgumentError add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError ```
* | | | Merge pull request #35892 from ryohashimoto/bulk_insert_logsEileen M. Uchitelle2019-04-081-0/+49
|\ \ \ \ | | | | | | | | | | Improve log messages for #insert_all` / `#upsert_all` etc. methods
| * | | | Improve log messages for #insert_all` / `#upsert_all` / `#insert` / `#upsert ↵Ryo Hashimoto2019-04-081-0/+49
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | etc. methods In #35077, `#insert_all` / `#upsert_all` / `#insert` / `#upsert` etc. methods are added. But Active Record logs only “Bulk Insert” log messages when they are invoked. This commit improves the log messages to use collect words for how invoked them.
* | | | When skipping duplicates in bulk insert on MySQL, avoid assigning id when ↵Bob Lail2019-04-081-0/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | not specified If `id` is an `AUTONUMBER` column, then my former strategy here of assigning `no_op_column` to an arbitrary column would fail in this specific scenario: 1. `model.columns.first` is an AUTONUMBER column 2. `model.columns.first` is not assigned in the insert attributes I added three tests: the first test covers the actual error; the second test documents that this _isn't_ a problem when a value is given for the AUTONUMBER column and the third test ensures that this no-op strategy isn't secretly doing an UPSERT.
* | | | Fix GROUP BY with calculate longer name field to respect `table_alias_length`Ryuta Kamizono2019-04-082-0/+12
|/ / / | | | | | | | | | Follow up of c9e4c848eeeb8999b778fa1ae52185ca5537fffe.
* | | There is no need to check `null_relation?` in `empty_scope?`Ryuta Kamizono2019-04-062-0/+7
| | | | | | | | | | | | `values[:extending]` includes `NullRelation` if `null_relation?`.
* | | Association loading isn't to be affected by null relation scopingRyuta Kamizono2019-04-061-0/+24
| | | | | | | | | | | | | | | | | | Follow up of #35868. Closes #19349.
* | | Merge pull request #35868 from ↵Ryuta Kamizono2019-04-062-7/+20
|\ \ \ | | | | | | | | | | | | | | | | kamipo/association_isnt_to_be_affected_by_scoping_consistently Association loading isn't to be affected by scoping consistently
| * | | Association loading isn't to be affected by scoping consistentlyRyuta Kamizono2019-04-052-7/+20
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow-up of 5c71000, #29834, and #30271. Currently, preloading and eager loading are not to be affected by scoping, with the exception of `unscoped`. But non eager loaded association access is still affected by scoping. Although this is a breaking change, the association loading will work consistently whether preloaded / eager loaded or not. Before: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => nil Comment.preload(:post).find(1).post # => #<Post id: 1, ...> Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...> end ``` After: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => #<Post id: 1, ...> Comment.preload(:post).find(1).post # => #<Post id: 1, ...> Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...> end ``` Fixes #34638. Fixes #35398.
* | | Fix typo for touch later test description. laster -> laterAbhay Nikam2019-04-051-1/+1
| | |
* | | Add missing `touch_all` delegation to relationRyuta Kamizono2019-04-052-1/+33
|/ / | | | | | | Follow up of #31513.
* | Remove duplicated `test_find_last`Ryuta Kamizono2019-04-051-5/+0
| | | | | | | | This is completely same with `test_last`.
* | Stash `left_joins` into `joins` to deduplicate redundant LEFT JOINRyuta Kamizono2019-04-053-2/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Originally the `JoinDependency` has the deduplication for eager loading (LEFT JOIN). This re-uses that deduplication for `left_joins`. And also, This makes left join order into part of joins, i.e.: Before: ``` association joins -> stash joins (eager loading, etc) -> string joins -> left joins ``` After: ``` association joins -> stash joins (eager loading, left joins, etc) -> string joins ``` Now string joins are able to refer left joins. Fixes #34325. Fixes #34332. Fixes #34536.
* | Merge pull request #35698 from mtsmfm/output-test-reportMatthew Draper2019-04-041-0/+2
|\ \ | | | | | | Output junit format test report
| * | Output junit format test reportFumiaki MATSUSHIMA2019-04-041-0/+2
| | |
* | | Fix `count(:all)` with eager loading and explicit select and orderRyuta Kamizono2019-04-042-0/+12
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This follows up ebc09ed9ad9a04338138739226a1a92c7a2707ee. We've still experienced a regression for `size` (`count(:all)`) with eager loading and explicit select and order when upgrading Rails to 5.1. In that case, the eager loading enforces `distinct` to subselect but still keep the custom select, it would cause the ORDER BY with DISTINCT issue. ``` % ARCONN=postgresql bundle exec ruby -w -Itest test/cases/relations_test.rb -n test_size_with_eager_loading_and_custom_select_and_order Using postgresql Run options: -n test_size_with_eager_loading_and_custom_select_and_order --seed 8356 # Running: E Error: RelationTest#test_size_with_eager_loading_and_custom_select_and_order: ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list LINE 1: ..." ON "comments"."post_id" = "posts"."id" ORDER BY comments.i... ^ ``` As another problem on `distinct` is enforced, the result of `count` becomes fewer than expected if `select` is given explicitly. e.g. ```ruby Post.select(:type).count # => 11 Post.select(:type).distinct.count # => 3 ``` As long as `distinct` is enforced, we need to care to keep the result of `count`. This fixes both the `count` with eager loading problems.
* | Optimizer hints should be applied on Top level query as much as possibleRyuta Kamizono2019-04-042-0/+16
| | | | | | | | | | I've experienced this issue in our app, some hints only works on Top level query (e.g. `MAX_EXECUTION_TIME`).
* | Don't drop internal metadata tablesRyuta Kamizono2019-04-041-4/+2
| | | | | | | | | | | | | | | | Some tests expects that internal metadata tables exists, and we should not use `create_table` in transactional tests, since DDL in MySQL causes implicit commit. https://travis-ci.org/rails/rails/jobs/515438937#L3829
* | Ensure `reset_table_name` when table name prefix/suffix is changedRyuta Kamizono2019-04-044-17/+15
| | | | | | | | | | Also, `reset_column_information` is unnecessary since `reset_table_name` does that too.
* | Clear query cache when truncate table(s)Ryuta Kamizono2019-04-041-8/+36
| |
* | Fix fragile testsRyuta Kamizono2019-04-041-2/+4
| |
* | Respect table name prefix/suffix for `truncate_all`Ryuta Kamizono2019-04-042-1/+59
| |
* | Merge pull request #35795 from alimi/cache-database-versionEileen M. Uchitelle2019-04-038-11/+34
|\ \ | | | | | | Cache database version in schema cache
| * | Cache database version in schema cacheAli Ibrahim2019-04-038-11/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * The database version will get cached in the schema cache file during the schema cache dump. When the database version check happens, the version will be pulled from the schema cache and thus avoid querying the database for the version. * If the schema cache file doesn't exist, we'll query the database for the version and cache it on the schema cache object. * To facilitate this change, all connection adapters now implement #get_database_version and #database_version. #database_version returns the value from the schema cache. * To take advantage of the cached database version, the database version check will now happen after the schema cache is set on the connection in the connection pool.
* | | Use official database name [ci skip]Ryuta Kamizono2019-04-031-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | * s/Postgres/PostgreSQL/ * s/MYSQL/MySQL/, s/Mysql/MySQL/ * s/Sqlite/SQLite/ Replaced all newly added them after 6089b31.
* | | Add `after_save_commit` callback as shortcut for `after_commit :hook, on: [ ↵David Heinemeier Hansson2019-04-021-0/+12
|/ / | | | | | | :create, :update ]`. (#35804)
* | Revert unused code and re-using query annotation for `update_all` and ↵Ryuta Kamizono2019-04-017-112/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `delete_all` This partly reverts #35617. #35617 includes unused code (for `InsertStatement`) and re-using query annotation for `update_all` and `delete_all`, which has not been discussed yet. If a relation has any annotation, I think it is mostly for SELECT query, so re-using annotation by default is not always desired behavior for me. We should discuss about desired behavior before publishing the implementation.
* | Extract insert test case from #35686Kasper Timm Hansen2019-03-311-0/+14
| |
* | Merge pull request #19333 from palkan/dirty-storeKasper Timm Hansen2019-03-312-0/+84
|\ \ | | | | | | Add dirty methods for store accessors
| * | Add saved changes helpers for store accessorsVladimir Dementyev2019-03-251-0/+11
| | |
| * | Add dirty methods for store accessorspalkan2019-03-252-0/+73
| | |
* | | Fix callbacks on has_many :through associations (#33249)Ryan Kerr2019-03-301-0/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When adding a child record via a has_many :through association, build_through_record would previously build the join record, and then assign the child record and source_type option to it. Because the before_add and after_add callbacks are called as part of build, however, this caused the callbacks to receive incomplete records, specifically without the other end of the has_many :through association. Collecting all attributes before building the join record ensures the callbacks receive the fully constructed record.
* | | Add `ActiveRecord::Relation#extract_associated` for extracting associated ↵David Heinemeier Hansson2019-03-291-0/+7
| | | | | | | | | | | | | | | record (#35784) * Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation
* | | Type cast falsy boolean symbols on boolean attribute as falseRyuta Kamizono2019-03-301-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before 34cc301, type casting by boolean attribute when querying is a no-op, so finding by truthy boolean string (i.e. `where(value: "true") # => value = 'true'`) didn't work as expected (matches it to FALSE in MySQL #32624). By type casting is ensured, a value on boolean attribute is always serialized to TRUE or FALSE. In PostgreSQL, `where(value: :false) # => value = 'false'` was a valid SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values are serialized as TRUE. I'd say using `:false` is mostly a developer's mistake (user's input basically comes as a string), but `:false` on boolean attribute is serialized as TRUE is not a desirable behavior for anybody. This allows falsy boolean symbols as false, i.e. `klass.create(value: :false).value? # => false` and `where(value: :false) # => value = FALSE`. Fixes #35676.
* | | Merge pull request #35496 from bogdan/right-preloadingRyuta Kamizono2019-03-281-0/+9
|\ \ \ | | | | | | | | Fix preloader to never reset associations in case they are already loaded
| * | | Fix preloader to never reset associations in case they are already loadedBogdan Gusiev2019-03-071-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch fixes the issue when association is preloaded with a custom preload scope which disposes the already preloaded target of the association by reseting it. When custom preload scope is used, the preloading is now performed into a separated Hash - #records_by_owner instead of the association. It removes the necessaty the reset the association after the preloading is complete so that reset of the preloaded association never happens. Preloading is still happening to the association when the preload scope is empty.
* | | | Don't change `collation_connection` in the current connectionRyuta Kamizono2019-03-271-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | We have a test case for `collation_connection` session variable, so it should not be changed in any other test. Fixes #35458.
* | | | Fix CI failure due to remaining tagging recordsRyuta Kamizono2019-03-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `TRUNCATE TABLE posts` also resets `AUTO_INCREMENT`. If newly created a post, it is wrongly associated with remaining tagging records. To un-associate remaining tagging record, use `post.create_tagging!` instead. Fixes #35751.
* | | | Use `assert_queries(0)` instead of `assert_no_queries` to ignore metadata ↵Yasuo Honda2019-03-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | queries Fix #35665 ```ruby $ ARCONN=mysql2 bin/test test/cases/scoping/named_scoping_test.rb test/cases/tasks/database_tasks_test.rb test/cases/associations/cascaded_eager_loading_test.rb test/cases/associations/eager_singularization_test.rb -n "/^(?:NamedScopingTest#(?:test_many_should_not_fire_query_if_scope_loaded)|ActiveRecord::DatabaseTasksDumpSchemaCacheTest#(?:test_dump_schema_cache)|CascadedEagerLoadingTest#(?:test_eager_association_loading_with_has_many_sti_and_subclasses)|EagerSingularizationTest#(?:test_eager_no_extra_singularization_has_many_through_belongs_to))$/" --seed 16818 Using mysql2 Run options: -n "/^(?:NamedScopingTest#(?:test_many_should_not_fire_query_if_scope_loaded)|ActiveRecord::DatabaseTasksDumpSchemaCacheTest#(?:test_dump_schema_cache)|CascadedEagerLoadingTest#(?:test_eager_association_loading_with_has_many_sti_and_subclasses)|EagerSingularizationTest#(?:test_eager_no_extra_singularization_has_many_through_belongs_to))$/" --seed 16818 ...F Failure: CascadedEagerLoadingTest#test_eager_association_loading_with_has_many_sti_and_subclasses [/home/yahonda/git/rails/activerecord/test/cases/associations/cascaded_eager_loading_test.rb:124]: 1 instead of 0 queries were executed. Queries: SHOW FULL FIELDS FROM `topics`. Expected: 0 Actual: 1 bin/test test/cases/associations/cascaded_eager_loading_test.rb:119 Finished in 6.894609s, 0.5802 runs/s, 1.0153 assertions/s. 4 runs, 7 assertions, 1 failures, 0 errors, 0 skips $ ```
* | | | Merge pull request #35683 from Kukunin/masterRyuta Kamizono2019-03-231-6/+31
|\ \ \ \ | | | | | | | | | | Bugfix: Fix false autosave for has_one :through association
| * | | | Fix unintended autosave on has_one through associationSergiy Kukunin2019-03-221-6/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #35680 The problem occurred, when a `has one through` association contains a foreign key (it belongs to the intermediate association). For example, Comment belongs to Post, Post belongs to Author, and Author `has_one :first_comment, through: :first_post`. In this case, the value of the foreign key is comparing with the original record, and since they are likely different, the association is marked as changed. So it updates every time when the origin record updates.
* | | | | Add Relation#annotate for SQL commentingMatt Yoho2019-03-2121-0/+513
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch has two main portions: 1. Add SQL comment support to Arel via Arel::Nodes::Comment. 2. Implement a Relation#annotate method on top of that. == Adding SQL comment support Adds a new Arel::Nodes::Comment node that represents an optional SQL comment and teachers the relevant visitors how to handle it. Comment nodes may be added to the basic CRUD statement nodes and set through any of the four (Select|Insert|Update|Delete)Manager objects. For example: manager = Arel::UpdateManager.new manager.table table manager.comment("annotation") manager.to_sql # UPDATE "users" /* annotation */ This new node type will be used by ActiveRecord::Relation to enable query annotation via SQL comments. == Implementing the Relation#annotate method Implements `ActiveRecord::Relation#annotate`, which accepts a comment string that will be appeneded to any queries generated by the relation. Some examples: relation = Post.where(id: 123).annotate("metadata string") relation.first # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123 # LIMIT 1 /* metadata string */ class Tag < ActiveRecord::Base scope :foo_annotated, -> { annotate("foo") } end Tag.foo_annotated.annotate("bar").first # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */ Also wires up the plumbing so this works with `#update_all` and `#delete_all` as well. This feature is useful for instrumentation and general analysis of queries generated at runtime.