-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_pg_test.go
More file actions
132 lines (109 loc) · 3.21 KB
/
migrate_pg_test.go
File metadata and controls
132 lines (109 loc) · 3.21 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package migrate_test
import (
"context"
"database/sql"
"embed"
"fmt"
"testing"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/ladzaretti/migrate"
"github.com/ladzaretti/migrate/migratetest"
)
var (
//go:embed testdata/pg/migrations
embedPostgresFS embed.FS
embeddedPostgresMigrations = migrate.EmbeddedMigrations{
FS: embedPostgresFS,
Path: "testdata/pg/migrations",
}
)
func postgresTestContainer(ctx context.Context) (*postgres.PostgresContainer, error) {
ctr, err := postgres.Run(ctx,
"postgres:16-alpine",
postgres.WithDatabase("database"),
postgres.WithUsername("postgres"),
postgres.WithPassword("postgres"),
postgres.WithSQLDriver("pgx"),
postgres.BasicWaitStrategies(),
)
if err != nil {
return nil, fmt.Errorf("create test container: %v", err)
}
if err := ctr.Snapshot(ctx); err != nil {
return nil, fmt.Errorf("create snapshot: %v", err)
}
return ctr, nil
}
func setupPostgresTestSuite(ctx context.Context, t *testing.T, rawMigrations []string, embeddedMigrations migrate.EmbeddedMigrations) (*testSuite, func()) {
t.Helper()
ctr, err := postgresTestContainer(ctx)
if err != nil {
t.Fatalf("create test container: %v", err)
}
connString, err := ctr.ConnectionString(ctx)
if err != nil {
t.Fatalf("connection string: %v", err)
}
cleanup := func() {
_ = testcontainers.TerminateContainer(ctr)
}
helper := func(ctx context.Context, t *testing.T) *sql.DB {
t.Helper()
if err := ctr.Restore(ctx); err != nil {
t.Fatalf("restore database: %v", err)
}
db, err := sql.Open("pgx", connString)
if err != nil {
t.Fatalf("open database: %v", err)
}
t.Cleanup(func() {
_ = db.Close()
})
return db
}
suite, err := newTestSuite(testSuiteConfig{
dbHelper: helper,
dialect: migrate.PostgreSQLDialect{},
embeddedMigrations: embeddedMigrations,
rawMigrations: rawMigrations,
})
if err != nil {
t.Fatalf("create test suite: %v", err)
}
return suite, cleanup
}
func TestMigrateWithPostgres(t *testing.T) {
rawMigrations := []string{
`CREATE TABLE
IF NOT EXISTS testing_migration_1 (
id INTEGER PRIMARY KEY,
another_id INTEGER,
something_else TEXT
);
`,
`CREATE TABLE
IF NOT EXISTS testing_migration_2 (
id INTEGER PRIMARY KEY,
another_id INTEGER,
something_else TEXT
);
`,
}
suite, cleanup := setupPostgresTestSuite(t.Context(), t, rawMigrations, embeddedPostgresMigrations)
defer cleanup()
t.Run("TestDialect", func(t *testing.T) {
if err := migratetest.TestDialect(t.Context(), suite.dbHelper(t.Context(), t), migrate.PostgreSQLDialect{}); err != nil {
t.Fatalf("TestDialect: %v", err)
}
})
t.Run("ApplyStringMigrations", suite.applyStringMigrations)
t.Run("ApplyEmbeddedMigrations", suite.applyEmbeddedMigrations)
t.Run("ApplyWithTxDisabled", suite.applyWithTxDisabled)
t.Run("ApplyWithNoChecksumValidation", suite.applyWithNoChecksumValidation)
t.Run("ApplyWithFilter", suite.applyWithFilter)
t.Run("ReapplyAll", suite.reapplyAll)
t.Run("RollsBackOnSQLError", suite.rollsBackOnSQLError)
t.Run("RollsBackOnValidationError", suite.rollsBackOnValidationError)
}