-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_modules.go
executable file
·173 lines (160 loc) · 4.57 KB
/
old_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
package main
import (
"database/sql"
"encoding/xml"
"fmt"
_ "github.com/go-sql-driver/mysql"
"io/ioutil"
"log"
"os"
)
const (
rootPath = "/home/www/dream/prodapproot/modules"
xmlFileName = "config.xml"
dirSep = "/"
DSNpsold = "ps:pass_to_ps@/ps?charset=utf8"
DSNold = "dlprod:pass_to_dlprod@/dlprod?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() {
dbps, err := sql.Open("mysql", DSNpsold)
checkErr(err)
defer dbps.Close()
//Connection to psnew database:
dbold, err := sql.Open("mysql", DSNold)
checkErr(err)
defer dbold.Close()
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)
module := GetModulesDataFromXml(str)
fmt.Println(discoverPath, ":", module.DisplayName, "(", module.Author, ")")
//var item TableModules
item := TableModules{}
record := dbold.QueryRow("SELECT id_module, name, active, version FROM dlprod.ps_module WHERE name=?", module.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(dbps, module, item)
}
}
func updateGatheredFromXmlAndDb(dbd *sql.DB, m XmlModule,r TableModules) {
// update from xml file:
//Check record is here:
item := TableModules{}
record := dbd.QueryRow("SELECT name_old, active_old, version_old FROM ps.modules WHERE pathname_old=?", m.Pathname)
err := record.Scan(&item.Name, &item.Active, &item.Version)
switch err {
case sql.ErrNoRows:
fmt.Println("No rows were returned in ps.modules")
case nil:
fmt.Println("Record in ps.modules founded:", item)
default:
panic(err)
}
// If no such record:
if err == sql.ErrNoRows {
stmt, err := dbd.Prepare("INSERT ps.modules SET id_old=?, pathname_old=?, name_old=?, author_old=?, version_old=?, active_old=?, description_old=?, is_configurable_old=?")
checkErr(err)
res, err := stmt.Exec(
r.IdModule,
m.Pathname,
m.DisplayName,
m.Author,
m.Version,
r.Active,
m.Description,
m.IsConfigurable,
)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(m.Pathname, m.DisplayName, affect, "- is inserted in DB")
} else {
stmt, err := dbd.Prepare("UPDATE ps.modules SET id_old=?, pathname_old=?, name_old=?, author_old=?, version_old=?, active_old=?, description_old=?, is_configurable_old=? WHERE pathname_old = ?")
checkErr(err)
res, err := stmt.Exec(
r.IdModule,
m.Pathname,
m.DisplayName,
m.Author,
m.Version,
r.Active,
m.Description,
m.IsConfigurable,
m.Pathname,
)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(m.Pathname, m.DisplayName, affect, "- is updated in DB")
}
return
}
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 {
var data XmlModule
xml.Unmarshal([]byte(rawXMLdata), &data)
return data
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}