-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.go
65 lines (59 loc) · 2.25 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
// Package pomegranate implements helper functions and a CLI (pmg) for creating
// and safely running SQL migrations.
//
// Go projects can use pomegranate to ingest .sql files into a Go binary,
// and run them from there. See the README.md on Github for examples and
// explanations.
package pomegranate
import (
"strconv"
"time"
)
// MigrationRecord provides information on which migrations are currently in effect. An array of
// MigrationRecords is referred to as a "state" throughout the Pomegranate source. These are
// treated as a stack; MigrationRecords are added (inserted into the DB) as migrations run forward,
// and popped off (deleted from the DB) as migrations are run backward.
type MigrationRecord struct {
Name string `db:"name"`
Time time.Time `db:"time"`
Who string `db:"who"`
}
// Migration contains the name and SQL for a migration. Arrays of Migrations
// are passed between many functions in the Pomegranate source.
// SeperateForwardStatements runs SQL statements seperately, delinieated by ";"
type Migration struct {
Name string
ForwardSQL []string
BackwardSQL []string
}
// QuotedTemplateForward returns the ForwardSQL field of the Migration, properly escaped for easy
// injection into a migrations.go template.
func (m Migration) QuotedTemplateForward() []string {
//first quote
fwdSQLArr := []string{}
for _, sql := range m.ForwardSQL {
fwdSQLArr = append(fwdSQLArr, strconv.Quote(sql))
}
return fwdSQLArr
}
// QuotedTemplateBackward returns the BackwardSQL field of the Migration, properly escaped for easy
// injection into a migrations.go template.
func (m Migration) QuotedTemplateBackward() []string {
//first quote
bwdSQLArr := []string{}
for _, sql := range m.BackwardSQL {
bwdSQLArr = append(bwdSQLArr, strconv.Quote(sql))
}
return bwdSQLArr
}
// MigrationLogRecord represents a specific migration run at a specific point in time. Unlike
// MigrationRecord, this is an append-only table, showing the complete history of all forward and
// backward migrations. It is populated automatically by a Postgres trigger created in the init
// migration.
type MigrationLogRecord struct {
ID int `db:"id"`
Time time.Time `db:"time"`
Name string `db:"name"`
Op string `db:"op"`
Who string `db:"who"`
}