Skip to content

Commit

Permalink
feat(migrate): add pending migrations function
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSamuel committed Aug 29, 2024
1 parent c6a436b commit ccc2d89
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package migrate
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -87,6 +88,49 @@ func List(ctx context.Context, session gocqlx.Session) ([]*Info, error) {
return v, nil
}

// Pending provides a listing of pending migrations.
func Pending(ctx context.Context, session gocqlx.Session, f fs.FS) ([]*Info, error) {
applied, err := List(ctx, session)
if err != nil {
return nil, err
}

fm, err := fs.Glob(f, "*.cql")
if err != nil {
return nil, fmt.Errorf("list migrations: %w", err)
}

if len(applied) > len(fm) {
return nil, fmt.Errorf("database is ahead")
}

pending := make([]*Info, 0, len(fm)-len(applied))

for i := range applied {
if applied[i].Name != fm[i] {
fmt.Println(applied[i].Name, fm[i], i)

Check failure on line 111 in migrate/migrate.go

View workflow job for this annotation

GitHub Actions / Build

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
return nil, errors.New("inconsistent migrations")
}
}

for _, name := range fm[len(applied):] {
c, err := fileChecksum(f, name)
if err != nil {
return nil, fmt.Errorf("calculate checksum for %q: %w", name, err)
}

info := &Info{
Name: filepath.Base(name),
StartTime: time.Now(),
Checksum: c,
}

pending = append(pending, info)
}

return pending, nil
}

func ensureInfoTable(ctx context.Context, session gocqlx.Session) error {
return session.ContextQuery(ctx, infoSchema, nil).ExecRelease()
}
Expand Down

0 comments on commit ccc2d89

Please sign in to comment.