stephenafamo / packr-migrate

A driver to read migration files for golang-migrate from packr boxes
1 stars 0 forks source link

Getting the file content double #1

Open kekskurse opened 3 years ago

kekskurse commented 3 years ago

I had the issue that if i use this package to get the migration i got the file content two times back e.g.

migration_file_name.sql

CREATE TABLE example {
 ...
 }

the mysql drive got the following query to execute

CREATE TABLE example {
 ...
 }
CREATE TABLE example {
 ...
 }

i fixed it by replacing the box.Open with box.Find and return a new stream

func (p *Packr) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
    if m, ok := p.migrations.Up(version); ok {
        b, err := p.box.Find(m.Raw)
        if err != nil {
            return nil, "", err
        }
        r := ioutil.NopCloser(bytes.NewBuffer(b))
        return r, m.Identifier, nil
    }
    return nil, "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: p.box.Path, Err: os.ErrNotExist}
}

func (p *Packr) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
    if m, ok := p.migrations.Down(version); ok {
        b, err := p.box.Find(m.Raw)
        if err != nil {
            return nil, "", err
        }
        r := ioutil.NopCloser(bytes.NewBuffer(b))
        return r, m.Identifier, nil
    }
    return nil, "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: p.box.Path, Err: os.ErrNotExist}
}

Its just a learning project so its probably a issue in my code and i don't want to debug that part right now. I got the solution from this gist. I thought I let you know. Feel free to just close this issue.

stephenafamo commented 3 years ago

Thanks for opening this issue.

Looking through packr/v2's code, I cannot see why this will be happening. If you can share your code, along with a sample of your migration files so I can replicate, that would be helpful.

You should also send in a PR so we can review these changes. I could make them myself, but since you already fixed it, I think it should be you.

However, if you're writing a new project, using Packr may not be the best choice.

  1. Pkger is in development which was meant to eventually replace Packr
  2. Go 1.16 now has a native file embed in the standard library see docs, and this aricle

I can see that golang-migrate does not yet have migration sources for these, but shouldn't be too difficult to write one.