supabase-operator/assets/migrations/migrations.go
Peter Kurfer 12090c913a
Some checks failed
E2E Tests / Run on Ubuntu (push) Failing after 20s
Lint / Run on Ubuntu (push) Failing after 3m45s
Tests / Run on Ubuntu (push) Failing after 16m8s
feat(db): prepare migrations and core CRD
2024-12-13 09:09:14 +01:00

62 lines
1 KiB
Go

package migrations
import (
"embed"
"io/fs"
"iter"
"path"
"slices"
"strings"
)
//go:embed */*.sql
var migrationsFS embed.FS
type Script struct {
FileName string
Content string
}
func InitScripts() iter.Seq2[Script, error] {
return readScripts(path.Join(".", "init-scripts"))
}
func MigrationScripts() iter.Seq2[Script, error] {
return readScripts(path.Join(".", "migrations"))
}
func readScripts(dir string) iter.Seq2[Script, error] {
return iter.Seq2[Script, error](func(yield func(Script, error) bool) {
files, err := migrationsFS.ReadDir(dir)
if err != nil {
yield(Script{}, err)
return
}
slices.SortFunc(files, func(a, b fs.DirEntry) int {
return strings.Compare(a.Name(), b.Name())
})
for _, file := range files {
if file.IsDir() {
continue
}
content, err := migrationsFS.ReadFile(path.Join(dir, file.Name()))
if err != nil {
if !yield(Script{}, err) {
return
}
}
s := Script{
FileName: file.Name(),
Content: string(content),
}
if !yield(s, nil) {
return
}
}
})
}