aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
Commit message (Collapse)AuthorAgeFilesLines
* Fix stubbed methods in test casesbogdanvlviv2018-07-102-3/+3
| | | | | | | | | | Remove returning of `false` value for stubbed `lock_thread=` methods since there aren't any needs in it. Remove unnecessary returning of `true` for stubbed `drop_database` method. Follow up #33309. Related to #33162, #33326.
* Replace shallow mocks with Ruby classesutilum2018-07-105-18/+47
| | | | | | | While preparing this I realised that some stubbed returns values serve no purpose, so this patch drops those as well. Step 3 in #33162
* Reduce mocking by testing value instead of method callutilum2018-07-093-18/+24
| | | | Step 2 in #33162
* Merge pull request #33309 from utilum/remove_unnecessary_mocha_stubsRafael França2018-07-095-48/+18
|\ | | | | Remove unnecessary Mocha stubs
| * Remove unnecessary Mocha stubsutilum2018-07-075-48/+18
| | | | | | | | | | | | Step 1 in #33162 [utilum + bogdanvlviv]
* | SQLite: Don't leak internal schema objectsRyuta Kamizono2018-07-091-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Related #31201. If creating custom primary key (like a string) in SQLite, it would also create an internal index implicitly which named begin with "sqlite_". It need to be hidden since the internal object names are reserved and prohibited for public use. See https://www.sqlite.org/fileformat2.html#intschema Fixes #33320.
* | Subsecond precision is not supported until MySQL 5.6.4Ryuta Kamizono2018-07-091-13/+13
| |
* | Fix default value for mysql time types with specified precisionNikolay Kondratyev2018-07-041-2/+12
|/ | | | | | | | | | | | | | | | The TIME, DATETIME, and TIMESTAMP types [have supported](https://mariadb.com/kb/en/library/microseconds-in-mariadb/) a fractional seconds precision from 0 to 6. Default values from time columns with specified precision is read as `current_timestamp(n)` from information schema. rake `db:schema:dump` produces `schema.rb` **without** default values for time columns with the specified precision: t.datetime "last_message_at", precision: 6, null: false rake `db:schema:dump` produces `schema.rb` **with** default values for time columns with the specified precision: t.datetime "last_message_at", precision: 6, default: -> { "current_timestamp(6)" }, null: false
* Don't share seen object cache between different join nodes in eager loadingRyuta Kamizono2018-07-031-0/+18
| | | | | | | | | | | Currently, the seen object cache is shared if join nodes have the same target class. But it is a wrong assumption, we can't share the seen object cache between different join nodes (e.g. `:readonly_account` and `:accounts` have the same target class `Account`, but the instances have the different state `readonly`). Fixes #26805. Closes #27737.
* Merge pull request #33242 from brasic/sqlite-readonlyRyuta Kamizono2018-07-031-0/+33
|\ | | | | | | Support readonly option in SQLite3Adapter
| * Support readonly option in SQLite3AdapterCarl Brasic2018-07-021-0/+33
|/ | | | | | | | | | | | | | | | | Readonly sqlite database files are very useful as a data format for storing configuration/lookup data that is too complicated for YAML files. But since such files would typically be committed to a source control repository, it's important to ensure that they are truly safe from being inadvertently modified. Unfortunately using unix permissions isn't enough, as sqlite will "helpfully" add the write bit to a database file whenever it's written to. sqlite3-ruby has supported a `:readonly` option since version 1.3.2 (see https://github.com/sparklemotion/sqlite3-ruby/commit/c20c9f5dd2990042) This simply passes that option through to the adapter if present in the config hash. I think this is best considered an adapter-specific option since no other supported database has an identical concept.
* Merge branch 'master' into homogeneous-allocationAaron Patterson2018-06-261-13/+0
|\ | | | | | | | | | | | | | | * master: Call initialize after allocate Remove `ActiveSupport::Concern` from `ActiveRecord::Aggregations` Add example for no_touching? in active_record/no_touching for api docs [ci skip] Generate a new key for each service test
| * Call initialize after allocateAaron Patterson2018-06-261-13/+0
| | | | | | | | | | | | | | | | If someone calls allocate on the object, they'd better also call an initialization routine too (you can't expect allocate to do any initialization work). Before this commit, AR objects that are instantiated from the database would call `define_attribute_methods` twice.
* | Speed up homogeneous AR lists / reduce allocationsAaron Patterson2018-06-251-0/+5
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit speeds up allocating homogeneous lists of AR objects. We can know if the result set contains an STI column before initializing every AR object, so this change pulls the "does this result set contain an STI column?" test up, then uses a specialized instantiation function. This way we only have to check for an STI column once rather than N times. This change also introduces a new initialization function that is meant for use when allocating AR objects that come from the database. Doing this allows us to eliminate one hash allocation per AR instance. Here is a benchmark: ```ruby require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name t.timestamps null: false end end class User < ActiveRecord::Base; end 2000.times do User.create!(name: "Gorby") end Benchmark.ips do |x| x.report("find") do User.limit(2000).to_a end end ``` Results: Before: ``` [aaron@TC activerecord (master)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb Warming up -------------------------------------- find 5.000 i/100ms Calculating ------------------------------------- find 56.192 (± 3.6%) i/s - 285.000 in 5.080940s ``` After: ``` [aaron@TC activerecord (homogeneous-allocation)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb Warming up -------------------------------------- find 7.000 i/100ms Calculating ------------------------------------- find 72.204 (± 2.8%) i/s - 364.000 in 5.044592s ```
* `references(:developers_projects_join)` isn't needed if using `where` with ↵Ryuta Kamizono2018-06-261-16/+2
| | | | hash condition
* OS X -> macOS [Closes #30313]Xavier Noria2018-06-231-1/+1
| | | | [Jon Moss & Xavier Noria]
* Add missing test case for `find` with a large numberRyuta Kamizono2018-06-221-0/+6
|
* Merge pull request #33074 from lsylvester/optimize-pluckRafael França2018-06-191-0/+12
|\ | | | | Reduce Memory Allocation when using .pluck
| * Avoid creating temporary arrays in ActiveRecord::Result#cast_values in order ↵Lachlan Sylvester2018-06-191-0/+12
| | | | | | | | to speed up pluck
* | Ensure to calculate column aliases after all table aliases are constructedRyuta Kamizono2018-06-191-2/+44
|/ | | | | | | | | | | | | | | | | Currently, column aliases which is used for eager loading are calculated before constructing all table aliases in FROM clause. `JoinDependency#join_constraints` constructs table aliases for `joins` first, and then always re-constructs table aliases for eager loading. If both `joins` and eager loading are given a same table association, the re-construction would cause the discrepancy between column aliases and table aliases. To avoid the discrepancy, the column aliases should be calculated after all table aliases are constructed. Fixes #30603.
* Fix `touch` option to behave consistently with `Persistence#touch` methodRyuta Kamizono2018-06-183-25/+63
| | | | | | | | | | | | | | | | `touch` option was added to `increment!` (#27660) and `update_counters` (#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, #31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
* Merge pull request #29373 from untidy-hair/store_accessor_enhanceRyuta Kamizono2018-06-131-0/+34
|\ | | | | | | Allow prefix/suffix options for store accessors
| * Update prefix and allow suffix options for store accessorsYukio Mizuta2018-06-121-0/+34
| |
* | Add ability to configure cache notifications infoEileen Uchitelle2018-06-121-1/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This may seem like an unnecessary refactoring but some apps want / need to configure the information passed to the query cache logger. In order to do that we can add a method here that can be easily overridden by the app itself, rather than hacking the query cache logger to include that information. To override apps can call ``` def cache_notifications_info super.merge(connected_host: "hostname") end ``` This will take what's already in the query cache logger and add `@something="yea"` to the object. At GitHub we use this to log the number of queries that are cached, the connection host and the connection url.
* | Don't use `target=`Rafael Mendonça França2018-06-111-1/+1
| | | | | | | | | | It mark the association as loaded and this can cause the object to be in an stale state.
* | Use `-=` to change the update the collection on the associationRafael Mendonça França2018-06-111-1/+1
| | | | | | | | | | This also mark the association as loaded given we changed it in memory and avoid the next access to the reader to make a query to the databse.
* | Fix alias confliction when joining same table on has many through with ↵Ryuta Kamizono2018-06-111-0/+4
| | | | | | | | | | | | | | | | | | | | left_joins This regression was caused by #30995 due to `Hash#fetch` won't invoke default proc. Just revert the change since #30995 is completely fixed by e9c1653. Fixes #33048.
* | Fix GROUP BY queries to apply LIMIT/OFFSET after aggregationsRyuta Kamizono2018-06-071-0/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If `eager_loading` is true, `apply_join_dependency` force applies LIMIT/OFFSET before JOINs by `limited_ids_for` to keep parent records count. But for aggregation queries, LIMIT/OFFSET should be applied after aggregations the same as SQL semantics. And also, we could not replace SELECT list by `limited_ids_for` when a query has a GROUP BY clause. It had never been worked since it will causes generating invalid SQL for MySQL, PostgreSQL, and probably most backends. ``` % ARCONN=postgresql be ruby -w -Itest test/cases/calculations_test.rb -n test_group_by_with_limit Using postgresql Run options: -n test_group_by_with_limit --seed 20925 # Running: E Error: CalculationsTest#test_group_by_with_limit: ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "posts.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT DISTINCT "posts"."id", "posts"."type" AS alias_0 FRO... ^ : SELECT DISTINCT "posts"."id", "posts"."type" AS alias_0 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" GROUP BY "posts"."type" ORDER BY "posts"."type" ASC LIMIT $1 ``` Fixes #8103. Closes #27249.
* | Fix `collection.create` to could be rolled back by `after_save`Ryuta Kamizono2018-06-071-0/+11
| | | | | | | | | | | | | | | | | | | | | | In `_create_record`, explicit `transaction` block requires rollback handling manually when `insert_record` is failed. We need to handle it in `_create_record`, not in `insert_record`, since our test cases expect a record added to target and returned even if `insert_record` is failed, Closes #31488.
* | Reuse existing model for testing duplicated children recordsRyuta Kamizono2018-06-071-14/+12
| | | | | | | | Follow up of #32952.
* | Merge pull request #33067 from kamipo/fix_force_equalityRafael França2018-06-061-0/+7
|\ \ | | | | | | Fix force equality checking not to break the serialized attribute with Array
| * | Fix force equality checking not to break the serialized attribute with ArrayRyuta Kamizono2018-06-061-0/+7
| | | | | | | | | | | | Context: https://github.com/rails/rails/commit/43ef00e5d7a55ad79bc840276d33cb70f1f5dde5#commitcomment-29256140
* | | Child joins should be aliased when merging relationsChalo Fernandez2018-06-061-1/+26
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Rails 5.2 does not alias child joins, causing an error about duplicated table/fields: Example: Using some code like: `Post.joins(:author, :categorizations).merge(Author.select(:id)).merge(Categorization.joins(:author))` *Before this fix:* ` SELECT ... FROM "posts" INNER JOIN "authors" ON ... INNER JOIN "authors" ON ... ` *After this fix:* ` SELECT ... FROM "posts" INNER JOIN "authors" ON ... INNER JOIN "authors" "authors_categorizations" ON ... ` Before 5.2, Rails aliased the joins, but wrongfully transformed them into a LEFT OUTER JOIN. This fix will keep them as INNER JOINS, but make sure child joins are aliased, to avoid errors.
* | Merge pull request #33029 from fedxgibson/raise_with_duplicate_columnsEileen M. Uchitelle2018-06-041-0/+11
|\ \ | | | | | | Migrations will raise an exception if there are multiple column defin…
| * | Migrations will raise an exception if there are multiple column definitions ↵Federico Martinez2018-06-011-0/+11
| | | | | | | | | | | | (same name).
* | | Initialization block is a part of `build_record`Ryuta Kamizono2018-06-041-0/+12
| | | | | | | | | | | | Should be done before `before_add` callbacks.
* | | Fix `save` in `after_create_commit` won't invoke extra `after_create_commit`Ryuta Kamizono2018-06-041-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since a record is already persisted in `after_create_commit`, so `save` should invoke only `after_update_commit`. This bug is caused by depending on `@_start_transaction_state` for rollback to consider whether it was `new_record` before being committed. If after commit callbacks caused another commit, the state before last commit is no longer `new_record`. Fixes #32831. Closes #18367. Closes #31106.
* | | `record.destroy` on new record won't invoke after create commit callbacksRyuta Kamizono2018-06-031-0/+8
|/ / | | | | | | Fixes #32806.
* | CI against MariaDB 10.3Yasuo Honda2018-06-011-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - MariaDB 10.3.7 is the first GA release https://mariadb.com/kb/en/library/mariadb-1037-release-notes/ - MariaDB 10.3 translates `LENGTH()` to `OCTET_LENGTH()` function https://mariadb.com/kb/en/library/sql_modeoracle-from-mariadb-103/ > MariaDB translates LENGTH() to OCTET_LENGTH() - MySQL does NOT translate `LENGTH()` to `OCTET_LENGTH()` However, it translates `OCTET_LENGTH()` to `LENGTH()` Here are generated schema dumps of this test to show the differences between MySQL and MariaDB: * MySQL 8.0 (Server version: 8.0.11 MySQL Community Server - GPL) ```ruby create_table \"virtual_columns\", options: \"ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci\", force: :cascade do |t| t.string \"name\" t.virtual \"upper_name\", type: :string, as: \"upper(`name`)\" t.virtual \"name_length\", type: :integer, as: \"length(`name`)\", stored: true t.virtual \"name_octet_length\", type: :integer, as: \"length(`name`)\", stored: true end ``` * Maria DB 10.3 (Server version: 10.3.7-MariaDB-1:10.3.7+maria~bionic-log mariadb.org binary distribution) ```ruby create_table \"virtual_columns\", options: \"ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci\", force: :cascade do |t| t.string \"name\" t.virtual \"upper_name\", type: :string, as: \"ucase(`name`)\" t.virtual \"name_length\", type: :integer, as: \"octet_length(`name`)\", stored: true t.virtual \"name_octet_length\", type: :integer, as: \"octet_length(`name`)\", stored: true end ```
* | Merge pull request #32952 from mechanicles/32940-fixRyuta Kamizono2018-05-291-0/+18
|\ \ | | | | | | | | | Fix parent record should not get saved with duplicate children records
| * | Fix parent record should not get saved with duplicate children recordsSantosh Wadghule2018-05-281-0/+18
| |/ | | | | | | - Fixes #32940
* | Ensure casting by boolean attribute when queryingRyuta Kamizono2018-05-292-43/+43
| | | | | | | | | | | | | | | | | | | | | | `QueryAttribute#value_for_database` calls only `type.serialize`, and `Boolean#serialize` is a no-op unlike other attribute types. It caused the issue #32624. Whether or not `serialize` will invoke `cast` is undefined in our test cases, but it actually does not work properly unless it does so for now. Fixes #32624.
* | Fix: Arel Postgresql visitor generates invalid SQL for GROUPING SETS.david2018-05-281-3/+3
| |
* | Parse raw value only when a value came from user in numericality validatorRyuta Kamizono2018-05-281-1/+4
| | | | | | | | | | | | | | | | Since `parse_raw_value_as_a_number` may not always parse raw value from database as a number without type casting (e.g. "$150.55" as money format). Fixes #32531.
* | Fix that association's after_touch is not called with counter cacheRyuta Kamizono2018-05-271-0/+17
| | | | | | | | | | | | | | | | | | | | | | | | Since #31405, using `#increment!` with touch option instead of `#touch` to touch belongs_to association if counter cache is enabled. It caused the regression since `#increment!` won't invoke after_touch callbacks even if touch option is given. To fix the regression, make `#increment!` invokes after_touch callbacks if touch option is given. Fixes #31559. Fixes #32408.
* | Fix inconsistent touching behavior between assigning and unassigningRyuta Kamizono2018-05-271-0/+25
| | | | | | | | | | | | | | | | | | | | | | On belongs_to with `touch: true` association, unassigned object is caused touching, but assigned object is not touched. And also, if primary key is customized, it will touch against the wrong target looked up by the customized key as primary key. This change ensures correctly touching consistently between assigning and unassigning.
* | Fix `belongs_to_counter_cache_after_update` to respect polymorphic type changeRyuta Kamizono2018-05-271-1/+12
| |
* | Fix `belongs_to_counter_cache_after_update` to respect custom primary key ↵Ryuta Kamizono2018-05-261-1/+7
| | | | | | | | | | | | | | | | | | | | counter If belongs_to primary key is customized, the callback will update counters against the wrong target looked up by the customized key as primary key. We need to convert the customized key into an object that can be referred to as primary key.
* | Fix `different_target?` to respect custom primary key counterRyuta Kamizono2018-05-261-0/+7
| |
* | Eager loading won't mutate owner recordRyuta Kamizono2018-05-251-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since #31575, `BelongsToAssociation#target=` replaces owner record's foreign key to fix an inverse association bug. But the method is not only used for inverse association but also used for eager loading/preloading, it caused some public behavior changes (#32338, #32375). To avoid any side-effect in loading associations, I reverted the overriding `#target=`, then introduced `#inversed_from` to replace foreign key in `set_inverse_instance`. Closes #32375.