Search code examples
ruby-on-railsrubyapartment-gem

rails Apartment gem having trouble with the generic elevator


I've just starting using apartment and I want to develop an elevator that works from a shared secret. However my initial tests have not been promising.

Where I am at right now is that I have the following:

app/middleware/static_elevator.rb

require 'apartment/elevators/generic'

class StaticElevator < Apartment::Elevators::Generic

  # @return {String} - The tenant to switch to
  def parse_tenant_name(request)
    # request is an instance of Rack::Request

    'my_schema_name'
  end
end

With:

config/initializers/apartment.rb

require 'apartment/elevators/generic'
# require 'apartment/elevators/domain'
#require 'apartment/elevators/subdomain'    
#
# Apartment Configuration
#
Apartment.configure do |config|

  config.tenant_names = lambda { Account.pluck(:schema) }

end

Rails.application.config.middleware.use 'StaticElevator'

So in essence each time a request comes in the database uses my_schema_name as the schema. In my controller I have:

class V1::StaffController < ApplicationController
  def index
    @staff = Staff.only_employees
  end
end

And my test is:

require 'rails_helper'

RSpec.describe V1::StaffController, type: :controller do
  context 'no query string' do
    subject { get :index }

    it 'gets only the employees' do
      expect(Staff).to receive(:only_employees).and_return([])
      subject
    end
  end
end

When I run the test I get:

Randomized with seed 63457

V1::StaffController
  no query string
    gets only the employees (FAILED - 1)

Failures:

  1) V1::StaffController no query string gets only the employees
     Failure/Error: expect(Staff).to receive(:only_employees).and_return([])

     ActiveRecord::StatementInvalid:
       PG::UndefinedTable: ERROR:  relation "users" does not exist
       LINE 5:                WHERE a.attrelid = '"users"'::regclass
                                                 ^
       :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                            pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                       FROM pg_attribute a LEFT JOIN pg_attrdef d
                         ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                      WHERE a.attrelid = '"users"'::regclass
                        AND a.attnum > 0 AND NOT a.attisdropped
                      ORDER BY a.attnum
     # ./spec/controllers/v1/staff_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # PG::UndefinedTable:
     #   ERROR:  relation "users" does not exist
     #   LINE 5:                WHERE a.attrelid = '"users"'::regclass
     #                                             ^
     #   ./spec/controllers/v1/staff_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

Top 1 slowest examples (0.01605 seconds, 94.7% of total time):
  V1::StaffController no query string gets only the employees
    0.01605 seconds ./spec/controllers/v1/staff_controller_spec.rb:7

Finished in 0.01694 seconds (files took 4.37 seconds to load)
1 example, 1 failure

Which says to me the schema isn't running. Am I correct? If so what am I doing wrong?


Solution

  • So it seems that even if you have the schema in the database already you need a schema.rb for rspec to work with. Once I provided a db/schema.rb suddenly all was well with the world and apartmant (and it's attendant tests) ran as expected.

    You can at least partly chalk this one up to operator overconfidence :)