MCPcopy
hub / github.com/rubenv/sql-migrate / ApplyMigrations

Function ApplyMigrations

sql-migrate/command_common.go:9–62  ·  view source on GitHub ↗
(dir migrate.MigrationDirection, dryrun bool, limit int, version int64)

Source from the content-addressed store, hash-verified

7)
8
9func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, version int64) error {
10 env, err := GetEnvironment()
11 if err != nil {
12 return fmt.Errorf("Could not parse config: %w", err)
13 }
14
15 db, dialect, err := GetConnection(env)
16 if err != nil {
17 return err
18 }
19 defer db.Close()
20
21 source := migrate.FileMigrationSource{
22 Dir: env.Dir,
23 }
24
25 if dryrun {
26 var migrations []*migrate.PlannedMigration
27
28 if version >= 0 {
29 migrations, _, err = migrate.PlanMigrationToVersion(db, dialect, source, dir, version)
30 } else {
31 migrations, _, err = migrate.PlanMigration(db, dialect, source, dir, limit)
32 }
33
34 if err != nil {
35 return fmt.Errorf("Cannot plan migration: %w", err)
36 }
37
38 for _, m := range migrations {
39 PrintMigration(m, dir)
40 }
41 } else {
42 var n int
43
44 if version >= 0 {
45 n, err = migrate.ExecVersion(db, dialect, source, dir, version)
46 } else {
47 n, err = migrate.ExecMax(db, dialect, source, dir, limit)
48 }
49
50 if err != nil {
51 return fmt.Errorf("Migration failed: %w", err)
52 }
53
54 if n == 1 {
55 ui.Output("Applied 1 migration")
56 } else {
57 ui.Output(fmt.Sprintf("Applied %d migrations", n))
58 }
59 }
60
61 return nil
62}
63
64func PrintMigration(m *migrate.PlannedMigration, dir migrate.MigrationDirection) {
65 switch dir {

Callers 2

RunMethod · 0.85
RunMethod · 0.85

Calls 7

GetEnvironmentFunction · 0.85
GetConnectionFunction · 0.85
PrintMigrationFunction · 0.85
PlanMigrationMethod · 0.80
ExecVersionMethod · 0.80
ExecMaxMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…