version.go

 1package goose
 2
 3import (
 4	"context"
 5	"database/sql"
 6	"fmt"
 7)
 8
 9// Version prints the current version of the database.
10func Version(db *sql.DB, dir string, opts ...OptionsFunc) error {
11	ctx := context.Background()
12	return VersionContext(ctx, db, dir, opts...)
13}
14
15// VersionContext prints the current version of the database.
16func VersionContext(ctx context.Context, db *sql.DB, dir string, opts ...OptionsFunc) error {
17	option := &options{}
18	for _, f := range opts {
19		f(option)
20	}
21	if option.noVersioning {
22		var current int64
23		migrations, err := CollectMigrations(dir, minVersion, maxVersion)
24		if err != nil {
25			return fmt.Errorf("failed to collect migrations: %w", err)
26		}
27		if len(migrations) > 0 {
28			current = migrations[len(migrations)-1].Version
29		}
30		log.Printf("goose: file version %v", current)
31		return nil
32	}
33
34	current, err := GetDBVersionContext(ctx, db)
35	if err != nil {
36		return err
37	}
38	log.Printf("goose: version %v", current)
39	return nil
40}
41
42var tableName = "goose_db_version"
43
44// TableName returns goose db version table name
45func TableName() string {
46	return tableName
47}
48
49// SetTableName set goose db version table name
50func SetTableName(n string) {
51	tableName = n
52}