tenant_realm 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec5080c5a6b4e997fd094811df564e8cb6c7520030a1fb0617917a155dd05223
4
- data.tar.gz: 159100e048f540997d9e9ffa33ba215c035357573bb297a65afa52bd6547d593
3
+ metadata.gz: 5b8289d86354fe7ee75ca07d9b04ca4f7193a2bdb12f97e90f06757f6ee4103d
4
+ data.tar.gz: 8bedca7bab527dd404765ac3d46db39364b9cce22416445f0e701760efdbc757
5
5
  SHA512:
6
- metadata.gz: 789611a5e9bc41b794a65e39d4d4cb48a69ad6d37a6d1bde72cb10e6f2bf499278f6715c5f50a7ba26023360408919bf921e33e72b9b994263747582eb0e8dc8
7
- data.tar.gz: a7dc2cddb27f123a9772bd53d4e4b1f3d39f9f5cad245f83de9329aa9c3bd6acebde917cb42ce84927b12b896396f8837fe715f5f22ede2cbcec365cb2d72bfc
6
+ metadata.gz: 60b01860846cc2db01b93c8701751bdb5b41f8fc3689b781b5b4a1a88985f7043246c89dd6527e87089560eb04c547cf0e51cd2d221d4c2997e73c524e440808
7
+ data.tar.gz: 16452e0151f60519c4137730503573b8bc965bb918f00f00805e02ae05584c1c4a7e7942c5ad92f08a5e0b48a0b169a7658cd67dc3b902c07f78a9614193c441
@@ -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
 
@@ -23,6 +23,8 @@ module TenantRealm
23
23
  end
24
24
 
25
25
  def cache_tenant(tenant)
26
+ return tenant if tenant.blank?
27
+
26
28
  tenant_unique_keys(tenant).each do |key|
27
29
  cached_tenant = tenant_kredis(key)
28
30
  cached_tenant.value = tenant
@@ -55,10 +55,10 @@ module TenantRealm
55
55
  YAML.dump(config, f)
56
56
  end
57
57
 
58
- ActiveRecord::Base.configurations.configurations << ActiveRecord::DatabaseConfigurations::HashConfig.new(
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TenantRealm
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.2'
5
5
  end
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
@@ -34,6 +34,10 @@ Gem::Specification.new do |spec|
34
34
  .vscode/settings.json
35
35
  LICENSE.txt
36
36
  lefthook.yml
37
+ docs/
38
+ biome.json
39
+ package.json
40
+ pnpm-lock.yaml
37
41
  ]
38
42
  )
39
43
  end
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.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-27 00:00:00.000000000 Z
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: