lib/apartment/adapters/postgresql_adapter.rb in ros-apartment-2.9.0 vs lib/apartment/adapters/postgresql_adapter.rb in ros-apartment-2.10.0
- old
+ new
@@ -70,15 +70,13 @@
# Set schema search path to new schema
#
def connect_to_new(tenant = nil)
return reset if tenant.nil?
+ raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless schema_exists?(tenant)
- tenant = tenant.to_s
- raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
-
- @current = tenant
+ @current = tenant.is_a?(Array) ? tenant.map(&:to_s) : tenant.to_s
Apartment.connection.schema_search_path = full_search_path
# When the PostgreSQL version is < 9.3,
# there is a issue for prepared statement with changing search_path.
# https://www.postgresql.org/docs/9.3/static/sql-prepare.html
@@ -147,19 +145,26 @@
# NOTE: due to this https://github.com/rails-on-services/apartment/issues/81
# unreproduceable error we're checking before trying to remove it
c.remove_instance_variable :@sequence_name if c.instance_variable_defined?(:@sequence_name)
end
end
+
+ def schema_exists?(schemas)
+ return true unless Apartment.tenant_presence_check
+
+ Array(schemas).all? { |schema| Apartment.connection.schema_exists?(schema.to_s) }
+ end
end
# Another Adapter for Postgresql when using schemas and SQL
class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter
PSQL_DUMP_BLACKLISTED_STATEMENTS = [
/SET search_path/i, # overridden later
/SET lock_timeout/i, # new in postgresql 9.3
/SET row_security/i, # new in postgresql 9.5
/SET idle_in_transaction_session_timeout/i, # new in postgresql 9.6
+ /SET default_table_access_method/i, # new in postgresql 12
/CREATE SCHEMA public/i,
/COMMENT ON SCHEMA public/i
].freeze
@@ -222,21 +227,21 @@
end
# rubocop:enable Layout/LineLength
# Temporary set Postgresql related environment variables if there are in @config
#
- def with_pg_env(&block)
+ def with_pg_env
pghost = ENV['PGHOST']
pgport = ENV['PGPORT']
pguser = ENV['PGUSER']
pgpassword = ENV['PGPASSWORD']
ENV['PGHOST'] = @config[:host] if @config[:host]
ENV['PGPORT'] = @config[:port].to_s if @config[:port]
ENV['PGUSER'] = @config[:username].to_s if @config[:username]
ENV['PGPASSWORD'] = @config[:password].to_s if @config[:password]
- block.call
+ yield
ensure
ENV['PGHOST'] = pghost
ENV['PGPORT'] = pgport
ENV['PGUSER'] = pguser
ENV['PGPASSWORD'] = pgpassword