-
Notifications
You must be signed in to change notification settings - Fork 27
/
db.go
145 lines (109 loc) · 3.58 KB
/
db.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
package gomigrate
import "strings"
type Migratable interface {
SelectMigrationTableSql() string
CreateMigrationTableSql() string
GetMigrationSql() string
MigrationLogInsertSql() string
MigrationLogDeleteSql() string
GetMigrationCommands(string) []string
}
// POSTGRES
type Postgres struct{}
func (p Postgres) SelectMigrationTableSql() string {
return "SELECT tablename FROM pg_catalog.pg_tables WHERE tablename = $1"
}
func (p Postgres) CreateMigrationTableSql() string {
return `CREATE TABLE gomigrate (
id SERIAL PRIMARY KEY,
migration_id BIGINT UNIQUE NOT NULL
)`
}
func (p Postgres) GetMigrationSql() string {
return `SELECT migration_id FROM gomigrate WHERE migration_id = $1`
}
func (p Postgres) MigrationLogInsertSql() string {
return "INSERT INTO gomigrate (migration_id) values ($1)"
}
func (p Postgres) MigrationLogDeleteSql() string {
return "DELETE FROM gomigrate WHERE migration_id = $1"
}
func (p Postgres) GetMigrationCommands(sql string) []string {
return []string{sql}
}
// MYSQL
type Mysql struct{}
func (m Mysql) SelectMigrationTableSql() string {
return "SELECT table_name FROM information_schema.tables WHERE table_name = ? AND table_schema = (SELECT DATABASE())"
}
func (m Mysql) CreateMigrationTableSql() string {
return `CREATE TABLE gomigrate (
id INT NOT NULL AUTO_INCREMENT,
migration_id BIGINT NOT NULL UNIQUE,
PRIMARY KEY (id)
)`
}
func (m Mysql) GetMigrationSql() string {
return `SELECT migration_id FROM gomigrate WHERE migration_id = ?`
}
func (m Mysql) MigrationLogInsertSql() string {
return "INSERT INTO gomigrate (migration_id) values (?)"
}
func (m Mysql) MigrationLogDeleteSql() string {
return "DELETE FROM gomigrate WHERE migration_id = ?"
}
func (m Mysql) GetMigrationCommands(sql string) []string {
count := strings.Count(sql, ";")
commands := strings.SplitN(string(sql), ";", count)
return commands
}
// MARIADB
type Mariadb struct {
Mysql
}
// SQLITE3
type Sqlite3 struct{}
func (s Sqlite3) SelectMigrationTableSql() string {
return "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?"
}
func (s Sqlite3) CreateMigrationTableSql() string {
return `CREATE TABLE gomigrate (
id INTEGER PRIMARY KEY,
migration_id INTEGER NOT NULL UNIQUE
)`
}
func (s Sqlite3) GetMigrationSql() string {
return "SELECT migration_id FROM gomigrate WHERE migration_id = ?"
}
func (s Sqlite3) MigrationLogInsertSql() string {
return "INSERT INTO gomigrate (migration_id) values (?)"
}
func (s Sqlite3) MigrationLogDeleteSql() string {
return "DELETE FROM gomigrate WHERE migration_id = ?"
}
func (s Sqlite3) GetMigrationCommands(sql string) []string {
return []string{sql}
}
// SqlServer
type SqlServer struct{}
func (s SqlServer) SelectMigrationTableSql() string {
return "SELECT name FROM sys.objects WHERE object_id = object_id(?)"
}
func (s SqlServer) CreateMigrationTableSql() string {
return `CREATE TABLE gomigrate (
id INT IDENTITY(1,1) PRIMARY KEY,
migration_id BIGINT NOT NULL
)`
}
func (s SqlServer) GetMigrationSql() string {
return `SELECT migration_id FROM gomigrate WHERE migration_id = ?`
}
func (s SqlServer) MigrationLogInsertSql() string {
return "INSERT INTO gomigrate (migration_id) values (?)"
}
func (s SqlServer) MigrationLogDeleteSql() string {
return "DELETE FROM gomigrate WHERE migration_id = ?"
}
func (s SqlServer) GetMigrationCommands(sql string) []string {
return []string{sql}
}