mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-04 08:40:28 +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,56 +80,25 @@ func run(cfg *config.Config) error {
|
||||||
}
|
}
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
err = func() error {
|
temp := filepath.Join(os.TempDir(), "migrations")
|
||||||
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the embed migrations to the temp directory.
|
dir, err := atlas.NewLocalDir(temp)
|
||||||
fsDir, err := migrations.Files.ReadDir(".")
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range fsDir {
|
options := []schema.MigrateOption{
|
||||||
if f.IsDir() {
|
schema.WithDir(dir),
|
||||||
continue
|
schema.WithDropColumn(true),
|
||||||
}
|
schema.WithDropIndex(true),
|
||||||
|
}
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
options := []schema.MigrateOption{
|
|
||||||
schema.WithDir(dir),
|
|
||||||
schema.WithDropColumn(true),
|
|
||||||
schema.WithDropIndex(true),
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Schema.Create(context.Background(), options...)
|
|
||||||
}()
|
|
||||||
|
|
||||||
|
err = c.Schema.Create(context.Background(), options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
Err(err).
|
Err(err).
|
||||||
|
@ -138,6 +107,12 @@ func run(cfg *config.Config) error {
|
||||||
Msg("failed creating schema resources")
|
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.db = c
|
||||||
app.repos = repo.EntAllRepos(c, cfg.Storage.Data)
|
app.repos = repo.EntAllRepos(c, cfg.Storage.Data)
|
||||||
app.services = services.NewServices(app.repos)
|
app.services = services.NewServices(app.repos)
|
||||||
|
|
|
@ -1,6 +1,44 @@
|
||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import "embed"
|
import (
|
||||||
|
"embed"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
// go:embed all:migrations
|
// go:embed all:migrations
|
||||||
var Files embed.FS
|
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