• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 12941917448

24 Jan 2025 02:14AM UTC coverage: 58.238% (-0.05%) from 58.292%
12941917448

Pull #3046

github

web-flow
Merge 322044b46 into 337aacd0d
Pull Request #3046: feat(cli): add repeatable migrations

25 of 48 new or added lines in 4 files covered. (52.08%)

6 existing lines in 2 files now uncovered.

7617 of 13079 relevant lines covered (58.24%)

202.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.05
/internal/migration/up/up.go
1
package up
2

3
import (
4
        "context"
5
        "fmt"
6
        "strings"
7

8
        "github.com/go-errors/errors"
9
        "github.com/jackc/pgconn"
10
        "github.com/jackc/pgx/v4"
11
        "github.com/spf13/afero"
12
        "github.com/supabase/cli/internal/utils"
13
        "github.com/supabase/cli/pkg/migration"
14
)
15

16
func Run(ctx context.Context, includeAll bool, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
×
17
        conn, err := utils.ConnectByConfig(ctx, config, options...)
×
18
        if err != nil {
×
19
                return err
×
20
        }
×
21
        defer conn.Close(context.Background())
×
22
        pending, err := GetPendingMigrations(ctx, includeAll, conn, fsys)
×
23
        if err != nil {
×
24
                return err
×
25
        }
×
26

UNCOV
27
        return migration.ApplyMigrations(ctx, pending, conn, afero.NewIOFS(fsys))
×
28
}
29

30
func GetPendingMigrations(ctx context.Context, includeAll bool, conn *pgx.Conn, fsys afero.Fs) ([]string, error) {
7✔
31
        remoteMigrations, err := migration.ListRemoteMigrations(ctx, conn)
7✔
32
        if err != nil {
7✔
33
                return nil, err
×
34
        }
×
35
        localMigrations, err := migration.ListLocalMigrations(utils.MigrationsDir, afero.NewIOFS(fsys))
7✔
36
        if err != nil {
8✔
37
                return nil, err
1✔
38
        }
1✔
39
        diff, err := migration.FindPendingMigrations(localMigrations, remoteMigrations)
6✔
40
        if errors.Is(err, migration.ErrMissingLocal) {
9✔
41
                utils.CmdSuggestion = suggestRevertHistory(diff)
3✔
42
        } else if errors.Is(err, migration.ErrMissingRemote) {
8✔
43
                if includeAll {
3✔
44
                        pending := localMigrations[len(remoteMigrations)+len(diff):]
1✔
45
                        return append(diff, pending...), nil
1✔
46
                }
1✔
47
                utils.CmdSuggestion = suggestIgnoreFlag(diff)
1✔
48
        }
49
        return diff, err
5✔
50
}
51

52
func suggestRevertHistory(versions []string) string {
3✔
53
        result := fmt.Sprintln("\nMake sure your local git repo is up-to-date. If the error persists, try repairing the migration history table:")
3✔
54
        result += fmt.Sprintln(utils.Bold("supabase migration repair --status reverted " + strings.Join(versions, " ")))
3✔
55
        result += fmt.Sprintln("\nAnd update local migrations to match remote database:")
3✔
56
        result += fmt.Sprintln(utils.Bold("supabase db pull"))
3✔
57
        return result
3✔
58
}
3✔
59

60
func suggestIgnoreFlag(paths []string) string {
1✔
61
        result := "\nRerun the command with --include-all flag to apply these migrations:\n"
1✔
62
        result += fmt.Sprintln(utils.Bold(strings.Join(paths, "\n")))
1✔
63
        return result
1✔
64
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc