lib/penthouse/tenants/schema_tenant.rb in penthouse-0.11.0 vs lib/penthouse/tenants/schema_tenant.rb in penthouse-0.12.0

- old
+ new

@@ -12,22 +12,24 @@ module Penthouse module Tenants class SchemaTenant < BaseTenant include Migratable - attr_accessor :tenant_schema, :persistent_schemas, :default_schema + attr_accessor :tenant_schema, :persistent_schemas, :default_schema, :previous_schema private :tenant_schema=, :persistent_schemas=, :default_schema= # @param identifier [String, Symbol] An identifier for the tenant # @param tenant_schema [String] your tenant's schema name in Postgres # @param persistent_schemas [Array<String>] The schemas you always want in the search path # @param default_schema [String] The global schema name, usually 'public' - def initialize(identifier, tenant_schema:, persistent_schemas: ["shared_extensions"], default_schema: "public") - super(identifier) + # @param previous_schema [String] The previous schema name, usually 'public' unless dealing with nested calls. + def initialize(identifier:, tenant_schema:, persistent_schemas: ["shared_extensions"], default_schema: "public", previous_schema: default_schema) + super self.tenant_schema = tenant_schema.freeze self.persistent_schemas = Array(persistent_schemas).flatten.freeze self.default_schema = default_schema.freeze + self.previous_schema = previous_schema.freeze freeze end # switches to the tenant schema to run the block, ensuring we switch back # afterwards, regardless of whether an exception occurs @@ -39,11 +41,11 @@ # set the search path to include the tenant ActiveRecord::Base.connection.schema_search_path = persistent_schemas.dup.unshift(tenant_schema).join(", ") block.yield(self) ensure # reset the search path back to the default - ActiveRecord::Base.connection.schema_search_path = persistent_schemas.dup.unshift(default_schema).join(", ") + ActiveRecord::Base.connection.schema_search_path = persistent_schemas.dup.unshift(previous_schema).join(", ") end end # creates the tenant schema # @param run_migrations [Boolean] whether or not to run migrations, defaults to Penthouse.configuration.migrate_tenants? @@ -65,10 +67,10 @@ ActiveRecord::Base.connection.exec_query(sql, 'Delete Schema') end # returns whether or not this tenant's schema exists # @return [Boolean] whether or not the tenant exists - def exists? + def exists?(**) sql = ActiveRecord::Base.send(:sanitize_sql_array, ["select 1 from pg_namespace where nspname = '%s'", tenant_schema]) result = ActiveRecord::Base.connection.exec_query(sql, "Schema Exists") !result.rows.empty? end