aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #35089 from ↵Eileen M. Uchitelle2019-02-041-0/+38
|\ | | | | | | | | eileencodes/fix-query-cache-for-database-switching Invalidate all query caches for current thread
| * Invalidate query cache for all connections in the current threadEileen Uchitelle2019-02-011-0/+38
| | | | | | | | | | | | This change ensures that all query cahces are cleared across all connections per handler for the current thread so if you write on one connection the read will have the query cache cleared.
* | Merge pull request #35132 from ↵Eileen M. Uchitelle2019-02-042-0/+47
|\ \ | | | | | | | | | | | | eileencodes/allow-application-to-change-handler-names Add ability to change the names of the default handlers
| * | Add ability to change the names of the default handlersEileen Uchitelle2019-02-012-0/+47
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When I wrote the `connected_to` and `connects_to` API's I wrote them with the idea in mind that it didn't really matter what the handlers/roles were called as long as those connecting to the roles knew which one wrote and which one read. With the introduction of the middleware Rails begins to assume it's `writing` and `reading` and there's no room for other roles. At GitHub we've been using this method for a long time so we have a ton of legacy code that uses different handler names `default` and `readonly`. We could rename all our code but I think this is better for a few reasons: - Legacy apps that have been using multiple databases for a long time can have an eaiser time switching. - If we later find this to cause more issues than it's worth we can easily deprecate. - We won't force old apps to rewrite the resolver middleware just to use a different handler. Adding the writing_role/reading_role required that I move the code that creates the first handler for writing to the railtie. If I didn't move this the core class would assign the handler before I was able to assign a new one in my configuration and I'd end up with 3 handlers instead of 2.
* | Merge pull request #35130 from rails/move-delay-to-options-argumentEileen M. Uchitelle2019-02-011-0/+48
|\ \ | | | | | | Refactor options for database selector middleware
| * | Refactor options for middlewareEileen Uchitelle2019-02-011-0/+48
| |/ | | | | | | | | | | | | | | | | | | Right now we only have one option that's supported, the delay. However I can see us supporting other options in the future. This PR refactors the options to get passed into the resolver so whether you're using middleware or using the config options you can pass options to the resolver. This will also make it easy to add new options in the future.
* | Merge pull request #35082 from Shopify/eagerly-materialize-test-transactionsRafael França2019-02-011-1/+1
|\ \ | |/ |/| Eagerly materialize the fixtures transaction
| * Eagerly materialize the fixtures transactionJean Boussier2019-01-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The transaction used to restore fixtures is an implementation detail that should be abstracted away. Idealy a test should behave the same wether or not transactional fixtures are enabled. However since transactions have been made lazy, the fixture transaction started leaking into tests case. e.g. consider the following (oversimplified) test: ```ruby class SQLSubscriber attr_accessor :sql def initialize @sql = [] end def call(*, event) sql << event[:sql] end end subscriber = SQLSubscriber.new ActiveSupport::Notifications.subscribe("sql.active_record", subscriber) User.connection.execute('SELECT 1', 'Generic name') assert_equal ['SELECT 1'], subscriber.sql ``` On Rails 6 it starts to break because the `sql` array will be `['BEGIN', 'SELECT 1']`. Several things are wrong here: - That transaction is not generated by the tested code, so it shouldn't be visible. - The transaction is not even closed yet, which again doesn't reflect the reality.
* | Revert "Merge pull request #33729 from kddeisz/plural-automatic-inverse"Ryuta Kamizono2019-02-011-12/+0
| | | | | | | | | | | | | | This reverts commit ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e, reversing changes made to 3d2caab7dc92a13d4dd369678d5b4ce659df8e52. Reason: 7c3da6e0030aa080fcb89af58b094ed50d861a44
* | Add regression test for has_many through record creationRyuta Kamizono2019-02-011-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | #33729 affected the behavior of the has_many through record creation. Since #33729, the intermediate reflection of simple has_many through association has `inverse_of` to the association, it causes extra through record creation, the extra through record required valid before the association record is saved. https://github.com/rails/rails/blob/23125378673bcc606b274027666a126573e136f8/activerecord/lib/active_record/associations/has_many_through_association.rb#L95-L102 I think that #33729 need to more work to care about has_many through association, that PR should be reverted to not break existing apps.
* | Write update_last_write_timestamp after requestJohn Hawthorn2019-01-301-0/+37
| | | | | | | | | | | | | | | | | | We need to update using the timestamp from the end of the request, not the start. For example, if a request spends 5+ seconds writing, we still want to wait another 5 seconds for replication lag. Since we now run the update after we yield, we need to use ensure to make sure we update the timestamp even if there is an exception.
* | Merge pull request #35073 from eileencodes/db-selectionEileen M. Uchitelle2019-01-301-0/+77
|\ \ | | | | | | Part 8: Multi db improvements, Adds basic automatic database switching to Rails
| * | Adds basic automatic database switching to RailsEileen Uchitelle2019-01-301-0/+77
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The following PR adds behavior to Rails to allow an application to automatically switch it's connection from the primary to the replica. A request will be sent to the replica if: * The request is a read request (`GET` or `HEAD`) * AND It's been 2 seconds since the last write to the database (because we don't want to send a user to a replica if the write hasn't made it to the replica yet) A request will be sent to the primary if: * It's not a GET/HEAD request (ie is a POST, PATCH, etc) * Has been less than 2 seconds since the last write to the database The implementation that decides when to switch reads (the 2 seconds) is "safe" to use in production but not recommended without adequate testing with your infrastructure. At GitHub in addition to the a 5 second delay we have a curcuit breaker that checks the replication delay and will send the query to a replica before the 5 seconds has passed. This is specific to our application and therefore not something Rails should be doing for you. You'll need to test and implement more robust handling of when to switch based on your infrastructure. The auto switcher in Rails is meant to be a basic implementation / API that acts as a guide for how to implement autoswitching. The impementation here is meant to be strict enough that you know how to implement your own resolver and operations classes but flexible enough that we're not telling you how to do it. The middleware is not included automatically and can be installed in your application with the classes you want to use for the resolver and operations passed in. If you don't pass any classes into the middleware the Rails default Resolver and Session classes will be used. The Resolver decides what parameters define when to switch, Operations sets timestamps for the Resolver to read from. For example you may want to use cookies instead of a session so you'd implement a Resolver::Cookies class and pass that into the middleware via configuration options. ``` config.active_record.database_selector = { delay: 2.seconds } config.active_record.database_resolver = MyResolver config.active_record.database_operations = MyResolver::MyCookies ``` Your classes can inherit from the existing classes and reimplment the methods (or implement more methods) that you need to do the switching. You only need to implement methods that you want to change. For example if you wanted to set the session token for the last read from a replica you would reimplement the `read_from_replica` method in your resolver class and implement a method that updates a new timestamp in your operations class.
* / Fix case when we want a UrlConfig but the URL is nilEileen Uchitelle2019-01-301-0/+8
|/ | | | | | | | | | | | | | | | | | | | Previously if the `url` key in a config hash was nil we'd ignore the configuration as invalid. This can happen when you're relying on a `DATABASE_URL` in the env and that is not set in the environment. ``` production: <<: *default url: ENV['DATABASE_URL'] ``` This PR fixes that case by checking if there is a `url` key in the config instead of checking if the `url` is not nil in the config. In addition to changing the conditional we then need to build a url hash to merge with the original hash in the `UrlConfig` object. Fixes #35091
* Allow changing text and blob size without giving the `limit` optionRyuta Kamizono2019-01-292-9/+27
| | | | | | | | | | | | | | In MySQL, the text column size is 65,535 bytes by default (1 GiB in PostgreSQL). It is sometimes too short when people want to use a text column, so they sometimes change the text size to mediumtext (16 MiB) or longtext (4 GiB) by giving the `limit` option. Unlike MySQL, PostgreSQL doesn't allow the `limit` option for a text column (raises ERROR: type modifier is not allowed for type "text"). So `limit: 4294967295` (longtext) couldn't be used in Action Text. I've allowed changing text and blob size without giving the `limit` option, it prevents that migration failure on PostgreSQL.
* PostgreSQL: Use native timestamp decoders of pg-1.1Lars Kanis2019-01-261-3/+3
| | | | | This improves performance of timestamp conversion and avoids additional string allocations.
* Make `t.timestamps` with precision by defaultRyuta Kamizono2019-01-262-0/+122
|
* Fix `t.timestamps` missing `null: false` in `change_table bulk: true`Ryuta Kamizono2019-01-262-0/+32
|
* Allow `column_exists?` giving options without typeRyuta Kamizono2019-01-262-12/+12
|
* Merge pull request #35042 from eileencodes/fix-error-message-for-missing-handlerEileen M. Uchitelle2019-01-252-3/+11
|\ | | | | Fix error raised when handler doesn't exist
| * Fix error raised when handler doesn't existEileen Uchitelle2019-01-252-3/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | While working on another feature for multiple databases (auto-switching) I observed that in development the first request won't autoload the application record connection for the primary database and may not yet know about the replica connection. In my test application this caused the application to thrown an error if I tried to send the first request to the replica before the replica was connected. This wouldn't be an issue in production because the application is preloaded. In order to fix this I decided to leave the original error message and delete the new error message. I updated the original error message to include the `role` to make it a bit clearer that the connection isn't established for that particular role. The error now reads: ``` No connection pool with 'primary' found for the 'reading' role. ``` A single database application will continue uisng the original error message: ``` No connection pool with 'primary' found. ```
* | Make `And` and `Case` into expression nodesKevin Deisz2019-01-241-0/+9
|/ | | | Allows aliasing, predications, ordering, and various other functions on `And` and `Case` nodes. This brings them in line with other nodes like `Binary` and `Unary`.
* Allow `column_exists?` to be passed `type` argument as a stringRyuta Kamizono2019-01-241-9/+4
| | | | | | | | | | | Currently `conn.column_exists?("testings", "created_at", "datetime")` returns false even if the table has the `created_at` column. That reason is that `column.type` is a symbol but passed `type` is not normalized to symbol unlike `column_name`, it is surprising behavior to me. I've improved that to normalize a value before comparison.
* activerecord: Fix statement cache for strictly cast attributesDylan Thacker-Smith2019-01-231-0/+6
|
* Merge pull request #35006 from kddeisz/alias-case-nodesRyuta Kamizono2019-01-221-0/+10
|\ | | | | Alias case nodes
| * Alias case nodesKevin Deisz2019-01-211-0/+10
| | | | | | | | When `Arel` was merged into `ActiveRecord` we lost the ability to alias case nodes. This adds it back.
* | Fix year value when casting a multiparameter time hashAndrew White2019-01-211-0/+22
|/ | | | | | | | | | | | | | | | | | | | | | | When assigning a hash to a time attribute that's missing a year component (e.g. a `time_select` with `:ignore_date` set to `true`) then the year defaults to 1970 instead of the expected 2000. This results in the attribute changing as a result of the save. Before: event = Event.new(start_time: { 4 => 20, 5 => 30 }) event.start_time # => 1970-01-01 20:30:00 UTC event.save event.reload event.start_time # => 2000-01-01 20:30:00 UTC After: event = Event.new(start_time: { 4 => 20, 5 => 30 }) event.start_time # => 2000-01-01 20:30:00 UTC event.save event.reload event.start_time # => 2000-01-01 20:30:00 UTC
* Fix type casting column default in `change_column`Ryuta Kamizono2019-01-201-1/+13
| | | | | | | | | | | | | | Since #31230, `change_column` is executed as a bulk statement. That caused incorrect type casting column default by looking up the before changed type, not the after changed type. In a bulk statement, we can't use `change_column_default_for_alter` if the statement changes the column type. This fixes the type casting to use the constructed target sql_type. Fixes #34938.
* activerecord: Fix where nil condition on composed_of attributeDylan Thacker-Smith2019-01-181-0/+18
|
* Ensure `StatementCache#execute` never raises `RangeError`Ryuta Kamizono2019-01-182-7/+13
| | | | | | | | | | | | | Since 31ffbf8d, finder methods no longer raise `RangeError`. So `StatementCache#execute` is the only place to raise the exception for finder queries. `StatementCache` is used for simple equality queries in the codebase. This means that if `StatementCache#execute` raises `RangeError`, the result could always be regarded as empty. So `StatementCache#execute` just return nil in that range error case, and treat that as empty in the caller side, then we can avoid catching the exception in much places.
* All of queries should return correct result even if including large numberRyuta Kamizono2019-01-184-2/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently several queries cannot return correct result due to incorrect `RangeError` handling. First example: ```ruby assert_equal true, Topic.where(id: [1, 9223372036854775808]).exists? assert_equal true, Topic.where.not(id: 9223372036854775808).exists? ``` The first example is obviously to be true, but currently it returns false. Second example: ```ruby assert_equal topics(:first), Topic.where(id: 1..9223372036854775808).find(1) ``` The second example also should return the object, but currently it raises `RecordNotFound`. It can be seen from the examples, the queries including large number assuming empty result is not always correct. Therefore, This change handles `RangeError` to generate executable SQL instead of raising `RangeError` to users to always return correct result. By this change, it is no longer raised `RangeError` to users.
* Merge pull request #34963 from ↵Rafael França2019-01-171-0/+1
|\ | | | | | | | | dylanahsmith/better-composed-of-single-field-query activerecord: Use a simpler query condition for aggregates with one mapping
| * activerecord: Use a simpler query condition for aggregates with one mappingDylan Thacker-Smith2019-01-171-0/+1
| |
* | Merge pull request #34966 from ↵Rafael Mendonça França2019-01-171-2/+6
|\ \ | | | | | | | | | | | | | | | bogdanvlviv/ensure-ar-relation-exists-allows-permitted-params Ensure that AR::Relation#exists? allows only permitted params
| * | Ensure that AR::Relation#exists? allows only permitted paramsbogdanvlviv2019-01-171-2/+6
| |/ | | | | | | | | Clarify changelog entry Related to #34891
* | Remove deprecated `#set_state` from the transaction objectRafael Mendonça França2019-01-171-11/+0
| |
* | Remove deprecated `#supports_statement_cache?` from the database adaptersRafael Mendonça França2019-01-171-4/+0
| |
* | Remove deprecated `#insert_fixtures` from the database adaptersRafael Mendonça França2019-01-172-22/+0
| |
* | Remove deprecated ↵Rafael Mendonça França2019-01-171-4/+0
| | | | | | | | `ActiveRecord::ConnectionAdapters::SQLite3Adapter#valid_alter_table_type?`
* | Do not allow passing the column name to `sum` when a block is passedRafael Mendonça França2019-01-171-3/+3
| |
* | Do not allow passing the column name to `count` when a block is passedRafael Mendonça França2019-01-171-3/+3
| |
* | Remove delegation of missing methods in a relation to arelRafael Mendonça França2019-01-171-16/+0
| |
* | Remove delegation of missing methods in a relation to private methods of the ↵Rafael Mendonça França2019-01-171-7/+0
| | | | | | | | class
* | Change `SQLite3Adapter` to always represent boolean values as integersRafael Mendonça França2019-01-171-13/+1
| |
* | Remove ability to specify a timestamp name for `#cache_key`Rafael Mendonça França2019-01-171-15/+0
| |
* | Remove deprecated `ActiveRecord::Migrator.migrations_path=`Rafael Mendonça França2019-01-171-10/+0
| |
* | Remove deprecated `expand_hash_conditions_for_aggregates`Rafael Mendonça França2019-01-171-6/+0
|/
* Refs #28025 nullify *_type column on polymorphic associations on :nu… ↵Laerti2019-01-152-0/+34
| | | | | | (#28078) This PR addresses the issue described in #28025. On `dependent: :nullify` strategy only the foreign key of the relation is nullified. However on polymorphic associations the `*_type` column is not nullified leaving the record with a NULL `*_id` but the `*_type` column is present.
* Merge pull request #34891 from gmcgibbon/ac_params_existsRafael França2019-01-141-0/+9
|\ | | | | Allow strong params in ActiveRecord::Base#exists?
| * Merge branch 'master' into ac_params_existsAaron Patterson2019-01-117-15/+39
| |\