pg_rls 0.2.5 → 1.0.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.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +55 -17
  3. data/.ruby-version +1 -0
  4. data/CHANGELOG.md +19 -2
  5. data/CODE_OF_CONDUCT.md +77 -29
  6. data/Guardfile +44 -0
  7. data/README.md +247 -83
  8. data/Rakefile +5 -12
  9. data/Steepfile +29 -0
  10. data/UPGRADE.md +106 -0
  11. data/app/models/pg_rls/admin.rb +24 -0
  12. data/app/models/pg_rls/current.rb +48 -0
  13. data/app/models/pg_rls/record.rb +13 -0
  14. data/app/models/pg_rls/tenant/searchable.rb +60 -0
  15. data/app/models/pg_rls/tenant/securable.rb +67 -0
  16. data/app/models/pg_rls/tenant/switchable.rb +40 -0
  17. data/app/models/pg_rls/tenant.rb +9 -0
  18. data/assets/logo.svg +8 -0
  19. data/docker-compose.yml +14 -0
  20. data/lib/generators/pg_rls/active_record/active_record_generator.rb +62 -65
  21. data/lib/generators/pg_rls/install/install_generator.rb +38 -0
  22. data/lib/generators/pg_rls/pg_rls_generator.rb +2 -1
  23. data/lib/generators/pg_rls/templates/USAGE +28 -0
  24. data/lib/generators/pg_rls/{active_record/templates → templates/app/models}/abstract_base_class.rb.tt +1 -3
  25. data/lib/generators/pg_rls/{active_record/templates → templates/app/models}/model.rb.tt +0 -2
  26. data/lib/generators/pg_rls/templates/config/initializers/pg_rls.rb.tt +58 -0
  27. data/lib/generators/pg_rls/templates/db/migrate/backport_pg_rls_table.rb.tt +14 -0
  28. data/lib/generators/pg_rls/templates/db/migrate/convert_to_pg_rls_table.rb.tt +5 -0
  29. data/lib/generators/pg_rls/templates/db/migrate/convert_to_pg_rls_tenant_table.rb.tt +5 -0
  30. data/lib/generators/pg_rls/templates/db/migrate/create_pg_rls_table.rb.tt +29 -0
  31. data/lib/generators/pg_rls/templates/db/migrate/create_pg_rls_tenant_table.rb.tt +29 -0
  32. data/lib/pg_rls/active_record/connection_adapters/connection_pool.rb +31 -0
  33. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/check_rls_user_privileges.rb +207 -0
  34. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/errors.rb +17 -0
  35. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/grant_rls_user_privileges.rb +167 -0
  36. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_functions.rb +91 -0
  37. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_policies.rb +56 -0
  38. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_triggers.rb +95 -0
  39. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_user_statements.rb +127 -0
  40. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/schema_dumper.rb +71 -0
  41. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/schema_statements.rb +120 -0
  42. data/lib/pg_rls/active_record/connection_adapters/postgre_sql/sql_helper_method.rb +30 -0
  43. data/lib/pg_rls/active_record/connection_adapters/postgre_sql.rb +36 -0
  44. data/lib/pg_rls/active_record/connection_adapters.rb +12 -0
  45. data/lib/pg_rls/active_record/database_shards.rb +74 -0
  46. data/lib/pg_rls/active_record/migration/command_recorder.rb +28 -0
  47. data/lib/pg_rls/active_record/migration.rb +11 -0
  48. data/lib/pg_rls/active_record/test_databases.rb +19 -0
  49. data/lib/pg_rls/active_record.rb +11 -0
  50. data/lib/pg_rls/active_support/string_ext.rb +17 -0
  51. data/lib/pg_rls/active_support.rb +9 -0
  52. data/lib/pg_rls/connection_config.rb +61 -0
  53. data/lib/pg_rls/deprecation.rb +14 -0
  54. data/lib/pg_rls/engine.rb +8 -0
  55. data/lib/pg_rls/error.rb +10 -0
  56. data/lib/pg_rls/generators/.keep +0 -0
  57. data/lib/pg_rls/railtie.rb +1 -11
  58. data/lib/pg_rls/tasks/.keep +0 -0
  59. data/lib/pg_rls/version.rb +3 -1
  60. data/lib/pg_rls.rb +67 -151
  61. data/rbs_collection.lock.yaml +132 -0
  62. data/rbs_collection.yaml +127 -0
  63. data/review_code.sh +33 -0
  64. data/sig/generators/pg_rls/active_record/active_record_generator.rbs +43 -0
  65. data/sig/generators/pg_rls/install/install_generator.rbs +20 -0
  66. data/sig/generators/pg_rls/pg_rls_generator.rbs +9 -0
  67. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/check_rls_user_privileges.rbs +53 -0
  68. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/errors.rbs +24 -0
  69. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/grant_rls_user_privileges.rbs +55 -0
  70. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/rls_functions.rbs +31 -0
  71. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/rls_policies.rbs +28 -0
  72. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/rls_triggers.rbs +35 -0
  73. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/rls_user_statements.rbs +48 -0
  74. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/schema_dumper.rbs +38 -0
  75. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/schema_statements.rbs +67 -0
  76. data/sig/pg_rls/active_record/connection_adapters/postgre_sql/sql_helper_method.rbs +21 -0
  77. data/sig/pg_rls/active_record/connection_adapters/postgresql.rbs +10 -0
  78. data/sig/pg_rls/active_record/connection_adapters.rbs +50 -0
  79. data/sig/pg_rls/active_record/database_shards.rbs +34 -0
  80. data/sig/pg_rls/active_record/migration/command_recorder.rbs +14 -0
  81. data/sig/pg_rls/active_record/migration.rbs +8 -0
  82. data/sig/pg_rls/active_record.rbs +7 -0
  83. data/sig/pg_rls/active_support/hash_ext.rbs +11 -0
  84. data/sig/pg_rls/active_support/string_ext.rbs +27 -0
  85. data/sig/pg_rls/active_support.rbs +7 -0
  86. data/sig/pg_rls/app/models/pg_rls/record.rbs +4 -0
  87. data/sig/pg_rls/connection_config.rbs +16 -0
  88. data/sig/pg_rls/deprecation.rbs +9 -0
  89. data/sig/pg_rls/engine.rbs +7 -0
  90. data/sig/pg_rls/errors.rbs +14 -0
  91. data/sig/pg_rls/railtie.rbs +6 -0
  92. data/sig/pg_rls/tenant_test_helper.rbs +14 -0
  93. data/sig/pg_rls.rbs +60 -0
  94. data/sig/support/active_record.rbs +86 -0
  95. data/sig/support/active_support.rbs +7 -0
  96. data/sig/support/fowardable.rbs +2 -0
  97. data/sig/support/pg.rbs +12 -0
  98. data/sig/support/rails.rbs +38 -0
  99. data/start.sh +30 -0
  100. metadata +167 -48
  101. data/.rspec +0 -3
  102. data/Gemfile +0 -21
  103. data/Gemfile.lock +0 -300
  104. data/LICENSE.txt +0 -21
  105. data/bin/console +0 -15
  106. data/bin/setup +0 -8
  107. data/lib/generators/pg_rls/active_record/templates/convert_migration.rb.tt +0 -11
  108. data/lib/generators/pg_rls/active_record/templates/convert_migration_backport.rb.tt +0 -12
  109. data/lib/generators/pg_rls/active_record/templates/init_convert_migration.rb.tt +0 -11
  110. data/lib/generators/pg_rls/active_record/templates/init_migration.rb.tt +0 -25
  111. data/lib/generators/pg_rls/active_record/templates/init_model.rb.tt +0 -24
  112. data/lib/generators/pg_rls/active_record/templates/migration.rb.tt +0 -17
  113. data/lib/generators/pg_rls/base.rb +0 -36
  114. data/lib/generators/pg_rls/install_generator.rb +0 -90
  115. data/lib/generators/pg_rls.rb +0 -19
  116. data/lib/generators/templates/README +0 -22
  117. data/lib/generators/templates/pg_rls.rb.tt +0 -48
  118. data/lib/pg_rls/Rakefile +0 -7
  119. data/lib/pg_rls/current/context.rb +0 -10
  120. data/lib/pg_rls/database/admin_statements.rb +0 -28
  121. data/lib/pg_rls/database/configurations.rb +0 -46
  122. data/lib/pg_rls/database/prepared.rb +0 -40
  123. data/lib/pg_rls/database/tasks/admin_database.rake +0 -40
  124. data/lib/pg_rls/errors/index.rb +0 -4
  125. data/lib/pg_rls/errors/rake_only_error.rb +0 -12
  126. data/lib/pg_rls/errors/tenant_not_found.rb +0 -13
  127. data/lib/pg_rls/logger.rb +0 -31
  128. data/lib/pg_rls/middleware/set_reset_connection.rb +0 -93
  129. data/lib/pg_rls/middleware/sidekiq/client.rb +0 -22
  130. data/lib/pg_rls/middleware/sidekiq/server.rb +0 -19
  131. data/lib/pg_rls/middleware/sidekiq.rb +0 -11
  132. data/lib/pg_rls/middleware.rb +0 -8
  133. data/lib/pg_rls/multi_tenancy.rb +0 -32
  134. data/lib/pg_rls/schema/down_statements.rb +0 -54
  135. data/lib/pg_rls/schema/dumper.rb +0 -36
  136. data/lib/pg_rls/schema/statements.rb +0 -72
  137. data/lib/pg_rls/schema/up_statements.rb +0 -104
  138. data/lib/pg_rls/tenant.rb +0 -153
data/Gemfile.lock DELETED
@@ -1,300 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- pg_rls (0.1.10)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- actioncable (7.1.1)
10
- actionpack (= 7.1.1)
11
- activesupport (= 7.1.1)
12
- nio4r (~> 2.0)
13
- websocket-driver (>= 0.6.1)
14
- zeitwerk (~> 2.6)
15
- actionmailbox (7.1.1)
16
- actionpack (= 7.1.1)
17
- activejob (= 7.1.1)
18
- activerecord (= 7.1.1)
19
- activestorage (= 7.1.1)
20
- activesupport (= 7.1.1)
21
- mail (>= 2.7.1)
22
- net-imap
23
- net-pop
24
- net-smtp
25
- actionmailer (7.1.1)
26
- actionpack (= 7.1.1)
27
- actionview (= 7.1.1)
28
- activejob (= 7.1.1)
29
- activesupport (= 7.1.1)
30
- mail (~> 2.5, >= 2.5.4)
31
- net-imap
32
- net-pop
33
- net-smtp
34
- rails-dom-testing (~> 2.2)
35
- actionpack (7.1.1)
36
- actionview (= 7.1.1)
37
- activesupport (= 7.1.1)
38
- nokogiri (>= 1.8.5)
39
- rack (>= 2.2.4)
40
- rack-session (>= 1.0.1)
41
- rack-test (>= 0.6.3)
42
- rails-dom-testing (~> 2.2)
43
- rails-html-sanitizer (~> 1.6)
44
- actiontext (7.1.1)
45
- actionpack (= 7.1.1)
46
- activerecord (= 7.1.1)
47
- activestorage (= 7.1.1)
48
- activesupport (= 7.1.1)
49
- globalid (>= 0.6.0)
50
- nokogiri (>= 1.8.5)
51
- actionview (7.1.1)
52
- activesupport (= 7.1.1)
53
- builder (~> 3.1)
54
- erubi (~> 1.11)
55
- rails-dom-testing (~> 2.2)
56
- rails-html-sanitizer (~> 1.6)
57
- activejob (7.1.1)
58
- activesupport (= 7.1.1)
59
- globalid (>= 0.3.6)
60
- activemodel (7.1.1)
61
- activesupport (= 7.1.1)
62
- activerecord (7.1.1)
63
- activemodel (= 7.1.1)
64
- activesupport (= 7.1.1)
65
- timeout (>= 0.4.0)
66
- activestorage (7.1.1)
67
- actionpack (= 7.1.1)
68
- activejob (= 7.1.1)
69
- activerecord (= 7.1.1)
70
- activesupport (= 7.1.1)
71
- marcel (~> 1.0)
72
- activesupport (7.1.1)
73
- base64
74
- bigdecimal
75
- concurrent-ruby (~> 1.0, >= 1.0.2)
76
- connection_pool (>= 2.2.5)
77
- drb
78
- i18n (>= 1.6, < 2)
79
- minitest (>= 5.1)
80
- mutex_m
81
- tzinfo (~> 2.0)
82
- ast (2.4.2)
83
- backport (1.2.0)
84
- base64 (0.1.1)
85
- benchmark (0.2.1)
86
- bigdecimal (3.1.4)
87
- builder (3.2.4)
88
- concurrent-ruby (1.2.2)
89
- connection_pool (2.4.1)
90
- crass (1.0.6)
91
- date (3.3.3)
92
- diff-lcs (1.5.0)
93
- drb (2.1.1)
94
- ruby2_keywords
95
- e2mmap (0.1.0)
96
- erubi (1.12.0)
97
- globalid (1.2.1)
98
- activesupport (>= 6.1)
99
- i18n (1.14.1)
100
- concurrent-ruby (~> 1.0)
101
- io-console (0.6.0)
102
- irb (1.8.1)
103
- rdoc
104
- reline (>= 0.3.8)
105
- jaro_winkler (1.5.6)
106
- json (2.6.3)
107
- kramdown (2.4.0)
108
- rexml
109
- kramdown-parser-gfm (1.1.0)
110
- kramdown (~> 2.0)
111
- language_server-protocol (3.17.0.3)
112
- logger (1.6.0)
113
- loofah (2.21.4)
114
- crass (~> 1.0.2)
115
- nokogiri (>= 1.12.0)
116
- mail (2.8.1)
117
- mini_mime (>= 0.1.1)
118
- net-imap
119
- net-pop
120
- net-smtp
121
- marcel (1.0.2)
122
- mini_mime (1.1.5)
123
- minitest (5.20.0)
124
- mutex_m (0.1.2)
125
- net-imap (0.4.1)
126
- date
127
- net-protocol
128
- net-pop (0.1.2)
129
- net-protocol
130
- net-protocol (0.2.1)
131
- timeout
132
- net-smtp (0.4.0)
133
- net-protocol
134
- nio4r (2.5.9)
135
- nokogiri (1.15.4-arm64-darwin)
136
- racc (~> 1.4)
137
- nokogiri (1.15.4-x86_64-linux)
138
- racc (~> 1.4)
139
- parallel (1.23.0)
140
- parser (3.2.2.4)
141
- ast (~> 2.4.1)
142
- racc
143
- psych (5.1.1)
144
- stringio
145
- racc (1.7.1)
146
- rack (3.0.8)
147
- rack-session (2.0.0)
148
- rack (>= 3.0.0)
149
- rack-test (2.1.0)
150
- rack (>= 1.3)
151
- rackup (2.1.0)
152
- rack (>= 3)
153
- webrick (~> 1.8)
154
- rails (7.1.1)
155
- actioncable (= 7.1.1)
156
- actionmailbox (= 7.1.1)
157
- actionmailer (= 7.1.1)
158
- actionpack (= 7.1.1)
159
- actiontext (= 7.1.1)
160
- actionview (= 7.1.1)
161
- activejob (= 7.1.1)
162
- activemodel (= 7.1.1)
163
- activerecord (= 7.1.1)
164
- activestorage (= 7.1.1)
165
- activesupport (= 7.1.1)
166
- bundler (>= 1.15.0)
167
- railties (= 7.1.1)
168
- rails-dom-testing (2.2.0)
169
- activesupport (>= 5.0.0)
170
- minitest
171
- nokogiri (>= 1.6)
172
- rails-html-sanitizer (1.6.0)
173
- loofah (~> 2.21)
174
- nokogiri (~> 1.14)
175
- railties (7.1.1)
176
- actionpack (= 7.1.1)
177
- activesupport (= 7.1.1)
178
- irb
179
- rackup (>= 1.0.0)
180
- rake (>= 12.2)
181
- thor (~> 1.0, >= 1.2.2)
182
- zeitwerk (~> 2.6)
183
- rainbow (3.1.1)
184
- rake (13.0.6)
185
- rbs (2.8.4)
186
- rdoc (6.5.0)
187
- psych (>= 4.0.0)
188
- redis-client (0.22.2)
189
- connection_pool
190
- regexp_parser (2.8.2)
191
- reline (0.3.9)
192
- io-console (~> 0.5)
193
- reverse_markdown (2.1.1)
194
- nokogiri
195
- rexml (3.2.6)
196
- rspec (3.12.0)
197
- rspec-core (~> 3.12.0)
198
- rspec-expectations (~> 3.12.0)
199
- rspec-mocks (~> 3.12.0)
200
- rspec-core (3.12.2)
201
- rspec-support (~> 3.12.0)
202
- rspec-expectations (3.12.3)
203
- diff-lcs (>= 1.2.0, < 2.0)
204
- rspec-support (~> 3.12.0)
205
- rspec-mocks (3.12.6)
206
- diff-lcs (>= 1.2.0, < 2.0)
207
- rspec-support (~> 3.12.0)
208
- rspec-support (3.12.1)
209
- rubocop (1.57.0)
210
- base64 (~> 0.1.1)
211
- json (~> 2.3)
212
- language_server-protocol (>= 3.17.0)
213
- parallel (~> 1.10)
214
- parser (>= 3.2.2.4)
215
- rainbow (>= 2.2.2, < 4.0)
216
- regexp_parser (>= 1.8, < 3.0)
217
- rexml (>= 3.2.5, < 4.0)
218
- rubocop-ast (>= 1.28.1, < 2.0)
219
- ruby-progressbar (~> 1.7)
220
- unicode-display_width (>= 2.4.0, < 3.0)
221
- rubocop-ast (1.29.0)
222
- parser (>= 3.2.1.0)
223
- rubocop-capybara (2.19.0)
224
- rubocop (~> 1.41)
225
- rubocop-factory_bot (2.24.0)
226
- rubocop (~> 1.33)
227
- rubocop-performance (1.19.1)
228
- rubocop (>= 1.7.0, < 2.0)
229
- rubocop-ast (>= 0.4.0)
230
- rubocop-rails (2.21.2)
231
- activesupport (>= 4.2.0)
232
- rack (>= 1.1)
233
- rubocop (>= 1.33.0, < 2.0)
234
- rubocop-rake (0.6.0)
235
- rubocop (~> 1.0)
236
- rubocop-rspec (2.24.1)
237
- rubocop (~> 1.33)
238
- rubocop-capybara (~> 2.17)
239
- rubocop-factory_bot (~> 2.22)
240
- ruby-progressbar (1.13.0)
241
- ruby2_keywords (0.0.5)
242
- sidekiq (7.3.1)
243
- concurrent-ruby (< 2)
244
- connection_pool (>= 2.3.0)
245
- logger
246
- rack (>= 2.2.4)
247
- redis-client (>= 0.22.2)
248
- solargraph (0.49.0)
249
- backport (~> 1.2)
250
- benchmark
251
- bundler (~> 2.0)
252
- diff-lcs (~> 1.4)
253
- e2mmap
254
- jaro_winkler (~> 1.5)
255
- kramdown (~> 2.3)
256
- kramdown-parser-gfm (~> 1.1)
257
- parser (~> 3.0)
258
- rbs (~> 2.0)
259
- reverse_markdown (~> 2.0)
260
- rubocop (~> 1.38)
261
- thor (~> 1.0)
262
- tilt (~> 2.0)
263
- yard (~> 0.9, >= 0.9.24)
264
- stringio (3.0.8)
265
- thor (1.2.2)
266
- tilt (2.3.0)
267
- timeout (0.4.0)
268
- tzinfo (2.0.6)
269
- concurrent-ruby (~> 1.0)
270
- unicode-display_width (2.5.0)
271
- webrick (1.8.1)
272
- websocket-driver (0.7.6)
273
- websocket-extensions (>= 0.1.0)
274
- websocket-extensions (0.1.5)
275
- yard (0.9.34)
276
- zeitwerk (2.6.12)
277
-
278
- PLATFORMS
279
- arm64-darwin-22
280
- arm64-darwin-23
281
- x86_64-linux
282
-
283
- DEPENDENCIES
284
- pg_rls!
285
- rails (~> 7.0, >= 7.0.4)
286
- rake
287
- rspec
288
- rubocop
289
- rubocop-performance
290
- rubocop-rails
291
- rubocop-rake
292
- rubocop-rspec
293
- sidekiq
294
- solargraph
295
-
296
- RUBY VERSION
297
- ruby 3.2.2p53
298
-
299
- BUNDLED WITH
300
- 2.4.22
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 Daniel Laloush
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'pg_rls'
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require 'irb'
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class PgRlsConvert<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
- def up
5
- convert_to_rls_table :<%= table_name %>
6
- end
7
-
8
- def down
9
- revert_rls_table :<%= table_name %>
10
- end
11
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class PgRlsBackport<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
- def up
5
- # Suggested Code:
6
- # PgRls.on_each_tenant do |tenant|
7
- # tenant.<%= table_name %>.in_batches(of: 100) do |<%= table_name %>|
8
- # <%= table_name %>.each { |<%= table_name.singularize %>| <%= table_name.singularize %>.update_attribute('tenant_id', tenant.tenant_id) }
9
- # end
10
- # end
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class PgRlsConvertTenant<%= PgRls.table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
- def up
5
- convert_to_rls_tenant_table :<%= table_name %>
6
- end
7
-
8
- def down
9
- revert_rls_tenant_table :<%= table_name %>
10
- end
11
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class PgRlsCreateTenant<%= PgRls.table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
- def up
5
- create_rls_tenant_table :<%= table_name %>, id: :uuid do |t|
6
- t.string :name
7
- t.string :logo
8
-
9
- t.string :identification
10
- t.string :subdomain
11
- t.string :domain
12
-
13
- t.timestamps
14
- end
15
-
16
- add_index :<%= table_name %>, :name, unique: true
17
- add_index :<%= table_name %>, :identification, unique: true
18
- add_index :<%= table_name %>, :domain, unique: true
19
- add_index :<%= table_name %>, :subdomain, unique: true
20
- end
21
-
22
- def down
23
- drop_rls_tenant_table :<%= table_name %>
24
- end
25
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- <% module_namespacing do -%>
4
- class <%= PgRls.class_name.camelize %> < <%= parent_class_name.classify %>
5
- <% attributes.select(&:reference?).each do |attribute| -%>
6
- belongs_to :<%= attribute.name %><%= ", polymorphic: true" if attribute.polymorphic? %>
7
- <% end -%>
8
- <% attributes.select(&:rich_text?).each do |attribute| -%>
9
- has_rich_text :<%= attribute.name %>
10
- <% end -%>
11
- <% attributes.select(&:attachment?).each do |attribute| -%>
12
- has_one_attached :<%= attribute.name %>
13
- <% end -%>
14
- <% attributes.select(&:attachments?).each do |attribute| -%>
15
- has_many_attached :<%= attribute.name %>
16
- <% end -%>
17
- <% attributes.select(&:token?).each do |attribute| -%>
18
- has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
19
- <% end -%>
20
- <% if attributes.any?(&:password_digest?) -%>
21
- has_secure_password
22
- <% end -%>
23
- end
24
- <% end -%>
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class PgRlsCreate<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
- def up
5
- create_rls_table :<%= table_name %><%= primary_key_type %> do |t|
6
- <% attributes.each do |attribute| -%>
7
- t.<%= attribute.type %> :<%= attribute.name %>
8
- <% end -%>
9
-
10
- t.timestamps null: false
11
- end
12
- end
13
-
14
- def down
15
- drop_rls_table :<%= table_name %>
16
- end
17
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PgRls
4
- # Main Definition for Generator
5
- module Base
6
- protected
7
-
8
- def nested_parent_name
9
- @class_path.join('/')
10
- end
11
-
12
- def nested_parent_id
13
- "#{nested_parent_name}_id"
14
- end
15
-
16
- def nested_parent_class_name
17
- nested_parent_name.classify
18
- end
19
-
20
- def plural_nested_parent_name
21
- nested_parent_name.pluralize
22
- end
23
-
24
- def class_path
25
- []
26
- end
27
-
28
- def regular_class_path
29
- []
30
- end
31
-
32
- def controller_class_path
33
- []
34
- end
35
- end
36
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails/generators/base'
4
- require 'securerandom'
5
-
6
- module PgRls
7
- module Generators
8
- MissingORMError = Class.new(Thor::Error)
9
- # Installer Generator
10
- class InstallGenerator < Rails::Generators::Base
11
- def initialize(*args)
12
- tenant_model_or_table = args.first
13
- if tenant_model_or_table.present?
14
- PgRls.table_name = tenant_model_or_table.first.pluralize
15
- PgRls.class_name = tenant_model_or_table.first.singularize
16
- end
17
- super
18
- end
19
- ENVIRONMENT_LINE = 'Rails.application.initialize!'
20
- ENVIRONMENT_PATH = 'config/environment.rb'
21
-
22
- APPLICATION_LINE = 'class Application < Rails::Application'
23
- APPLICATION_PATH = 'config/application.rb'
24
-
25
- APPLICATION_RECORD_LINE = 'class ApplicationRecord < ActiveRecord::Base'
26
- APPLICATION_RECORD_PATH = 'app/models/application_record.rb'
27
-
28
- APPLICATION_CONTROLLER_LINE = 'class ApplicationController < ActionController::Base'
29
- APPLICATION_CONTROLLER_PATH = 'app/controllers/application_controller.rb'
30
-
31
- source_root File.expand_path('../templates', __dir__)
32
-
33
- desc 'Creates a PgRls initializer and copy locale files to your application.'
34
-
35
- hook_for :orm, required: true
36
-
37
- def orm_error_message
38
- <<~ERROR
39
- An ORM must be set to install PgRls in your application.
40
- Be sure to have an ORM like Active Record or loaded in your
41
- app or configure your own at `config/application.rb`.
42
- config.generators do |g|
43
- g.orm :your_orm_gem
44
- end
45
- ERROR
46
- end
47
-
48
- def copy_initializer
49
- raise MissingORMError, orm_error_message unless options[:orm]
50
-
51
- inject_include_to_application_controller
52
- template 'pg_rls.rb.tt', 'config/initializers/pg_rls.rb'
53
- end
54
-
55
- def inject_include_to_environment
56
- return if environment_already_included?
57
-
58
- gsub_file(ENVIRONMENT_PATH, /(#{Regexp.escape(ENVIRONMENT_LINE)})/mio) do |match|
59
- "require_relative 'initializers/pg_rls'\n#{match}"
60
- end
61
- end
62
-
63
- def inject_include_to_application_controller
64
- return if aplication_controller_already_included?
65
-
66
- gsub_file(APPLICATION_CONTROLLER_PATH, /(#{Regexp.escape(APPLICATION_CONTROLLER_LINE)})/mio) do |match|
67
- "#{match}\n include PgRls::MultiTenancy\n"
68
- end
69
- end
70
-
71
- def aplication_controller_already_included?
72
- File.readlines(APPLICATION_CONTROLLER_PATH).grep(/include PgRls::MultiTenancy/).any?
73
- end
74
-
75
- def environment_already_included?
76
- File.readlines(ENVIRONMENT_PATH).grep(%r{require_relative 'initializers/pg_rls'}).any?
77
- end
78
-
79
- def initialize_error_text
80
- <<~ERROR
81
- TO DO
82
- ERROR
83
- end
84
-
85
- def show_readme
86
- readme 'README' if behavior == :invoke
87
- end
88
- end
89
- end
90
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails/generators/named_base'
4
- require 'rails/generators/active_model'
5
- require 'rails/generators/active_record/migration'
6
- require 'active_record'
7
-
8
- module PgRls
9
- module Generators # :nodoc:
10
- class PgRlsGenerator < Rails::Generators::NamedBase # :nodoc:
11
- include PgRls::Generators::Migration
12
-
13
- # Set the current directory as base for the inherited generators.
14
- def self.base_root
15
- __dir__
16
- end
17
- end
18
- end
19
- end
@@ -1,22 +0,0 @@
1
- README
2
- ===============================================================================
3
- WARNING!!
4
-
5
- PgRls required that ActiveRecord format migration as SQL
6
-
7
- Once you remove a tenant all of his data would be removed as well
8
-
9
- If you're setting a custom user, make sure to regenerate the structure.sql since
10
- Postgresql policies are created on each user
11
-
12
- ===============================================================================
13
-
14
- to generate secure model run
15
-
16
- rails g pg_rls model_name
17
-
18
- or
19
-
20
- rails generate pg_rls model_name
21
-
22
- ===============================================================================
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pg_rls'
4
-
5
- PgRls.setup do |config|
6
- ActiveRecord::ConnectionAdapters::AbstractAdapter.include PgRls::Schema::Statements
7
-
8
- # Do not remove this value after initialization
9
- config.class_name = :<%= PgRls.class_name %>
10
- config.table_name = :<%= PgRls.table_name %>
11
- config.search_methods = <%= PgRls.search_methods %>
12
- # If you are using `solid_queue`, `solid_cache`, or `solid_cable` with a sharding configuration,
13
- # we recommend excluding these shards from Row-Level Security (RLS) to avoid the need to reset
14
- # RLS on each shard.
15
- #
16
- # By default, RLS will be enabled for all shards.
17
- # You can specify which shards to exclude from RLS using the `config.excluded_shards` option:
18
- #
19
- # config.excluded_shards = []
20
- #
21
- # Note: While it's technically possible to leave `solid_cache` and `solid_cable` under RLS,
22
- # it is generally unnecessary and may introduce complexity without added benefit.
23
-
24
- ##
25
- ## Uncomment this lines if you have a custome user per environment
26
- ## don't forget to grant the required privilange in order for it to run
27
- ##
28
- ## Remember that PgRls is adding triggers that would set the RLS to the default user
29
- ## make sure you recreate the structure.sql on each environment
30
- ##
31
- # config.username = Rails.application.credentials.dig(:database, :username)
32
- # config.password = Rails.application.credentials.dig(:database, :password)
33
-
34
- ## ------------------------------ Middleware SetResetConnection -----------------------------
35
- ## Uncomment this lines if you're using SetResetConnection Middleware
36
- #
37
- # config.session_store_server = Rails.application.config_for(:redis).session
38
- #
39
- ## Uncomment this line if you're not using warden as your authentication system or if you
40
- ## changed the default warden key. Devise, uses warden authentication.
41
- # config.session_store_default_warden_key = '2'
42
- #
43
- ## Uncomment this line if you're setting a diferent session key than stablished under your
44
- ## redis server configuration
45
- # config.session_key_prefix = '_hub_session'
46
- ##
47
- ## ------------------------------ Middleware SetResetConnection -----------------------------
48
- end
data/lib/pg_rls/Rakefile DELETED
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../pg_rls'
4
-
5
- path = File.expand_path(__dir__)
6
-
7
- Dir.glob("#{path}/database/tasks/**/*.rake").each { |f| import f }
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PgRls
4
- # Current Context
5
- module Current
6
- class Context < ActiveSupport::CurrentAttributes
7
- attribute :tenant
8
- end
9
- end
10
- end