-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_modules.go
executable file
·202 lines (186 loc) · 5.58 KB
/
new_modules.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
package main
import (
"database/sql"
"encoding/xml"
"fmt"
_ "github.com/go-sql-driver/mysql"
"io/ioutil"
"log"
"os"
)
const (
rootPath = "/home/www/ps/newapproot/modules"
xmlFileName = "config.xml"
dirSep = "/"
DSNpsold = "ps:pass_to_ps@/ps?charset=utf8"
DSNpsnew = "psnew:pass_to_psnew@/psnew?charset=utf8"
)
type TableModules struct {
IdModule int
Name string
Active int
Version string
}
type Modules struct {
Id int
IdNew int
IdOld int
PathnameOld string
PathnameNew string
NameOld string
NameNew string
AuthorOld string
AuthorNew string
VersionOld string
VersionNew string
ActiveOld int
ActiveNew int
IsConfigurableOld int
IsConfigurableNew int
AvailableUrl string
DescriptionOld string
DescriptionNew string
}
func main() {
//Connection to old database:
dbold, err := sql.Open("mysql", DSNpsold)
checkErr(err)
defer dbold.Close()
//Connection to psnew database:
dbnew, err := sql.Open("mysql", DSNpsnew)
checkErr(err)
defer dbnew.Close()
//Test connections
err = dbold.Ping()
checkErr(err)
err = dbnew.Ping()
checkErr(err)
dir, err := ioutil.ReadDir(rootPath)
if err != nil {
log.Fatal(err)
}
for _, fn := range dir {
//If is a directory then done normaly
if !fn.Mode().IsDir() {
continue
}
discoverPath := rootPath + dirSep + fn.Name() + dirSep + xmlFileName
bs, err := ioutil.ReadFile(discoverPath)
//If file exists then done normaly
if os.IsNotExist(err) {
continue
}
str := string(bs)
outp := GetModulesDataFromXml(str)
fmt.Println(discoverPath, ":", outp.DisplayName, "(", outp.Author, ")")
//var item TableModules
item := TableModules{}
record := dbnew.QueryRow("SELECT id_module, name, active, version FROM psnew.ps_module WHERE name=?", outp.Pathname)
err = record.Scan(&item.IdModule,&item.Name, &item.Active, &item.Version)
switch err {
case sql.ErrNoRows:
fmt.Println("No rows were returned!")
//return
case nil:
fmt.Println("Record:", item)
default:
panic(err)
}
updateGatheredFromXmlAndDb(dbold, outp, item)
}
}
func updateGatheredFromXmlAndDb(dbd *sql.DB, m XmlModule, r TableModules) {
// update from xml file:
//Check record is here:
item := TableModules{}
record := dbd.QueryRow("SELECT id, name_new, active_new, version_new FROM ps.modules WHERE pathname_new=? OR pathname_old = ? OR description_old = ?", m.Pathname, m.Pathname, m.Description)
err := record.Scan(&item.IdModule, &item.Name, &item.Active, &item.Version)
switch err {
case sql.ErrNoRows:
fmt.Println("No rows were returned in ps.modules")
//return
case nil:
fmt.Println("Record in ps.modules founded:Id=", item.IdModule)
default:
panic(err)
}
// If no such record:
if err == sql.ErrNoRows {
stmt, err := dbd.Prepare("INSERT ps.modules SET id_new=?, pathname_new=?, name_new=?, author_new=?, version_new=?, active_new=?, is_configurable_new=?, description_new=?")
checkErr(err)
res, err := stmt.Exec(
r.IdModule,
m.Pathname,
m.DisplayName,
m.Author,
m.Version,
r.Active,
m.IsConfigurable,
m.Description,
)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(m.Pathname, m.DisplayName, affect, "- is inserted in DB")
} else {
// If has record:
stmt, err := dbd.Prepare("UPDATE ps.modules SET id_new=?, pathname_new=?, name_new=?, author_new=?, version_new=?, active_new=?, is_configurable_new=?, description_new=? WHERE id = ?")
checkErr(err)
res, err := stmt.Exec(
r.IdModule,
m.Pathname,
m.DisplayName,
m.Author,
m.Version,
r.Active,
m.IsConfigurable,
m.Description,
item.IdModule,
)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(m.Pathname, m.DisplayName, affect, "- is updated in DB")
}
return
}
/* Example the Prestashop module xml description:
rawData := `
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>belvg_related_products</name>
<displayName><![CDATA[Related Products]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Related Products]]></description>
<author><![CDATA[BelVG]]></author>
<tab><![CDATA[advertising_marketing]]></tab>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
`
*/
type XmlModule struct {
XMLName xml.Name `xml:"module"`
Pathname string `xml:"name"`
DisplayName string `xml:"displayName"`
Version string `xml:"version"`
Description string `xml:"description"`
Author string `xml:"author"`
Tab string `xml:"tab"`
IsConfigurable string `xml:"is_configurable"`
NeedInstance string `xml:"need_instance"`
LimitedCountries string `xml:"limited_countries"`
}
func GetModulesDataFromXml(rawXMLdata string) XmlModule {
//func GetModulesName(rawXMLdata string) {
var data XmlModule
xml.Unmarshal([]byte(rawXMLdata), &data)
//fmt.Println(string(data.Name))
return data
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}