Class: Apartment::Adapters::PostgresqlSchemaAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/apartment/adapters/postgresql_adapter.rb

Overview

Separate Adapter for Postgresql when using schemas

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#default_tenant

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#create, #current_database, #current_tenant, #each, #process, #seed_data, #switch, #switch!

Constructor Details

#initialize(config) ⇒ PostgresqlSchemaAdapter


36
37
38
39
40
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 36

def initialize(config)
  super

  reset
end

Instance Method Details

#currentObject


76
77
78
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 76

def current
  @current || default_tenant
end

#drop(tenant) ⇒ Object

Drop the tenant

@param {String} tenant Database (schema) to drop

46
47
48
49
50
51
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 46

def drop(tenant)
  Apartment.connection.execute(%{DROP SCHEMA "#{tenant}" CASCADE})

rescue *rescuable_exceptions
  raise TenantNotFound, "The schema #{tenant.inspect} cannot be found."
end

#process_excluded_modelsObject

Reset search path to default search_path

Set the table_name to always use the default namespace for excluded models

56
57
58
59
60
61
62
63
64
65
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 56

def process_excluded_models
  Apartment.excluded_models.each do |excluded_model|
    excluded_model.constantize.tap do |klass|
      # Ensure that if a schema *was* set, we override
      table_name = klass.table_name.split('.', 2).last

      klass.table_name = "#{default_tenant}.#{table_name}"
    end
  end
end

#resetObject

Reset schema search path to the default schema_search_path

@return {String} default schema search path

71
72
73
74
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 71

def reset
  @current = default_tenant
  Apartment.connection.schema_search_path = full_search_path
end