tenant_realm 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tasks/migrate.rake +10 -0
- data/lib/tenant_realm/cache/kredis_cache.rb +2 -0
- data/lib/tenant_realm/db_context.rb +25 -3
- data/lib/tenant_realm/version.rb +1 -1
- data/lib/tenant_realm.rb +1 -1
- data/tenant_realm.gemspec +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b8289d86354fe7ee75ca07d9b04ca4f7193a2bdb12f97e90f06757f6ee4103d
|
4
|
+
data.tar.gz: 8bedca7bab527dd404765ac3d46db39364b9cce22416445f0e701760efdbc757
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60b01860846cc2db01b93c8701751bdb5b41f8fc3689b781b5b4a1a88985f7043246c89dd6527e87089560eb04c547cf0e51cd2d221d4c2997e73c524e440808
|
7
|
+
data.tar.gz: 16452e0151f60519c4137730503573b8bc965bb918f00f00805e02ae05584c1c4a7e7942c5ad92f08a5e0b48a0b169a7658cd67dc3b902c07f78a9614193c441
|
data/lib/tasks/migrate.rake
CHANGED
@@ -5,6 +5,16 @@ namespace :tenant_realm do
|
|
5
5
|
task migrate: :environment do
|
6
6
|
tenants = TenantRealm::Tenant.tenants
|
7
7
|
|
8
|
+
puts 'Migrating primary'
|
9
|
+
root_db_config = TenantRealm::DbContext.root_db_config
|
10
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection(root_db_config) do
|
11
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
12
|
+
TenantRealm::DbContext.dump_schema(
|
13
|
+
shard: :primary,
|
14
|
+
db_config: root_db_config
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
8
18
|
tenants.each do |tenant|
|
9
19
|
shard = TenantRealm::Utils.shard_name_from_tenant(tenant:)
|
10
20
|
|
@@ -55,10 +55,10 @@ module TenantRealm
|
|
55
55
|
YAML.dump(config, f)
|
56
56
|
end
|
57
57
|
|
58
|
-
ActiveRecord::Base.configurations.configurations <<
|
59
|
-
Rails.env,
|
58
|
+
ActiveRecord::Base.configurations.configurations << build_db_hash_config(
|
60
59
|
shard_name,
|
61
|
-
tenant_shard_config
|
60
|
+
tenant_shard_config,
|
61
|
+
Rails.env
|
62
62
|
)
|
63
63
|
|
64
64
|
sym_shard = shard_name.to_sym
|
@@ -110,8 +110,30 @@ module TenantRealm
|
|
110
110
|
db_config
|
111
111
|
end
|
112
112
|
|
113
|
+
def dump_schema(db_config:, shard:)
|
114
|
+
return unless ActiveRecord.dump_schema_after_migration
|
115
|
+
|
116
|
+
schema_format = ENV.fetch('SCHEMA_FORMAT', ActiveRecord.schema_format).to_sym
|
117
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema(
|
118
|
+
build_db_hash_config(
|
119
|
+
shard.to_s,
|
120
|
+
db_config,
|
121
|
+
Rails.env
|
122
|
+
),
|
123
|
+
schema_format
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
113
127
|
private
|
114
128
|
|
129
|
+
def build_db_hash_config(shard, db_config, env_name = Rails.env)
|
130
|
+
ActiveRecord::DatabaseConfigurations::HashConfig.new(
|
131
|
+
env_name,
|
132
|
+
shard,
|
133
|
+
db_config
|
134
|
+
)
|
135
|
+
end
|
136
|
+
|
115
137
|
def build_connected_shards
|
116
138
|
@@shards.keys.each_with_object({}) do |shard, shards|
|
117
139
|
sym_shard = shard.to_sym
|
data/lib/tenant_realm/version.rb
CHANGED
data/lib/tenant_realm.rb
CHANGED
@@ -33,10 +33,10 @@ module TenantRealm
|
|
33
33
|
identifier = Utils.identifier_resolver(request)
|
34
34
|
tenant = Tenant.tenant(identifier)
|
35
35
|
db_config = Utils.dig_db_config(tenant:)
|
36
|
-
shard = Utils.shard_name_from_tenant(tenant:)
|
37
36
|
|
38
37
|
return :primary if db_config.blank?
|
39
38
|
|
39
|
+
shard = Utils.shard_name_from_tenant(tenant:)
|
40
40
|
Config.current.tenant = tenant
|
41
41
|
DbContext.add_shard(shard:, db_config:)
|
42
42
|
ActiveRecord::Base.connects_to(shards: DbContext.connected_shards)
|
data/tenant_realm.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tenant_realm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alpha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby on Rails gem to support multi-tenant
|
14
14
|
email:
|