2024-01-04 17:55:26 +00:00
|
|
|
// Package migrations provides a way to embed the migrations into the binary.
|
2022-09-28 04:26:44 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"os"
|
2023-11-16 02:43:44 +00:00
|
|
|
"path"
|
2022-09-28 04:26:44 +00:00
|
|
|
)
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
//go:embed all:migrations
|
2022-09-28 04:26:44 +00:00
|
|
|
var Files embed.FS
|
|
|
|
|
|
|
|
// Write writes the embedded migrations to a temporary directory.
|
|
|
|
// It returns an error and a cleanup function. The cleanup function
|
|
|
|
// should be called when the migrations are no longer needed.
|
|
|
|
func Write(temp string) error {
|
2023-02-18 06:41:01 +00:00
|
|
|
err := os.MkdirAll(temp, 0o755)
|
2022-09-28 04:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
fsDir, err := Files.ReadDir("migrations")
|
2022-09-28 04:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fsDir {
|
|
|
|
if f.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-16 02:43:44 +00:00
|
|
|
b, err := Files.ReadFile(path.Join("migrations", f.Name()))
|
2022-09-28 04:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-16 02:43:44 +00:00
|
|
|
err = os.WriteFile(path.Join(temp, f.Name()), b, 0o644)
|
2022-09-28 04:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|