2

postgres スキーマを使用してマルチテナンシー tut に取り組んでいます。

テスト データベース内の各テナントのスキーマは、テストを正しく実行するために、各実行後にクリアする必要があります。

rspec は次のエラーを生成します

An error occurred in an after hook
  PG::Error: ERROR:  syntax error at or near "where"
LINE 5:       where nspowner != (select oid from pg_roles where role...


1) account scoping displays only account A's records
  Failure/Error: let!(:account_a) { FactoryGirl.create(:account_with_schema) }
  Apartment::SchemaExists:
    The schema test1 already exists.
  # ./app/models/subscribem/account.rb:20:in `create_schema'
  # ./spec/support/factories/account_factory.rb:11:in `block (4 levels) in <top (required)>'
  # ./spec/features/accounts/scoping_spec.rb:4:in `block (2 levels) in <top (required)>'

これは、各テスト後にデータベースをクリアしようとするコードです。

spec_helper.rb

 config.after(:each) do
   Apartment::Database.reset
   DatabaseCleaner.clean
   connection = ActiveRecord::Base.connection.raw_connection
   schema = connection.query(%Q{
    SELECT 'drop schema ' || nspname || ' cascade;' 
    from pg_namespace 
    where nspname != 'public' AND 
    where nspowner != (select oid from pg_roles where rolname = 'postgres'); 
   })
   schemas.each do |query|
    connection.query(query.values.first)
   end 
 end
4

1 に答える 1

1

SQL にいくつか問題があるようです。

まず、2 つのWHERE節があります。AND代わりに を使用してAND WHEREください。次に、サブクエリ条件NOT INではなく、おそらく使用する必要があります。!=

試す:

schema = connection.query(%Q{
  SELECT 'drop schema ' || nspname || ' cascade;' 
  from pg_namespace 
  where nspname != 'public' AND 
  nspowner NOT IN (select oid from pg_roles where rolname = 'postgres'); 
})
于 2013-04-26T14:06:59.517 に答える