forked from GuiaBolso/darwin
-
Notifications
You must be signed in to change notification settings - Fork 11
/
models.go
80 lines (65 loc) · 1.85 KB
/
models.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
package darwin
import (
"crypto/md5"
"fmt"
"time"
)
const (
// Ignored means that the migrations was not appied to the database.
Ignored Status = iota
// Applied means that the migrations was successfully applied to the database.
Applied
// Pending means that the migrations is a new migration and it is waiting
// to be applied to the database.
Pending
// Error means that the migration could not be applied to the database.
Error
)
// =============================================================================
// Status is a migration status value.
type Status int
// String implements the Stringer interface.
func (s Status) String() string {
switch s {
case Ignored:
return "IGNORED"
case Applied:
return "APPLIED"
case Pending:
return "PENDING"
case Error:
return "ERROR"
default:
return "INVALID"
}
}
// =============================================================================
// Migration represents a database migrations.
type Migration struct {
Version float64
Description string
Script string
}
// Checksum calculate the Script md5.
func (m Migration) Checksum() string {
return fmt.Sprintf("%x", md5.Sum([]byte(m.Script)))
}
// MigrationInfo is a struct used in the infoChan to inform clients about
// the migration being applied.
type MigrationInfo struct {
Status Status
Error error
Migration Migration
}
// MigrationRecord is the entry in schema table.
type MigrationRecord struct {
Version float64
Description string
Checksum string
AppliedAt time.Time
ExecutionTime time.Duration
}
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 }