-
Notifications
You must be signed in to change notification settings - Fork 247
/
manager.go
53 lines (42 loc) · 1.63 KB
/
manager.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
package migrations
import (
"database/sql"
"fmt"
"regexp"
"github.com/authzed/spicedb/pkg/migrate"
)
const migrationNamePattern = `^[_a-zA-Z]*$`
var (
noNonatomicMigration migrate.MigrationFunc[Wrapper]
noTxMigration migrate.TxMigrationFunc[TxWrapper] // nolint: deadcode, unused, varcheck
migrationNameRe = regexp.MustCompile(migrationNamePattern)
// Manager is the singleton migration manager instance for MySQL
Manager = migrate.NewManager[*MySQLDriver, Wrapper, TxWrapper]()
)
// Wrapper makes it possible to forward the table schema needed for MySQL MigrationFunc to run
type Wrapper struct {
db *sql.DB
tables *tables
}
// TxWrapper makes it possible to forward the table schema to a transactional migration func.
type TxWrapper struct {
tx *sql.Tx
tables *tables
}
func mustRegisterMigration(version, replaces string, up migrate.MigrationFunc[Wrapper], upTx migrate.TxMigrationFunc[TxWrapper]) {
if err := registerMigration(version, replaces, up, upTx); err != nil {
panic("failed to register migration " + err.Error())
}
}
func registerMigration(version, replaces string, up migrate.MigrationFunc[Wrapper], upTx migrate.TxMigrationFunc[TxWrapper]) error {
// validate migration names to ensure they are compatible with mysql column names
for _, v := range []string{version, replaces} {
if match := migrationNameRe.MatchString(version); !match {
return fmt.Errorf("migration from '%s' to '%s': '%s' is an invalid mysql migration version, expected to match pattern '%s'",
replaces, version, v, migrationNamePattern,
)
}
}
// register the migration
return Manager.Register(version, replaces, up, upTx)
}