Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package sql
import (
"bytes"
"github.com/gobuffalo/packr"
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/source"
"io"
"io/ioutil"
"os"
"sync"
)
type PackrSource struct {
lock sync.Mutex
box packr.Box
migrations *source.Migrations
}
func (s *PackrSource) loadMigrations() (*source.Migrations, error) {
migrations := source.NewMigrations()
for _, filename := range s.box.List() {
migration, err := source.Parse(filename)
if err != nil {
return nil, err
}
migrations.Append(migration)
}
return migrations, nil
}
func (s *PackrSource) Open(url string) (source.Driver, error) {
s.lock.Lock()
defer s.lock.Unlock()
s.box = packr.NewBox(url)
if migrations, err := s.loadMigrations(); err != nil {
return nil, err
} else {
s.migrations = migrations
return s, nil
}
}
func (s *PackrSource) Close() error {
s.lock.Lock()
defer s.lock.Unlock()
s.migrations = nil
return nil
}
func (s *PackrSource) First() (version uint, err error) {
if v, ok := s.migrations.First(); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *PackrSource) Prev(version uint) (prevVersion uint, err error) {
if v, ok := s.migrations.Prev(version); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *PackrSource) Next(version uint) (nextVersion uint, err error) {
if v, ok := s.migrations.Next(version); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *PackrSource) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
if migration, ok := s.migrations.Up(version); !ok {
return nil, "", os.ErrNotExist
} else {
b := s.box.Bytes(migration.Raw)
return ioutil.NopCloser(bytes.NewBuffer(b)),
migration.Identifier,
nil
}
}
func (s *PackrSource) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
if migration, ok := s.migrations.Down(version); !ok {
return nil, "", migrate.ErrNilVersion
} else {
b := s.box.Bytes(migration.Raw)
return ioutil.NopCloser(bytes.NewBuffer(b)),
migration.Identifier,
nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment