Module: PgSaurus::Migration::SetRoleMethod::Extension

Defined in:
lib/pg_saurus/migration/set_role_method.rb

Overview

Module to be prepended into ActiveRecord::Migration which allows enhancing the exec_migration method.

Instance Method Summary collapse

Instance Method Details

#exec_migration(conn, direction) ⇒ void

This method returns an undefined value.

Wrap original ‘exec_migration` to run migration with set role.

Parameters:

  • conn (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
  • direction (Symbol)

    :up or :down



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pg_saurus/migration/set_role_method.rb', line 98

def exec_migration(conn, direction)
  if role
    begin
      conn.execute "SET ROLE #{role}"
      super(conn, direction)
    ensure
      conn.execute "RESET ROLE"
    end
  elsif PgSaurus.config.ensure_role_set && !keep_default_role?
    msg =
      "Role for migration #{self.class} is not set\n\n" \
      "You've configured PgSaurus with ensure_role_set=true. \n" \
      "That means that every migration must explicitly set role with set_role method.\n\n" \
      "Example:\n" \
      "  class CreateNewTable < ActiveRecord::Migration\n" \
      "    set_role \"superhero\"\n" \
      "  end\n\n" \
      "If you want to set ensure_role_set=false, take a look at config/initializers/pg_saurus.rb\n\n"
    raise PgSaurus::RoleNotSetError, msg
  else
    super(conn, direction)
  end
end