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

fix creating concurrent migration file #708

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, m
}

path := filepath.Join(dir, filename)
if _, err := os.Stat(path); !os.IsNotExist(err) {
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest replacing this with the code mentioned in the documentation to os.IsNotExist function
!errors.Is(err, fs.ErrNotExist)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this code could be simplidfied in fort of fast failure:

_, err := os.Stat(path)
if err == nil {
	return fmt.Errorf("failed to create migration file: %w", os.ErrExist)
}

if !errors.Is(err, fs.ErrNotExist){
        return fmt.Errorf("failed to create migration file: %w", err)
}

return fmt.Errorf("failed to create migration file: %w", err)
}

if err == nil {
return fmt.Errorf("failed to create migration file: %w", os.ErrExist)
}

f, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create migration file: %w", err)
Expand Down