Class: Apartment::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/apartment/adapters/abstract_adapter.rb

Overview

Abstract adapter from which all the Apartment DB related adapters will inherit the base logic

Defined Under Namespace

Classes: SeparateDbConnectionHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AbstractAdapter

@constructor

@param {Hash} config Database config


15
16
17
# File 'lib/apartment/adapters/abstract_adapter.rb', line 15

def initialize(config)
  @config = config
end

Instance Attribute Details

#default_tenantObject

Return the original public tenant

@return {String} default tenant name


54
55
56
# File 'lib/apartment/adapters/abstract_adapter.rb', line 54

def default_tenant
  @default_tenant || Apartment.default_tenant
end

Instance Method Details

#create(tenant) ⇒ Object

Create a new tenant, import schema, seed if appropriate

@param {String} tenant Tenant name


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/apartment/adapters/abstract_adapter.rb', line 23

def create(tenant)
  run_callbacks :create do
    create_tenant(tenant)

    switch(tenant) do
      import_database_schema

      # Seed data if appropriate
      seed_data if Apartment.seed_after_create

      yield if block_given?
    end
  end
end

#currentObject

Note alias_method here doesn’t work with inheritence apparently ??



46
47
48
# File 'lib/apartment/adapters/abstract_adapter.rb', line 46

def current
  Apartment.connection.current_database
end

#drop(tenant) ⇒ Object

Drop the tenant

@param {String} tenant name


62
63
64
65
66
67
68
# File 'lib/apartment/adapters/abstract_adapter.rb', line 62

def drop(tenant)
  with_neutral_connection(tenant) do |conn|
    drop_command(conn, tenant)
  end
rescue *rescuable_exceptions => e
  raise_drop_tenant_error!(tenant, e)
end

#each(tenants = Apartment.tenant_names) ⇒ Object

Iterate over all tenants, switch to tenant and yield tenant name



100
101
102
103
104
# File 'lib/apartment/adapters/abstract_adapter.rb', line 100

def each(tenants = Apartment.tenant_names)
  tenants.each do |tenant|
    switch(tenant) { yield tenant }
  end
end

#environmentify(tenant) ⇒ Object

Prepend the environment if configured and the environment isn’t already there

@param {String} tenant Database name
@return {String} tenant name with Rails environment *optionally* prepended


135
136
137
138
139
140
141
142
143
144
145
# File 'lib/apartment/adapters/abstract_adapter.rb', line 135

def environmentify(tenant)
  return tenant if tenant.nil? || tenant.include?(Rails.env)

  if Apartment.prepend_environment
    "#{Rails.env}_#{tenant}"
  elsif Apartment.append_environment
    "#{tenant}_#{Rails.env}"
  else
    tenant
  end
end

#initObject

Initialize Apartment config options such as excluded_models



40
41
42
# File 'lib/apartment/adapters/abstract_adapter.rb', line 40

def init
  process_excluded_models
end

#process_excluded_modelsObject

Establish a new connection for each specific excluded model



108
109
110
111
112
113
114
# File 'lib/apartment/adapters/abstract_adapter.rb', line 108

def process_excluded_models
  # All other models will shared a connection (at Apartment.connection_class)
  # and we can modify at will
  Apartment.excluded_models.each do |excluded_model|
    process_excluded_model(excluded_model)
  end
end

#resetObject

Reset the tenant connection to the default



118
119
120
# File 'lib/apartment/adapters/abstract_adapter.rb', line 118

def reset
  Apartment.establish_connection @config
end

#seed_dataObject Also known as: seed

Load the rails seed file into the db



124
125
126
127
# File 'lib/apartment/adapters/abstract_adapter.rb', line 124

def seed_data
  # Don't log the output of seeding the db
  silence_warnings { load_or_raise(Apartment.seed_data_file) } if Apartment.seed_data_file
end

#switch(tenant = nil) ⇒ Object

Connect to tenant, do your biz, switch back to previous tenant

@param {String?} tenant to connect to


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/apartment/adapters/abstract_adapter.rb', line 86

def switch(tenant = nil)
  previous_tenant = current
  switch!(tenant)
  yield
ensure
  begin
    switch!(previous_tenant)
  rescue StandardError => _e
    reset
  end
end

#switch!(tenant = nil) ⇒ Object

Switch to a new tenant

@param {String} tenant name


74
75
76
77
78
79
80
# File 'lib/apartment/adapters/abstract_adapter.rb', line 74

def switch!(tenant = nil)
  run_callbacks :switch do
    connect_to_new(tenant).tap do
      Apartment.connection.clear_query_cache
    end
  end
end