This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
64 lines (57 loc) · 1.89 KB
/
status.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
package goose
import (
"context"
"database/sql"
"errors"
"fmt"
"path/filepath"
"time"
)
// Status prints the status of all migrations.
func Status(db *sql.DB, dir string, opts ...OptionsFunc) error {
ctx := context.Background()
return StatusContext(ctx, db, dir, opts...)
}
// StatusContext prints the status of all migrations.
func StatusContext(ctx context.Context, db *sql.DB, dir string, opts ...OptionsFunc) error {
option := &options{}
for _, f := range opts {
f(option)
}
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return fmt.Errorf("failed to collect migrations: %w", err)
}
if option.noVersioning {
log.Printf(" Applied At Migration\n")
log.Printf(" =======================================\n")
for _, current := range migrations {
log.Printf(" %-24s -- %v\n", "no versioning", filepath.Base(current.Source))
}
return nil
}
// must ensure that the version table exists if we're running on a pristine DB
if _, err := EnsureDBVersionContext(ctx, db); err != nil {
return fmt.Errorf("failed to ensure DB version: %w", err)
}
log.Printf(" Applied At Migration\n")
log.Printf(" =======================================\n")
for _, migration := range migrations {
if err := printMigrationStatus(ctx, db, migration.Version, filepath.Base(migration.Source)); err != nil {
return fmt.Errorf("failed to print status: %w", err)
}
}
return nil
}
func printMigrationStatus(ctx context.Context, db *sql.DB, version int64, script string) error {
m, err := store.GetMigration(ctx, db, TableName(), version)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("failed to query the latest migration: %w", err)
}
appliedAt := "Pending"
if m != nil && m.IsApplied {
appliedAt = m.Timestamp.Format(time.ANSIC)
}
log.Printf(" %-24s -- %v\n", appliedAt, script)
return nil
}