-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
75 lines (61 loc) · 2.25 KB
/
config.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package igmigrator
import (
"os"
"regexp"
"strings"
"github.com/worldline-go/logz"
)
// Config provides a way to specify some optional configuration.
//
// Most configuration options have sane defaults, which should not be changed if not specifically required.
//
// By default, no callbacks are not set.
type Config struct {
// MigrationsDir can provide a directory that will hold the migration files.
//
// By default, has value of `/var/migrations/`, and should not be changed if not required.
// It is possible to set this value from environment variable `IGMIGRATOR_MIGRATION_DIR`
// if value for this variable is not set.
MigrationsDir string
// Schema can specify which schema(using `set search_path`) should be used to run migrations in.
//
// By default, it will not change schema.
Schema string
// MigrationTable can provide table name for the table that will hold migrations.
//
// By default, has value of 'migrations', and should not be changed if not required.
// It is possible to set this value from environment variable `IGMIGRATION_MIGRATION_TABLE`
// if value for this variable is not set.
MigrationTable string
// BeforeMigrationsFunc will be called after current DB version is retrieved
BeforeMigrationsFunc
// AfterSingleMigrationFunc will be called after each single transaction was run
AfterSingleMigrationFunc
// AfterAllMigrationsFunc will be executed when all migrations were executed successfully.
// It will not be called if any error happened.
AfterAllMigrationsFunc
// Values for expand function in migration files.
Values map[string]string
Logger logz.Adapter
}
// SetDefaults will update missing values with default ones(if any).
func (c *Config) SetDefaults() {
replaceRegexp := regexp.MustCompile("[^a-zA-Z0-9_]")
trim := func(input string) string {
return replaceRegexp.ReplaceAllLiteralString(input, "")
}
setString := func(s *string, env, def string) {
*s = strings.TrimSpace(*s)
if *s == "" {
*s = os.Getenv(env)
}
if *s == "" {
*s = def
}
}
c.Schema = strings.TrimSpace(c.Schema)
setString(&c.MigrationsDir, "IGMIGRATOR_MIGRATION_DIR", "migrations")
setString(&c.MigrationTable, "IGMIGRATION_MIGRATION_TABLE", "migration")
c.MigrationTable = trim(c.MigrationTable)
c.Schema = trim(c.Schema)
}