aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
Commit message (Collapse)AuthorAgeFilesLines
* Consolidate the duplicated code that building range predicateRyuta Kamizono2019-01-085-36/+21
| | | | | This slightly change the code in the Arel to allow +/-INFINITY as open ended since the Active Record expects that behavior. See 5ecbeda.
* :recycle: Fix mysql type map for enum and setbannzai2019-01-082-2/+6
|
* Merge pull request #34773 from ↵Eileen M. Uchitelle2019-01-042-0/+57
|\ | | | | | | | | eileencodes/share-fixture-connections-with-multiple-handlers For fixtures share the connection pool when there are multiple handlers
| * Share the connection pool when there are multiple handlersEileen Uchitelle2019-01-032-0/+57
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In an application that has a primary and replica database the data inserted on the primary connection will not be able to be read by the replica connection. In a test like this: ``` test "creating a home and then reading it" do home = Home.create!(owner: "eileencodes") ActiveRecord::Base.connected_to(role: :default) do assert_equal 3, Home.count end ActiveRecord::Base.connected_to(role: :readonly) do assert_equal 3, Home.count end end ``` The home inserted in the beginning of the test can't be read by the replica database because when the test is started a transasction is opened byy `setup_fixtures`. That transaction remains open for the remainder of the test until we are done and run `teardown_fixtures`. Because the data isn't actually committed to the database the replica database cannot see the data insertion. I considered a couple ways to fix this. I could have written a database cleaner like class that would allow the data to be committed and then clean up that data afterwards. But database cleaners can make the database slow and the point of the fixtures is to be fast. In GitHub we solve this by sharing the connection pool for the replicas with the primary (writing) connection. This is a bit hacky but it works. Additionally since we define `replica? || preventing_writes?` as the code that blocks writes to the database this will still prevent writing on the replica / readonly connection. So we get all the behavior of multiple connections for the same database without slowing down the database. In this PR the code loops through the handlers. If the handler doesn't match the default handler then it retrieves the connection pool from the default / writing handler and assigns the reading handler's connections to that pool. Then in enlist_fixture_connections it maps all the connections for the default handler because all the connections are now available on that handler so we don't need to loop through them again. The test uses a temporary connection pool so we can test this with sqlite3_mem. This adapter doesn't behave the same as the others and after looking over how the query cache test works I think this is the most correct. The issues comes when calling `connects_to` because that establishes new connections and confuses the sqlite3_mem adapter. I'm not entirely sure why but I wanted to make sure we tested all adapters for this change and I checked that it wasn't the shared connection code that was causing issues - it was the `connects_to` code.
* | Merge pull request #34858 from ↵Ryuta Kamizono2019-01-042-7/+3
|\ \ | | | | | | | | | | | | albertoalmagro/make-rails-compatible-accross-ruby-versions Make average compatible across ruby versions
| * | Make average compatible accross Ruby versionsAlberto Almagro2019-01-041-1/+1
| | | | | | | | | | | | | | | | | | | | | Since Ruby 2.6.0 NilClass#to_d is returning `BigDecimal` 0.0, this breaks `average` compatibility with prior Ruby versions. This patch makes `average` return `nil` in all Ruby versions when there are no rows.
| * | Revert "Fix NumericData.average test on ruby 2.6"Alberto Almagro2019-01-041-6/+2
| | | | | | | | | | | | This reverts commit 89b4612ffc97e6648f5cf807906ae210e05acdda.
* | | Merge the redundant `when Symbol` case to the `when String, ...`Ryuta Kamizono2019-01-041-2/+1
| | |
* | | 2x faster `connection.type_cast`Ryuta Kamizono2019-01-041-6/+1
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `nil`, `Numeric`, and `String` are most basic objects which are passed to `type_cast`. But now each `when *types_which_need_no_typecasting` evaluation allocates extra two arrays, it makes `type_cast` slower. The `types_which_need_no_typecasting` was introduced at #15351, but the method isn't useful (never used any adapters) since all adapters (sqlite3, mysql2, postgresql, oracle-enhanced, sqlserver) still overrides the `_type_cast`. Just expanding the method would make the `type_cast` 2x faster. ```ruby module ActiveRecord module TypeCastFast def type_cast_fast(value, column = nil) value = id_value_for_database(value) if value.is_a?(Base) if column value = type_cast_from_column(column, value) end _type_cast_fast(value) rescue TypeError to_type = column ? " to #{column.type}" : "" raise TypeError, "can't cast #{value.class}#{to_type}" end private def _type_cast_fast(value) case value when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data value.to_s when true then unquoted_true when false then unquoted_false # BigDecimals need to be put in a non-normalized form and quoted. when BigDecimal then value.to_s("F") when nil, Numeric, String then value when Type::Time::Value then quoted_time(value) when Date, Time then quoted_date(value) else raise TypeError end end end end conn = ActiveRecord::Base.connection conn.extend ActiveRecord::TypeCastFast Benchmark.ips do |x| x.report("type_cast") { conn.type_cast("foo") } x.report("type_cast_fast") { conn.type_cast_fast("foo") } x.compare! end ``` ``` Warming up -------------------------------------- type_cast 58.733k i/100ms type_cast_fast 101.364k i/100ms Calculating ------------------------------------- type_cast 708.066k (± 5.9%) i/s - 3.583M in 5.080866s type_cast_fast 1.424M (± 2.3%) i/s - 7.197M in 5.055860s Comparison: type_cast_fast: 1424240.0 i/s type_cast: 708066.0 i/s - 2.01x slower ```
* | Merge pull request #33985 from eugeneius/attribute_methods_schema_cacheKasper Timm Hansen2019-01-036-1/+46
|\ \ | | | | | | Only define attribute methods from schema cache
| * | Only define attribute methods from schema cacheEugene Kenny2018-09-286-1/+46
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | To define the attribute methods for a model, Active Record needs to know the schema of the underlying table, which is usually achieved by making a request to the database. This is undesirable behaviour while the app is booting, for two reasons: it makes the boot process dependent on the availability of the database, and it means every new process will make one query for each table, which can cause issues for large applications. However, if the application is using the schema cache dump feature, then the schema cache already contains the necessary information, and we can define the attribute methods without causing any extra database queries.
* | | fix activerecord reaper_testVladimir Dementyev2019-01-021-1/+1
| | |
* | | Merge pull request #34836 from kamipo/class_level_update_without_idsRyuta Kamizono2019-01-022-1/+17
|\ \ \ | |_|/ |/| | Restore an ability that class level `update` without giving ids
| * | Restore an ability that class level `update` without giving idsRyuta Kamizono2019-01-022-1/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | That ability was introduced at #11898 as `Relation#update` without giving ids, so the ability on the class level is not documented and not tested. c83e30d which fixes #33470 has lost two undocumented abilities. One has fixed at 5c65688, but I missed the ability on the class level. Removing any feature should not be suddenly happened in a stable version even if that is not documented. I've restored the ability and added test case to avoid any regression in the future. Fixes #34743.
* | | MariaDB: Remove version checking lower the 5.5.8Ryuta Kamizono2019-01-021-10/+2
| | | | | | | | | | | | Since we already bumped the minimum version of MySQL to 5.5.8 at #33853.
* | | Add assertions for `ActiveRecord::Base.current_role`Ryuta Kamizono2019-01-021-0/+6
| | | | | | | | | | | | Since the `current_role` is public API.
* | | Add test case for `preventing_writes?`Ryuta Kamizono2019-01-022-1/+11
| | | | | | | | | | | | Since the `preventing_writes?` is public API.
* | | Bring ActiveRecord::Core's API document back [ci skip]yuuji.yaginuma2019-01-021-3/+1
|/ / | | | | | | | | If exist `:nodoc:` before method define, it affects all subsequent method definitions.
* | Fix TypeError: no implicit conversion of Arel::Attributes::Attribute into ↵Ryuta Kamizono2019-01-022-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | String properly This reverts 27c6c07 since `arel_attr.to_s` is not right way to avoid the type error. That to_s returns `"#<struct Arel::Attributes::Attribute ...>"`, there is no reason to match the regex to the inspect form. And also, the regex path is not covered by our test cases. I've tweaked the regex for redundant part and added assertions for the regex path.
* | Bump license years for 2019Arun Agrawal2018-12-312-2/+2
| |
* | Use high level API on `migration_context` instead of using low level API ↵Ryuta Kamizono2018-12-282-13/+11
| | | | | | | | | | | | | | | | directly Since `migration_context` has `migrations_paths` itself and provides methods which returning values from parsed migration files, so there is no reason to use the `parse_migration_filename` low level API directly.
* | Deprecate passing `migrations_paths` to ↵Ryuta Kamizono2018-12-284-12/+20
| | | | | | | | | | | | | | `connection.assume_migrated_upto_version` Since #31727, `migrations_paths` in `assume_migrated_upto_version` is no longer used.
* | Remove redundant assignning to `current_env`Ryuta Kamizono2018-12-281-1/+0
| | | | | | | | This was introduced at https://github.com/rails/rails/commit/cfa1df4b07bee5b2bbcbf9edd2ac287b4fb23c18#diff-b36b9c41be30b05dc14d09d7f3b192efR436.
* | Merge pull request #34806 from bogdan/reuse-find-targetRyuta Kamizono2018-12-274-30/+20
|\ \ | | | | | | | | | Reuse AR::Association#find_target method
| * | Reuse AR::Association#find_target methodBogdan Gusiev2018-12-274-29/+21
| | |
* | | No need to handle if FrozenError is availableYasuo Honda2018-12-235-9/+5
| | | | | | | | | | | | | | | | | | | | | Rails 6 requires Ruby 2.5, which introduces `FrozenError` https://docs.ruby-lang.org/en/2.5.0/NEWS.html Related to #31520
* | | Merge pull request #34753 from ↵Eileen M. Uchitelle2018-12-213-0/+19
|\ \ \ | | | | | | | | | | | | | | | | eileencodes/raise-less-confusing-error-if-handler-doesnt-exist Raise helpful error when role doesn't exist
| * | | Raise helpful error when role doesn't existEileen Uchitelle2018-12-213-0/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If you try to call `connected_to` with a role that doesn't have an established connection you used to get an error that said: ``` >> ActiveRecord::Base.connected_to(role: :i_dont_exist) { Home.first } ActiveRecord::ConnectionNotEstablished Exception: No connection pool with 'primary' found. ``` This is confusing because the connection could be established but we spelled the role wrong. I've changed this to raise if the `role` used in `connected_to` doesn't have an associated handler. Users who encounter this should either check that the role is spelled correctly (writin -> writing), establish a connection to that role in the model with connects_to, or use the `database` keyword for the `role`. I think this will provide a less confusing error message for those starting out with multiple databases.
* | | | Merge pull request #34742 from kamipo/row_format_dynamic_by_defaultRyuta Kamizono2018-12-213-7/+40
|\ \ \ \ | | | | | | | | | | MySQL: `ROW_FORMAT=DYNAMIC` create table option by default
| * | | | MySQL: `ROW_FORMAT=DYNAMIC` create table option by defaultRyuta Kamizono2018-12-193-7/+40
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since MySQL 5.7.9, the `innodb_default_row_format` option defines the default row format for InnoDB tables. The default setting is `DYNAMIC`. The row format is required for indexing on `varchar(255)` with `utf8mb4` columns. As long as using MySQL 5.6, CI won't be passed even if MySQL server setting is properly configured the same as MySQL 5.7 (`innodb_file_per_table = 1`, `innodb_file_format = 'Barracuda'`, and `innodb_large_prefix = 1`) since InnoDB table is created as the row format `COMPACT` by default on MySQL 5.6, therefore indexing on string with `utf8mb4` columns aren't succeeded. Making `ROW_FORMAT=DYNAMIC` create table option by default for legacy MySQL version would mitigate the indexing issue on the user side, and it makes CI would be passed on MySQL 5.6 which is configured properly.
* | | | Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin blockRyuta Kamizono2018-12-2125-354/+286
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently we sometimes find a redundant begin block in code review (e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205). I'd like to enable `Style/RedundantBegin` cop to avoid that, since rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5 (https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with that situation than before.
* | | | Module#{attr,attr_accessor,attr_reader,attr_writer} become public since Ruby 2.5Ryuta Kamizono2018-12-213-3/+3
| | | | | | | | | | | | | | | | https://bugs.ruby-lang.org/issues/14132
* | | | Module#{define_method,alias_method,undef_method,remove_method} become public ↵Ryuta Kamizono2018-12-217-13/+13
| | | | | | | | | | | | | | | | | | | | | | | | since Ruby 2.5 https://bugs.ruby-lang.org/issues/14133
* | | | Merge pull request #30973 from k0kubun/prefer-block-parameterRyuta Kamizono2018-12-201-10/+2
|\ \ \ \ | | | | | | | | | | Unify _read_attribute definition to use &block
| * | | | Unify _read_attribute definition to use &blockTakashi Kokubun2018-12-201-10/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Thanks to ko1, passing block parameter to another method is significantly optimized in Ruby 2.5. https://bugs.ruby-lang.org/issues/14045 Thus we no longer need to keep this ugly hack.
* | | | | Require Ruby 2.5 for Rails 6.Kasper Timm Hansen2018-12-192-3/+3
| |/ / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Generally followed the pattern for https://github.com/rails/rails/pull/32034 * Removes needless CI configs for 2.4 * Targets 2.5 in rubocop * Updates existing CHANGELOG entries for fewer merge conflicts * Removes Hash#slice extension as that's inlined on Ruby 2.5. * Removes the need for send on define_method in MethodCallAssertions.
* | | | Use `utf8mb4` charset for internal tables if the row format `DYNAMIC` by defaultRyuta Kamizono2018-12-192-10/+10
| | | | | | | | | | | | | | | | The indexing issue on `utf8mb4` columns is resolved since MySQL 5.7.9.
* | | | Add test case for ce48b5a366482d4b4c4c053e1e39e79d71987197Ryuta Kamizono2018-12-181-0/+7
| | | |
* | | | Enable `Layout/SpaceAfterSemicolon` cop to avoid newly adding odd spacingRyuta Kamizono2018-12-131-3/+3
| | | | | | | | | | | | | | | | Ref https://github.com/rails/rails/commit/59ff1ba30d9f4d34b4d478104cc3f453e553c67a#diff-38fb97fba84b1ef0f311c4110a597c44R35
* | | | Ensure that preventing writes is invoked before `materialize_transactions` ↵Ryuta Kamizono2018-12-122-13/+23
| | | | | | | | | | | | | | | | consistently
* | | | Add AR::Base.connected_to?John Hawthorn2018-12-112-0/+34
| | | | | | | | | | | | | | | | | | | | This can be used to check the currently connected role. It's meant to mirror AR::Base.connected_to
* | | | An empty transaction does not raise the `ReadOnlyError` if preventing writesRyuta Kamizono2018-12-114-3/+13
| | | | | | | | | | | | | | | | | | | | | | | | BEGIN transaction would cause COMMIT or ROLLBACK, so unless COMMIT and ROLLBACK aren't treated as write queries as well as BEGIN, the `ReadOnlyError` would be raised.
* | | | Merge pull request #34666 from vinistock/upgrade_rubocop_and_fix_offensesRafael França2018-12-103-59/+59
|\ \ \ \ | | | | | | | | | | Upgrade Rubocop to 0.61.1 and fix offenses
| * | | | Upgrade Rubocop to 0.61.1 and fix offensesVinicius Stock2018-12-103-59/+59
| | | | |
* | | | | An explain query does not raise the `ReadOnlyError` if preventing writesRyuta Kamizono2018-12-114-3/+11
| | | | |
* | | | | Don't treat begin and rollback transactions as write queriesRyuta Kamizono2018-12-115-6/+17
|/ / / / | | | | | | | | | | | | | | | | Otherwise `save` method would raise the `ReadOnlyError` against `BEGIN` and `ROLLBACK` queries.
* | | | Prevent write queries with prepared statements for mysql2 adapterRyuta Kamizono2018-12-111-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this change, mysql2 adapter with prepared statements won't pass `base_test.rb`. ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/base_test.rb Using mysql2 Run options: --seed 27614 # Running: ....S..............................F Failure: BasicsTest#test_creating_a_record_raises_if_preventing_writes [test/cases/base_test.rb:1493]: ActiveRecord::ReadOnlyError expected but nothing was raised. rails test test/cases/base_test.rb:1492 ...F Failure: BasicsTest#test_deleting_a_record_raises_if_preventing_writes [test/cases/base_test.rb:1513]: ActiveRecord::ReadOnlyError expected but nothing was raised. rails test test/cases/base_test.rb:1510 ............................................................................................................F Failure: BasicsTest#test_updating_a_record_raises_if_preventing_writes [test/cases/base_test.rb:1503]: ActiveRecord::ReadOnlyError expected but nothing was raised. rails test test/cases/base_test.rb:1500 .......... Finished in 2.534490s, 62.7345 runs/s, 149.5370 assertions/s. 159 runs, 379 assertions, 3 failures, 0 errors, 1 skips ```
* | | | Prevent write queries for `exec_query`Ryuta Kamizono2018-12-113-1/+46
| | | | | | | | | | | | | | | | Follow up #34505.
* | | | Merge pull request #34632 from rails/rename-write-on-read-errorEileen M. Uchitelle2018-12-078-17/+21
|\ \ \ \ | | | | | | | | | | Rename error that occurs when writing on a read
| * | | | Rename error that occurs when writing on a readEileen Uchitelle2018-12-078-17/+21
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I originally named this `StatementInvalid` because that's what we do in GitHub, but `@tenderlove` pointed out that this means apps can't test for or explitly rescue this error. `StatementInvalid` is pretty broad so I've renamed this to `ReadOnlyError`.