Class: Apartment::Adapters::AbstractAdapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AbstractAdapter

@constructor

@param {Hash} config Database config

11
12
13
# File 'lib/apartment/adapters/abstract_adapter.rb', line 11

def initialize(config)
  @config = config
end

Instance Attribute Details

#default_tenantObject Also known as: default_schema

Return the original public tenant

@return {String} default tenant name

60
61
62
# File 'lib/apartment/adapters/abstract_adapter.rb', line 60

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

19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/apartment/adapters/abstract_adapter.rb', line 19

def create(tenant)
  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

#currentObject

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


52
53
54
# File 'lib/apartment/adapters/abstract_adapter.rb', line 52

def current
  Apartment.connection.current_database
end

#current_databaseObject

Get the current tenant name

@return {String} current tenant name

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

def current_database
  Apartment::Deprecation.warn "[Deprecation Warning] `current_database` is now deprecated, please use `current`"
  current
end

#current_tenantObject

Get the current tenant name

@return {String} current tenant name

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

def current_tenant
  Apartment::Deprecation.warn "[Deprecation Warning] `current_tenant` is now deprecated, please use `current`"
  current
end

#drop(tenant) ⇒ Object

Drop the tenant

@param {String} tenant name

69
70
71
72
73
74
75
# File 'lib/apartment/adapters/abstract_adapter.rb', line 69

def drop(tenant)
  # Apartment.connection.drop_database   note that drop_database will not throw an exception, so manually execute
  Apartment.connection.execute("DROP DATABASE #{environmentify(tenant)}" )

rescue *rescuable_exceptions
  raise TenantNotFound, "The tenant #{environmentify(tenant)} cannot be found"
end

#each(tenants = Apartment.tenant_names) ⇒ Object

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


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

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

#process(tenant = nil, &block) ⇒ Object

Deprecated

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

def process(tenant = nil, &block)
  Apartment::Deprecation.warn("[Deprecation Warning] `process` is now deprecated. Please use `switch`")
  switch(tenant, &block)
end

#process_excluded_modelsObject

Establish a new connection for each specific excluded model


125
126
127
128
129
130
# File 'lib/apartment/adapters/abstract_adapter.rb', line 125

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|
    excluded_model.constantize.establish_connection @config
  end
end

#resetObject

Reset the tenant connection to the default


134
135
136
# File 'lib/apartment/adapters/abstract_adapter.rb', line 134

def reset
  Apartment.establish_connection @config
end

#seed_dataObject Also known as: seed

Load the rails seed file into the db


140
141
142
# File 'lib/apartment/adapters/abstract_adapter.rb', line 140

def seed_data
  silence_stream(STDOUT){ load_or_abort("#{Rails.root}/db/seeds.rb") } # Don't log the output of seeding the db
end

#switch(tenant = nil) ⇒ Object

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

@param {String?} tenant to connect to

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/apartment/adapters/abstract_adapter.rb', line 93

def switch(tenant = nil)
  if block_given?
    begin
      previous_tenant = current
      switch!(tenant)
      yield

    ensure
      switch!(previous_tenant) rescue reset
    end
  else
    Apartment::Deprecation.warn("[Deprecation Warning] `switch` now requires a block reset to the default tenant after the block. Please use `switch!` instead if you don't want this")
    switch!(tenant)
  end
end

#switch!(tenant = nil) ⇒ Object

Switch to a new tenant

@param {String} tenant name

81
82
83
84
85
86
87
# File 'lib/apartment/adapters/abstract_adapter.rb', line 81

def switch!(tenant = nil)
  return reset if tenant.nil?

  connect_to_new(tenant).tap do
    Apartment.connection.clear_query_cache
  end
end