Skip to content

Commit

Permalink
Make sure RLS role is set when connection is re-established
Browse files Browse the repository at this point in the history
E.g. after a db restart
  • Loading branch information
stevschmid committed Sep 23, 2024
1 parent bbd6cf0 commit 9f9583a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/rls/extensions/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module PostgreSQLAdapter
SET_TENANT_ID_SQL = "SET rls.tenant_id = %s".freeze
RESET_TENANT_ID_SQL = "RESET rls.tenant_id".freeze

def initialize(...)
def configure_connection
# new or re-establishing connection
super
execute(format(SET_ROLE_SQL, quote(RLS.role))) unless Thread.current[:rls_admin]
end
Expand Down
8 changes: 6 additions & 2 deletions spec/unit/rls/extensions/postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ def execute(str)
str
end

def configure_connection
# noop
end

def clear_query_cache
nil
end
end

let(:connection) { MyAdapter.new }

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

context "when admin" do
Expand Down

0 comments on commit 9f9583a

Please sign in to comment.