mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add postgres schema to the search_path on database connection (#12634)
Rather than rely on the user running the gitea server and db setting the schema search_path correctly - if gitea is run with a schema we should simply set the search_path to have that schema first in the path. Fix #12505 Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		@@ -198,16 +198,6 @@ func initIntegrationTest() {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Make the user's default search path the created schema; this will affect new connections
 | 
			
		||||
		if _, err = db.Exec(fmt.Sprintf(`ALTER USER "%s" SET search_path = %s`, setting.Database.User, setting.Database.Schema)); err != nil {
 | 
			
		||||
			log.Fatalf("db.Exec: ALTER USER SET search_path: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Make the current connection's search the created schema
 | 
			
		||||
		if _, err = db.Exec(fmt.Sprintf(`SET search_path = %s`, setting.Database.Schema)); err != nil {
 | 
			
		||||
			log.Fatalf("db.Exec: ALTER USER SET search_path: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	case setting.Database.UseMSSQL:
 | 
			
		||||
		host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
 | 
			
		||||
		db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
 | 
			
		||||
 
 | 
			
		||||
@@ -155,6 +155,16 @@ 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