0

Basically, I have a rails app that has one Postgres database as the primary and reads and writes to both it and 4 independent mysql databases. Transactional tests work just fine when testing behavior that only affects the Postgres database, but actions that affect the mysql databases are not wrapped in a transaction.

I can't, for the life of me, figure out how to piggyback on the transaction that's being used for the Postgres database when running my tests.

ActiveRecord models that connect to the Postgres database all inherit from ApplicationRecord which looks like this:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary }
end

And models that interact with the mysql databases inherit from a MysqlRecord that looks like this:

class MysqlRecord < ActiveRecord::Base
  self.abstract_class = true
end

with each mysql database having an additional namespace and base record that the individual models inherit from like this:

class Alpha::Base < MysqlRecord
  self.abstract_class = true
  connects_to database: { writing: :alpha, reading: :alpha }
end

class Beta::Base < MysqlRecord
  self.abstract_class = true
  connects_to database: { writing: :beta, reading: :beta }
end

class Alpha::SomeModel < Alpha::Base
  # ... model logic in here
end

class Beta::OtherModel < Beta::Base
  # ... etc.
end

How can I start transactions on the Mysql databases and set savepoints/call rollback whenever the same is done for the Postgres database? I'm totally okay with a sledgehammer approach that does it for all databases on every test, even if individual mysql databases aren't involved in some of them, I just really want to wrap all the database operations in transactions in the tests.

Some additional notes: this is a rails app that started out on Rails 4 in 2016 and is now on Rails 7.1.3.4 and is using RSpec 3.13 - I originally wrote all of the code that managed the connections to the secondary mysql databases (with a fair amount of duct tape and chewing gum holding things together) and was thrilled to delete all that code to use the built-in multidb support now integrated in rails, and am hoping to add new tests that are somewhat performant as I add additional new features to the codebase.

Even just a hint as to where in the Rails source I can poke around to see where the built in support for this stuff is would be helpful, as I can't seem to find where it goes down.

3
  • 1
    Attach the Mysql databases as foreign in the PostgreSQL database using mysql_fdw and issue all queries against the PostgreSQL database. Commented Jul 29, 2024 at 4:31
  • @Stefanov.sm in my case postgres is managed by heroku, so I don't think that's an option, but that does look like an interesting plugin, thank you!
    – Eric
    Commented Jul 29, 2024 at 15:20
  • Yet ask heroku, maybe they support a bunch of FDW's . Using foreign schemas is quite common in Postgres Commented Jul 29, 2024 at 16:50

1 Answer 1

0

My problem was ultimately caused by the storage engine for the MySQL tables not supporting transactions. I inherited this (20 year old) project and the underlying MySQL databases were using the MyISAM storage engine, which does not support transactions. In the end, I solved my problem by running the following migration on all of the mysql databases, and Rails' built-in transaction support in tests then worked as expected:

    Alpha::Base.connection.tables.each do |table|
      execute "ALTER TABLE #{table} ENGINE=InnoDB"
    end
    
    # and again for each base class
1
  • I'll also add that what I referred to as the "sledgehammer" approach in my question does indeed appear to be how rails handles transactions with multiple databases in tests by default.
    – Eric
    Commented Aug 4, 2024 at 21:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.