-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathversion.go
More file actions
111 lines (94 loc) · 2.19 KB
/
version.go
File metadata and controls
111 lines (94 loc) · 2.19 KB
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
package mg
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
type VersionSQLBuilder interface {
FetchApplied() string
FetchCurrentApplied() string
InsretApply(version uint64) string
DeleteApply(version uint64) string
CreateTable() string
}
type (
vPostgres struct {
table string
}
vMySQL struct {
table string
}
)
func FetchVersionSQLBuilder(driver, table string) VersionSQLBuilder {
switch driver {
case "postgres":
return &vPostgres{
table: table,
}
case "mysql":
return &vMySQL{
table: table,
}
}
return nil
}
func (v *vPostgres) FetchApplied() string {
return fmt.Sprintf(
"SELECT applied_version FROM %s;",
v.table,
)
}
func (v *vPostgres) FetchCurrentApplied() string {
return fmt.Sprintf(
"SELECT applied_version FROM %s ORDER BY applied_version DESC LIMIT 1;",
v.table,
)
}
func (v *vPostgres) InsretApply(version uint64) string {
return fmt.Sprintf(
"INSERT INTO %s (applied_version) VALUES (%d);",
v.table, version,
)
}
func (v *vPostgres) DeleteApply(version uint64) string {
return fmt.Sprintf(
"DELETE FROM %s WHERE applied_version = %d;",
v.table, version,
)
}
func (v *vPostgres) CreateTable() string {
return fmt.Sprintf(
"CREATE TABLE IF NOT EXISTS %s (applied_version BIGSERIAL PRIMARY KEY, created_at timestamp with time zone NOT NULL DEFAULT now());",
v.table,
)
}
func (v *vMySQL) FetchApplied() string {
return fmt.Sprintf(
"SELECT applied_version FROM %s;",
v.table,
)
}
func (v *vMySQL) FetchCurrentApplied() string {
return fmt.Sprintf(
"SELECT applied_version FROM %s ORDER BY applied_version DESC LIMIT 1;",
v.table,
)
}
func (v *vMySQL) InsretApply(version uint64) string {
return fmt.Sprintf(
"INSERT INTO %s (applied_version) VALUES (%d);",
v.table, version,
)
}
func (v *vMySQL) DeleteApply(version uint64) string {
return fmt.Sprintf(
"DELETE FROM %s WHERE applied_version = %d;",
v.table, version,
)
}
func (v *vMySQL) CreateTable() string {
return fmt.Sprintf(
"CREATE TABLE IF NOT EXISTS %s (applied_version bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (applied_version)) ENGINE=InnoDB;",
v.table,
)
}