Class: Apartment::Adapters::PostgresqlSchemaAdapter

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

Overview

Separate Adapter for Postgresql when using schemas

Direct Known Subclasses

JDBCPostgresqlSchemaAdapter

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#create, #current, #current_database, #process, #seed_data, #switch

Constructor Details

#initialize(config) ⇒ PostgresqlSchemaAdapter

Returns a new instance of PostgresqlSchemaAdapter.



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

def initialize(config)
  super

  reset
end

Instance Method Details

#current_tenantObject



79
80
81
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 79

def current_tenant
  @current_tenant || Apartment.default_schema
end

#drop(tenant) ⇒ Object

Drop the tenant

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


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

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

rescue *rescuable_exceptions
  raise SchemaNotFound, "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


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

def process_excluded_models
  Apartment.excluded_models.each do |excluded_model|
    excluded_model.constantize.tap do |klass|
      # some models (such as delayed_job) seem to load and cache their column names before this,
      # so would never get the default prefix, so reset first
      klass.reset_column_information

      # Ensure that if a schema *was* set, we override
      table_name = klass.table_name.split('.', 2).last

      klass.table_name = "#{Apartment.default_schema}.#{table_name}"
    end
  end
end

#resetObject

Reset schema search path to the default schema_search_path

@return {String} default schema search path


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

def reset
  @current_tenant = Apartment.default_schema
  Apartment.connection.schema_search_path = full_search_path
end