/
back_repo.go
243 lines (190 loc) · 6.42 KB
/
back_repo.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// do not modify, generated file
package orm
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/fullstack-lang/gong/test2/go/models"
"github.com/tealeg/xlsx/v3"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// BackRepoStruct supports callback functions
type BackRepoStruct struct {
// insertion point for per struct back repo declarations
BackRepoA BackRepoAStruct
BackRepoB BackRepoBStruct
CommitFromBackNb uint // records commit increments when performed by the back
PushFromFrontNb uint // records commit increments when performed by the front
stage *models.StageStruct
}
func NewBackRepo(stage *models.StageStruct, filename string) (backRepo *BackRepoStruct) {
// adjust naming strategy to the stack
gormConfig := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "github_com_fullstack_lang_gong_test_go_", // table name prefix
},
}
db, err := gorm.Open(sqlite.Open(filename), gormConfig)
// since testsim is a multi threaded application. It is important to set up
// only one open connexion at a time
dbDB_inMemory, err := db.DB()
if err != nil {
panic("cannot access DB of db" + err.Error())
}
// it is mandatory to allow parallel access, otherwise, bizarre errors occurs
dbDB_inMemory.SetMaxOpenConns(1)
if err != nil {
panic("Failed to connect to database!")
}
// adjust naming strategy to the stack
db.Config.NamingStrategy = &schema.NamingStrategy{
TablePrefix: "github_com_fullstack_lang_gong_test_go_", // table name prefix
}
err = db.AutoMigrate( // insertion point for reference to structs
&ADB{},
&BDB{},
)
if err != nil {
msg := err.Error()
panic("problem with migration " + msg + " on package github.com/fullstack-lang/gong/test/go")
}
backRepo = new(BackRepoStruct)
// insertion point for per struct back repo declarations
backRepo.BackRepoA = BackRepoAStruct{
Map_ADBID_APtr: make(map[uint]*models.A, 0),
Map_ADBID_ADB: make(map[uint]*ADB, 0),
Map_APtr_ADBID: make(map[*models.A]uint, 0),
db: db,
stage: stage,
}
backRepo.BackRepoB = BackRepoBStruct{
Map_BDBID_BPtr: make(map[uint]*models.B, 0),
Map_BDBID_BDB: make(map[uint]*BDB, 0),
Map_BPtr_BDBID: make(map[*models.B]uint, 0),
db: db,
stage: stage,
}
stage.BackRepo = backRepo
backRepo.stage = stage
return
}
func (backRepo *BackRepoStruct) GetStage() (stage *models.StageStruct) {
stage = backRepo.stage
return
}
func (backRepo *BackRepoStruct) GetLastCommitFromBackNb() uint {
return backRepo.CommitFromBackNb
}
func (backRepo *BackRepoStruct) GetLastPushFromFrontNb() uint {
return backRepo.PushFromFrontNb
}
func (backRepo *BackRepoStruct) IncrementCommitFromBackNb() uint {
if backRepo.stage.OnInitCommitCallback != nil {
backRepo.stage.OnInitCommitCallback.BeforeCommit(backRepo.stage)
}
if backRepo.stage.OnInitCommitFromBackCallback != nil {
backRepo.stage.OnInitCommitFromBackCallback.BeforeCommit(backRepo.stage)
}
backRepo.CommitFromBackNb = backRepo.CommitFromBackNb + 1
return backRepo.CommitFromBackNb
}
func (backRepo *BackRepoStruct) IncrementPushFromFrontNb() uint {
if backRepo.stage.OnInitCommitCallback != nil {
backRepo.stage.OnInitCommitCallback.BeforeCommit(backRepo.stage)
}
if backRepo.stage.OnInitCommitFromFrontCallback != nil {
backRepo.stage.OnInitCommitFromFrontCallback.BeforeCommit(backRepo.stage)
}
backRepo.PushFromFrontNb = backRepo.PushFromFrontNb + 1
return backRepo.CommitFromBackNb
}
// Commit the BackRepoStruct inner variables and link to the database
func (backRepo *BackRepoStruct) Commit(stage *models.StageStruct) {
// insertion point for per struct back repo phase one commit
backRepo.BackRepoA.CommitPhaseOne(stage)
backRepo.BackRepoB.CommitPhaseOne(stage)
// insertion point for per struct back repo phase two commit
backRepo.BackRepoA.CommitPhaseTwo(backRepo)
backRepo.BackRepoB.CommitPhaseTwo(backRepo)
backRepo.IncrementCommitFromBackNb()
}
// Checkout the database into the stage
func (backRepo *BackRepoStruct) Checkout(stage *models.StageStruct) {
// insertion point for per struct back repo phase one commit
backRepo.BackRepoA.CheckoutPhaseOne()
backRepo.BackRepoB.CheckoutPhaseOne()
// insertion point for per struct back repo phase two commit
backRepo.BackRepoA.CheckoutPhaseTwo(backRepo)
backRepo.BackRepoB.CheckoutPhaseTwo(backRepo)
}
// Backup the BackRepoStruct
func (backRepo *BackRepoStruct) Backup(stage *models.StageStruct, dirPath string) {
os.MkdirAll(dirPath, os.ModePerm)
// insertion point for per struct backup
backRepo.BackRepoA.Backup(dirPath)
backRepo.BackRepoB.Backup(dirPath)
}
// Backup in XL the BackRepoStruct
func (backRepo *BackRepoStruct) BackupXL(stage *models.StageStruct, dirPath string) {
os.MkdirAll(dirPath, os.ModePerm)
// open an existing file
file := xlsx.NewFile()
// insertion point for per struct backup
backRepo.BackRepoA.BackupXL(file)
backRepo.BackRepoB.BackupXL(file)
var b bytes.Buffer
writer := bufio.NewWriter(&b)
file.Write(writer)
theBytes := b.Bytes()
filename := filepath.Join(dirPath, "bckp.xlsx")
err := ioutil.WriteFile(filename, theBytes, 0644)
if err != nil {
log.Panic("Cannot write the XL file", err.Error())
}
}
// Restore the database into the back repo
func (backRepo *BackRepoStruct) Restore(stage *models.StageStruct, dirPath string) {
backRepo.stage.Commit()
backRepo.stage.Reset()
backRepo.stage.Checkout()
//
// restauration first phase (create DB instance with new IDs)
//
// insertion point for per struct backup
backRepo.BackRepoA.RestorePhaseOne(dirPath)
backRepo.BackRepoB.RestorePhaseOne(dirPath)
//
// restauration second phase (reindex pointers with the new ID)
//
// insertion point for per struct backup
backRepo.BackRepoA.RestorePhaseTwo()
backRepo.BackRepoB.RestorePhaseTwo()
backRepo.stage.Checkout()
}
// Restore the database into the back repo
func (backRepo *BackRepoStruct) RestoreXL(stage *models.StageStruct, dirPath string) {
// clean the stage
backRepo.stage.Reset()
// commit the cleaned stage
backRepo.stage.Commit()
// open an existing file
filename := filepath.Join(dirPath, "bckp.xlsx")
file, err := xlsx.OpenFile(filename)
_ = file
if err != nil {
log.Panic("Cannot read the XL file", err.Error())
}
//
// restauration first phase (create DB instance with new IDs)
//
// insertion point for per struct backup
backRepo.BackRepoA.RestoreXLPhaseOne(file)
backRepo.BackRepoB.RestoreXLPhaseOne(file)
// commit the restored stage
backRepo.stage.Commit()
}