Skip to content

Commit

Permalink
Try this
Browse files Browse the repository at this point in the history
  • Loading branch information
stevschmid committed Mar 26, 2024
1 parent 0f2719f commit 1536222
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/tasks/rls.rake
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace :rls do
RLS.without_rls do
# Make sure query can be executed even if database in database.yml is not around yet
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |config|
config = config.configuration_hash.merge(database: 'postgres', schema_search_path: 'public')
config = config.configuration_hash.merge(database: "postgres", schema_search_path: "public")
ActiveRecord::Base.establish_connection(config)

ActiveRecord::Base.connection.execute <<~SQL
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace :rls do
RLS.without_rls do
# Make sure query can be executed even if database in database.yml is not around yet
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |config|
config = config.configuration_hash.merge(database: 'postgres', schema_search_path: 'public')
config = config.configuration_hash.merge(database: "postgres", schema_search_path: "public")
ActiveRecord::Base.establish_connection(config)

ActiveRecord::Base.connection.execute <<~SQL
Expand Down
39 changes: 24 additions & 15 deletions spec/tasks/rls_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,34 @@
end
end

describe "rls:create_role" do
subject { -> { run_and_capture_rake("rls:create_role") } }
describe "setup role for dev/test env" do
before do
connection = double("Connection")
allow(ActiveRecord::Base).to receive(:connection).and_return(connection)
allow(connection).to receive(:execute)
allow(connection).to receive(:disconnect!)
end

specify do
expect(RLS).to receive(:disable!).ordered
expect(RLS.connection).to receive(:execute).with(/CREATE ROLE "dummy_rls_test"/).ordered
expect(RLS).to receive(:enable!).ordered
subject.call
describe "rls:create_role" do
subject { -> { run_and_capture_rake("rls:create_role") } }

specify do
expect(RLS).to receive(:disable!).ordered
expect(connection).to receive(:execute).with(/CREATE ROLE "dummy_rls_test" WITH NOLOGIN/).ordered
expect(RLS).to receive(:enable!).ordered
subject.call
end
end
end

describe "rls:drop_role" do
subject { -> { run_and_capture_rake("rls:drop_role") } }
describe "rls:drop_role" do
subject { -> { run_and_capture_rake("rls:drop_role") } }

specify do
expect(RLS).to receive(:disable!).ordered
expect(RLS.connection).to receive(:execute).with(/DROP ROLE "dummy_rls_test"/).ordered
expect(RLS).to receive(:enable!).ordered
subject.call
specify do
expect(RLS).to receive(:disable!).ordered
expect(connection).to receive(:execute).with(/DROP ROLE "dummy_rls_test"/).ordered
expect(RLS).to receive(:enable!).ordered
subject.call
end
end
end

Expand Down

0 comments on commit 1536222

Please sign in to comment.