aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
Commit message (Collapse)AuthorAgeFilesLines
...
| * | Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a ↵Finn Young2019-02-171-0/+9
| | | | | | | | | | | | single ordering argument.
* | | Fix `order` with custom attributesRyuta Kamizono2019-02-173-2/+13
| | | | | | | | | | | | This follows up 0ee96d13de29680e148ccb8e5b68025f29fd091c.
* | | Merge pull request #35299 from kamipo/fix_mismatched_foreign_keyRyuta Kamizono2019-02-171-3/+80
|\ \ \ | | | | | | | | | | | | Fix the regex that extract mismatched foreign key information
| * | | Fix the regex that extract mismatched foreign key informationRyuta Kamizono2019-02-171-3/+80
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The CI failure for `test_errors_for_bigint_fks_on_integer_pk_table` is due to the poor regex that extract all ``` `(\w+)` ``` like parts from the message (`:foreign_key` should be `"old_car_id"`, but `"engines"`): https://travis-ci.org/rails/rails/jobs/494123455#L1703 I've improved the regex more strictly and have more exercised mismatched foreign key tests. Fixes #35294
* | | Remove `NoForeignKeySupportTest` which is no longer reachedRyuta Kamizono2019-02-162-46/+0
| | | | | | | | | | | | Since #35212, foreign key feature is supported by all adapters.
* | | Merge pull request #35286 from matthewdunbar/masterRyuta Kamizono2019-02-161-0/+12
|\ \ \ | | | | | | | | | | | | Properly handle cached queries with too many bind parameters
| * | | Properly handle cached queries with too many bind parametersMatthew Dunbar2019-02-141-0/+10
| | | |
* | | | Deprecate using class level querying methods if the receiver scope regarded ↵Ryuta Kamizono2019-02-152-14/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | as leaked This deprecates using class level querying methods if the receiver scope regarded as leaked, since #32380 and #35186 may cause that silently leaking information when people upgrade the app. We need deprecation first before making those.
* | | | Revert "Merge pull request #35186 from ↵Ryuta Kamizono2019-02-153-12/+36
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | kamipo/fix_leaking_scope_on_relation_create" This reverts commit b67d5c6dedbf033515a96a95d24d085bf99a0d07, reversing changes made to 2e018361c7c51e36d1d98bf770b7456d78dee68b. Reason: #35186 may cause that silently leaking information when people upgrade the app. We need deprecation first before making this.
* | | Merge pull request #35242 from ↵Eileen M. Uchitelle2019-02-142-43/+117
|\ \ \ | |/ / |/| | | | | | | | eileencodes/add-setter-and-deprecation-for-configurations-hashes Add setter and deprecation for configurations hashes
| * | Improve errors and handling of hashes for database configurationseileencodes2019-02-142-43/+117
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In chat Sam Saffron asked how to use the setter now that configurations is no longer a hash and you need to do AR::Base.configurations["test"]=. Technically you can do `ActiveRecord::Base.configurations = { the hash }` but I realized the old way throws an error and is unintuitive. To aid in the transition from hashes to objects this PR makes a few changes: 1) Re-adds a deprecated hash setter `[]=` that will add a new hash to the configurations list OR replace an existing hash if that environment is already present. This won't be supported in future Rails versions but a good error is important. 2) Changed to throw deprecation warnings on the methods we decided to support for hash conversion and raise on the methods we don't support. 3) Refactored the setter/getter hash deprecation warnings messages and rewrote them. Getters message: ``` DEPRECATION WARNING: `ActiveRecord::Base.configurations` no longer returns a hash. Methods that act on the hash like `values` are deprecated and will be removed in Rails 6.1. Use the `configs_for` method to collect and iterate over the database configurations. ``` Setter message: ``` DEPRECATION WARNING: Setting `ActiveRecord::Base.configurations` with `[]=` is deprecated. Use `ActiveRecord::Base.configurations=` directly to set the configurations instead. ``` 4) Rewrote the legacy configurations test file to test all the public methods in the DatabaseConfigurations class.
* | | Revert "Chaining named scope is no longer leaking to class level querying ↵Ryuta Kamizono2019-02-143-6/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | methods" This reverts #32380, since this may cause that silently leaking information when people upgrade the app. We need deprecation first before making this.
* | | Merge pull request #35244 from palkan/fix/activerecord-database-uri-resolveEileen M. Uchitelle2019-02-131-0/+10
|\ \ \ | | | | | | | | Fix database configurations building when DATABASE_URL present
| * | | Fix database configurations building when DATABASE_URL presentVladimir Dementyev2019-02-121-0/+10
| | | |
* | | | Revert "Merge pull request #35127 from bogdan/counter-cache-loading"Ryuta Kamizono2019-02-131-36/+14
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit eec3e28a1abf75676dcee58308ee5721bb53c325, reversing changes made to 5588fb4802328a2183f4a55c36d6703ee435f85c. Reason: Marking as loaded without actual loading is too greedy optimization. See more context #35239. Closes #35239. [Edouard CHIN & Ryuta Kamizono]
* | | Merge pull request #35237 from eileencodes/fix-teardown-in-db-selector-testEileen M. Uchitelle2019-02-121-0/+4
|\ \ \ | | | | | | | | Fix DatabaseSelector test that fails sometimes
| * | | Fix DatabaseSelector test that fails sometimeseileencodes2019-02-121-0/+4
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On CI we've seen a few test failures when the DatabaseSelectorTest runs before the ConnectionHandlersMultiDbTest. This is because it's creating 2 handlers but not properly tearing them down. Example failure: ``` Failure: ActiveRecord::ConnectionAdapters::ConnectionHandlersMultiDbTest#test_connects_to_with_single_configuration [/rails/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb:241]: Expected: 1 Actual: 2 ```
* / / Fix `pluck` and `select` with custom attributesRyuta Kamizono2019-02-138-7/+31
|/ / | | | | | | | | | | | | | | | | Currently custom attributes are always qualified by the table name in the generated SQL wrongly even if the table doesn't have the named column, it would cause an invalid SQL error. Custom attributes should only be qualified if the table has the same named column.
* | Add `remove_foreign_key` for `change_table`Ryuta Kamizono2019-02-111-0/+11
| |
* | SQLite3: Implement `add_foreign_key` and `remove_foreign_key`Ryuta Kamizono2019-02-114-15/+47
| | | | | | | | | | | | | | | | | | | | | | | | I implemented Foreign key create in `create_table` for SQLite3 at #24743. This follows #24743 to implement `add_foreign_key` and `remove_foreign_key`. Unfortunately SQLite3 has one limitation that `PRAGMA foreign_key_list(table-name)` doesn't have constraint name. So we couldn't implement find/remove foreign key by name for now. Fixes #35207. Closes #31343.
* | More exercise table name prefix and suffix testsRyuta Kamizono2019-02-111-25/+81
| |
* | Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2019-02-101-3/+3
| | | | | | | | https://travis-ci.org/rails/rails/jobs/491045821#L1528-L1531
* | Merge pull request #35203 from chiastolite/add_column_without_column_namesRyuta Kamizono2019-02-101-0/+11
|\ \ | | | | | | | | | Do not allow to add column without column name
| * | Do not allow to add column without column nameHiroyuki Morita2019-02-101-0/+9
|/ /
* | Merge pull request #35193 from kamipo/fix_exists_with_distinct_and_offsetRyuta Kamizono2019-02-081-0/+10
|\ \ | | | | | | Fix `relation.exists?` with giving both `distinct` and `offset`
| * | Fix `relation.exists?` with giving both `distinct` and `offset`Ryuta Kamizono2019-02-081-0/+10
| |/ | | | | | | | | | | | | | | | | | | | | The `distinct` affects (reduces) rows of the result, so it is important part when both `distinct` and `offset` are given. Replacing SELECT clause to `1 AS one` and removing `distinct` and `order` is just optimization for the `exists?`, we should not apply the optimization for that case. Fixes #35191.
* | Merge pull request #35178 from bogdan/has-many-sizeRyuta Kamizono2019-02-081-0/+15
|\ \ | |/ |/| Bugfix has_many association #size when ids reader is cached and assoc…
| * Bugfix has_many association #size when ids reader is cached and association ↵Bogdan Gusiev2019-02-081-0/+15
| | | | | | | | is changed
* | Address `test_belongs_to_does_not_use_order_by` failure due to checking ↵Yasuo Honda2019-02-082-3/+4
| | | | | | | | | | | | | | | | order by for metadata queries Also, `test_has_one_does_not_use_order_by` should not check metadata queries. Fixes #35098
* | Fix `relation.create` to avoid leaking scope to initialization block and ↵Ryuta Kamizono2019-02-073-4/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks `relation.create` populates scope attributes to new record by `scoping`, it is necessary to assign the scope attributes to the record and to find STI subclass from the scope attributes. But the effect of `scoping` is class global, it was caused undesired behavior that pollute all class level querying methods in initialization block and callbacks (`after_initialize`, `before_validation`, `before_save`, etc), which are user provided code. To avoid the leaking scope issue, restore the original current scope before initialization block and callbacks are invoked. Fixes #9894. Fixes #17577. Closes #31526.
* | Fix `CollectionProxy#concat` to return self by alias it to `#<<`Yuya Tanaka2019-02-062-2/+4
| | | | | | | | Formerly it was returning arguments (`records` array).
* | Relation no longer respond to Arel methodsRyuta Kamizono2019-02-062-18/+7
| | | | | | | | This follows up d97980a16d76ad190042b4d8578109714e9c53d0.
* | Merge pull request #32380 from kamipo/fix_leaking_scopeRyuta Kamizono2019-02-062-0/+17
|\ \ | | | | | | Chaining named scope is no longer leaking to class level querying methods
| * | Chaining named scope is no longer leaking to class level querying methodsRyuta Kamizono2019-02-062-0/+17
| |/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Active Record uses `scoping` to delegate to named scopes from relations for propagating the chaining source scope. It was needed to restore the source scope in named scopes, but it was caused undesired behavior that pollute all class level querying methods. Example: ```ruby class Topic < ActiveRecord::Base scope :toplevel, -> { where(parent_id: nil) } scope :children, -> { where.not(parent_id: nil) } scope :has_children, -> { where(id: Topic.children.select(:parent_id)) } end # Works as expected. Topic.toplevel.where(id: Topic.children.select(:parent_id)) # Doesn't work due to leaking `toplevel` to `Topic.children`. Topic.toplevel.has_children ``` Since #29301, the receiver in named scopes has changed from the model class to the chaining source scope, so the polluting class level querying methods is no longer required for that purpose. Fixes #14003.
* / Respect ENV variables when finding DBs etc for the test suiteMatthew Draper2019-02-061-0/+6
|/ | | | | If they're not set we'll still fall back to localhost, but this makes it possible to run the tests against a remote Postgres / Redis / whatever.
* Merge pull request #35127 from bogdan/counter-cache-loadingRyuta Kamizono2019-02-051-5/+35
|\ | | | | Bugfix association loading behavior when counter cache is zero
| * Bugfix association loading behavior when counter cache is zeroBogdan Gusiev2019-02-051-5/+35
| |
* | 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-043-0/+48
|\ \ \ | | | | | | | | | | | | | | | | 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-013-0/+48
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-012-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | #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.