2022-09-28 04:26:44 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := Files.ReadFile(filepath.Join("migrations", f.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
err = os.WriteFile(filepath.Join(temp, f.Name()), b, 0o644)
|
2022-09-28 04:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|