ankane/multiverse

Rspec Compatablity

Closed this issue · 0 comments

Summary

When in an RSpec example group, the tables on the separate database can't be found.

Illustration

I can access the separate database without a hitch when I boot up rails console:

irb(main):001:0> pp Product.connection

#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x00007fa4a78c9b48
 @config=
  {:adapter=>"postgresql",
   :encoding=>"unicode",
   :pool=>5,
   :database=>"catalog_development"},... 

irb(main):002:0> Product.connection.tables
=> ["schema_migrations", "ar_internal_metadata", "products"]

irb(main):009:0> Product.all
=> #<ActiveRecord::Relation ... [#<Product id: 1, ...

However, in my spec files, though it seems to be connecting to the right database, it can't seem to find the tables:

[1] pry > Product.connection
=> #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x00007f9cac62bfb0
 @config=
  {:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"catalog_test"},...

[2] pry > Product.connection.tables
=> []

[3] pry > Product.all
=> #<Product::ActiveRecord_Relation:0x3fce562b86e4>

[4] pry > Product.create(name: "foo")
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "products" does not exist... (omitted)

Conclusion

I'm not sure what's causing this error and any assistance would be welcome!! I don't think this is related per se to the multiverse gem as I tried to add a second database by hand and ran into the exact same issue. Instead, it may have something to do with the way RSpec interacts with the database. I will continue looking for solutions and post back here if/when I find one that works.

Solution

I needed to run:
$ DB=catalogue rails db:test:prepare