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

supabase / cli / 12707310256

10 Jan 2025 10:00AM UTC coverage: 58.531% (-0.09%) from 58.616%
12707310256

push

github

web-flow
fix: unmarshal remote override into base config (#3028)

* fix: unmarshal remote override into base config

* chore: account for project ref flag when loading config

* fix: restore project id from base config

36 of 76 new or added lines in 27 files covered. (47.37%)

13 existing lines in 2 files now uncovered.

7588 of 12964 relevant lines covered (58.53%)

202.7 hits per line

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

0.0
/internal/db/remote/commit/commit.go
1
package commit
2

3
import (
4
        "context"
5
        _ "embed"
6
        "fmt"
7
        "path/filepath"
8

9
        "github.com/go-errors/errors"
10
        "github.com/jackc/pgconn"
11
        "github.com/jackc/pgx/v4"
12
        "github.com/spf13/afero"
13
        "github.com/supabase/cli/internal/db/diff"
14
        "github.com/supabase/cli/internal/db/dump"
15
        "github.com/supabase/cli/internal/migration/list"
16
        "github.com/supabase/cli/internal/migration/repair"
17
        "github.com/supabase/cli/internal/utils"
18
        "github.com/supabase/cli/internal/utils/flags"
19
        "github.com/supabase/cli/pkg/migration"
20
)
21

22
func Run(ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
×
23
        // Sanity checks.
×
NEW
24
        if err := flags.LoadConfig(fsys); err != nil {
×
25
                return err
×
26
        }
×
27

28
        if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
×
29
                return run(p, ctx, schema, config, fsys)
×
30
        }); err != nil {
×
31
                return err
×
32
        }
×
33

34
        fmt.Println("Finished " + utils.Aqua("supabase db remote commit") + ".")
×
35
        return nil
×
36
}
37

38
func run(p utils.Program, ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
×
39
        // 1. Assert `supabase/migrations` and `schema_migrations` are in sync.
×
40
        w := utils.StatusWriter{Program: p}
×
41
        conn, err := utils.ConnectByConfigStream(ctx, config, w)
×
42
        if err != nil {
×
43
                return err
×
44
        }
×
45
        defer conn.Close(context.Background())
×
46
        if err := assertRemoteInSync(ctx, conn, fsys); err != nil {
×
47
                return err
×
48
        }
×
49

50
        // 2. Fetch remote schema changes
51
        if len(schema) == 0 {
×
52
                schema, err = migration.ListUserSchemas(ctx, conn)
×
53
                if err != nil {
×
54
                        return err
×
55
                }
×
56
        }
57
        timestamp := utils.GetCurrentTimestamp()
×
58
        if err := fetchRemote(p, ctx, schema, timestamp, config, fsys); err != nil {
×
59
                return err
×
60
        }
×
61

62
        // 3. Insert a row to `schema_migrations`
63
        return repair.UpdateMigrationTable(ctx, conn, []string{timestamp}, repair.Applied, false, fsys)
×
64
}
65

66
func fetchRemote(p utils.Program, ctx context.Context, schema []string, timestamp string, config pgconn.Config, fsys afero.Fs) error {
×
67
        path := filepath.Join(utils.MigrationsDir, timestamp+"_remote_commit.sql")
×
68
        // Special case if this is the first migration
×
69
        if migrations, err := migration.ListLocalMigrations(utils.MigrationsDir, afero.NewIOFS(fsys)); err != nil {
×
70
                return err
×
71
        } else if len(migrations) == 0 {
×
72
                p.Send(utils.StatusMsg("Committing initial migration on remote database..."))
×
73
                return dump.Run(ctx, path, config, nil, nil, false, false, false, false, false, fsys)
×
74
        }
×
75

76
        w := utils.StatusWriter{Program: p}
×
77
        // Diff remote db (source) & shadow db (target) and write it as a new migration.
×
78
        output, err := diff.DiffDatabase(ctx, schema, config, w, fsys, diff.DiffSchemaMigra)
×
79
        if err != nil {
×
80
                return err
×
81
        }
×
82
        if len(output) == 0 {
×
83
                return errors.New("No schema changes found")
×
84
        }
×
85
        return utils.WriteFile(path, []byte(output), fsys)
×
86
}
87

88
func assertRemoteInSync(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error {
×
89
        remoteMigrations, err := migration.ListRemoteMigrations(ctx, conn)
×
90
        if err != nil {
×
91
                return err
×
92
        }
×
93
        localMigrations, err := list.LoadLocalVersions(fsys)
×
94
        if err != nil {
×
95
                return err
×
96
        }
×
97

98
        conflictErr := errors.New("The remote database's migration history is not in sync with the contents of " + utils.Bold(utils.MigrationsDir) + `. Resolve this by:
×
99
- Updating the project from version control to get the latest ` + utils.Bold(utils.MigrationsDir) + `,
×
100
- Pushing unapplied migrations with ` + utils.Aqua("supabase db push") + `,
×
101
- Or failing that, manually editing supabase_migrations.schema_migrations table with ` + utils.Aqua("supabase migration repair") + ".")
×
102
        if len(remoteMigrations) != len(localMigrations) {
×
103
                return conflictErr
×
104
        }
×
105

106
        for i, remoteTimestamp := range remoteMigrations {
×
107
                if localMigrations[i] != remoteTimestamp {
×
108
                        return conflictErr
×
109
                }
×
110
        }
111

112
        return nil
×
113
}
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