Skip to content

Commit

Permalink
Reset & set role for fixtures
Browse files Browse the repository at this point in the history
Towards crashtech#33
  • Loading branch information
mlt committed Jul 19, 2019
1 parent 1b75f0e commit 5f40397
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/torque/postgresql/adapter/database_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ def column_definitions(table_name) # :nodoc:
SQL
end

def insert_fixtures_set(*_args)
r = Rails.configuration.respond_to?(:test_role) && Rails.configuration.test_role
set_role if r
super
return unless r

set_role(r)
ActiveRecord::Base.configurations[Rails.env]['username'] = r
end

def reset_pk_sequence!(*_args)
r = Rails.configuration.respond_to?(:test_role) && Rails.configuration.test_role
set_role if r
super
return unless r

set_role(r)
ActiveRecord::Base.configurations[Rails.env]['username'] = r
end

end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/torque/postgresql/adapter/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def create_table(table_name, **options, &block)
super table_name, **options, &block
end

def role
@role ||= select_value('select current_user')
end

def set_role(role = nil)
@role = role
execute "set role #{role.present? ? quote(role) : 'none'}"
end

private

# This waits for the second call to really return the table definition
Expand Down

0 comments on commit 5f40397

Please sign in to comment.