Skip to content

Commit 7842b92

Browse files
committed
Tests
1 parent 1c61b86 commit 7842b92

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ test:
3838
@mkdir $(COVERAGE_DIR)
3939
make test-with-flags TEST_FLAGS='-v -race -covermode atomic -coverprofile $$(COVERAGE_DIR)/combined.txt -bench=. -benchmem -timeout 20m'
4040

41-
4241
test-with-flags:
4342
@echo SOURCE: $(SOURCE)
4443
@echo DATABASE_TEST: $(DATABASE_TEST)

database/cassandra/cassandra.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (c *Cassandra) Run(migration io.Reader) error {
287287
// TODO: cast to Cassandra error and get line number
288288
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
289289
}
290-
if err := c.Trigger(database.TrigRunPre, struct {
290+
if err := c.Trigger(database.TrigRunPost, struct {
291291
Query string
292292
}{Query: string(migr)}); err != nil {
293293
return database.Error{OrigErr: err, Err: "failed to trigger RunPost"}

database/cockroachdb/cockroachdb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ func (c *CockroachDb) ensureVersionTable() (err error) {
411411
}
412412

413413
func (c *CockroachDb) ensureLockTable() error {
414+
414415
// check if lock table exists
415416
var count int
416417
query := `SELECT COUNT(1) FROM information_schema.tables WHERE table_name = $1 AND table_schema = (SELECT current_schema()) LIMIT 1`

database/testing/migrate_testing.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package testing
55

66
import (
7+
"github.com/golang-migrate/migrate/v4/database"
8+
"reflect"
79
"testing"
810
)
911

@@ -28,7 +30,74 @@ func TestMigrateDrop(t *testing.T, m *migrate.Migrate) {
2830

2931
func TestMigrateUp(t *testing.T, m *migrate.Migrate) {
3032
t.Log("UP")
33+
34+
tt := &triggerTest{
35+
t: t,
36+
m: m,
37+
triggered: map[string]bool{
38+
migrate.TrigRunMigrationPre: false,
39+
migrate.TrigRunMigrationPost: false,
40+
migrate.TrigRunMigrationVersionPre: false,
41+
migrate.TrigRunMigrationVersionPost: false,
42+
database.TrigRunPre: false,
43+
database.TrigRunPost: false,
44+
},
45+
}
46+
47+
m.Triggers = map[string]func(r migrate.TriggerResponse) error{
48+
migrate.TrigRunMigrationPre: tt.trigMigrationCheck,
49+
migrate.TrigRunMigrationPost: tt.trigMigrationCheck,
50+
migrate.TrigRunMigrationVersionPre: tt.trigMigrationCheck,
51+
migrate.TrigRunMigrationVersionPost: tt.trigMigrationCheck,
52+
}
53+
54+
m.AddDatabaseTriggers(map[string]func(response interface{}) error{
55+
database.TrigRunPre: tt.trigDatabaseMigrationCheck,
56+
database.TrigRunPost: tt.trigDatabaseMigrationCheck,
57+
})
58+
3159
if err := m.Up(); err != nil {
3260
t.Fatal(err)
3361
}
62+
63+
if !tt.triggered[migrate.TrigRunMigrationPre] {
64+
t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationPre)
65+
}
66+
if !tt.triggered[migrate.TrigRunMigrationPost] {
67+
t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationPost)
68+
}
69+
if !tt.triggered[migrate.TrigRunMigrationVersionPre] {
70+
t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationVersionPre)
71+
}
72+
if !tt.triggered[migrate.TrigRunMigrationVersionPost] {
73+
t.Fatalf("expected trigger %s to be called, but it was not", migrate.TrigRunMigrationVersionPost)
74+
}
75+
if !tt.triggered[database.TrigRunPre] {
76+
t.Fatalf("expected database trigger %s to be called, but it was not", database.TrigRunPre)
77+
}
78+
if !tt.triggered[database.TrigRunPost] {
79+
t.Fatalf("expected database trigger %s to be called, but it was not", database.TrigRunPost)
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 {
90+
tt.triggered[r.Trigger] = true
91+
return nil
92+
}
93+
94+
func (tt *triggerTest) trigDatabaseMigrationCheck(response interface{}) error {
95+
val := reflect.ValueOf(response)
96+
field := val.FieldByName("Trigger")
97+
if !field.IsValid() {
98+
tt.t.Fatalf("expected response to have a Trigger field, got %T", response)
99+
}
100+
101+
tt.triggered[field.String()] = true
102+
return nil
34103
}

migrate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ func (m *Migrate) Trigger(name string, detail interface{}) error {
238238
return nil
239239
}
240240

241+
func (m *Migrate) AddDatabaseTriggers(t map[string]func(response interface{}) error) {
242+
m.databaseDrv.AddTriggers(t)
243+
}
244+
241245
// Close closes the source and the database.
242246
func (m *Migrate) Close() (source error, database error) {
243247
databaseSrvClose := make(chan error)

0 commit comments

Comments
 (0)