-
Notifications
You must be signed in to change notification settings - Fork 2
/
scopes.go
86 lines (80 loc) 路 2.43 KB
/
scopes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package scopes
import (
"fmt"
"reflect"
"gorm.io/gorm"
)
// WithTenantSchema alters the table name to prefix it with the tenant schema.
//
// The table name is retrieved from the statement if set manually, otherwise
// attempts to get it from the model or destination.
//
// Example with table name set manually:
//
// type Book struct { ... } // does not implement Tabler interface, must set TableName manually
//
// db.Table("books").Scopes(scopes.WithTenantSchema("tenant1")).Find(&Book{})
// // SELECT * FROM tenant1.books;
//
// Example with Tabler interface:
//
// type Book struct { ... }
//
// func (u *Book) TableName() string { return "books" } // implements Tabler interface, no need to set TableName manually
//
// db.Scopes(scopes.WithTenantSchema("tenant2")).Find(&Book{})
// // SELECT * FROM tenant2.books;
//
// Example with model set manually:
//
// type Book struct { ... }
//
// db.Model(&Book{}).Scopes(scopes.WithTenantSchema("tenant3")).Find(&Book{}) // model is set manually.
// // SELECT * FROM tenant3.books;
func WithTenantSchema(tenant string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
var (
tableName string
)
switch {
case db.Statement.Table != "":
// if the table name is set manually, use it
tableName = db.Statement.Table
case db.Statement.Model != nil:
// if the table name is not set manually, try to get it from the model
tableName = tableNameFromReflectValue(db.Statement.Model)
case db.Statement.Dest != nil:
// if the table name is not set manually, try to get it from the model
tableName = tableNameFromReflectValue(db.Statement.Dest)
}
if tableName != "" {
return db.Table(fmt.Sprintf("%s.%s", tenant, tableName))
}
// otherwise, return an error
db.AddError(gorm.ErrModelValueRequired)
return db
}
}
// getTableName returns the table name from the model
func getTableName(val interface{}) (string, bool) {
if s, ok := val.(interface{ TableName() string }); ok {
return s.TableName(), true
}
return "", false
}
func tableNameFromReflectValue(val interface{}) string {
switch value := reflect.Indirect(reflect.ValueOf(val)); value.Kind() {
case reflect.Struct:
newElem := reflect.New(value.Type()).Interface()
if name, ok := getTableName(newElem); ok {
return name
}
case reflect.Slice:
elemType := value.Type().Elem()
newElem := reflect.New(elemType).Interface()
if name, ok := getTableName(newElem); ok {
return name
}
}
return ""
}