forked from DavidHuie/gomigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
114 lines (86 loc) · 2.79 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
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)
) ENGINE=MyISAM`
}
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}
}