aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
Commit message (Collapse)AuthorAgeFilesLines
* Normalize time value not to be affected by summer timeRyuta Kamizono2018-07-252-10/+40
| | | | Follow up of #33358 for SQLite3.
* Merge pull request #30919 from seanlinsley/17622-before_save_strict_argumentsRyuta Kamizono2018-07-231-0/+27
|\ | | | | | | Add strict argument checking to ActiveRecord callbacks
| * add strict argument checking to ActiveRecord callbacksSean Linsley2018-07-221-0/+23
|/ | | | This ends up adding it to all save-related callbacks defined in `ActiveRecord::DefineCallbacks`, including e.g. `after_create`. Which should be fine: they didn't support `:on` in the first place.
* Replace permissive Mocha expectationsutilum2018-07-222-32/+86
| | | | | | | | | | | | | | | | | | | | | | | | | Step 6 in #33162 When using Mocha like this: `ActiveRecord::Base.expects(:establish_connection).with(some_args)`, the expectations created look something like this: ``` @expectations= [#<Expectation:0x561350d968e0 expected exactly once, not yet invoked: ActiveRecord::Base.establish_connection("adapter" => "mysql2", "database" => nil) >, #<Expectation:0x561350dab8f8 allowed any number of times, not yet invoked: ActiveRecord::Base.establish_connection(any_parameters) >, #<Expectation:0x561350dc30c0 allowed any number of times, not yet invoked: ActiveRecord::Base.connection(any_parameters) >] ``` Minitest mocking (and the way we use it in `MethodCallAssertions`) expressly refuses to facilitate such permissiive expectations, insisting that all calls be specified in the actual expected order. This patch replaces such calls to `Mocha#expects` with `ActiveSupport::Testing::MethodCallAssertions` and specifies all expected calls in the epxected order.
* Replace Mocha#stubs with assert_called_withutilum2018-07-221-6/+11
| | | | A correct, but not obvious use of `ActiveSupport::Testing::MethodCallAssertions`, which might also have been part of #33337 or #33391.
* Fix Mocha replacement that slipped out of #33337utilum2018-07-221-11/+15
|
* tests for use of primary_key with limitBrian Christian2018-07-191-0/+15
|
* Merge pull request #33394 from kamipo/avoid_extra_scopingEileen M. Uchitelle2018-07-192-0/+6
|\ | | | | Avoid extra scoping in delegating to klass methods in the `scope` block
| * Avoid extra scoping in delegating to klass methods in the `scope` blockRyuta Kamizono2018-07-192-0/+6
| | | | | | | | | | | | | | | | | | | | Since #29301, delegating to klass methods in the `scope` block would cause extra scoping by the receiver itself. The extra scoping would always override intermediate scoping like `unscoped` and caused the regression #33387. To keep the original scoping behavior, should avoid the extra scoping in the `scope` block. Fixes #33387.
* | Merge pull request #33391 from utilum/method_call_assertions_instead_of_mochaRyuta Kamizono2018-07-195-98/+154
|\ \ | |/ |/| Use MethodCallAssertions instead of Mocha#expects
| * Use MethodCallAssertions instead of Mocha#expectsutilum2018-07-195-98/+154
| | | | | | | | | | | | | | | | | | | | Many calls to `Mocha#expects` preceded the introduction of `ActiveSupport::Testing::MethodCallAssertions` in 53f64c0fb, and many are simple to replace with `MethodCallAssertions`. This patch makes all these simple replacements. Step 5 in #33162
* | Fix `insert_fixtures_set` to be restored original connection flagsRyuta Kamizono2018-07-191-5/+80
|/ | | | | | #33363 has two regressions. First one is that `insert_fixtures_set` is failed if flags is an array. Second one is that connection flags are not restored if `set_server_option` is not supported.
* Merge pull request #33363 from ahorek/transaction_bugRafael França2018-07-181-0/+15
|\ | | | | use set_server_option if possible
| * use set_server_option if possiblepavel2018-07-151-0/+15
| |
* | Merge pull request #33358 from azbshiri/summer-timeRafael França2018-07-181-2/+24
|\ \ | | | | | | Normalize the date component to 2000-01-01 automatically
| * | Test DST both in local and utc timezoneAlireza Bashiri2018-07-171-2/+24
| | | | | | | | | | | | | | | Also? Updated failing test.
* | | Replace some more Mocha#stub calls with Minitestutilum2018-07-173-25/+46
| | | | | | | | | | | | Missed these in preparing #33337
* | | Remove unnecessary Mocha stubutilum2018-07-171-1/+0
|/ / | | | | | | Should have been removed in #33309.
* | Clarify test casesbogdanvlviv2018-07-153-15/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | Remove extra stub of `ActiveRecord::Base::connection` in `activerecord/test/cases/tasks/mysql_rake_test.rb`. Remove extra stub of `File::exist?` in `activerecord/test/cases/tasks/sqlite_rake_test.rb`. `ActiveRecord::Base::establish_connection` shouldn't return `true` in test cases. Related to https://github.com/rails/rails/pull/33337.
* | Stub with Minitest instead of Mochautilum2018-07-156-461/+699
|/ | | | Step 4 in #33162
* Enable `Layout/EmptyLinesAroundBlockBody` to reduce review cost in the futureRyuta Kamizono2018-07-129-21/+0
| | | | | | | We sometimes ask "✂️ extra blank lines" to a contributor in reviews like https://github.com/rails/rails/pull/33337#discussion_r201509738. It is preferable to deal automatically without depending on manpower.
* 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-092-19/+19
| |
* | Fix default value for mysql time types with specified precisionNikolay Kondratyev2018-07-042-2/+14
|/ | | | | | | | | | | | | | | | 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-193-5/+55
|/ | | | | | | | | | | | | | | | | 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-186-27/+69
| | | | | | | | | | | | | | | | `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-133-0/+38
|\ | | | | | | Allow prefix/suffix options for store accessors
| * Update prefix and allow suffix options for store accessorsYukio Mizuta2018-06-123-0/+38
| |
* | 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-072-0/+21
| | | | | | | | | | | | | | | | | | | | | | 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.