Remove unnecessary panics from core implementation

This commit is contained in:
Dale Hui 2019-02-19 01:59:04 -08:00
parent 9b449be538
commit 0f8263de2e
2 changed files with 11 additions and 9 deletions

View File

@ -16,7 +16,7 @@ import (
// Test runs tests against database implementations.
func Test(t *testing.T, d database.Driver, migration []byte) {
if migration == nil {
panic("test must provide migration reader")
t.Fatal("test must provide migration reader")
}
TestNilVersion(t, d) // test first
@ -46,7 +46,7 @@ func TestLockAndUnlock(t *testing.T, d database.Driver) {
case <-done:
return
case <-timeout:
panic(fmt.Sprintf("Timeout after 15 seconds. Looks like a deadlock in Lock/UnLock.\n%#v", d))
t.Fatal(fmt.Sprintf("Timeout after 15 seconds. Looks like a deadlock in Lock/UnLock.\n%#v", d))
}
}
}()
@ -81,7 +81,7 @@ func TestLockAndUnlock(t *testing.T, d database.Driver) {
func TestRun(t *testing.T, d database.Driver, migration io.Reader) {
if migration == nil {
panic("migration can't be nil")
t.Fatal("migration can't be nil")
}
if err := d.Run(migration); err != nil {

View File

@ -5,6 +5,7 @@
package migrate
import (
"errors"
"fmt"
"os"
"sync"
@ -25,10 +26,11 @@ var DefaultPrefetchMigrations = uint(10)
var DefaultLockTimeout = 15 * time.Second
var (
ErrNoChange = fmt.Errorf("no change")
ErrNilVersion = fmt.Errorf("no migration")
ErrLocked = fmt.Errorf("database locked")
ErrLockTimeout = fmt.Errorf("timeout: can't acquire database lock")
ErrNoChange = errors.New("no change")
ErrNilVersion = errors.New("no migration")
ErrInvalidVersion = errors.New("version must be >= -1")
ErrLocked = errors.New("database locked")
ErrLockTimeout = errors.New("timeout: can't acquire database lock")
)
// ErrShortLimit is an error returned when not enough migrations
@ -357,7 +359,7 @@ func (m *Migrate) Run(migration ...*Migration) error {
// It resets the dirty state to false.
func (m *Migrate) Force(version int) error {
if version < -1 {
panic("version must be >= -1")
return ErrInvalidVersion
}
if err := m.lock(); err != nil {
@ -722,7 +724,7 @@ func (m *Migrate) runMigrations(ret <-chan interface{}) error {
}
default:
panic("unknown type")
return fmt.Errorf("unknown type: %T with value: %+v", r, r)
}
}
return nil