Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding users to T-SQL db_owner database role #481

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/bin/pg_dump/dumpall_babel_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,38 @@ getBabelfishRolesQuery(PGconn *conn, PQExpBuffer buf, char *role_catalog,
appendPQExpBuffer(buf, "WHERE database_name = '%s' ", escaped_bbf_db_name);
pfree(escaped_bbf_db_name);
}

/* Append roles associated with db_owner members which are not present in bbf catalogs */
appendPQExpBuffer(buf,
"UNION "
"SELECT pr.rolname "
"FROM ( "
"SELECT DISTINCT pr.rolname AS member_role "
"FROM sys.babelfish_authid_user_ext ta "
"JOIN pg_roles r ON r.rolname = ta.rolname "
"JOIN pg_auth_members m ON r.oid = m.roleid "
"JOIN pg_roles pr ON pr.oid = m.member "
"WHERE pr.rolname != ta.rolname "
"AND ta.orig_username = 'db_owner' ");

/* Only dump users of the specific logical database we are currently dumping. */
if (bbf_db_name != NULL)
{
/*
* Get escaped bbf_db_name to handle special characters in it.
* 2*strlen+1 bytes are required for PQescapeString according to the documentation.
*/
char *escaped_bbf_db_name = pg_malloc(2 * strlen(bbf_db_name) + 1);

PQescapeString(escaped_bbf_db_name, bbf_db_name, strlen(bbf_db_name));
appendPQExpBuffer(buf, "AND ta.database_name = '%s' ", escaped_bbf_db_name);
pfree(escaped_bbf_db_name);
}

appendPQExpBuffer(buf,
") AS db_owner_members "
"JOIN pg_roles pr ON pr.rolname = sys.babelfish_truncate_identifier(db_owner_members.member_role || '_obj') ");

appendPQExpBuffer(buf, "), "
"bbf_roles AS (SELECT rc.* FROM %s rc INNER JOIN bbf_catalog bcat "
"ON rc.rolname = bcat.rolname) ", role_catalog);
Expand Down Expand Up @@ -327,6 +359,38 @@ getBabelfishRoleMembershipQuery(PGconn *conn, PQExpBuffer buf,
appendPQExpBuffer(buf, "WHERE database_name = '%s' ", escaped_bbf_db_name);
pfree(escaped_bbf_db_name);
}

/* Append roles associated with db_owner members which are not present in bbf catalogs */
appendPQExpBuffer(buf,
"UNION "
"SELECT pr.rolname "
"FROM ( "
"SELECT DISTINCT pr.rolname AS member_role "
"FROM sys.babelfish_authid_user_ext ta "
"JOIN pg_roles r ON r.rolname = ta.rolname "
"JOIN pg_auth_members m ON r.oid = m.roleid "
"JOIN pg_roles pr ON pr.oid = m.member "
"WHERE pr.rolname != ta.rolname "
"AND ta.orig_username = 'db_owner' ");

/* Only dump users of the specific logical database we are currently dumping. */
if (bbf_db_name != NULL)
{
/*
* Get escaped bbf_db_name to handle special characters in it.
* 2*strlen+1 bytes are required for PQescapeString according to the documentation.
*/
char *escaped_bbf_db_name = pg_malloc(2 * strlen(bbf_db_name) + 1);

PQescapeString(escaped_bbf_db_name, bbf_db_name, strlen(bbf_db_name));
appendPQExpBuffer(buf, "AND ta.database_name = '%s' ", escaped_bbf_db_name);
pfree(escaped_bbf_db_name);
}

appendPQExpBuffer(buf,
") AS db_owner_members "
"JOIN pg_roles pr ON pr.rolname = sys.babelfish_truncate_identifier(db_owner_members.member_role || '_obj') ");

appendPQExpBuffer(buf, "), "
"bbf_roles AS (SELECT rc.* FROM %s rc INNER JOIN bbf_catalog bcat "
"ON rc.rolname = bcat.rolname) ", role_catalog);
Expand Down
Loading