diff --git a/lib/torque/postgresql/adapter/database_statements.rb b/lib/torque/postgresql/adapter/database_statements.rb index 6dfbab6..d287bc2 100644 --- a/lib/torque/postgresql/adapter/database_statements.rb +++ b/lib/torque/postgresql/adapter/database_statements.rb @@ -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 diff --git a/lib/torque/postgresql/adapter/schema_statements.rb b/lib/torque/postgresql/adapter/schema_statements.rb index ce52f0e..1d4d9c9 100644 --- a/lib/torque/postgresql/adapter/schema_statements.rb +++ b/lib/torque/postgresql/adapter/schema_statements.rb @@ -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