Skip to content

Commit

Permalink
Change signature of migrations.ReadMigrationFile (#177)
Browse files Browse the repository at this point in the history
Read migrations from from an abitrary `io.Reader` rather than a
filename.

The CLI is concerned with files, but the module API should work with a
`Reader`.
  • Loading branch information
andrew-farries committed Oct 6, 2023
1 parent a24eeef commit 0fb46f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 8 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -31,7 +32,13 @@ func startCmd() *cobra.Command {
}
defer m.Close()

migration, err := migrations.ReadMigrationFile(args[0])
file, err := os.Open(args[0])
if err != nil {
return fmt.Errorf("opening migration file: %w", err)
}
defer file.Close()

migration, err := migrations.ReadMigration(file)
if err != nil {
return fmt.Errorf("reading migration file: %w", err)
}
Expand Down
13 changes: 2 additions & 11 deletions pkg/migrations/op_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"os"
)

type OpName string
Expand Down Expand Up @@ -37,16 +36,8 @@ func TemporaryName(name string) string {
return "_pgroll_new_" + name
}

func ReadMigrationFile(file string) (*Migration, error) {
// read operations from file
jsonFile, err := os.Open(file)
if err != nil {
return nil, err
}
defer jsonFile.Close()

// read our opened xmlFile as a byte array.
byteValue, err := io.ReadAll(jsonFile)
func ReadMigration(r io.Reader) (*Migration, error) {
byteValue, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0fb46f2

Please sign in to comment.