forked from pressly/goose
/
up.go
69 lines (67 loc) · 1.86 KB
/
up.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
package goose
import (
"database/sql"
"fmt"
"path/filepath"
)
// Up performs all types of migrations upwards, depending on the params.
func Up(db *sql.DB, dir string, includeMissing bool, onlyOne bool, endVersion *int64, dryRun bool) error {
var migrations Migrations
currentVersion, err := GetDBVersion(db)
if err != nil {
return err
}
unappliedMigrations, err := CollectUnappliedMigrations(db, dir)
if err != nil {
return err
}
if includeMissing {
migrations = unappliedMigrations
} else {
migrations, err = CollectMigrations(dir, currentVersion, maxVersion)
if err != nil {
return err
}
if len(unappliedMigrations) != len(migrations) {
return fmt.Errorf("missing migrations found! please run goose status -show-unapplied-only to find missing migrations, or run this command again with the -include-missing flag to apply them as well")
}
}
if dryRun {
log.Printf("goose: dry run. the following migrations would be applied. current version: %d\n", currentVersion)
} else {
log.Printf("goose: applying migrations. current version: %d\n", currentVersion)
}
statuses, err := dbMigrationsStatus(db)
if err != nil {
return err
}
for _, migration := range migrations {
if endVersion != nil && migration.Version > *endVersion {
break
}
if _, ok := statuses[migration.Version]; ok {
log.Printf("goose version was out of sync. skipping already-applied migration %v\n", filepath.Base(migration.Source))
continue
}
if dryRun {
log.Println(filepath.Base(migration.Source))
} else {
if err := migration.Up(db); err != nil {
return err
}
}
if onlyOne {
break
}
}
currentVersion, err = GetDBVersion(db)
if err != nil {
return err
}
if dryRun {
log.Println("goose: no more migrations would be run.")
} else {
log.Printf("goose: no more migrations to apply. current version: %d\n", currentVersion)
}
return nil
}