pg_rls 1.0.0 → 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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pg_rls/version.rb +1 -1
  3. metadata +1 -37
  4. data/.rspec +0 -3
  5. data/LICENSE.txt +0 -21
  6. data/lib/generators/pg_rls/active_record/templates/abstract_base_class.rb.tt +0 -9
  7. data/lib/generators/pg_rls/active_record/templates/convert_migration.rb.tt +0 -11
  8. data/lib/generators/pg_rls/active_record/templates/convert_migration_backport.rb.tt +0 -12
  9. data/lib/generators/pg_rls/active_record/templates/init_convert_migration.rb.tt +0 -11
  10. data/lib/generators/pg_rls/active_record/templates/init_migration.rb.tt +0 -25
  11. data/lib/generators/pg_rls/active_record/templates/init_model.rb.tt +0 -24
  12. data/lib/generators/pg_rls/active_record/templates/migration.rb.tt +0 -17
  13. data/lib/generators/pg_rls/active_record/templates/model.rb.tt +0 -24
  14. data/lib/generators/pg_rls/base.rb +0 -36
  15. data/lib/generators/pg_rls/install_generator.rb +0 -90
  16. data/lib/generators/pg_rls.rb +0 -19
  17. data/lib/generators/templates/README +0 -22
  18. data/lib/generators/templates/pg_rls.rb.tt +0 -48
  19. data/lib/pg_rls/Rakefile +0 -7
  20. data/lib/pg_rls/current/context.rb +0 -10
  21. data/lib/pg_rls/database/admin_statements.rb +0 -28
  22. data/lib/pg_rls/database/configurations.rb +0 -46
  23. data/lib/pg_rls/database/prepared.rb +0 -40
  24. data/lib/pg_rls/database/tasks/admin_database.rake +0 -40
  25. data/lib/pg_rls/errors/index.rb +0 -4
  26. data/lib/pg_rls/errors/rake_only_error.rb +0 -12
  27. data/lib/pg_rls/errors/tenant_not_found.rb +0 -13
  28. data/lib/pg_rls/logger.rb +0 -31
  29. data/lib/pg_rls/middleware/set_reset_connection.rb +0 -93
  30. data/lib/pg_rls/middleware/sidekiq/client.rb +0 -22
  31. data/lib/pg_rls/middleware/sidekiq/server.rb +0 -19
  32. data/lib/pg_rls/middleware/sidekiq.rb +0 -11
  33. data/lib/pg_rls/middleware.rb +0 -8
  34. data/lib/pg_rls/multi_tenancy.rb +0 -32
  35. data/lib/pg_rls/schema/down_statements.rb +0 -54
  36. data/lib/pg_rls/schema/dumper.rb +0 -36
  37. data/lib/pg_rls/schema/statements.rb +0 -72
  38. data/lib/pg_rls/schema/up_statements.rb +0 -104
  39. data/lib/pg_rls/tenant.rb +0 -153
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2174ba1b02af4d50da23cd95648ca0098b376cef2f30796a159dbfc6b1ccf8f
4
- data.tar.gz: e375b3153b94a1ed679f8ab69def4067f7b3c151a6d17c70584c17782498a9af
3
+ metadata.gz: 278c5a74c59b9b7b98709a4d36bd50d30facc864b140b231e5aa7d5a1bc26cfd
4
+ data.tar.gz: 30fab69ef11cf047c3c25213463160f66959c0d6f2da97a09e1bbfb6365247aa
5
5
  SHA512:
6
- metadata.gz: f76546f92cbb5ab1b8f59741df0e504878ee68c755f2fe3791e576789a7f88edf17644929da8752882eb0c8f1dcce4f57fc090cd60763f96d9fb6e4f584f20d1
7
- data.tar.gz: f2f377d2f78ddba9ca9776dc64880f00875993f4ed02364e85d3156ebbdeb7ae1a4bb75a925707ff7d39f2f61449a4a56c9f425ae55db40c1dd00bdf7eb2ded3
6
+ metadata.gz: 6df51d2edd3e608a4a9a0bdfd5c42c21cf9462b85e6a3b572f151991990d292b6e6fd625578254923145b836ea4129aacffcc08aeca0ca03eda9537be4dc8caa
7
+ data.tar.gz: 6f409cafedc4a4208e110cca2c8e13d76175272e4c4d022b1e87d3c1b3991e2dfe875f1fa80b6a1141654afa8fe31cbe6dd3c99faf29b5eccb50970e81207e76
@@ -2,6 +2,6 @@
2
2
 
3
3
  # :nocov:
4
4
  module PgRls
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.1"
6
6
  end
7
7
  # :nocov:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rls
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
@@ -87,13 +87,11 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - ".rspec"
91
90
  - ".rubocop.yml"
92
91
  - ".ruby-version"
93
92
  - CHANGELOG.md
94
93
  - CODE_OF_CONDUCT.md
95
94
  - Guardfile
96
- - LICENSE.txt
97
95
  - README.md
98
96
  - Rakefile
99
97
  - Steepfile
@@ -107,19 +105,8 @@ files:
107
105
  - app/models/pg_rls/tenant/switchable.rb
108
106
  - assets/logo.svg
109
107
  - docker-compose.yml
110
- - lib/generators/pg_rls.rb
111
108
  - lib/generators/pg_rls/active_record/active_record_generator.rb
112
- - lib/generators/pg_rls/active_record/templates/abstract_base_class.rb.tt
113
- - lib/generators/pg_rls/active_record/templates/convert_migration.rb.tt
114
- - lib/generators/pg_rls/active_record/templates/convert_migration_backport.rb.tt
115
- - lib/generators/pg_rls/active_record/templates/init_convert_migration.rb.tt
116
- - lib/generators/pg_rls/active_record/templates/init_migration.rb.tt
117
- - lib/generators/pg_rls/active_record/templates/init_model.rb.tt
118
- - lib/generators/pg_rls/active_record/templates/migration.rb.tt
119
- - lib/generators/pg_rls/active_record/templates/model.rb.tt
120
- - lib/generators/pg_rls/base.rb
121
109
  - lib/generators/pg_rls/install/install_generator.rb
122
- - lib/generators/pg_rls/install_generator.rb
123
110
  - lib/generators/pg_rls/pg_rls_generator.rb
124
111
  - lib/generators/pg_rls/templates/USAGE
125
112
  - lib/generators/pg_rls/templates/app/models/abstract_base_class.rb.tt
@@ -130,10 +117,7 @@ files:
130
117
  - lib/generators/pg_rls/templates/db/migrate/convert_to_pg_rls_tenant_table.rb.tt
131
118
  - lib/generators/pg_rls/templates/db/migrate/create_pg_rls_table.rb.tt
132
119
  - lib/generators/pg_rls/templates/db/migrate/create_pg_rls_tenant_table.rb.tt
133
- - lib/generators/templates/README
134
- - lib/generators/templates/pg_rls.rb.tt
135
120
  - lib/pg_rls.rb
136
- - lib/pg_rls/Rakefile
137
121
  - lib/pg_rls/active_record.rb
138
122
  - lib/pg_rls/active_record/connection_adapters.rb
139
123
  - lib/pg_rls/active_record/connection_adapters/connection_pool.rb
@@ -155,32 +139,12 @@ files:
155
139
  - lib/pg_rls/active_support.rb
156
140
  - lib/pg_rls/active_support/string_ext.rb
157
141
  - lib/pg_rls/connection_config.rb
158
- - lib/pg_rls/current/context.rb
159
- - lib/pg_rls/database/admin_statements.rb
160
- - lib/pg_rls/database/configurations.rb
161
- - lib/pg_rls/database/prepared.rb
162
- - lib/pg_rls/database/tasks/admin_database.rake
163
142
  - lib/pg_rls/deprecation.rb
164
143
  - lib/pg_rls/engine.rb
165
144
  - lib/pg_rls/error.rb
166
- - lib/pg_rls/errors/index.rb
167
- - lib/pg_rls/errors/rake_only_error.rb
168
- - lib/pg_rls/errors/tenant_not_found.rb
169
145
  - lib/pg_rls/generators/.keep
170
- - lib/pg_rls/logger.rb
171
- - lib/pg_rls/middleware.rb
172
- - lib/pg_rls/middleware/set_reset_connection.rb
173
- - lib/pg_rls/middleware/sidekiq.rb
174
- - lib/pg_rls/middleware/sidekiq/client.rb
175
- - lib/pg_rls/middleware/sidekiq/server.rb
176
- - lib/pg_rls/multi_tenancy.rb
177
146
  - lib/pg_rls/railtie.rb
178
- - lib/pg_rls/schema/down_statements.rb
179
- - lib/pg_rls/schema/dumper.rb
180
- - lib/pg_rls/schema/statements.rb
181
- - lib/pg_rls/schema/up_statements.rb
182
147
  - lib/pg_rls/tasks/.keep
183
- - lib/pg_rls/tenant.rb
184
148
  - lib/pg_rls/version.rb
185
149
  - rbs_collection.lock.yaml
186
150
  - rbs_collection.yaml
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
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.
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- <% module_namespacing do -%>
4
- class <%= abstract_class_name %> < ApplicationRecord
5
- self.abstract_class = true
6
-
7
- connects_to database: { <%= ActiveRecord.writing_role %>: :<%= database -%> }
8
- end
9
- <% end -%>
@@ -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,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- <% module_namespacing do -%>
4
- class <%= class_name %> < <%= 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,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
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PgRls
4
- module Admin
5
- module ActiveRecord
6
- module Migrator
7
- def initialize(*args)
8
- PgRls.instance_variable_set(:@as_db_admin, true)
9
- super
10
- end
11
- end
12
-
13
- module Tasks
14
- module DatabaseTasks
15
- def resolve_configuration(configuration)
16
- PgRls.instance_variable_set(:@as_db_admin, true) unless PgRls.as_db_admin?
17
- super
18
- end
19
-
20
- def migration_class
21
- PgRls.instance_variable_set(:@as_db_admin, true) unless PgRls.as_db_admin?
22
- super
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record/database_configurations'
4
-
5
- module ActiveRecord
6
- # ActiveRecord::DatabaseConfigurations
7
- class DatabaseConfigurations
8
- class HashConfig
9
- def initialize(env_name, name, configuration_hash)
10
- @env_name = env_name
11
- @name = name
12
- @configuration_hash = configuration_hash
13
- end
14
-
15
- def configuration_hash
16
- return @configuration_hash if PgRls.excluded_shards.include?(@name.to_s)
17
-
18
- reset_pg_rls_configuration if db_changed?
19
-
20
- return admin_configuration_hash if PgRls.as_db_admin?
21
-
22
- rls_configuration_hash
23
- end
24
-
25
- def admin_configuration_hash
26
- @admin_configuration_hash ||= @configuration_hash
27
- end
28
-
29
- def rls_configuration_hash
30
- @rls_configuration_hash ||= @configuration_hash.deep_dup.tap do |config|
31
- config[:username] = PgRls.username
32
- config[:password] = PgRls.password
33
- end.freeze
34
- end
35
-
36
- def db_changed?
37
- admin_configuration_hash[:database] != @configuration_hash[:database]
38
- end
39
-
40
- def reset_pg_rls_configuration
41
- @rls_configuration_hash = nil
42
- @admin_configuration_hash = nil
43
- end
44
- end
45
- end
46
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PgRls
4
- module Database
5
- # Prepare database for test unit
6
- module Prepared
7
- class << self
8
- def grant_user_credentials(name: PgRls.username, schema: 'public')
9
- PgRls.admin_execute <<-SQL.squish
10
- DO
11
- $do$
12
- BEGIN
13
- IF NOT EXISTS (
14
- SELECT table_catalog, table_schema, table_name, privilege_type
15
- FROM information_schema.table_privileges
16
- WHERE grantee = '#{name}'
17
- ) THEN
18
- GRANT ALL PRIVILEGES ON TABLE schema_migrations TO #{name};
19
- GRANT USAGE ON SCHEMA #{schema} TO #{name};
20
- ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema}
21
- GRANT USAGE, SELECT
22
- ON SEQUENCES TO #{name};
23
- ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema}
24
- GRANT SELECT, INSERT, UPDATE, DELETE
25
- ON TABLES TO #{name};
26
- GRANT SELECT, INSERT, UPDATE, DELETE
27
- ON ALL TABLES IN SCHEMA #{schema}
28
- TO #{name};
29
- GRANT USAGE, SELECT
30
- ON ALL SEQUENCES IN SCHEMA #{schema}
31
- TO #{name};
32
- END IF;
33
- END;
34
- $do$;
35
- SQL
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # OVERIDE RAILS TASK
4
- Rake::TaskManager.class_eval do
5
- def alias_task(fq_name)
6
- new_name = "#{fq_name}:original"
7
- @tasks[new_name] = @tasks.delete(fq_name) unless @tasks[fq_name].nil?
8
- end
9
- end
10
-
11
- def alias_task(fq_name)
12
- Rake.application.alias_task(fq_name)
13
- end
14
-
15
- def override_task(*args, &)
16
- name, _params, _deps = Rake.application.resolve_args(args.dup)
17
- fq_name = Rake.application.instance_variable_get(:@scope).to_a.reverse.push(name).join(':')
18
- alias_task(fq_name)
19
- Rake::Task.define_task(*args, &)
20
- end
21
-
22
- namespace :db do
23
- include PgRls::Schema::UpStatements
24
-
25
- override_task :load_config do
26
- PgRls.instance_variable_set(:@as_db_admin, true)
27
- Rake::Task['db:load_config:original'].invoke
28
- end
29
-
30
- override_task grant_usage: :load_config do
31
- create_rls_user
32
- end
33
-
34
- namespace :test do
35
- override_task grant_usage: :load_config do
36
- PgRls.instance_variable_set(:@as_db_admin, true)
37
- create_rls_user
38
- end
39
- end
40
- end