-
Notifications
You must be signed in to change notification settings - Fork 216
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
Allow define a search_path on the database #401
base: main
Are you sure you want to change the base?
Allow define a search_path on the database #401
Conversation
|
||
var searchPathStr string | ||
err = db.QueryRow(` | ||
SELECT (pg_options_to_table(drs.setconfig)).option_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL pg_options_to_table 👀
|
||
func getDBRoleSettings(db *DBConnection, dbId string) (pq.ByteaArray, error) { | ||
var dbRoleConfigItems pq.ByteaArray | ||
dbSQL := `SELECT setconfig FROM pg_catalog.pg_database AS d, pg_catalog.pg_db_role_setting AS drs WHERE d.datname = $1 AND d.oid = drs.setdatabase` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dbSQL := `SELECT setconfig FROM pg_catalog.pg_database AS d, pg_catalog.pg_db_role_setting AS drs WHERE d.datname = $1 AND d.oid = drs.setdatabase` | |
dbSQL := `SELECT setconfig FROM pg_catalog.pg_database AS d, pg_catalog.pg_db_role_setting AS drs WHERE d.datname = $1 AND d.oid = drs.setdatabase AND setrole = 0` |
I think you need to filter on setrole = 0
too, otherwise if someone do ALTER ROLE x IN DATABASE y SET search_path = 'test'
on this database, this query will have multiple results.
e.g.:
postgres=# create database test; create role test;
CREATE DATABASE
CREATE ROLE
postgres=# alter database test set search_path = 'test'; alter role test in database test set search_path = 'other';
ALTER DATABASE
ALTER ROLE
postgres=# SELECT drs.* FROM pg_catalog.pg_database AS d, pg_catalog.pg_db_role_setting AS drs WHERE d.datname = 'test' AND d.oid = drs.setdatabase;
setdatabase | setrole | setconfig
-------------+---------+---------------------
16384 | 0 | {search_path=test}
16384 | 16385 | {search_path=other}
(2 rows)
This will probably work anyway as readSearchPath
will take the first which is most probably the good one but I think the ordering isn't guarantee
searchPathPrefix := "search_path" | ||
for _, v := range roleConfig { | ||
config := string(v) | ||
if strings.HasPrefix(config, searchPathPrefix) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I discovered pg_options_to_table
thanks to you, we should probably use it here too.
If you don't have time I can do it in another PR though
@NitriKx Thanks for your work on that 🙏 |
Thanks for your feedback @cyrilgdn, will try to do the changes (and rebase) next week 🙂 |
f2c2e47
to
dea1401
Compare
@NitriKx |
Heey will try to work on this next week sorry |
In this PR I've added a
search_path
parameter on thepostgresql_database
resource, which allows to alter the database to set a value for its search path.It's inspired from what has been done by on the role (the
readSearchPath
function has been moved inhelpers.go
as I've re-used it.Web documentation has been updated as well 🙂