This repository was archived by the owner on Jun 1, 2022. It is now read-only.
forked from lonja/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathup.go
More file actions
153 lines (133 loc) · 3.31 KB
/
up.go
File metadata and controls
153 lines (133 loc) · 3.31 KB
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
package goose
import (
"database/sql"
"fmt"
"time"
)
// UpTo migrates up to a specific version.
func UpTo(db *sql.DB, dir string, version int64) error {
migrations, err := CollectMigrations(dir, minVersion, version)
if err != nil {
return err
}
for {
current, err := GetDBVersion(db)
if err != nil {
return err
}
next, err := migrations.Next(current)
if err != nil {
if err == ErrNoNextVersion {
log.Printf("goose: no migrations to run. current version: %d\n", current)
return nil
}
return err
}
if err = next.Up(db); err != nil {
return err
}
}
}
// Up applies all available migrations.
func Up(db *sql.DB, dir string) error {
return UpTo(db, dir, maxVersion)
}
func UpAll(db *sql.DB, dir string) error {
applied, err := AppliedDBVersions(db)
if err != nil {
return err
}
migrations, err := CollectAllMigrations(dir, applied, minVersion, MaxVersion)
if err != nil {
return err
}
for {
current, err := GetDBVersion(db)
if err != nil {
return err
}
next, err := migrations.Next(current)
if err != nil {
if err == ErrNoNextVersion {
log.Printf("goose: no migrations to run. current version: %d\n", current)
return nil
}
return err
}
if err = next.Up(db); err != nil {
return err
}
}
}
func fixUp(db *sql.DB) error {
log.Print("goose: fixing migrations order\n")
rows, err := GetDialect().dbVersionQuery(db)
if err != nil {
return err
}
defer rows.Close()
tx, err := db.Begin()
var prevRow *MigrationRecord
for rows.Next() {
row := new(MigrationRecord)
if err = rows.Scan(&row.ID, &row.VersionID, &row.IsApplied, &row.TStamp); err != nil {
log.Fatal("error scanning rows:", err)
}
if prevRow == nil {
prevRow = row
continue
}
if prevRow.ID > row.ID && prevRow.VersionID < row.VersionID {
if err := swapRows(tx, prevRow, row); err != nil {
_ = tx.Rollback()
return err
}
continue
} else if prevRow.ID < row.ID && prevRow.VersionID > row.VersionID {
if err := swapRows(tx, prevRow, row); err != nil {
_ = tx.Rollback()
return err
}
prevRow = row
}
prevRow = row
}
return tx.Commit()
}
func swapRows(tx *sql.Tx, row1 *MigrationRecord, row2 *MigrationRecord) error {
row2.ID, row1.ID = row1.ID, row2.ID
q := fmt.Sprintf(`UPDATE "%s" SET version_id = %d, is_applied = %t, tstamp = '%s' WHERE id = %d;`, TableName(), row1.VersionID, row1.IsApplied, row1.TStamp.Format(time.RFC3339), row1.ID)
_, err := tx.Exec(q)
if err != nil {
return err
}
q = fmt.Sprintf(`UPDATE "%s" SET version_id = %d, is_applied = %t, tstamp = '%s' WHERE id = %d;`, TableName(), row2.VersionID, row2.IsApplied, row2.TStamp.Format(time.RFC3339), row2.ID)
_, err = tx.Exec(q)
if err != nil {
return err
}
log.Printf("OK swapped %d and %d", row1.VersionID, row2.VersionID)
return nil
}
// UpByOne migrates up by a single version.
func UpByOne(db *sql.DB, dir string) error {
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
}
currentVersion, err := GetDBVersion(db)
if err != nil {
return err
}
next, err := migrations.Next(currentVersion)
if err != nil {
if err == ErrNoNextVersion {
log.Printf("goose: no migrations to run. current version: %d\n", currentVersion)
}
return err
}
if err = next.Up(db); err != nil {
return err
}
return nil
}