Skip to content

Commit

Permalink
Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
stevschmid committed Mar 25, 2024
1 parent e7d7358 commit d89b5b3
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
6 changes: 3 additions & 3 deletions lib/rls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

module RLS
class << self
SET_CUSTOMER_ID_SQL = 'SET rls.tenant_id = %s'.freeze
RESET_CUSTOMER_ID_SQL = 'RESET rls.tenant_id'.freeze
SET_CUSTOMER_ID_SQL = "SET rls.tenant_id = %s".freeze
RESET_CUSTOMER_ID_SQL = "RESET rls.tenant_id".freeze

def configure(&block)
block.call(configuration)
Expand Down Expand Up @@ -59,7 +59,7 @@ def process(tenant_id, &block)
end

def set!(tenant_id)
connection.rls_set(tenant_id:)
connection.rls_set(tenant_id: tenant_id)
end

def reset!
Expand Down
6 changes: 3 additions & 3 deletions lib/rls/extensions/postgresql_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module RLS
module Extensions
module PostgreSQLAdapter
SET_ROLE_SQL = 'SET ROLE %s'.freeze
SET_ROLE_SQL = "SET ROLE %s".freeze

SET_TENANT_ID_SQL = 'SET rls.tenant_id = %s'.freeze
RESET_TENANT_ID_SQL = 'RESET rls.tenant_id'.freeze
SET_TENANT_ID_SQL = "SET rls.tenant_id = %s".freeze
RESET_TENANT_ID_SQL = "RESET rls.tenant_id".freeze

def initialize(...)
super
Expand Down
10 changes: 8 additions & 2 deletions lib/rls/extensions/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ module SchemaDumper
def tables(stream)
super

sql = "SELECT policyname, tablename, cmd, permissive, roles, qual, with_check FROM pg_policies WHERE schemaname = 'public' ORDER BY tablename"
sql = <<~SQL
SELECT policyname, tablename, cmd, permissive, roles, qual, with_check
FROM pg_policies
WHERE schemaname = 'public'
ORDER BY tablename
SQL

@connection.execute(sql).each do |row|
stream.puts " rls_tenant_table \"#{row['tablename']}\""
stream.puts " rls_tenant_table \"#{row["tablename"]}\""
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rls/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module RLS
class Railtie < Rails::Railtie
config.rls = ActiveSupport::OrderedOptions.new
config.rls.role = 'app_rls'
config.rls.role = "app_rls"

config.to_prepare do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(RLS::Extensions::PostgreSQLAdapter)
Expand Down
10 changes: 5 additions & 5 deletions lib/tasks/rls.rake
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# frozen_string_literal: true

Rake.application.top_level_tasks.each do |task_name|
next unless task_name.start_with?('db:')
next unless task_name.start_with?("db:")

# disable RLS role before running the task
Rake::Task[task_name].enhance(['rls:disable'])
Rake::Task[task_name].enhance(["rls:disable"])

# enable RLS role after running the task
Rake::Task[task_name].enhance do
Rake::Task['rls:enable'].invoke
Rake::Task["rls:enable"].invoke
end

# make sure they run again if multiple tasks are run
Rake::Task[task_name].enhance do
Rake::Task['rls:enable'].reenable
Rake::Task['rls:disable'].reenable
Rake::Task["rls:enable"].reenable
Rake::Task["rls:disable"].reenable
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/postgresql_adapter_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
expect(role).to eq "app_rls"
end

context 'when admin' do
context "when admin" do
specify do
RLS.admin = true
connection.disconnect!
Expand Down
3 changes: 2 additions & 1 deletion spec/integration/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
get "/posts"
expect(response.body).not_to include("ACME Post")

post "/posts", params: { post: { title: "New G-Virus", content: "This is classified information", tenant_id: umbrella_corp.id } }
post "/posts",
params: { post: { title: "New G-Virus", content: "This is classified information", tenant_id: umbrella_corp.id } }
expect(response).to have_http_status(:found)

get "/posts"
Expand Down
4 changes: 2 additions & 2 deletions spec/tasks/rls_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
end
end

context 'single task' do
context "single task" do
let(:top_level_tasks) { ["db:migrate:status"] }

subject { -> { run_and_capture_rake("db:migrate:status") } }
Expand All @@ -75,7 +75,7 @@
end
end

context 'multiple tasks' do
context "multiple tasks" do
let(:top_level_tasks) { ["db:migrate", "db:version"] }

subject { -> { run_and_capture_rake("db:migrate", "db:version") } }
Expand Down
20 changes: 10 additions & 10 deletions spec/unit/rls/extensions/postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,36 @@ def clear_query_cache

let(:connection) { MyAdapter.new }

describe '#initialize' do
it 'sets the role' do
describe "#initialize" do
it "sets the role" do
expect_any_instance_of(MyAdapter).to receive(:execute).with("SET ROLE 'app_rls'")
connection # initialize
end

context 'when admin' do
context "when admin" do
before { RLS.admin = true }

it 'does not set the role' do
it "does not set the role" do
expect_any_instance_of(MyAdapter).not_to receive(:execute).with("SET ROLE 'app_rls'")
connection # initialize
end
end
end

describe '#rls_set' do
it 'sets the tenant_id' do
describe "#rls_set" do
it "sets the tenant_id" do
expect(connection).to receive(:execute).with("SET rls.tenant_id = '123'")
connection.rls_set(tenant_id: '123')
connection.rls_set(tenant_id: "123")
end
end

describe '#rls_reset' do
it 'resets the tenant_id' do
describe "#rls_reset" do
it "resets the tenant_id" do
expect(connection).to receive(:execute).with("RESET rls.tenant_id")
connection.rls_reset
end

it 'clears the query cache' do
it "clears the query cache" do
expect(connection).to receive(:clear_query_cache)
connection.rls_reset
end
Expand Down

0 comments on commit d89b5b3

Please sign in to comment.