ruby on rails - REASSIGN/DROP OWNED BY for all databases on postgres server -
i have situation trying remove list of roles particular database, role in use on multiple databases on server.
as per the documentation dropping roles, have followed instructions:
reassign owned doomed_role successor_role; drop owned doomed_role; -- repeat above commands in each database of cluster drop role doomed_role;
the documentation states:
because reassign owned [and drop owned] cannot access objects in other databases, necessary run in each database contains objects owned role.
as expected, when run above commands:
conn = activerecord::base.connection conn.execute("reassign owned viewer_role postgres") conn.execute("drop owned viewer_role") conn.execute("drop role if exists viewer_role")
i getting error message:
activerecord::statementinvalid: pg::dependentobjectsstillexist: error: role "viewer_role" cannot dropped because objects depend on detail: 35 objects in database inst2 35 objects in database inst3 35 objects in database inst4
within context of rails app, how call reassign owned
, drop owned
databases?
my guess i'm going have connect each database. i've found solution works:
module dbs @@connections = {} # databases conn = activerecord::base.connection; dbs = conn.select_values('select datname pg_database datistemplate = false') # create connection each database - raise lots , lots of warnings dbs.each |db_name| @@connections[db_name] ||= begin config = activerecord::base.connection_config config = config.merge({database: db_name}) model = class.new(activerecord::base) self.const_set("#{db_name.classify}", model) # needed rails raises error if class not have name :@ model.establish_connection(config) model.connection end end def self.connections @@connections end end roles.each |role_name| dbs.connections.values.each |conn| conn.execute("reassign owned #{role_name} postgres") conn.execute("drop owned #{role_name}") end activerecord::base.connection.execute("drop role if exists #{role_name}") end
however, possible to same thing using activerecord::base.connection
established?
Comments
Post a Comment