Skip to content

allow setting "default search path" #9

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

Closed
rehatkathuria opened this issue Jan 27, 2018 · 13 comments
Closed

allow setting "default search path" #9

rehatkathuria opened this issue Jan 27, 2018 · 13 comments
Assignees
Labels
enhancement New feature or request

Comments

@rehatkathuria
Copy link

Related to #8.

@vzsg
Copy link
Member

vzsg commented Jan 29, 2018

Not just databases, the schema name should be configurable too.

@Joannis Joannis added the enhancement New feature or request label Jan 29, 2018
@tanner0101 tanner0101 mentioned this issue Feb 1, 2018
5 tasks
@tanner0101
Copy link
Member

@vzsg schema name?

@vzsg
Copy link
Member

vzsg commented Feb 1, 2018

Sorry, I was being imprecise. What I actually meant is setting a default search path after the connection is made, some database drivers expose it. The search path is important in (typ. enterprisish) scenarios where schemas are used as an extra level of grouping/access control over tables.

This has been a pretty common feature request for the libpq-based postgresql package and the -provider.

@tanner0101
Copy link
Member

Ah okay, will re-open to track that issue then.

@tanner0101 tanner0101 reopened this Feb 1, 2018
@tanner0101 tanner0101 added this to the 1.0.0 milestone Feb 1, 2018
@tanner0101 tanner0101 changed the title Support database names allow setting "default search path" Feb 1, 2018
@tanner0101 tanner0101 removed this from the 1.0.0 milestone Jun 15, 2018
@tanner0101
Copy link
Member

Looks like this can be done by simply running:

SET search_path TO myschema;

This could be done in boot.swift. We could eventually add a convenience for this via the provider, but not a priority for the 1.0.0 release.

@fwgreen
Copy link

fwgreen commented Jun 15, 2018

Thanks for finding a solution. I'm very eager to see how using boot.swift would work.

public func boot(_ app: Application) throws {
    // what would actually go here?
}

@tanner0101
Copy link
Member

@fwgreen something like this:

public func boot(_ app: Application) throws {
    try app.withNewConnection(to: .psql) { conn in
        return conn.simpleQuery("SET search_path TO myschema").transform(to: ())
    }.wait()
}

@MrMage
Copy link
Contributor

MrMage commented Jun 15, 2018

I think that would only change that setting for that single (ephemeral) connection. You could prepend it with ALTER DATABASE xyz, I think.

@fwgreen
Copy link

fwgreen commented Jun 15, 2018

I can confirm that prepending with ALTER DATABASE does work!

public func boot(_ app: Application) throws {
    try app.withNewConnection(to: .psql) { conn in
        return conn.simpleQuery("ALTER DATABASE my_db SET search_path TO my_schema").transform(to: ())
    }.wait()
}

Maybe one day users could have something as simple as

static let entity = "my_schema.my_table"

But this gets us up and running, so I'll take it. Thank you all!

@tanner0101
Copy link
Member

tanner0101 commented Jun 22, 2018

Ah, didn't realize that was ephemeral to the connection. You can use DatabasesConfig to add connection configuration closures:

var databases = DatabasesConfig()
// register your dbs and what have you
databases.appendConfigurationHandler(on: .psql) { conn in
    return conn.simpleQuery("SET search_path TO myschema")
}
services.register(databases)

appendConfigurationHandler will run the closure each time a new connection is created.

@fwgreen
Copy link

fwgreen commented Mar 14, 2019

@tanner0101 Will this workaround continue to work in the next major release?

@BlameOmar
Copy link

This workaround is not perfect, at least not in Vapor 3. PostgreSQLConnection contains a TableNameCache, which maps OIDs <-> table names using a pair of dictionaries. Unfortunately, this isn't a 1:1 relation in the general case, and breaks whenever multiple schemas use the same table names. I wanted to create new schemas per testcase (eg. 'testing_UserTests_20190718_202247'), which would allow my tests to execute in parallel, but the corrupted TableNameCache cause them to not find their own tables.

I have not tried this in Vapor 4. It seems like the Postgres driver has been completely redone, so it's possible that this works now. However, without explicit support and the tests to back it up, it's possible that this accidentally breaks even if it is currently working. I'm willing to help in this regard. Let me know what your plans are for this.

@tanner0101
Copy link
Member

TableNameCache no longer exists so that shouldn't be a problem anymore. I've added a searchPath property to PostgresConfiguration. This property is an optional array of strings.

configuration.searchPath = ["foo", "bar", "baz"]

When set, the following query will be run automatically on new connections:

SET search_path = "foo", "bar", "baz"

tanner0101 added a commit that referenced this issue Apr 24, 2020
tanner0101 added a commit that referenced this issue Apr 29, 2020
* 2.0.0 gm

* file updates

* update guide docs

* fix img

* fix img

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* code cleanup

* add search path

* add searchPath fixes #9

* optional password

* fix decoder force unwrap #175

* pass server hostname, fixes #178

* array type test

* test fluent gm branch

* master + import fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants