mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Ensure that schema search path is set with every connection on postgres (#14131)
* Ensure that schema search path is set with every connection on postgres 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> * no golangci-lint that is not a helpful suggestion Signed-off-by: Andrew Thornton <art27@cantab.net> * Use Execer if available Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user