mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-04 16:50:27 +00:00
move temp write logic to migrations package
This commit is contained in:
parent
1b66a051fc
commit
0acb254581
2 changed files with 60 additions and 47 deletions
|
@ -80,42 +80,13 @@ func run(cfg *config.Config) error {
|
|||
}
|
||||
}(c)
|
||||
|
||||
err = func() error {
|
||||
temp := filepath.Join(os.TempDir(), "migrations")
|
||||
defer func() {
|
||||
err := os.RemoveAll(temp)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("failed to remove temp directory")
|
||||
}
|
||||
}()
|
||||
|
||||
err := os.MkdirAll(temp, 0755)
|
||||
err = migrations.Write(temp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write the embed migrations to the temp directory.
|
||||
fsDir, err := migrations.Files.ReadDir(".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, f := range fsDir {
|
||||
if f.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
b, err := migrations.Files.ReadFile(filepath.Join("migrations", f.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join(temp, f.Name()), b, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
dir, err := atlas.NewLocalDir(temp)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -127,9 +98,7 @@ func run(cfg *config.Config) error {
|
|||
schema.WithDropIndex(true),
|
||||
}
|
||||
|
||||
return c.Schema.Create(context.Background(), options...)
|
||||
}()
|
||||
|
||||
err = c.Schema.Create(context.Background(), options...)
|
||||
if err != nil {
|
||||
log.Fatal().
|
||||
Err(err).
|
||||
|
@ -138,6 +107,12 @@ func run(cfg *config.Config) error {
|
|||
Msg("failed creating schema resources")
|
||||
}
|
||||
|
||||
err = os.RemoveAll(temp)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to remove temporary directory for database migrations")
|
||||
return err
|
||||
}
|
||||
|
||||
app.db = c
|
||||
app.repos = repo.EntAllRepos(c, cfg.Storage.Data)
|
||||
app.services = services.NewServices(app.repos)
|
||||
|
|
|
@ -1,6 +1,44 @@
|
|||
package migrations
|
||||
|
||||
import "embed"
|
||||
import (
|
||||
"embed"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// go:embed all:migrations
|
||||
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 {
|
||||
err := os.MkdirAll(temp, 0755)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fsDir, err := Files.ReadDir(".")
|
||||
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
|
||||
}
|
||||
|
||||
err = os.WriteFile(filepath.Join(temp, f.Name()), b, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue