rls_multi_tenant 0.1.0 → 0.1.1

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: e90e7f280c785aa09f9f643da2420a38423f888592a750d4587dcc961ede1de1
4
- data.tar.gz: cb1ec28221f70d26bcf6444be404feccf6098417325921692f8c95ba4d945a3c
3
+ metadata.gz: cd8460a34e519c1566192ff9270fbfbb46e2a301e03a4595ed988f30f674905e
4
+ data.tar.gz: ba268783660a508782cde4cba72321d75b4401f283d15117e3082b2cc6f7f097
5
5
  SHA512:
6
- metadata.gz: e28c40e201630a87955c890b4f417d43c0e7418674a6bf776d44cd4a39b28cb6c49b4873c6807e0e21247b8e120682a986e1bccf95cdc47d1be29f2b63c213c6
7
- data.tar.gz: 986900e03f1234f1f19813f1680af7675861c35a437b64582512854bdc9f7d05828a78a6bf6755f656a2b05fa7e47447312c83d53796bea4929962f1ed18b70c
6
+ metadata.gz: 62626c3242330a98395cb490faf38b9e7db0017869d0dc479c2994a72cc26c81e040289f21d7c6470a0d42fdb2e1b704a54b895c079ebd74d8447bfb0f1364d3
7
+ data.tar.gz: de61ee7155791d99a4e3e19931ec9e4efb1f741bd01d13270e2d2b69c851eea167df6cdd15d1c4e47344f9f8574af0da7c0a9af7708d35a0e8aa91a09383b491
data/README.md CHANGED
@@ -16,7 +16,7 @@ A Rails gem that provides PostgreSQL Row Level Security (RLS) based multi-tenanc
16
16
  Add this line to your application's Gemfile:
17
17
 
18
18
  ```ruby
19
- gem 'rls_multi_tenant', path: 'gems/rls_multi_tenant'
19
+ gem 'rls_multi_tenant'
20
20
  ```
21
21
 
22
22
  And then execute:
@@ -38,11 +38,7 @@ module RlsMultiTenant
38
38
  end
39
39
 
40
40
  def create_app_user_migration
41
- unless Dir.glob(File.join(destination_root, "db/migrate/*_create_app_user.rb")).any?
42
- create_migration_with_timestamp("create_app_user", 2)
43
- else
44
- say "App user migration already exists, skipping creation", :yellow
45
- end
41
+ create_app_user_migrations_for_all_databases
46
42
  end
47
43
 
48
44
  def create_tenant_migration
@@ -71,6 +67,50 @@ module RlsMultiTenant
71
67
  say "="*60, :green
72
68
  end
73
69
 
70
+ def create_app_user_migrations_for_all_databases
71
+ # Get database configuration for current environment
72
+ db_config = Rails.application.config.database_configuration[Rails.env]
73
+
74
+ # Handle both single database and multiple databases configuration
75
+ databases_to_process = if db_config.is_a?(Hash) && db_config.key?('primary')
76
+ # Multiple databases configuration
77
+ db_config
78
+ else
79
+ # Single database configuration - treat as primary
80
+ { 'primary' => db_config }
81
+ end
82
+
83
+ databases_to_process.each do |db_name, config|
84
+ next if db_name == 'primary' # Skip primary database, handle it separately
85
+
86
+ # Check if migrations_paths is defined for this database
87
+ if config['migrations_paths']
88
+ migration_paths = Array(config['migrations_paths'])
89
+ migration_paths.each do |migration_path|
90
+ migration_dir = File.join(destination_root, migration_path)
91
+
92
+ # Check if migration already exists in this path
93
+ unless Dir.glob(File.join(migration_dir, "*_create_app_user.rb")).any?
94
+ FileUtils.mkdir_p(migration_dir) unless File.directory?(migration_dir)
95
+ create_migration_with_timestamp_for_path("create_app_user", 2, migration_path)
96
+ say "Created app user migration for #{db_name} in #{migration_path}", :green
97
+ else
98
+ say "App user migration already exists for #{db_name} in #{migration_path}, skipping creation", :yellow
99
+ end
100
+ end
101
+ else
102
+ say "No migrations_paths defined for database '#{db_name}', skipping app user migration", :yellow
103
+ end
104
+ end
105
+
106
+ # Handle primary database (default behavior)
107
+ unless Dir.glob(File.join(destination_root, "db/migrate/*_create_app_user.rb")).any?
108
+ create_migration_with_timestamp("create_app_user", 2)
109
+ else
110
+ say "App user migration already exists for primary database, skipping creation", :yellow
111
+ end
112
+ end
113
+
74
114
  private
75
115
 
76
116
  def create_migration_with_timestamp(migration_type, order)
@@ -86,6 +126,16 @@ module RlsMultiTenant
86
126
  template "create_tenant.rb", "db/migrate/#{timestamp}_create_tenants.rb"
87
127
  end
88
128
  end
129
+
130
+ def create_migration_with_timestamp_for_path(migration_type, order, migration_path)
131
+ base_timestamp = Time.current.strftime("%Y%m%d%H%M")
132
+ timestamp = "#{base_timestamp}#{sprintf('%02d', order)}"
133
+
134
+ case migration_type
135
+ when "create_app_user"
136
+ template "create_app_user.rb", "#{migration_path}/#{timestamp}_create_app_user.rb"
137
+ end
138
+ end
89
139
  end
90
140
  end
91
141
  end
@@ -30,8 +30,7 @@ module RlsMultiTenant
30
30
  private
31
31
 
32
32
  def create_app_user_migration
33
- timestamp = Time.current.strftime("%Y%m%d%H%M%S")
34
- template "create_app_user.rb", "db/migrate/#{timestamp}_create_app_user.rb"
33
+ create_app_user_migrations_for_all_databases
35
34
  end
36
35
 
37
36
  def create_enable_rls_migration
@@ -49,6 +48,44 @@ module RlsMultiTenant
49
48
  template "enable_uuid_extension.rb", "db/migrate/#{timestamp}_enable_uuid_extension.rb"
50
49
  end
51
50
 
51
+ def create_app_user_migrations_for_all_databases
52
+ # Get database configuration for current environment
53
+ db_config = Rails.application.config.database_configuration[Rails.env]
54
+
55
+ # Handle both single database and multiple databases configuration
56
+ databases_to_process = if db_config.is_a?(Hash) && db_config.key?('primary')
57
+ # Multiple databases configuration
58
+ db_config
59
+ else
60
+ # Single database configuration - treat as primary
61
+ { 'primary' => db_config }
62
+ end
63
+
64
+ databases_to_process.each do |db_name, config|
65
+ next if db_name == 'primary' # Skip primary database, handle it separately
66
+
67
+ # Check if migrations_paths is defined for this database
68
+ if config['migrations_paths']
69
+ migration_paths = Array(config['migrations_paths'])
70
+ migration_paths.each do |migration_path|
71
+ migration_dir = File.join(destination_root, migration_path)
72
+ FileUtils.mkdir_p(migration_dir) unless File.directory?(migration_dir)
73
+
74
+ timestamp = Time.current.strftime("%Y%m%d%H%M%S")
75
+ template "create_app_user.rb", "#{migration_path}/#{timestamp}_create_app_user.rb"
76
+ say "Created app user migration for #{db_name} in #{migration_path}", :green
77
+ end
78
+ else
79
+ say "No migrations_paths defined for database '#{db_name}', skipping app user migration", :yellow
80
+ end
81
+ end
82
+
83
+ # Handle primary database (default behavior)
84
+ timestamp = Time.current.strftime("%Y%m%d%H%M%S")
85
+ template "create_app_user.rb", "db/migrate/#{timestamp}_create_app_user.rb"
86
+ say "Created app user migration for primary database", :green
87
+ end
88
+
52
89
  def table_name
53
90
  @table_name ||= ask("Enter table name for RLS:")
54
91
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RlsMultiTenant
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rls_multi_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Coding Ways