rails 公寓 gem 通用电梯有问题
rails Apartment gem having trouble with the generic elevator
我刚开始使用公寓,我想开发一种使用共享密钥运行的电梯。然而,我的初步测试并不乐观。
我现在所处的位置是:
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
有:
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'
所以基本上每次请求进入数据库时都使用 my_schema_name 作为模式。在我的控制器中我有:
class V1::StaffController < ApplicationController
def index
@staff = Staff.only_employees
end
end
我的测试是:
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
当我 运行 我得到的测试:
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
这对我来说架构不是 运行ning。我对么?如果是这样,我做错了什么?
看来您的测试数据库尚未正确迁移。
你之后会发生什么运行
bundle exec rake db:migrate RAILS_ENV=test
另外:在您的示例中,您只在 parse_tenant_name 中返回一个静态字符串,这应该是动态的,例如,当您将租户名称存储在 request.session[:key].
根据您的配置,此租户名称也应出现在 Account.pluck(:schema) 中。
所以看起来即使您在数据库中已经有了模式,您也需要 schema.rb 才能使用 rspec。一旦我提供了 db/schema.rb 突然一切都很好,世界和公寓(以及它的伴随测试)运行 正如预期的那样。
您至少可以部分地将此归咎于操作员过度自信:)
我刚开始使用公寓,我想开发一种使用共享密钥运行的电梯。然而,我的初步测试并不乐观。
我现在所处的位置是:
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
有:
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'
所以基本上每次请求进入数据库时都使用 my_schema_name 作为模式。在我的控制器中我有:
class V1::StaffController < ApplicationController
def index
@staff = Staff.only_employees
end
end
我的测试是:
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
当我 运行 我得到的测试:
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
这对我来说架构不是 运行ning。我对么?如果是这样,我做错了什么?
看来您的测试数据库尚未正确迁移。
你之后会发生什么运行
bundle exec rake db:migrate RAILS_ENV=test
另外:在您的示例中,您只在 parse_tenant_name 中返回一个静态字符串,这应该是动态的,例如,当您将租户名称存储在 request.session[:key].
根据您的配置,此租户名称也应出现在 Account.pluck(:schema) 中。
所以看起来即使您在数据库中已经有了模式,您也需要 schema.rb 才能使用 rspec。一旦我提供了 db/schema.rb 突然一切都很好,世界和公寓(以及它的伴随测试)运行 正如预期的那样。
您至少可以部分地将此归咎于操作员过度自信:)