-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeds.go
77 lines (65 loc) · 1.88 KB
/
embeds.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
76
77
package database
import (
"io/fs"
"strings"
"github.com/pkg/errors"
"zombiezen.com/go/sqlite/sqlitemigration"
)
type DomainEmbeds struct {
MigrationsFS fs.FS
}
type MigrationFile struct {
Domain string
File string
}
type Embeds struct {
AppID int32
DomainEmbeds map[string]DomainEmbeds
MigrationFiles []MigrationFile
PrepareConnQueriesFS fs.FS
}
const (
migrationUpFileExt = ".up.sql"
migrationDownFileExt = ".down.sql"
)
func (e Embeds) readMigrations(up bool) (migrations []string, err error) {
fileExt := migrationUpFileExt
if !up {
fileExt = migrationDownFileExt
}
migrations = make([]string, len(e.MigrationFiles))
for i, migrationFile := range e.MigrationFiles {
domainEmbed, ok := e.DomainEmbeds[migrationFile.Domain]
if !ok {
return nil, errors.Errorf("couldn't find migration domain %s", migrationFile.Domain)
}
rawMigration, err := readFile(migrationFile.File+fileExt, domainEmbed.MigrationsFS)
if err != nil {
return nil, errors.Wrapf(
err, "couldn't read migration file %s%s from domain %s",
migrationFile.File, fileExt, migrationFile.Domain,
)
}
migrations[i] = strings.TrimSpace(string(rawMigration))
}
return migrations, nil
}
func (e Embeds) ReadUpMigrations() (migrations []string, err error) {
migrations, err = e.readMigrations(true)
return migrations, errors.Wrap(err, "couldn't read up-migrations")
}
func (e Embeds) ReadDownMigrations() (migrations []string, err error) {
migrations, err = e.readMigrations(true)
return migrations, errors.Wrap(err, "couldn't read down-migrations")
}
func (e Embeds) NewSchema() (sqlitemigration.Schema, error) {
migrations, err := e.ReadUpMigrations()
if err != nil {
return sqlitemigration.Schema{}, errors.Wrap(err, "couldn't read migrations")
}
return sqlitemigration.Schema{
AppID: e.AppID,
Migrations: migrations,
// TODO: implement RepeatableMigration support
}, nil
}