forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_initial_data.go
143 lines (128 loc) · 3.62 KB
/
load_initial_data.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package uadmin
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
type initialDataRecords []map[string]interface{}
type initialData struct {
Init []string `json:"init"`
Data map[string]initialDataRecords `json:"data"`
Finish []string `json:"finish"`
}
// loadInitialData reads a file named initial_data.json and
// saves its content in the database
func loadInitialData() error {
buf, err := ioutil.ReadFile("initial_data.json")
if err != nil {
return nil
}
// Load json daa into struct
data := initialData{}
err = json.Unmarshal(buf, &data)
if err != nil {
return fmt.Errorf("loadInitialData.Unmarshal: Error parsing json file. %s", err)
}
// Execute SQL in Init section
for _, SQL := range data.Init {
// Check if this is a uadmin command
if strings.HasPrefix(SQL, "!") {
command := strings.Split(SQL, " ")
switch strings.ToUpper(command[0]) {
case "!MIGRATE":
if len(command) < 2 {
return fmt.Errorf("invalid uadmin command in initial_data.json in init section. %s", SQL)
}
// Check if the model name is correct
if _, ok := models[command[1]]; !ok {
if len(command) < 2 {
return fmt.Errorf("model name does not exist in initial_data.json in init section. %s", SQL)
}
}
db.AutoMigrate(models[command[1]])
}
continue
}
// This is a SQL command
err = db.Exec(SQL).Error
if err != nil {
return fmt.Errorf("loadInitialData.Exec: Error in in Init section (%s). %s", SQL, err)
}
}
// Load data
for table, records := range data.Data {
// get modelname from table name
// For the record:
// - Name : OrderItem
// - DisplayName: Order Items
// - ModelName : orderitem
// - TableName : order_items
tabeFound := false
for k, v := range Schema {
// check if table is a ModelName
if table == k {
tabeFound = true
break
}
// check if the table is a database TableName
// and convert it into modelname
if v.TableName == table {
table = k
tabeFound = true
break
}
// check if the table is a Name
// and convert it into modelname
if v.Name == table {
table = k
tabeFound = true
break
}
}
if !tabeFound {
return fmt.Errorf("loadInitialData: Table not found for (%s)", table)
}
// Put records into Model Array
modelArray, _ := NewModelArray(table, true)
buf, _ = json.Marshal(records)
err = json.Unmarshal(buf, modelArray.Interface())
if err != nil {
return fmt.Errorf("loadInitialData.Unmarshal: Error parsing Data records in (%s). %s", table, err)
}
// Save records
for i := 0; i < modelArray.Elem().Len(); i++ {
err = Save(modelArray.Elem().Index(i).Addr().Interface())
if err != nil {
return fmt.Errorf("loadInitialData.Save: Error in %s[%d]. %s", table, i, err)
}
}
}
// Execute SQL in Finish section
for _, SQL := range data.Finish {
// Check if this is a uadmin command
if strings.HasPrefix(SQL, "!") {
command := strings.Split(SQL, " ")
switch strings.ToUpper(command[0]) {
case "!MIGRATE":
if len(command) < 2 {
return fmt.Errorf("invalid uadmin command in initial_data.json in init section. %s", SQL)
}
// Check if the model name is correct
if _, ok := models[command[1]]; !ok {
if len(command) < 2 {
return fmt.Errorf("model name does not exist in initial_data.json in init section. %s", SQL)
}
}
db.AutoMigrate(models[command[1]])
}
continue
}
// This is a SQL command
err = db.Exec(SQL).Error
if err != nil {
return fmt.Errorf("loadInitialData.Exec: Error in in Finish section (%s). %s", SQL, err)
}
}
return nil
}