Skip to content

Commit

Permalink
Revert "Open and parse files lazily"
Browse files Browse the repository at this point in the history
This reverts commit 859f73b.
  • Loading branch information
KeiichiHirobe committed Feb 3, 2022
1 parent 859f73b commit 5d0f4bf
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 262 deletions.
147 changes: 51 additions & 96 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
"sort"
Expand Down Expand Up @@ -120,59 +121,13 @@ func SetIgnoreUnknown(v bool) {
migSet.IgnoreUnknown = v
}

type MigrationData struct {
Up []string
Down []string
DisableTransactionUp bool
DisableTransactionDown bool
}

type migrationFile struct {
dir http.FileSystem
root string
baseName string
}

type Migration struct {
Id string
Id string
Up []string
Down []string

// data may be nil if not parsed yet.
data *MigrationData
// file is information of migration file, which is used to parse later.
// file may be nil if migration file has already been parsed when Migration is initialized.
file *migrationFile
}

// Data parses migration file if not yet, and returns *MigrationData
func (m *Migration) Data() (*MigrationData, error) {
if m.data != nil {
return m.data, nil
}
err := m.loadFile()
if err != nil {
return nil, err
}
return m.data, nil
}

func (m *Migration) loadFile() error {
if m.file == nil {
return fmt.Errorf("Error m.file must not be nil when call loadFile")
}
root := m.file.root
name := m.file.baseName
file, err := m.file.dir.Open(path.Join(root, name))
if err != nil {
return fmt.Errorf("Error while opening %s: %s", name, err)
}
defer func() { _ = file.Close() }()

migrationData, err := ParseMigration(name, file)
if err != nil {
return fmt.Errorf("Error while parsing %s: %s", name, err)
}
m.data = migrationData
return nil
DisableTransactionUp bool
DisableTransactionDown bool
}

func (m Migration) Less(other *Migration) bool {
Expand Down Expand Up @@ -311,14 +266,11 @@ func findMigrations(dir http.FileSystem, root string) ([]*Migration, error) {

for _, info := range files {
if strings.HasSuffix(info.Name(), ".sql") {
migration := &Migration{
Id: info.Name(),
file: &migrationFile{
dir: dir,
root: root,
baseName: info.Name(),
},
migration, err := migrationFromFile(dir, root, info)
if err != nil {
return nil, err
}

migrations = append(migrations, migration)
}
}
Expand All @@ -329,6 +281,21 @@ func findMigrations(dir http.FileSystem, root string) ([]*Migration, error) {
return migrations, nil
}

func migrationFromFile(dir http.FileSystem, root string, info os.FileInfo) (*Migration, error) {
path := path.Join(root, info.Name())
file, err := dir.Open(path)
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
Expand Down Expand Up @@ -358,14 +325,11 @@ func (a AssetMigrationSource) FindMigrations() ([]*Migration, error) {
return nil, err
}

migrationData, err := ParseMigration(name, bytes.NewReader(file))
migration, err := ParseMigration(name, bytes.NewReader(file))
if err != nil {
return nil, err
}
migration := &Migration{
Id: name,
data: migrationData,
}

migrations = append(migrations, migration)
}
}
Expand Down Expand Up @@ -418,14 +382,11 @@ func (p PackrMigrationSource) FindMigrations() ([]*Migration, error) {
return nil, err
}

migrationData, err := ParseMigration(name, bytes.NewReader(file))
migration, err := ParseMigration(name, bytes.NewReader(file))
if err != nil {
return nil, err
}
migration := &Migration{
Id: name,
data: migrationData,
}

migrations = append(migrations, migration)
}
}
Expand All @@ -437,18 +398,23 @@ func (p PackrMigrationSource) FindMigrations() ([]*Migration, error) {
}

// Migration parsing
func ParseMigration(id string, r io.ReadSeeker) (*MigrationData, error) {
func ParseMigration(id string, r io.ReadSeeker) (*Migration, error) {
m := &Migration{
Id: id,
}

parsed, err := sqlparse.ParseMigration(r)
if err != nil {
return nil, fmt.Errorf("Error parsing migration (%s): %s", id, err)
}

return &MigrationData{
Up: parsed.UpStatements,
Down: parsed.DownStatements,
DisableTransactionUp: parsed.DisableTransactionUp,
DisableTransactionDown: parsed.DisableTransactionDown,
}, nil
m.Up = parsed.UpStatements
m.Down = parsed.DownStatements

m.DisableTransactionUp = parsed.DisableTransactionUp
m.DisableTransactionDown = parsed.DisableTransactionDown

return m, nil
}

type SqlExecutor interface {
Expand Down Expand Up @@ -609,11 +575,7 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
// Add missing migrations up to the last run migration.
// This can happen for example when merges happened.
if len(existingMigrations) > 0 {
catchUp, err := ToCatchup(migrations, existingMigrations, record)
if err != nil {
return nil, nil, err
}
result = append(result, catchUp...)
result = append(result, ToCatchup(migrations, existingMigrations, record)...)
}

// Figure out which migrations to apply
Expand All @@ -623,21 +585,18 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
toApplyCount = max
}
for _, v := range toApply[0:toApplyCount] {
migrationData, err := v.Data()
if err != nil {
return nil, nil, err
}

if dir == Up {
result = append(result, &PlannedMigration{
Migration: v,
Queries: migrationData.Up,
DisableTransaction: migrationData.DisableTransactionUp,
Queries: v.Up,
DisableTransaction: v.DisableTransactionUp,
})
} else if dir == Down {
result = append(result, &PlannedMigration{
Migration: v,
Queries: migrationData.Down,
DisableTransaction: migrationData.DisableTransactionDown,
Queries: v.Down,
DisableTransaction: v.DisableTransactionDown,
})
}
}
Expand Down Expand Up @@ -724,7 +683,7 @@ func ToApply(migrations []*Migration, current string, direction MigrationDirecti
panic("Not possible")
}

func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration) ([]*PlannedMigration, error) {
func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration) []*PlannedMigration {
missing := make([]*PlannedMigration, 0)
for _, migration := range migrations {
found := false
Expand All @@ -735,18 +694,14 @@ func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration)
}
}
if !found && migration.Less(lastRun) {
migrationData, err := migration.Data()
if err != nil {
return nil, err
}
missing = append(missing, &PlannedMigration{
Migration: migration,
Queries: migrationData.Up,
DisableTransaction: migrationData.DisableTransactionUp,
Queries: migration.Up,
DisableTransaction: migration.DisableTransactionUp,
})
}
}
return missing, nil
return missing
}

func GetMigrationRecords(db *sql.DB, dialect string) ([]*MigrationRecord, error) {
Expand Down
Loading

0 comments on commit 5d0f4bf

Please sign in to comment.