-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.go
54 lines (45 loc) · 1.29 KB
/
migration.go
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
// migration.go Copyright (c) 2023 z0ne.
// All Rights Reserved.
// Licensed under the Apache 2.0 License.
// See LICENSE the project root for license information.
//
// SPDX-License-Identifier: Apache-2.0
// Package mgx is a simple migration tool for pgx
package mgx
import (
"context"
"fmt"
)
// MigrationFunc is a wrapper around a function so that it implements the Migration interface.
type MigrationFunc func(context.Context, Commands) error
// Migration is the migration interface
type Migration interface {
fmt.Stringer
Run(context.Context, Commands) error
}
type migrationFuncWrapper struct {
fn MigrationFunc
name string
}
// Run the migration
func (m *migrationFuncWrapper) Run(ctx context.Context, tx Commands) error {
return m.fn(ctx, tx)
}
// String returns the name of the migration
func (m *migrationFuncWrapper) String() string {
return m.name
}
// NewMigration creates a migration from a function.
func NewMigration(name string, fn MigrationFunc) Migration {
return &migrationFuncWrapper{
name: name,
fn: fn,
}
}
// NewRawMigration creates a migration from a raw SQL string.
func NewRawMigration(name, sql string) Migration {
return &migrationFuncWrapper{
name: name,
fn: func(ctx context.Context, tx Commands) error { _, err := tx.Exec(ctx, sql); return err },
}
}