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

supabase / cli / 13392899334

18 Feb 2025 02:28PM UTC coverage: 58.349% (+0.07%) from 58.278%
13392899334

push

github

sweatybridge
chore: remove unnecessary config load

2 of 4 new or added lines in 2 files covered. (50.0%)

252 existing lines in 9 files now uncovered.

7789 of 13349 relevant lines covered (58.35%)

201.84 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/pkg/migration"
19
)
20

21
func Run(ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
×
22
        if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
×
23
                return run(p, ctx, schema, config, fsys)
×
24
        }); err != nil {
×
25
                return err
×
26
        }
×
UNCOV
27
        fmt.Println("Finished " + utils.Aqua("supabase db remote commit") + ".")
×
28
        return nil
×
29
}
30

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

43
        // 2. Fetch remote schema changes
44
        if len(schema) == 0 {
×
45
                schema, err = migration.ListUserSchemas(ctx, conn)
×
46
                if err != nil {
×
47
                        return err
×
48
                }
×
49
        }
50
        timestamp := utils.GetCurrentTimestamp()
×
51
        if err := fetchRemote(p, ctx, schema, timestamp, config, fsys); err != nil {
×
52
                return err
×
53
        }
×
54

55
        // 3. Insert a row to `schema_migrations`
56
        return repair.UpdateMigrationTable(ctx, conn, []string{timestamp}, repair.Applied, false, fsys)
×
57
}
58

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

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

81
func assertRemoteInSync(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error {
×
82
        remoteMigrations, err := migration.ListRemoteMigrations(ctx, conn)
×
83
        if err != nil {
×
84
                return err
×
85
        }
×
86
        localMigrations, err := list.LoadLocalVersions(fsys)
×
87
        if err != nil {
×
88
                return err
×
89
        }
×
90

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

99
        for i, remoteTimestamp := range remoteMigrations {
×
100
                if localMigrations[i] != remoteTimestamp {
×
101
                        return conflictErr
×
102
                }
×
103
        }
104

105
        return nil
×
106
}
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