Class: Apartment::Adapters::Mysql2Adapter

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

Overview

v4 MySQL adapter using database-per-tenant isolation (mysql2 driver).

Resolves tenant-specific connection configs by setting the ‘database` key to the environmentified tenant name. Lifecycle operations (create/drop) execute DDL against the default connection.

Direct Known Subclasses

TrilogyAdapter

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#connection_config

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#create, #default_tenant, #drop, #environmentify, #initialize, #migrate, #process_excluded_models, #process_pinned_model, #process_pinned_models, #seed, #tenant_container_gone?, #validated_connection_config

Constructor Details

This class inherits a constructor from Apartment::Adapters::AbstractAdapter

Instance Method Details

#failsafe_error_classesObject

The database-per-tenant missing-tenant error: connecting to a dropped database raises ActiveRecord::NoDatabaseError (MySQL error 1049) — an unambiguous signal. It surfaces raw at query time, or wrapped in ApartmentError when ConnectionHandling resolves the pool (the dev-mode pending-migration check), so both are listed; #container_error? gates on the unwrapped NoDatabaseError. Inherited by TrilogyAdapter.



44
45
46
# File 'lib/apartment/adapters/mysql2_adapter.rb', line 44

def failsafe_error_classes
  [ActiveRecord::NoDatabaseError, Apartment::ApartmentError]
end

#qualify_pinned_table_name(klass) ⇒ Object



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

def qualify_pinned_table_name(klass)
  db_name = base_config['database']

  if klass.apartment_explicit_table_name?
    original = klass.table_name
    table = original.sub(/\A[^.]+\./, '')
    klass.table_name = "#{db_name}.#{table}"
    klass.apartment_mark_processed!(:explicit, original)
  else
    original_prefix = klass.table_name_prefix
    klass.table_name_prefix = "#{db_name}."
    klass.reset_table_name
    klass.apartment_mark_processed!(:convention, original_prefix)
  end
end

#resolve_connection_config(tenant, base_config: nil) ⇒ Object



33
34
35
36
# File 'lib/apartment/adapters/mysql2_adapter.rb', line 33

def resolve_connection_config(tenant, base_config: nil)
  config = base_config || send(:base_config)
  config.merge('database' => environmentify(tenant))
end

#shared_pinned_connection?Boolean

Returns:

  • (Boolean)


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

def shared_pinned_connection?
  !Apartment.config.force_separate_pinned_pool
end