Skip to content

Commit

Permalink
Fix error returns, close query rows
Browse files Browse the repository at this point in the history
  • Loading branch information
exavolt committed Mar 21, 2024
1 parent 9f57f27 commit 422ee4a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions fwish.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var (
// ErrSchemaIndexFileNotFound is returned when a migration source
// does not contain the fwish.yaml file
ErrSchemaIndexFileNotFound = errors.New("fwish: schema index file not found")

ErrSchemaHasFailedMigration = errors.New("fwish: schema has failed migration")
)

// Might want store the tx in here too
Expand Down Expand Up @@ -236,15 +238,15 @@ func (m *Migrator) Migrate(db DB, schemaName string) (num int, err error) {
var searchPath string
err = st.db.QueryRow("SHOW search_path").Scan(&searchPath)
if err != nil {
return 0, err
return -1, err
}

_, err = st.db.Exec("SET search_path TO " + st.schemaName)
if err != nil {
return 0, err
return -1, err
}
defer func() {
_, err = st.db.Exec("SET search_path TO " + searchPath)
_, err := st.db.Exec("SET search_path TO " + searchPath)
if err != nil {
if logger := m.logger; logger != nil {
logger.Output(2, "SET search_path returned error: "+err.Error())
Expand All @@ -254,13 +256,13 @@ func (m *Migrator) Migrate(db DB, schemaName string) (num int, err error) {

err = m.validateDBSchema(st)
if err != nil {
return 0, err
return -1, err
}

if st.installedRank == -1 {
err = m.ensureDBSchemaInitialized(st)
if err != nil {
return 0, err
return -1, err
}
}

Expand All @@ -276,7 +278,7 @@ func (m *Migrator) Migrate(db DB, schemaName string) (num int, err error) {
}
err = m.executeMigration(st, int32(i+1), &sf)
if err != nil {
return 0, err
return -1, err
}
num++
}
Expand Down Expand Up @@ -415,6 +417,7 @@ func (m *Migrator) validateDBSchema(st *state) error {
}
return nil
}
defer rows.Close()

var i, rank int32
var version, script string
Expand All @@ -433,8 +436,7 @@ func (m *Migrator) validateDBSchema(st *state) error {
}

if !success {
// what to do?
panic("DB has failed migration")
return ErrSchemaHasFailedMigration
}

if int(i) > len(m.versions) {
Expand Down

0 comments on commit 422ee4a

Please sign in to comment.