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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pg_saurus/migration/set_role_method.rb', line 52

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