Class: ActiveRecord::Tenanted::UntenantedConnectionPool

Inherits:
ConnectionAdapters::NullPool
  • Object
show all
Defined in:
lib/active_record/tenanted/untenanted_connection_pool.rb

Overview

In an untenanted context, instances of this class are returned by ‘Tenant.connection_pool`.

Many places in Rails assume that ‘.connection_pool` can be called and will return an object, and so we can’t just raise an exception if it’s called while untenanted.

Instead, this class exists to provide a minimal set of features that don’t need a database connection, and that will raise if a connection is attempted.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_config, model) ⇒ UntenantedConnectionPool

Returns a new instance of UntenantedConnectionPool.



15
16
17
18
19
20
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 15

def initialize(db_config, model)
  super()

  @db_config = db_config
  @model = model
end

Instance Attribute Details

#db_configObject (readonly)

:nodoc:



13
14
15
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 13

def db_config
  @db_config
end

Instance Method Details

#checkoutObject



39
40
41
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 39

def checkout(...)
  raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
end

#lease_connectionObject



35
36
37
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 35

def lease_connection(...)
  raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
end

#new_connectionObject



47
48
49
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 47

def new_connection(...)
  raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
end

#schema_cacheObject



27
28
29
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 27

def schema_cache
  ActiveRecord::ConnectionAdapters::BoundSchemaReflection.new(schema_reflection, self)
end

#schema_reflectionObject



22
23
24
25
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 22

def schema_reflection
  schema_cache_path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
  ActiveRecord::ConnectionAdapters::SchemaReflection.new(schema_cache_path)
end

#sizeObject



31
32
33
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 31

def size
  db_config.max_connections
end

#with_connectionObject



43
44
45
# File 'lib/active_record/tenanted/untenanted_connection_pool.rb', line 43

def with_connection(...)
  raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
end