add Close() func #7

This commit is contained in:
mattes 2014-08-25 17:44:45 +02:00
parent c4c955d345
commit 0cc80701a7
5 changed files with 48 additions and 1 deletions

View File

@ -13,12 +13,17 @@ func (driver *Driver) Initialize(url string) error {
return nil
}
func (driver *Driver) Close() error {
return nil
}
func (driver *Driver) FilenameExtension() string {
return "sh"
}
func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
defer close(pipe)
pipe <- f
return
}

View File

@ -18,6 +18,10 @@ type Driver interface {
// that has to be made.
Initialize(url string) error
// Close is the last function to be called.
// Close any open connection here.
Close() error
// FilenameExtension returns the extension of the migration files.
// The returned string must not begin with a dot.
FilenameExtension() string

View File

@ -33,6 +33,13 @@ func (driver *Driver) Initialize(url string) error {
return nil
}
func (driver *Driver) Close() error {
if err := driver.db.Close(); err != nil {
return err
}
return nil
}
func (driver *Driver) ensureVersionTableExists() error {
if _, err := driver.db.Exec("CREATE TABLE IF NOT EXISTS " + tableName + " (version int not null primary key);"); err != nil {
return err
@ -46,7 +53,6 @@ func (driver *Driver) FilenameExtension() string {
func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
defer close(pipe)
pipe <- f
tx, err := driver.db.Begin()

View File

@ -87,4 +87,7 @@ func TestMigrate(t *testing.T) {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}

View File

@ -26,6 +26,9 @@ func Up(pipe chan interface{}, url, migrationsPath string) {
applyMigrationFiles, err := files.ToLastFrom(version)
if err != nil {
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
@ -38,9 +41,15 @@ func Up(pipe chan interface{}, url, migrationsPath string) {
break
}
}
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
} else {
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
@ -64,6 +73,9 @@ func Down(pipe chan interface{}, url, migrationsPath string) {
applyMigrationFiles, err := files.ToFirstFrom(version)
if err != nil {
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
@ -76,9 +88,15 @@ func Down(pipe chan interface{}, url, migrationsPath string) {
break
}
}
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
} else {
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
@ -142,6 +160,9 @@ func Migrate(pipe chan interface{}, url, migrationsPath string, relativeN int) {
applyMigrationFiles, err := files.From(version, relativeN)
if err != nil {
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
@ -154,9 +175,15 @@ func Migrate(pipe chan interface{}, url, migrationsPath string, relativeN int) {
break
}
}
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
if err2 := d.Close(); err != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
@ -242,10 +269,12 @@ func initDriverAndReadMigrationFilesAndGetVersion(url, migrationsPath string) (d
}
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
if err != nil {
d.Close() // TODO what happens with errors from this func?
return nil, nil, 0, err
}
version, err := d.Version()
if err != nil {
d.Close() // TODO what happens with errors from this func?
return nil, nil, 0, err
}
return d, &files, version, nil