diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 7a223813..fd2ea683 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -97,11 +97,16 @@ def tenant_exists?(tenant) end def create_tenant_command(conn, tenant) - schema = %(BEGIN; - CREATE SCHEMA "#{tenant}"; - COMMIT;) - - conn.execute(schema) + # NOTE: This was causing some tests to fail because of the database strategy for rspec + if ActiveRecord::Base.connection.open_transactions > 0 + conn.execute(%(CREATE SCHEMA "#{tenant}")) + else + schema = %(BEGIN; + CREATE SCHEMA "#{tenant}"; + COMMIT;) + + conn.execute(schema) + end rescue *rescuable_exceptions => e rollback_transaction(conn) raise e