mirror of
https://github.com/go-gitea/gitea.git
synced 2026-06-14 03:29:55 +00:00
fix: pgsql lint (#38022)
This commit is contained in:
@@ -4,8 +4,10 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"gitea.dev/modules/setting"
|
"gitea.dev/modules/setting"
|
||||||
@@ -14,61 +16,34 @@ import (
|
|||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
)
|
)
|
||||||
|
|
||||||
var registerOnce sync.Once
|
type postgresSchemaDriver struct{}
|
||||||
|
|
||||||
func registerPostgresSchemaDriver() {
|
var registerPostgresSchemaDriver = sync.OnceFunc(func() {
|
||||||
registerOnce.Do(func() {
|
sql.Register(sqlDriverPostgresSchema, &postgresSchemaDriver{})
|
||||||
sql.Register(sqlDriverPostgresSchema, &postgresSchemaDriver{})
|
dialects.RegisterDriver(sqlDriverPostgresSchema, dialects.QueryDriver("postgres"))
|
||||||
dialects.RegisterDriver(sqlDriverPostgresSchema, dialects.QueryDriver("postgres"))
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type postgresSchemaDriver struct {
|
// Open opens the postgres connection in the default manner with default schema support.
|
||||||
pq.Driver
|
// It immediately runs "set_config" to set the search_path appropriately.
|
||||||
}
|
func (*postgresSchemaDriver) Open(connStr string) (driver.Conn, error) {
|
||||||
|
conn, err := pq.Driver{}.Open(connStr)
|
||||||
// Open opens a new connection to the database. name is a connection string.
|
|
||||||
// This function opens the postgres connection in the default manner but immediately
|
|
||||||
// runs set_config to set the search_path appropriately
|
|
||||||
func (d *postgresSchemaDriver) Open(name string) (driver.Conn, error) {
|
|
||||||
conn, err := d.Driver.Open(name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return conn, err
|
return nil, err
|
||||||
}
|
}
|
||||||
schemaValue, _ := driver.String.ConvertValue(setting.Database.Schema)
|
|
||||||
|
|
||||||
// golangci lint is incorrect here - there is no benefit to using driver.ExecerContext here
|
connExec, ok := conn.(driver.ExecerContext)
|
||||||
// and in any case pq does not implement it
|
if !ok {
|
||||||
if execer, ok := conn.(driver.Execer); ok { //nolint:staticcheck // see above
|
return nil, errors.New("postgres driver does not implement ExecerContext interface")
|
||||||
_, err := execer.Exec(`SELECT set_config(
|
}
|
||||||
|
_, err = connExec.ExecContext(context.Background(), `SELECT set_config(
|
||||||
'search_path',
|
'search_path',
|
||||||
$1 || ',' || current_setting('search_path'),
|
$1 || ',' || current_setting('search_path'),
|
||||||
false)`, []driver.Value{schemaValue})
|
false)`,
|
||||||
if err != nil {
|
[]driver.NamedValue{{Ordinal: 1, Value: setting.Database.Schema}},
|
||||||
_ = conn.Close()
|
)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
stmt, err := conn.Prepare(`SELECT set_config(
|
|
||||||
'search_path',
|
|
||||||
$1 || ',' || current_setting('search_path'),
|
|
||||||
false)`)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = conn.Close()
|
_ = conn.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer stmt.Close()
|
|
||||||
|
|
||||||
// driver.String.ConvertValue will never return err for string
|
|
||||||
|
|
||||||
// golangci lint is incorrect here - there is no benefit to using stmt.ExecWithContext here
|
|
||||||
_, err = stmt.Exec([]driver.Value{schemaValue}) //nolint:staticcheck // see above
|
|
||||||
if err != nil {
|
|
||||||
_ = conn.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user