aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/test_fixtures.rb
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Eagerly materialize the fixtures transactionJean Boussier2019-01-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Share the connection pool when there are multiple handlersEileen Uchitelle2019-01-031-0/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Move test_fixtures and render_context to separate filesGannon McGibbon2018-10-031-0/+201