rls_multi_tenant 0.1.0 → 0.1.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 +4 -4
- data/README.md +1 -1
- data/lib/rls_multi_tenant/generators/install/install_generator.rb +55 -5
- data/lib/rls_multi_tenant/generators/migration/migration_generator.rb +39 -2
- data/lib/rls_multi_tenant/generators/migration/templates/create_app_user.rb +4 -0
- data/lib/rls_multi_tenant/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fcb89757e26727014390f0ce50eed58d22e760d4249508bf58c550533e0ef14c
|
|
4
|
+
data.tar.gz: 21d57b4c19ff0f5117bd62df654b2815eb75a54749adf8a689448816a29f3141
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0c00dde13e466a6d466f8170b3732775654bdb1655d1985717d551667313d3a250cbaf74067a770f14600da7d64582d1d2b241e668f8f3b6f831752d3767f9f5
|
|
7
|
+
data.tar.gz: 4a3df9f10af7671d2ed572b8d3f1009155bff00014f6823e130a39b3f4d50a1b5de9e2a86ce271e38e7378e99185694435e6332012e7091a6b287c803ac81c5b
|
data/README.md
CHANGED
|
@@ -38,11 +38,7 @@ module RlsMultiTenant
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
def create_app_user_migration
|
|
41
|
-
|
|
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
|
-
|
|
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
|
|
@@ -18,6 +18,10 @@ class CreateAppUser < ActiveRecord::Migration[<%= Rails.version.to_f %>]
|
|
|
18
18
|
execute "GRANT CONNECT ON DATABASE #{ActiveRecord::Base.connection.current_database} TO #{app_user};"
|
|
19
19
|
execute "GRANT USAGE ON SCHEMA public TO #{app_user};"
|
|
20
20
|
execute "GRANT CREATE ON SCHEMA public TO #{app_user};"
|
|
21
|
+
|
|
22
|
+
# Grant permissions on system tables
|
|
23
|
+
execute "GRANT SELECT ON TABLE schema_migrations TO #{app_user};"
|
|
24
|
+
execute "GRANT SELECT ON TABLE ar_internal_metadata TO #{app_user};"
|
|
21
25
|
end
|
|
22
26
|
|
|
23
27
|
def down
|