MCPcopy Index your code
hub / github.com/riverqueue/river / Example_migrate

Function Example_migrate

rivermigrate/example_migrate_test.go:19–70  ·  view source on GitHub ↗

Example_migrate demonstrates the use of River's Go migration API by migrating up and down.

()

Source from the content-addressed store, hash-verified

17// Example_migrate demonstrates the use of River's Go migration API by migrating
18// up and down.
19func Example_migrate() {
20 ctx := context.Background()
21
22 dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL())
23 if err != nil {
24 panic(err)
25 }
26 defer dbPool.Close()
27
28 driver := riverpgxv5.New(dbPool)
29 migrator, err := rivermigrate.New(driver, &rivermigrate.Config{
30 // Test schema with no migrations for purposes of this test.
31 Schema: riverdbtest.TestSchema(ctx, testutil.PanicTB(), driver, &riverdbtest.TestSchemaOpts{Lines: []string{}}),
32 })
33 if err != nil {
34 panic(err)
35 }
36
37 printVersions := func(res *rivermigrate.MigrateResult) {
38 for _, version := range res.Versions {
39 fmt.Printf("Migrated [%s] version %d\n", strings.ToUpper(string(res.Direction)), version.Version)
40 }
41 }
42
43 // Migrate to version 3. An actual call may want to omit all MigrateOpts,
44 // which will default to applying all available up migrations.
45 res, err := migrator.Migrate(ctx, rivermigrate.DirectionUp, &rivermigrate.MigrateOpts{
46 TargetVersion: 3,
47 })
48 if err != nil {
49 panic(err)
50 }
51 printVersions(res)
52
53 // Migrate down by three steps. Down migrating defaults to running only one
54 // step unless overridden by an option like MaxSteps or TargetVersion.
55 res, err = migrator.Migrate(ctx, rivermigrate.DirectionDown, &rivermigrate.MigrateOpts{
56 MaxSteps: 3,
57 })
58 if err != nil {
59 panic(err)
60 }
61 printVersions(res)
62
63 // Output:
64 // Migrated [UP] version 1
65 // Migrated [UP] version 2
66 // Migrated [UP] version 3
67 // Migrated [DOWN] version 3
68 // Migrated [DOWN] version 2
69 // Migrated [DOWN] version 1
70}

Callers

nothing calls this directly

Calls 7

TestDatabaseURLFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
TestSchemaFunction · 0.92
PanicTBFunction · 0.92
CloseMethod · 0.65
MigrateMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…