forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
73 lines (64 loc) · 1.95 KB
/
version.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
package goose
import (
"database/sql"
"fmt"
)
// Version prints the current version of the database.
func Version(db *sql.DB, dir string, opts ...OptionsFunc) error {
return defaultProvider.Version(db, dir, opts...)
}
// Version prints the current version of the database.
func (p *Provider) Version(db *sql.DB, dir string, opts ...OptionsFunc) error {
migrationVersion, dbVersion, err := p.GetVersions(db, dir, opts...)
if err != nil {
return err
}
if migrationVersion != -1 {
p.log.Printf("goose: file version %v\n", migrationVersion)
}
if migrationVersion != -1 {
p.log.Printf("goose: version %v\n", dbVersion)
}
return nil
}
// GetVersion will return the current version of the migration, and database version, or -1, -1 if not
// found or if there is an error
// If db is nil, or the option.noVersioning is specificed, then the dbVersion will be -1.
func (p *Provider) GetVersions(db *sql.DB, dir string, opts ...OptionsFunc) (migrationVersion int64, dbVersion int64, err error) {
if p == nil {
return -1, -1, nil
}
var (
option = applyOptions(opts)
)
migrationVersion, dbVersion = -1, -1
migrations, err := p.CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return -1, -1, fmt.Errorf("failed to collect migrations: %w", err)
}
if len(migrations) > 0 {
migrationVersion = migrations[len(migrations)-1].Version
}
if option.noVersioning {
return migrationVersion, dbVersion, nil
}
dbVersion, err = p.GetDBVersion(db)
return migrationVersion, dbVersion, err
}
// TableName returns goose db version table name
func TableName() string {
return defaultProvider.tableName
}
// TableName returns goose db version table name
func (p *Provider) TableName() string {
return p.tableName
}
// SetTableName set goose db version table name
func SetTableName(n string) {
defaultProvider.SetTableName(n)
}
// SetTableName set goose db version table name
func (p *Provider) SetTableName(n string) {
p.tableName = n
p.dialect.SetTableName(n)
}