penthouse 0.7.5 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/penthouse/configuration.rb +7 -5
- data/lib/penthouse/migrator.rb +3 -23
- data/lib/penthouse/tenants/octopus_schema_tenant.rb +1 -1
- data/lib/penthouse/tenants/octopus_shard_tenant.rb +1 -1
- data/lib/penthouse/version.rb +1 -1
- data/lib/penthouse.rb +16 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92248bef6760f1ed6455c27c851fce79e267b8e7
|
4
|
+
data.tar.gz: cc5a1f2bfdda094227a8449123dd8178a09305a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41bcde8417c7e7c4ee517682c47230a91dcf72fa918b8834a7ad21032daf3c661ea5c38374c129a9cbdc7b5754faf65eaf85d783f72aceca938996fc20301e85
|
7
|
+
data.tar.gz: 2c1fe638a8a09aabce3a3c80813f4f4f4ed5106b794688f6b083b8c35413dd7e94d1387370feeeb7a2bc26acd777d592991e4804e056278d619cbe784c8d1278
|
data/README.md
CHANGED
@@ -30,7 +30,11 @@ Penthouse.configure do |config|
|
|
30
30
|
# enhance migrations to migrate all tenants
|
31
31
|
config.migrate_tenants = true
|
32
32
|
# setup a proc which will return the tenants
|
33
|
-
config.
|
33
|
+
config.tenants = Proc.new do
|
34
|
+
Account.each_with_object({}) do |account, result|
|
35
|
+
result.merge!(account.slug => account)
|
36
|
+
end
|
37
|
+
end
|
34
38
|
end
|
35
39
|
|
36
40
|
Rails.application.config.middleware.use Penthouse::App
|
@@ -12,19 +12,21 @@
|
|
12
12
|
|
13
13
|
module Penthouse
|
14
14
|
class Configuration
|
15
|
-
attr_accessor :router, :runner, :migrate_tenants, :db_schema_file, :
|
15
|
+
attr_accessor :router, :runner, :migrate_tenants, :db_schema_file, :tenants
|
16
16
|
|
17
17
|
# @param router [Penthouse::Routers::BaseRouter] the default router for your application to use
|
18
18
|
# @param runner [Penthouse::Runners::BaseRunner] the default runner for your application to use
|
19
19
|
# @param migrate_tenants [Boolean] whether you want Penthouse to automatically migrate all tenants
|
20
|
-
# @param db_schema_file [String] a path to your schema file
|
21
|
-
#
|
22
|
-
|
20
|
+
# @param db_schema_file [String] a path to your schema file
|
21
|
+
# (typically `db/schema.rb` or `db/structure.sql` in Rails)
|
22
|
+
# @param tenants [Proc] some code which must return an hash of tenant identifiers (strings/symbols)
|
23
|
+
# mapped to tenant objects, which can be anything your runner needs
|
24
|
+
def initialize(router: nil, runner: nil, migrate_tenants: false, db_schema_file: nil, tenants: -> { raise NotImplementedError })
|
23
25
|
self.router = router
|
24
26
|
self.runner = runner
|
25
27
|
self.migrate_tenants = migrate_tenants
|
26
28
|
self.db_schema_file = db_schema_file
|
27
|
-
self.
|
29
|
+
self.tenants = tenants
|
28
30
|
|
29
31
|
if migrate_tenants? && !db_schema_file
|
30
32
|
raise ArgumentError, "If you want to migrate tenants, we need a path to a DB schema file"
|
data/lib/penthouse/migrator.rb
CHANGED
@@ -42,76 +42,56 @@ module Penthouse
|
|
42
42
|
|
43
43
|
module ClassMethods
|
44
44
|
def migrate_with_penthouse(migrations_paths, target_version = nil, &block)
|
45
|
-
puts "#migrate_with_penthouse called"
|
46
|
-
|
47
45
|
unless Penthouse.configuration.migrate_tenants?
|
48
|
-
puts "Skipping penthouse integration"
|
49
46
|
return migrate_without_penthouse(migrations_paths, target_version, &block)
|
50
47
|
end
|
51
48
|
|
52
49
|
Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do |tenant|
|
53
|
-
puts "Migrating #{tenant.identifier}"
|
54
|
-
puts "calling #migrate_without_penthouse"
|
55
50
|
migrate_without_penthouse(migrations_paths, target_version, &block)
|
56
51
|
end
|
57
52
|
end
|
58
53
|
|
59
54
|
def up_with_penthouse(migrations_paths, target_version = nil, &block)
|
60
|
-
puts "#up_with_penthouse called"
|
61
|
-
|
62
55
|
unless Penthouse.configuration.migrate_tenants?
|
63
|
-
puts "Skipping penthouse integration"
|
64
56
|
return up_without_penthouse(migrations_paths, target_version, &block)
|
65
57
|
end
|
66
58
|
|
67
59
|
Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do |tenant|
|
68
|
-
puts "Migrating #{tenant.identifier}"
|
69
|
-
puts "calling #up_without_penthouse"
|
70
60
|
up_without_penthouse(migrations_paths, target_version, &block)
|
71
61
|
end
|
72
62
|
end
|
73
63
|
|
74
64
|
def down_with_penthouse(migrations_paths, target_version = nil, &block)
|
75
|
-
puts "#down_with_penthouse called"
|
76
|
-
|
77
65
|
unless Penthouse.configuration.migrate_tenants?
|
78
|
-
puts "Skipping penthouse integration"
|
79
66
|
return down_without_penthouse(migrations_paths, target_version, &block)
|
80
67
|
end
|
81
68
|
|
82
69
|
Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do |tenant|
|
83
|
-
puts "Migrating #{tenant.identifier}"
|
84
|
-
puts "calling #down_without_penthouse"
|
85
70
|
down_without_penthouse(migrations_paths, target_version, &block)
|
86
71
|
end
|
87
72
|
end
|
88
73
|
|
89
74
|
def run_with_penthouse(direction, migrations_paths, target_version)
|
90
|
-
puts "#run_with_penthouse called"
|
91
|
-
|
92
75
|
unless Penthouse.configuration.migrate_tenants?
|
93
|
-
puts "Skipping penthouse integration"
|
94
76
|
return run_without_penthouse(direction, migrations_paths, target_version)
|
95
77
|
end
|
96
78
|
|
97
79
|
Penthouse.each_tenant(tenant_identifiers: tenants_to_migrate) do |tenant|
|
98
|
-
puts "Migrating #{tenant.identifier}"
|
99
|
-
puts "calling #run_without_penthouse"
|
100
80
|
run_without_penthouse(direction, migrations_paths, target_version)
|
101
81
|
end
|
102
82
|
end
|
103
83
|
|
84
|
+
private
|
85
|
+
|
104
86
|
def tenants_to_migrate
|
105
87
|
return @tenants_to_migrate if defined?(@tenants_to_migrate)
|
106
88
|
@tenants_to_migrate = begin
|
107
|
-
if (t = ENV["TENANT"] || ENV["TENANTS"])
|
89
|
+
if !!(t = (ENV["TENANT"] || ENV["TENANTS"]))
|
108
90
|
t.split(",").map(&:strip)
|
109
91
|
else
|
110
92
|
Penthouse.tenant_identifiers
|
111
93
|
end
|
112
94
|
end
|
113
|
-
puts "#tenants_to_migrate = #{@tenants_to_migrate}"
|
114
|
-
@tenants_to_migrate
|
115
95
|
end
|
116
96
|
end
|
117
97
|
end
|
@@ -20,7 +20,7 @@ module Penthouse
|
|
20
20
|
# @param block [Block] The code to execute within the schema
|
21
21
|
# @yield [SchemaTenant] The current tenant instance
|
22
22
|
# @return [void]
|
23
|
-
def call(shard:
|
23
|
+
def call(shard: Octopus.master_shard, &block)
|
24
24
|
Octopus.using(shard) do
|
25
25
|
super(&block)
|
26
26
|
end
|
@@ -18,7 +18,7 @@ module Penthouse
|
|
18
18
|
# @param identifier [String, Symbol] An identifier for the tenant
|
19
19
|
# @param shard [String, Symbol] the configured Octopus shard to use for this tenant
|
20
20
|
# @param tenant_schema [String] your tenant's schema name within the Postgres shard, typically just 'public' as the shard should be dedicated
|
21
|
-
def initialize(
|
21
|
+
def initialize(identifier, shard:, tenant_schema: "public", **options)
|
22
22
|
self.shard = shard
|
23
23
|
super(identifier, tenant_schema: tenant_schema, **options)
|
24
24
|
end
|
data/lib/penthouse/version.rb
CHANGED
data/lib/penthouse.rb
CHANGED
@@ -6,19 +6,20 @@ require "penthouse/runners/base_runner"
|
|
6
6
|
|
7
7
|
module Penthouse
|
8
8
|
class TenantNotFound < RuntimeError; end
|
9
|
+
CURRENT_TENANT_KEY = 'penthouse_tenant'.freeze
|
9
10
|
|
10
11
|
class << self
|
11
12
|
# Retrieves the currently active tenant identifier
|
12
13
|
# @return [String, Symbol] the current tenant name
|
13
14
|
def tenant
|
14
|
-
Thread.current[
|
15
|
+
Thread.current[CURRENT_TENANT_KEY]
|
15
16
|
end
|
16
17
|
|
17
18
|
# Sets the currently active tenant identifier
|
18
19
|
# @param tenant_identifier [String, Symbol] the identifier for the tenant
|
19
20
|
# @return [void]
|
20
21
|
def tenant=(tenant_identifier)
|
21
|
-
Thread.current[
|
22
|
+
Thread.current[CURRENT_TENANT_KEY] = tenant_identifier
|
22
23
|
end
|
23
24
|
|
24
25
|
# Similar to Penthouse.tenant=, except this will switch back after the given
|
@@ -28,7 +29,7 @@ module Penthouse
|
|
28
29
|
# @param block [Block] the code to execute
|
29
30
|
# @yield [String, Symbol] the identifier for the tenant
|
30
31
|
# @return [void]
|
31
|
-
def with_tenant(tenant_identifier, default_tenant: tenant, &block)
|
32
|
+
def with_tenant(tenant_identifier, default_tenant: self.tenant, &block)
|
32
33
|
self.tenant = tenant_identifier
|
33
34
|
block.yield(tenant_identifier)
|
34
35
|
ensure
|
@@ -42,8 +43,8 @@ module Penthouse
|
|
42
43
|
# @param block [Block] the code to execute
|
43
44
|
# @yield [String, Symbol] the identifier for the tenant
|
44
45
|
# @return [void]
|
45
|
-
def each_tenant(tenant_identifiers:
|
46
|
-
|
46
|
+
def each_tenant(tenant_identifiers: self.tenant_identifiers, default_tenant: self.tenant, runner: self.configuration.runner, &block)
|
47
|
+
tenant_identifiers.each do |tenant_identifier|
|
47
48
|
switch(tenant_identifier, runner: runner, &block)
|
48
49
|
end
|
49
50
|
end
|
@@ -54,7 +55,7 @@ module Penthouse
|
|
54
55
|
# @param block [Block] the code to execute
|
55
56
|
# @yield [Penthouse::Tenants::BaseTenant] the tenant instance
|
56
57
|
# @return [void]
|
57
|
-
def switch(tenant_identifier, runner: configuration.runner, &block)
|
58
|
+
def switch(tenant_identifier, runner: self.configuration.runner, &block)
|
58
59
|
runner.call(tenant_identifier, &block)
|
59
60
|
end
|
60
61
|
|
@@ -62,7 +63,7 @@ module Penthouse
|
|
62
63
|
# @param tenant_identifier [String, Symbol] the identifier for the tenant
|
63
64
|
# @see Penthouse::Tenants::BaseTenant#delete
|
64
65
|
# @return [void]
|
65
|
-
def create(tenant_identifier, runner: configuration.runner, **options)
|
66
|
+
def create(tenant_identifier, runner: self.configuration.runner, **options)
|
66
67
|
switch(tenant_identifier, runner: runner) do |tenant|
|
67
68
|
tenant.create(**options)
|
68
69
|
end
|
@@ -72,7 +73,7 @@ module Penthouse
|
|
72
73
|
# @param tenant_identifier [String, Symbol] the identifier for the tenant
|
73
74
|
# @see Penthouse::Tenants::BaseTenant#delete
|
74
75
|
# @return [void]
|
75
|
-
def delete(tenant_identifier, runner: configuration.runner, **options)
|
76
|
+
def delete(tenant_identifier, runner: self.configuration.runner, **options)
|
76
77
|
switch(tenant_identifier, runner: runner) do |tenant|
|
77
78
|
tenant.delete(**options)
|
78
79
|
end
|
@@ -97,10 +98,16 @@ module Penthouse
|
|
97
98
|
)
|
98
99
|
end
|
99
100
|
|
101
|
+
# Returns a hash of tenant identifiers based on the configured setting
|
102
|
+
# @return [Hash<String, Symbol => Object>] the hash of tenants
|
103
|
+
def tenants
|
104
|
+
configuration.tenants.call
|
105
|
+
end
|
106
|
+
|
100
107
|
# Returns a array of tenant identifiers based on the configured setting
|
101
108
|
# @return [Array<String, Symbol>] the list of tenant identifiers
|
102
109
|
def tenant_identifiers
|
103
|
-
|
110
|
+
tenants.keys
|
104
111
|
end
|
105
112
|
end
|
106
113
|
end
|