-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtenanted.rb
More file actions
72 lines (53 loc) · 2.51 KB
/
tenanted.rb
File metadata and controls
72 lines (53 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true
require "active_record"
require "zeitwerk"
loader = Zeitwerk::Loader.for_gem_extension(ActiveRecord)
loader.inflector.inflect(
"sqlite" => "SQLite",
"mysql" => "MySQL",
)
loader.setup
module ActiveRecord
module Tenanted
# Base exception class for the library.
class Error < StandardError; end
# Raised when database access is attempted without a current tenant having been set.
class NoTenantError < Error; end
# Raised when database access is attempted on a record whose tenant does not match the current tenant.
class WrongTenantError < Error; end
# Raised when attempting to locate a GlobalID without a tenant.
class MissingTenantError < Error; end
# Raised when attempting to create a tenant that already exists.
class TenantExistsError < Error; end
# Raised when attempting to create a tenant with illegal characters in it.
class BadTenantNameError < Error; end
# Raised when the application's tenant configuration is invalid.
class TenantConfigurationError < Error; end
# Raised when implicit creation is disabled and a tenant is referenced that does not exist
class TenantDoesNotExistError < Error; end
# Raised when the Rails integration is being invoked but has not been configured.
class IntegrationNotConfiguredError < Error; end
# Raised when an unsupported database adapter is used.
class UnsupportedDatabaseError < Error; end
# Raised when a tenant database switch via USE fails.
class TenantSwitchError < Error; end
# Raised when resetting a connection to the fallback database fails during checkin.
class TenantResetError < Error; end
# Raised when a tenant switch is attempted while a database transaction is open.
class TenantSwitchInTransactionError < Error; end
# Return the constantized connection class configured in `config.active_record_tenanted.connection_class`,
# or nil if none is configured.
def self.connection_class
# TODO: cache this / speed this up
Rails.application.config.active_record_tenanted.connection_class&.constantize
end
# Return an Array of the tenanted database configurations.
def self.base_configs(configurations = ActiveRecord::Base.configurations)
configurations
.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, include_hidden: true)
.select { |c| c.configuration_hash[:tenanted] }
end
end
end
loader.eager_load
ActiveSupport.run_load_hooks :active_record_tenanted, ActiveRecord::Tenanted