forgejo/models/db/sql_postgres_with_schema.go
Mathieu Fenniak 29eddd311b chore: upgrade to https://code.forgejo.org/xorm/xorm v1.4.0 (#12639)
Upgrade Forgejo to our forked [xorm v1.4.0](https://code.forgejo.org/xorm/xorm/compare/v1.3.9-forgejo.12...v1.4.0), which is now named `code.forgejo.org/xorm/xorm` to reflect the current expectation that it is a permanent fork.  A small number of API changes were made recently in https://code.forgejo.org/xorm/xorm/issues/120 which are accounted for in this PR, in addition to the module rename.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12639
Reviewed-by: Otto <otto@codeberg.org>
2026-05-20 20:20:08 +02:00

59 lines
1.8 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package db
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"forgejo.org/modules/setting"
"code.forgejo.org/xorm/xorm/dialects"
"github.com/jackc/pgx/v5/stdlib"
)
func init() {
// Register pgx-based driver as "postgresschema" for PostgreSQL with schema support
// This wraps pgx/v5/stdlib and injects search_path configuration on connection
// For PostgreSQL without schema, engine.go uses "pgx" directly (registered by pgx/v5/stdlib)
drv := &postgresSchemaDriver{innerDriver: &stdlib.Driver{}}
sql.Register("postgresschema", drv)
dialects.RegisterDriver("postgresschema", dialects.QueryDriver("postgres"))
}
type postgresSchemaDriver struct {
innerDriver *stdlib.Driver
}
// 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.innerDriver.Open(name)
if err != nil {
return conn, err
}
// pgx implements ExecerContext, not the deprecated Execer interface
execer, ok := conn.(driver.ExecerContext)
if !ok {
_ = conn.Close()
return nil, errors.New("pgx driver connection does not implement ExecerContext")
}
_, err = execer.ExecContext(context.Background(), `SELECT set_config(
'search_path',
$1 || ',' || current_setting('search_path'),
false)`, []driver.NamedValue{{Ordinal: 1, Value: setting.Database.Schema}})
if err != nil {
_ = conn.Close()
return nil, err
}
return conn, nil
}