You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
792 B
41 lines
792 B
package pgsql
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
"zestack.dev/cast"
|
|
)
|
|
|
|
// func init() {
|
|
// db.RegisterDriver("pgsql", func(opts *db.DriverOptions) db.Driver {
|
|
// return &Driver{
|
|
// Migrator: Migrator{
|
|
// DB: opts.DB,
|
|
// Log: opts.Log,
|
|
// Retry: opts.Retry,
|
|
// },
|
|
// }
|
|
// })
|
|
// }
|
|
|
|
type Driver struct {
|
|
Migrator
|
|
}
|
|
|
|
func (*Driver) UseTenant(tx *gorm.DB, tenantId uint) (reset func() error, err error) {
|
|
return SetSearchPath(tx, TenantSchema(tenantId))
|
|
}
|
|
|
|
func (*Driver) CurrentTenant(tx *gorm.DB) (tenant uint, ok bool) {
|
|
databse := CurrentSearchPath(tx)
|
|
basename := TenantBasename()
|
|
|
|
if databse != "" && strings.HasPrefix(databse, basename) {
|
|
var err error
|
|
tenant, err = cast.Uint(databse[len(basename):])
|
|
ok = err == nil && tenant > 0
|
|
}
|
|
|
|
return
|
|
}
|
|
|