-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstruct.go
48 lines (40 loc) · 1.13 KB
/
construct.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
package drift
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func ConstructDatabaseState(values ...interface{}) (state DatabaseState, err error) {
state = DatabaseState{
Tables: make(map[string]TableState),
}
db, err := gorm.Open("sqlite3", ":memory:")
for _, value := range values {
scope := db.NewScope(value)
modelStruct := scope.GetModelStruct()
tableName := modelStruct.TableName(db)
tableState := TableState{
Name: tableName,
Columns: make(map[string]ColumnState),
}
fmt.Println(scope.TableName())
for _, field := range modelStruct.PrimaryFields {
fmt.Println(" + primary:", field.Name)
tableState.Columns[field.Name] = ColumnState{
FQName: tableState.Name + "." + field.Name,
Name: field.Name,
InPrimaryKey: true,
}
}
for _, field := range modelStruct.StructFields {
fmt.Println(" - field:", field.Name)
tableState.Columns[field.Name] = ColumnState{
FQName: tableState.Name + "." + field.Name,
Name: field.Name,
InPrimaryKey: false,
}
}
state.Tables[tableName] = tableState
}
return
}