Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer close of migration file #430

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/xataio/pgroll/cmd/flags"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/roll"

"github.com/spf13/cobra"
)

func startCmd() *cobra.Command {
Expand Down Expand Up @@ -43,17 +43,10 @@ func startCmd() *cobra.Command {
}

func runMigrationFromFile(ctx context.Context, m *roll.Roll, fileName string, complete bool) error {
file, err := os.Open(fileName)
migration, err := readMigration(fileName)
if err != nil {
return fmt.Errorf("opening migration file: %w", err)
}

migration, err := migrations.ReadMigration(file)
if err != nil {
file.Close()
return fmt.Errorf("reading migration file: %w", err)
return err
}
file.Close()

sp, _ := pterm.DefaultSpinner.WithText("Starting migration...").Start()
cb := func(n int64) {
Expand Down Expand Up @@ -83,3 +76,18 @@ func runMigrationFromFile(ctx context.Context, m *roll.Roll, fileName string, co

return nil
}

func readMigration(fileName string) (*migrations.Migration, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("opening migration file: %w", err)
}
defer file.Close()

migration, err := migrations.ReadMigration(file)
if err != nil {
return nil, fmt.Errorf("reading migration file: %w", err)
}

return migration, nil
}