-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libs): add automatic migration dir discovery (#1673)
- Loading branch information
Showing
2 changed files
with
84 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/uptrace/bun" | ||
) | ||
|
||
func CollectMigrationFiles(fs embed.FS) ([]Migration, error) { | ||
entries, err := fs.ReadDir("migrations") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "collecting migration files") | ||
} | ||
|
||
ret := make([]Migration, len(entries)) | ||
for i, entry := range entries { | ||
fileContent, err := fs.ReadFile(filepath.Join("migrations", entry.Name())) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "reading migration file %s", entry.Name()) | ||
} | ||
|
||
ret[i] = Migration{ | ||
Name: entry.Name(), | ||
UpWithContext: func(ctx context.Context, tx bun.Tx) error { | ||
_, err := tx.ExecContext(ctx, string(fileContent)) | ||
return err | ||
}, | ||
} | ||
} | ||
|
||
return ret, nil | ||
} |