Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
How can I run `SET ROLE` automatically for postgres ActiveRecord migrations?

I'd like to setup app-specific users for a database, just for auditing/debugging purposes, but use the root "postgres" user role for table creation, since it already has the default privileges set up appropriately (and also because in my case, most of the tables are already owned by that user).

The app would only need this table creation/modification permission when running migrations, but I would be fine with them having it all the time if that's easier. However, I haven't found any way to use SET ROLE successfully in Rails.

I thought it would work to add a new rake task that sets the role before the db:migrate task runs, like this:

# in lib/tasks/set_role.rake
namespace :db do
  task :set_role do
    ActiveRecord::Base.connection.execute("SET ROLE postgres")
  end
end

# in initializers/migration_hooks.rb
Rake::Task["db:migrate"].enhance ["db:set_role_postsgres"]

But that didn't work...

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I'm not sure why the above didn't work, but I just had to put this all in the .rake file, and then it worked:

# lib/tasks/migration_hooks.rb

namespace :db do
  task set_role_postsgres: :environment do
    puts "Setting Role to 'postgres'"
    ActiveRecord::Base.connection.execute("SET ROLE postgres")
  end
end

Rake::Task["db:load_config"].enhance ["db:set_role_postsgres"]

Note: The db:load_config task is a prerequisite for all db tasks, so this should handle setting the role for not only db:migrate, but also db:migrate:up, db:migrate:down, db:rollback, etc.

over 4 years ago · Santiago Trujillo Relatório

0

I just found myself needing such functionality, but for all ActiveRecord connections. In a Rails v7 application I used the following monkey-patch:

module PostgreSQLAdapterWithSetRole
  def initialize(connection, logger, connection_parameters, config)
    super(connection, logger, connection_parameters, config)

    if config[:role]
      connection.exec("SET ROLE #{config[:role]};")
    end
  end
end

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(PostgreSQLAdapterWithSetRole)

This reads a role configuration value from database.yml and if set issues SET ROLE immediately after the connection is established.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda