Skip to content

Commit 35f3e3e

Browse files
committed
migrate: move MigrationTask to separate file
1 parent a79e50e commit 35f3e3e

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

migrate.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,59 +55,6 @@ func (e ErrDirty) Error() string {
5555
return fmt.Sprintf("Dirty database version %v. Fix and force version.", e.Version)
5656
}
5757

58-
// MigrationTask is a callback function type that can be used to execute a
59-
// Golang based migration step after a SQL based migration step has been
60-
// executed. The callback function receives the migration and the database
61-
// driver as arguments.
62-
type MigrationTask func(migr *Migration, driver database.Driver) error
63-
64-
// options is a set of optional options that can be set when a Migrate instance
65-
// is created.
66-
type options struct {
67-
// tasks is a map of MigrationTask functions that can be used to execute
68-
// a Golang based migration step after a SQL based migration step ha
69-
// been executed. The key is the migration version and the value is the
70-
// callback function that should be run _after_ the step was executed
71-
// (but within the same database transaction).
72-
tasks map[uint]MigrationTask
73-
}
74-
75-
// defaultOptions returns a new options struct with default values.
76-
func defaultOptions() options {
77-
return options{
78-
tasks: make(map[uint]MigrationTask),
79-
}
80-
}
81-
82-
// Option is a function that can be used to set options on a Migrate instance.
83-
type Option func(*options)
84-
85-
// WithMigrationTasks is an option that can be used to set a map of
86-
// MigrationTask functions that can be used to execute a Golang based migration
87-
// step after a SQL based migration step has been executed. The key is the
88-
// migration version and the value is the task function that should be run
89-
// _after_ the step was executed (but before the version is marked as cleanly
90-
// executed). An error returned from the task will cause the migration to fail
91-
// and the step to be marked as dirty.
92-
func WithMigrationTasks(tasks map[uint]MigrationTask) Option {
93-
return func(o *options) {
94-
o.tasks = tasks
95-
}
96-
}
97-
98-
// WithMigrationTask is an option that can be used to set a MigrationTask
99-
// function that can be used to execute a Golang based migration step after the
100-
// SQL based migration step with the given version number has been executed. The
101-
// task is the function that should be run _after_ the step was executed
102-
// (but before the version is marked as cleanly executed). An error returned
103-
// from the task will cause the migration to fail and the step to be marked as
104-
// dirty.
105-
func WithMigrationTask(version uint, task MigrationTask) Option {
106-
return func(o *options) {
107-
o.tasks[version] = task
108-
}
109-
}
110-
11158
type Migrate struct {
11259
sourceName string
11360
sourceDrv source.Driver

migration_task.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package migrate
2+
3+
import "github.com/golang-migrate/migrate/v4/database"
4+
5+
// MigrationTask is a callback function type that can be used to execute a
6+
// Golang based migration step after a SQL based migration step has been
7+
// executed. The callback function receives the migration and the database
8+
// driver as arguments.
9+
type MigrationTask func(migr *Migration, driver database.Driver) error
10+
11+
// options is a set of optional options that can be set when a Migrate instance
12+
// is created.
13+
type options struct {
14+
// tasks is a map of MigrationTask functions that can be used to execute
15+
// a Golang based migration step after a SQL based migration step ha
16+
// been executed. The key is the migration version and the value is the
17+
// callback function that should be run _after_ the step was executed
18+
// (but within the same database transaction).
19+
tasks map[uint]MigrationTask
20+
}
21+
22+
// defaultOptions returns a new options struct with default values.
23+
func defaultOptions() options {
24+
return options{
25+
tasks: make(map[uint]MigrationTask),
26+
}
27+
}
28+
29+
// Option is a function that can be used to set options on a Migrate instance.
30+
type Option func(*options)
31+
32+
// WithMigrationTasks is an option that can be used to set a map of
33+
// MigrationTask functions that can be used to execute a Golang based migration
34+
// step after a SQL based migration step has been executed. The key is the
35+
// migration version and the value is the task function that should be run
36+
// _after_ the step was executed (but before the version is marked as cleanly
37+
// executed). An error returned from the task will cause the migration to fail
38+
// and the step to be marked as dirty.
39+
func WithMigrationTasks(tasks map[uint]MigrationTask) Option {
40+
return func(o *options) {
41+
o.tasks = tasks
42+
}
43+
}
44+
45+
// WithMigrationTask is an option that can be used to set a MigrationTask
46+
// function that can be used to execute a Golang based migration step after the
47+
// SQL based migration step with the given version number has been executed. The
48+
// task is the function that should be run _after_ the step was executed
49+
// (but before the version is marked as cleanly executed). An error returned
50+
// from the task will cause the migration to fail and the step to be marked as
51+
// dirty.
52+
func WithMigrationTask(version uint, task MigrationTask) Option {
53+
return func(o *options) {
54+
o.tasks[version] = task
55+
}
56+
}

0 commit comments

Comments
 (0)