• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

golang-migrate / migrate / 16295271531

15 Jul 2025 01:55PM UTC coverage: 56.553% (+0.2%) from 56.314%
16295271531

Pull #1294

github

dsyers
lint
Pull Request #1294: Triggers

790 of 1325 new or added lines in 24 files covered. (59.62%)

4 existing lines in 4 files now uncovered.

5277 of 9331 relevant lines covered (56.55%)

55.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.91
/database/testing/migrate_testing.go
1
// Package testing has the database tests.
2
// All database drivers must pass the Test function.
3
// This lives in it's own package so it stays a test dependency.
4
package testing
5

6
import (
7
        "github.com/golang-migrate/migrate/v4/database"
8
        "reflect"
9
        "testing"
10
)
11

12
import (
13
        "github.com/golang-migrate/migrate/v4"
14
)
15

16
// TestMigrate runs integration-tests between the Migrate layer and database implementations.
17
func TestMigrate(t *testing.T, m *migrate.Migrate) {
61✔
18
        TestMigrateUp(t, m)
61✔
19
        TestMigrateDrop(t, m)
61✔
20
}
61✔
21

22
// Regression test for preventing a regression for #164 https://github.com/golang-migrate/migrate/pull/173
23
// Similar to TestDrop(), but tests the dropping mechanism through the Migrate logic instead, to check for
24
// double-locking during the Drop logic.
25
func TestMigrateDrop(t *testing.T, m *migrate.Migrate) {
61✔
26
        if err := m.Drop(); err != nil {
61✔
27
                t.Fatal(err)
×
28
        }
×
29
}
30

31
func TestMigrateUp(t *testing.T, m *migrate.Migrate) {
61✔
32
        t.Log("UP")
61✔
33

61✔
34
        tt := &triggerTest{
61✔
35
                t: t,
61✔
36
                m: m,
61✔
37
                triggered: map[string]bool{
61✔
38
                        migrate.TrigRunMigrationPre:         false,
61✔
39
                        migrate.TrigRunMigrationPost:        false,
61✔
40
                        migrate.TrigRunMigrationVersionPre:  false,
61✔
41
                        migrate.TrigRunMigrationVersionPost: false,
61✔
42
                        database.TrigRunPre:                 false,
61✔
43
                        database.TrigRunPost:                false,
61✔
44
                },
61✔
45
        }
61✔
46

61✔
47
        m.Triggers = map[string]func(r migrate.TriggerResponse) error{
61✔
48
                migrate.TrigRunMigrationPre:         tt.trigMigrationCheck,
61✔
49
                migrate.TrigRunMigrationPost:        tt.trigMigrationCheck,
61✔
50
                migrate.TrigRunMigrationVersionPre:  tt.trigMigrationCheck,
61✔
51
                migrate.TrigRunMigrationVersionPost: tt.trigMigrationCheck,
61✔
52
        }
61✔
53

61✔
54
        m.AddDatabaseTriggers(map[string]func(response interface{}) error{
61✔
55
                database.TrigRunPre:  tt.trigDatabaseMigrationCheck,
61✔
56
                database.TrigRunPost: tt.trigDatabaseMigrationCheck,
61✔
57
        })
61✔
58

61✔
59
        if err := m.Up(); err != nil {
61✔
60
                t.Fatal(err)
×
61
        }
×
62

63
        if !tt.triggered[migrate.TrigRunMigrationPre] {
61✔
NEW
64
                t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationPre)
×
NEW
65
        }
×
66
        if !tt.triggered[migrate.TrigRunMigrationPost] {
61✔
NEW
67
                t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationPost)
×
NEW
68
        }
×
69
        if !tt.triggered[migrate.TrigRunMigrationVersionPre] {
61✔
NEW
70
                t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationVersionPre)
×
NEW
71
        }
×
72
        if !tt.triggered[migrate.TrigRunMigrationVersionPost] {
61✔
NEW
73
                t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationVersionPost)
×
NEW
74
        }
×
75
        if !tt.triggered[database.TrigRunPre] {
61✔
NEW
76
                t.Fatalf("expected database trigger %s to be called, but it was not", database.TrigRunPre)
×
NEW
77
        }
×
78
        if !tt.triggered[database.TrigRunPost] {
61✔
NEW
79
                t.Fatalf("expected database trigger %s to be called, but it was not", database.TrigRunPost)
×
NEW
80
        }
×
81
}
82

83
type triggerTest struct {
84
        t         *testing.T
85
        m         *migrate.Migrate
86
        triggered map[string]bool
87
}
88

89
func (tt *triggerTest) trigMigrationCheck(r migrate.TriggerResponse) error {
804✔
90
        tt.triggered[r.Trigger] = true
804✔
91
        return nil
804✔
92
}
804✔
93

94
func (tt *triggerTest) trigDatabaseMigrationCheck(response interface{}) error {
726✔
95
        val := reflect.ValueOf(response)
726✔
96
        field := val.FieldByName("Trigger")
726✔
97
        if !field.IsValid() {
726✔
NEW
98
                tt.t.Fatalf("expected response to have a Trigger field, got %T", response)
×
NEW
99
        }
×
100

101
        tt.triggered[field.String()] = true
726✔
102
        return nil
726✔
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc