From 49434c45af754572bd721b88d4db40a8bf2f5cf7 Mon Sep 17 00:00:00 2001 From: Will Dixon Date: Thu, 12 Dec 2019 21:21:51 -0500 Subject: [PATCH] Close the file when finished parsing them --- migrate.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/migrate.go b/migrate.go index a8ddc2d6..a458b87f 100644 --- a/migrate.go +++ b/migrate.go @@ -2,6 +2,7 @@ package migrate import ( "bytes" + "os" "database/sql" "errors" "fmt" @@ -228,14 +229,9 @@ func findMigrations(dir http.FileSystem) ([]*Migration, error) { for _, info := range files { if strings.HasSuffix(info.Name(), ".sql") { - file, err := dir.Open(info.Name()) + migration, err := migrationFromFile(dir, info) if err != nil { - return nil, fmt.Errorf("Error while opening %s: %s", info.Name(), err) - } - - migration, err := ParseMigration(info.Name(), file) - if err != nil { - return nil, fmt.Errorf("Error while parsing %s: %s", info.Name(), err) + return nil, err } migrations = append(migrations, migration) @@ -248,6 +244,20 @@ func findMigrations(dir http.FileSystem) ([]*Migration, error) { return migrations, nil } +func migrationFromFile(dir http.FileSystem, info os.FileInfo) (*Migration, error) { + file, err := dir.Open(info.Name()) + if err != nil { + return nil, fmt.Errorf("Error while opening %s: %s", info.Name(), err) + } + defer func () { _ = file.Close() }() + + migration, err := ParseMigration(info.Name(), file) + if err != nil { + return nil, fmt.Errorf("Error while parsing %s: %s", info.Name(), err) + } + return migration, nil +} + // Migrations from a bindata asset set. type AssetMigrationSource struct { // Asset should return content of file in path if exists