Skip to content

Commit

Permalink
fix: refactor find seed files for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Sep 25, 2024
1 parent 936f5df commit 5c41c1e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
23 changes: 9 additions & 14 deletions internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"time"

"github.com/docker/docker/client"
"github.com/go-errors/errors"
"github.com/go-git/go-git/v5"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/pkg/migration"
)

// Assigned using `-ldflags` https://stackoverflow.com/q/11354518
Expand Down Expand Up @@ -160,21 +160,16 @@ var (
// Match the glob patterns from the config to get a deduplicated
// array of all migrations files to apply in the declared order.
func GetSeedFiles(fsys afero.Fs) ([]string, error) {
seedPaths := Config.Db.Seed.SqlPaths
var files []string
for _, pattern := range seedPaths {
var globs []string
for _, pattern := range Config.Db.Seed.SqlPaths {
fullPattern := filepath.Join(SupabaseDirPath, pattern)
matches, err := afero.Glob(fsys, fullPattern)
if err != nil {
return nil, errors.Errorf("failed to apply glob pattern for %w", err)
}
if len(matches) == 0 {
fmt.Fprintf(os.Stderr, "%s Your pattern %s matched 0 seed files.\n", Yellow("WARNING:"), pattern)
}
sort.Strings(matches)
files = append(files, matches...)
globs = append(globs, fullPattern)
}
files, err := migration.FindSeedFiles(globs, afero.NewIOFS(fsys))
if err == nil && len(files) == 0 {
fmt.Fprintf(os.Stderr, "%s Your pattern %s matched 0 seed files.\n", Yellow("WARNING:"), Config.Db.Seed.SqlPaths)
}
return RemoveDuplicates(files), nil
return files, err
}

func GetCurrentTimestamp() string {
Expand Down
30 changes: 30 additions & 0 deletions pkg/migration/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,40 @@ import (
"io/fs"
"os"
"path/filepath"
"sort"

"github.com/go-errors/errors"
"github.com/jackc/pgx/v4"
)

// Find all files that match the specified glob patterns without duplicates.
// Results are ordered by input glob pattern, followed by lexical order.
func FindSeedFiles(globs []string, fsys fs.FS) ([]string, error) {
var files []string
for _, pattern := range globs {
matches, err := fs.Glob(fsys, pattern)
if err != nil {
return nil, errors.Errorf("failed to apply glob pattern: %w", err)
}
sort.Strings(matches)
files = append(files, matches...)
}
return removeDuplicates(files), nil
}

func removeDuplicates[T comparable](slice []T) []T {
// Remove elements in-place
result := slice[:0]
set := make(map[T]struct{})
for _, item := range slice {
if _, exists := set[item]; !exists {
set[item] = struct{}{}
result = append(result, item)
}
}
return result
}

func SeedData(ctx context.Context, pending []string, conn *pgx.Conn, fsys fs.FS) error {
for _, path := range pending {
fmt.Fprintf(os.Stderr, "Seeding data from %s...\n", path)
Expand Down

0 comments on commit 5c41c1e

Please sign in to comment.