MCPcopy
hub / github.com/riverqueue/river / TestMigrator

Function TestMigrator

rivermigrate/river_migrate_test.go:62–930  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

60}
61
62func TestMigrator(t *testing.T) {
63 t.Parallel()
64
65 ctx := context.Background()
66
67 migrationsBundle := buildTestMigrationsBundle(t)
68
69 type testBundle struct {
70 dbPool *pgxpool.Pool
71 driver *driverWithAlternateLine
72 logger *slog.Logger
73 schema string
74 }
75
76 setup := func(t *testing.T) (*Migrator[pgx.Tx], *testBundle) {
77 t.Helper()
78
79 // Not all migrations can be executed together in a single transaction.
80 // Examples include `CREATE INDEX CONCURRENTLY`, or adding an enum value
81 // that's used by a later migration. As such, the migrator and its tests
82 // must use a full database with commits between each migration.
83 //
84 // To make this easier to clean up afterward, we create a new, clean schema
85 // for each test run and then drop it afterward.
86 dbPool := riversharedtest.DBPool(ctx, t)
87 schema := "river_migrate_test_" + randutil.Hex(8)
88 _, err := dbPool.Exec(ctx, "CREATE SCHEMA "+schema)
89 require.NoError(t, err)
90
91 t.Cleanup(func() {
92 _, err := dbPool.Exec(ctx, fmt.Sprintf("DROP SCHEMA %s CASCADE", schema))
93 require.NoError(t, err)
94 })
95
96 bundle := &testBundle{
97 dbPool: dbPool,
98 driver: &driverWithAlternateLine{Driver: riverpgxv5.New(dbPool)},
99 logger: riversharedtest.Logger(t),
100 schema: schema,
101 }
102
103 migrator, err := New(bundle.driver, &Config{
104 Logger: bundle.logger,
105 Schema: schema,
106 })
107 require.NoError(t, err)
108 migrator.migrations = migrationsBundle.WithTestVersionsMap
109
110 return migrator, bundle
111 }
112
113 t.Run("NewUnknownLine", func(t *testing.T) {
114 t.Parallel()
115
116 _, bundle := setup(t)
117
118 _, err := New(bundle.driver, &Config{Line: "unknown_line"})
119 require.EqualError(t, err, "migration line does not exist: unknown_line")

Callers

nothing calls this directly

Calls 15

DBPoolFunction · 0.92
HexFunction · 0.92
NewFunction · 0.92
LoggerFunction · 0.92
MapFunction · 0.92
WithReplacementsFunction · 0.92
seqOneToFunction · 0.85
dbExecErrorFunction · 0.85
seqDownToFunction · 0.85
CleanupMethod · 0.80
ExistingVersionsTxMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…