Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor find seed files for reuse #2710

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading