1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 10:18:38 +00:00

Ensure that schema search path is set with every connection on postgres (#14131) (#14216)

Backport #14131

Unfortunately every connection to postgres requires that the search path is
set appropriately.

This PR shadows the postgres driver to ensure that as soon as a connection
is open, the search_path is set appropriately.

Fix #14088

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath
2021-01-02 17:58:53 +00:00
committed by GitHub
parent 7a0a133d7c
commit 2fd708a397
2 changed files with 85 additions and 11 deletions

View File

@@ -145,7 +145,16 @@ func getEngine() (*xorm.Engine, error) {
return nil, err
}
engine, err := xorm.NewEngine(setting.Database.Type, connStr)
var engine *xorm.Engine
if setting.Database.UsePostgreSQL && len(setting.Database.Schema) > 0 {
// OK whilst we sort out our schema issues - create a schema aware postgres
registerPostgresSchemaDriver()
engine, err = xorm.NewEngine("postgresschema", connStr)
} else {
engine, err = xorm.NewEngine(setting.Database.Type, connStr)
}
if err != nil {
return nil, err
}
@@ -155,16 +164,6 @@ func getEngine() (*xorm.Engine, error) {
engine.Dialect().SetParams(map[string]string{"DEFAULT_VARCHAR": "nvarchar"})
}
engine.SetSchema(setting.Database.Schema)
if setting.Database.UsePostgreSQL && len(setting.Database.Schema) > 0 {
// Add the schema to the search path
if _, err := engine.Exec(`SELECT set_config(
'search_path',
? || ',' || current_setting('search_path'),
false)`,
setting.Database.Schema); err != nil {
return nil, err
}
}
return engine, nil
}