forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
104 lines (90 loc) · 2.04 KB
/
sync.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
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
type SyncUser2 struct {
Id int64
Name string `xorm:"unique"`
Age int `xorm:"index"`
Title string
Address string
Genre string
Area string
Date int
}
type SyncLoginInfo2 struct {
Id int64
IP string `xorm:"index"`
UserId int64
AddedCol int
// timestamp should be updated by database, so only allow get from db
TimeStamp string
// assume
Nonuse int `xorm:"unique"`
Newa string `xorm:"index"`
}
func sync(engine *xorm.Engine) error {
return engine.Sync(&SyncLoginInfo2{}, &SyncUser2{})
}
func sqliteEngine() (*xorm.Engine, error) {
f := "sync.db"
//os.Remove(f)
return xorm.NewEngine("sqlite3", f)
}
func mysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
}
func postgresEngine() (*xorm.Engine, error) {
return xorm.NewEngine("postgres", "dbname=xorm_test sslmode=disable")
}
type engineFunc func() (*xorm.Engine, error)
func main() {
//engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
//engines := []engineFunc{sqliteEngine}
//engines := []engineFunc{mysqlEngine}
engines := []engineFunc{postgresEngine}
for _, enginefunc := range engines {
Orm, err := enginefunc()
fmt.Println("--------", Orm.DriverName, "----------")
if err != nil {
fmt.Println(err)
return
}
Orm.ShowSQL = true
err = sync(Orm)
if err != nil {
fmt.Println(err)
}
_, err = Orm.Where("id > 0").Delete(&SyncUser2{})
if err != nil {
fmt.Println(err)
}
user := &SyncUser2{
Name: "testsdf",
Age: 15,
Title: "newsfds",
Address: "fasfdsafdsaf",
Genre: "fsafd",
Area: "fafdsafd",
Date: 1000,
}
_, err = Orm.Insert(user)
if err != nil {
fmt.Println(err)
return
}
isexist, err := Orm.IsTableExist("sync_user2")
if err != nil {
fmt.Println(err)
return
}
if !isexist {
fmt.Println("sync_user2 is not exist")
return
}
}
}