Skip to content

How to change data_migrations table owner? #248

@mike-usa

Description

@mike-usa

For rake/rails db:migrate (i.e., schema_migrations), a rake task can be used to intercept all migrations using the db:load_config hook. A new connection can be created and then database users/roles can be set on that ActiveRecord base connection, which is then used by all subsequent tasks.

The database.yml might use an application user that has write access to tables that are owned by the DBA. For safety, we might want to perform migrations by the enduser’s credentials (ensuring they’re the DBA).

So the load_config hook loads the database config for the environment and then overwrites the user (inherited from caller). The hook then executes SQL to set its role (e.g., in PostgreSQL SET ROLE dba;). Any subsequent migration is authored and executed by that role (including table creation).

In attempting to do this with data-migrate, I can intercept the connection and set the user, but it doesn’t seem to maintain the session. The role setting is lost. I assume:

  1. this may be related to threading
  2. data migration maybe creating a copy of the configuration, but not using the same database session

Is there a recommended way to set user and role only for data-migration tasks?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions