Skip to content

Commit c310d77

Browse files
authored
Merge pull request #21 from jmezo/migrate-ctx-timeout
add migrate function with context timeout
2 parents 32d3e09 + 8eb39ac commit c310d77

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

migration/migration.go

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"embed"
66
"errors"
7+
"fmt"
78
"io"
89
"io/fs"
910
"os"
@@ -117,6 +118,34 @@ func MigrationToolWithError(conn string, down bool, opts Opts, embeds ...embed.F
117118
return err
118119
}
119120

121+
func MigrationToolWithErrorCtx(ctx context.Context, conn string, down bool, opts Opts, embeds ...embed.FS) error {
122+
resultCh := make(chan error, 1)
123+
go func() {
124+
m, err := Get(ctx, conn, opts, embeds...)
125+
if err != nil {
126+
resultCh <- err
127+
return
128+
}
129+
if m == nil {
130+
resultCh <- errors.New("migration is nil")
131+
return
132+
}
133+
134+
if down {
135+
resultCh <- m.Down()
136+
} else {
137+
resultCh <- m.Up()
138+
}
139+
}()
140+
141+
select {
142+
case err := <-resultCh:
143+
return err
144+
case <-ctx.Done():
145+
return fmt.Errorf("migration operation timed out: %w", ctx.Err())
146+
}
147+
}
148+
120149
func PrepareCockroach(conn string) string {
121150
return strings.Replace(conn, "postgres://", "cockroach://", 1)
122151
}

0 commit comments

Comments
 (0)