Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support fetching migration history table to files (#2113)
feat: support fetching migration history to local
- Loading branch information
1 parent
50aa1ab
commit cecb69b
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fetch | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/go-errors/errors" | ||
"github.com/jackc/pgconn" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/spf13/afero" | ||
"github.com/supabase/cli/internal/migration/history" | ||
"github.com/supabase/cli/internal/utils" | ||
) | ||
|
||
func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error { | ||
if err := utils.MkdirIfNotExistFS(fsys, utils.MigrationsDir); err != nil { | ||
return err | ||
} | ||
if empty, err := afero.IsEmpty(fsys, utils.MigrationsDir); err != nil { | ||
return errors.Errorf("failed to read migrations: %w", err) | ||
} else if !empty { | ||
console := utils.NewConsole() | ||
title := fmt.Sprintf("Do you want to overwrite existing files in %s directory?", utils.Bold(utils.MigrationsDir)) | ||
if !console.PromptYesNo(title, true) { | ||
return context.Canceled | ||
} | ||
} | ||
result, err := fetchMigrationHistory(ctx, config, options...) | ||
if err != nil { | ||
return err | ||
} | ||
for _, r := range result { | ||
name := fmt.Sprintf("%s_%s.sql", r.Version, r.Name) | ||
path := filepath.Join(utils.MigrationsDir, name) | ||
contents := strings.Join(r.Statements, ";\n") + ";\n" | ||
if err := afero.WriteFile(fsys, path, []byte(contents), 0644); err != nil { | ||
return errors.Errorf("failed to write migration: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func fetchMigrationHistory(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) ([]history.SchemaMigration, error) { | ||
conn, err := utils.ConnectByConfig(ctx, config, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close(context.Background()) | ||
return history.ReadMigrationTable(ctx, conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters