-
Notifications
You must be signed in to change notification settings - Fork 33
/
driver.go
165 lines (134 loc) · 3.3 KB
/
driver.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
package darwin
import (
"database/sql"
"fmt"
"time"
)
// MigrationRecord is the entry in schema table
type MigrationRecord struct {
Version float64
Description string
Checksum string
AppliedAt time.Time
ExecutionTime time.Duration
}
// Driver a database driver abstraction
type Driver interface {
Create() error
Insert(e MigrationRecord) error
All() ([]MigrationRecord, error)
Exec(string) (time.Duration, error)
}
// GenericDriver is the default Driver, it can be configured to any database.
type GenericDriver struct {
DB *sql.DB
Dialect Dialect
}
// NewGenericDriver creates a new GenericDriver configured with db and dialect.
// Panic if db or dialect is nil
func NewGenericDriver(db *sql.DB, dialect Dialect) *GenericDriver {
if db == nil {
panic("darwin: sql.DB is nil")
}
if dialect == nil {
panic("darwin: dialect is nil")
}
return &GenericDriver{DB: db, Dialect: dialect}
}
// Create create the table darwin_migrations if necessary
func (m *GenericDriver) Create() error {
err := transaction(m.DB, func(tx *sql.Tx) error {
_, err := tx.Exec(m.Dialect.CreateTableSQL())
return err
})
return err
}
// Insert insert a migration entry into database
func (m *GenericDriver) Insert(e MigrationRecord) error {
err := transaction(m.DB, func(tx *sql.Tx) error {
_, err := tx.Exec(m.Dialect.InsertSQL(),
e.Version,
e.Description,
e.Checksum,
e.AppliedAt.Unix(),
e.ExecutionTime,
)
return err
})
return err
}
// All returns all migrations applied
func (m *GenericDriver) All() ([]MigrationRecord, error) {
entries := []MigrationRecord{}
rows, err := m.DB.Query(m.Dialect.AllSQL())
if err != nil {
return []MigrationRecord{}, err
}
for rows.Next() {
var (
version float64
description string
checksum string
appliedAt int64
executionTime float64
)
rows.Scan(
&version,
&description,
&checksum,
&appliedAt,
&executionTime,
)
entry := MigrationRecord{
Version: version,
Description: description,
Checksum: checksum,
AppliedAt: time.Unix(appliedAt, 0),
ExecutionTime: time.Duration(executionTime),
}
entries = append(entries, entry)
}
rows.Close()
return entries, nil
}
// Exec execute sql scripts into database
func (m *GenericDriver) Exec(script string) (time.Duration, error) {
start := time.Now()
err := transaction(m.DB, func(tx *sql.Tx) error {
_, err := tx.Exec(script)
return err
})
return time.Since(start), err
}
// transaction is a utility function to execute the SQL inside a transaction
// Panic if db is nil
// see: http://stackoverflow.com/a/23502629
func transaction(db *sql.DB, f func(*sql.Tx) error) (err error) {
if db == nil {
panic("darwin: sql.DB is nil")
}
tx, err := db.Begin()
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
switch p := p.(type) {
case error:
err = p
default:
err = fmt.Errorf("%s", p)
}
}
if err != nil {
tx.Rollback()
return
}
err = tx.Commit()
}()
return f(tx)
}
type byMigrationRecordVersion []MigrationRecord
func (b byMigrationRecordVersion) Len() int { return len(b) }
func (b byMigrationRecordVersion) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byMigrationRecordVersion) Less(i, j int) bool { return b[i].Version < b[j].Version }