Module: Apartment::Database

Extended by:
Database, Forwardable
Included in:
Database
Defined in:
lib/apartment/database.rb,
lib/apartment/adapters/mysql2_adapter.rb,
lib/apartment/adapters/postgis_adapter.rb,
lib/apartment/adapters/sqlite3_adapter.rb,
lib/apartment/adapters/jdbc_mysql_adapter.rb,
lib/apartment/adapters/postgresql_adapter.rb,
lib/apartment/adapters/jdbc_postgresql_adapter.rb

Overview

The main entry point to Apartment functions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config=(value) ⇒ Object

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



13
14
15
# File 'lib/apartment/database.rb', line 13

def config=(value)
  @config = value
end

Class Method Details

.jdbc_mysql_adapter(config) ⇒ Object



6
7
8
# File 'lib/apartment/adapters/jdbc_mysql_adapter.rb', line 6

def self.jdbc_mysql_adapter(config)
  Adapters::JDBCMysqlAdapter.new config
end

.jdbc_postgresql_adapter(config) ⇒ Object



6
7
8
9
10
# File 'lib/apartment/adapters/jdbc_postgresql_adapter.rb', line 6

def self.jdbc_postgresql_adapter(config)
  Apartment.use_schemas ?
    Adapters::JDBCPostgresqlSchemaAdapter.new(config) :
    Adapters::JDBCPostgresqlAdapter.new(config)
end

.mysql2_adapter(config) ⇒ Object



6
7
8
9
10
# File 'lib/apartment/adapters/mysql2_adapter.rb', line 6

def self.mysql2_adapter(config)
  Apartment.use_schemas ?
    Adapters::Mysql2SchemaAdapter.new(config) :
    Adapters::Mysql2Adapter.new(config)
end

.postgis_adapter(config) ⇒ Object



8
9
10
11
12
# File 'lib/apartment/adapters/postgis_adapter.rb', line 8

def self.postgis_adapter(config)
  Apartment.use_schemas ?
    Adapters::PostgresqlSchemaAdapter.new(config) :
    Adapters::PostgresqlAdapter.new(config)
end

.postgresql_adapter(config) ⇒ Object



6
7
8
9
10
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 6

def self.postgresql_adapter(config)
  Apartment.use_schemas ?
    Adapters::PostgresqlSchemaAdapter.new(config) :
    Adapters::PostgresqlAdapter.new(config)
end

.sqlite3_adapter(config) ⇒ Object



5
6
7
# File 'lib/apartment/adapters/sqlite3_adapter.rb', line 5

def self.sqlite3_adapter(config)
  Adapters::Sqlite3Adapter.new(config)
end

Instance Method Details

#adapterObject

Fetch the proper multi-tenant adapter based on Rails config

@return {subclass of Apartment::AbstractAdapter}


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apartment/database.rb', line 25

def adapter
  Thread.current[:apartment_adapter] ||= begin
    adapter_method = "#{config[:adapter]}_adapter"

    if defined?(JRUBY_VERSION)
      if config[:adapter] =~ /mysql/
        adapter_method = 'jdbc_mysql_adapter'
      elsif config[:adapter] =~ /postgresql/
        adapter_method = 'jdbc_postgresql_adapter'
      end
    end

    begin
      require "apartment/adapters/#{adapter_method}"
    rescue LoadError
      raise "The adapter `#{adapter_method}` is not yet supported"
    end

    unless respond_to?(adapter_method)
      raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter"
    end

    send(adapter_method, config)
  end
end

#initObject

Initialize Apartment config options such as excluded_models



17
18
19
# File 'lib/apartment/database.rb', line 17

def init
  process_excluded_models
end

#reload!(config = nil) ⇒ Object

Reset config and adapter so they are regenerated



53
54
55
56
# File 'lib/apartment/database.rb', line 53

def reload!(config = nil)
  Thread.current[:apartment_adapter] = nil
  @config = config
end