Skip to content

Commit c157747

Browse files
committed
Use Execer if available
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 2087824 commit c157747

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

models/sql_postgres_with_schema.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ func (d *postgresSchemaDriver) Open(name string) (driver.Conn, error) {
3636
if err != nil {
3737
return conn, err
3838
}
39+
schemaValue, _ := driver.String.ConvertValue(setting.Database.Schema)
40+
41+
// golangci lint is incorrect here - there is no benefit to using driver.ExecerContext here
42+
// and in any case pq does not implement it
43+
if execer, ok := conn.(driver.Execer); ok { //nolint
44+
_, err := execer.Exec(`SELECT set_config(
45+
'search_path',
46+
$1 || ',' || current_setting('search_path'),
47+
false)`, []driver.Value{schemaValue}) //nolint
48+
if err != nil {
49+
_ = conn.Close()
50+
return nil, err
51+
}
52+
return conn, nil
53+
}
3954

4055
stmt, err := conn.Prepare(`SELECT set_config(
4156
'search_path',
@@ -48,7 +63,6 @@ func (d *postgresSchemaDriver) Open(name string) (driver.Conn, error) {
4863
defer stmt.Close()
4964

5065
// driver.String.ConvertValue will never return err for string
51-
schemaValue, _ := driver.String.ConvertValue(setting.Database.Schema)
5266

5367
// golangci lint is incorrect here - there is no benefit to using stmt.ExecWithContext here
5468
_, err = stmt.Exec([]driver.Value{schemaValue}) //nolint
@@ -57,5 +71,5 @@ func (d *postgresSchemaDriver) Open(name string) (driver.Conn, error) {
5771
return nil, err
5872
}
5973

60-
return conn, err
74+
return conn, nil
6175
}

0 commit comments

Comments
 (0)