forked from DavidHuie/gomigrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
175 lines (132 loc) · 4.16 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
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
package gomigrate
import (
"strconv"
"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}
}
// CockroachDB
type CockroachDB struct {
Postgres
}
// 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 {
delimiter := ";"
// we look at the first line of the migration for `delimiter foo`.
// If found, we strip the line off, unquote the value, and use it as the delimiter
if strings.HasPrefix(sql, "delimiter ") {
delimiterOffset := len("delimiter ")
contentSplit := strings.SplitN(sql[delimiterOffset:], "\n", 2)
delimiter = strings.TrimSpace(contentSplit[0])
if len(contentSplit) > 1 {
sql = contentSplit[1]
} else {
sql = ""
}
delimiterUnquoted, err := strconv.Unquote(delimiter)
if err == nil {
delimiter = delimiterUnquoted
}
}
return strings.Split(sql, delimiter)
}
// 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}
}
// MSSQL
type Mssql struct{}
func (m Mssql) SelectMigrationTableSql() string {
return "SELECT table_name FROM information_schema.tables WHERE table_name = ?"
}
func (m Mssql) CreateMigrationTableSql() string {
return `CREATE TABLE gomigrate (
id INT NOT NULL IDENTITY,
migration_id BIGINT NOT NULL UNIQUE,
PRIMARY KEY (id)
)`
}
func (m Mssql) GetMigrationSql() string {
return `SELECT migration_id FROM gomigrate WHERE migration_id = ?`
}
func (m Mssql) MigrationLogInsertSql() string {
return "INSERT INTO gomigrate (migration_id) values (?)"
}
func (m Mssql) MigrationLogDeleteSql() string {
return "DELETE FROM gomigrate WHERE migration_id = ?"
}
func (m Mssql) GetMigrationCommands(sql string) []string {
return []string{sql}
}