migrate: add support for new Go io/fs.FS

This patch adds FromFS function and lets you use the new go:embed directive to add the migration files to the binary.
Also, migration tests use an in memory FS impl instead of working with tmp directories.
This commit is contained in:
Michał Matczuk
2021-04-29 18:19:32 +02:00
committed by Michal Jan Matczuk
parent 7980a955be
commit fe1498fe13
6 changed files with 73 additions and 107 deletions

View File

@@ -8,7 +8,7 @@ import (
"crypto/md5"
"encoding/hex"
"io"
"os"
"io/fs"
)
var encode = hex.EncodeToString
@@ -18,15 +18,15 @@ func checksum(b []byte) string {
return encode(v[:])
}
func fileChecksum(path string) (string, error) {
f, err := os.Open(path)
func fileChecksum(f fs.FS, path string) (string, error) {
file, err := f.Open(path)
if err != nil {
return "", nil
}
defer f.Close()
defer file.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
if _, err := io.Copy(h, file); err != nil {
return "", err
}
v := h.Sum(nil)