Skip to content

Commit ff0ae0c

Browse files
committed
feat: filter out noise from diff output
* only diff functions, tables, types, (materialized) views * ignore diffs from auth, extensions, pgbouncer, realtime, storage, supabase_realtime * ignore extension objects
1 parent 854bcc0 commit ff0ae0c

File tree

4 files changed

+75
-26
lines changed

4 files changed

+75
-26
lines changed

internal/db/changes/changes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ EOSQL
196196
// 2. Diff it (target) with local db (source), write it as a new migration.
197197
{
198198
out, err := utils.DockerExec(ctx, utils.DifferId, []string{
199-
"sh", "-c", "/venv/bin/python3 -u cli.py " +
199+
"sh", "-c", "/venv/bin/python3 -u cli.py --json-diff " +
200200
"'postgres://postgres:postgres@" + utils.DbId + ":5432/" + currBranch + "' " +
201201
"'postgres://postgres:postgres@" + utils.DbId + ":5432/" + utils.ShadowDbName + "'",
202202
})

internal/db/commit/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ EOSQL
197197
// 2. Diff it (target) with local db (source), write it as a new migration.
198198
{
199199
out, err := utils.DockerExec(ctx, utils.DifferId, []string{
200-
"sh", "-c", "/venv/bin/python3 -u cli.py " +
200+
"sh", "-c", "/venv/bin/python3 -u cli.py --json-diff " +
201201
"'postgres://postgres:postgres@" + utils.DbId + ":5432/" + currBranch + "' " +
202202
"'postgres://postgres:postgres@" + utils.DbId + ":5432/" + utils.ShadowDbName + "'",
203203
})

internal/db/remote/commit/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ EOSQL
353353
&container.Config{
354354
Image: utils.DifferImage,
355355
Entrypoint: []string{
356-
"sh", "-c", "/venv/bin/python3 -u cli.py " +
356+
"sh", "-c", "/venv/bin/python3 -u cli.py --json-diff " +
357357
"'" + url + "' " +
358358
"'postgres://postgres:postgres@" + dbId + ":5432/postgres'",
359359
},

internal/utils/utils.go

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ import (
2626
"github.com/spf13/viper"
2727
)
2828

29-
type (
30-
DiffEntry struct {
31-
Type string `json:"type"`
32-
Title string `json:"title"`
33-
Status string `json:"status"`
34-
SourceDdl string `json:"source_ddl"`
35-
TargetDdl string `json:"target_ddl"`
36-
DiffDdl string `json:"diff_ddl"`
37-
GroupName string `json:"group_name"`
38-
SourceSchemaName *string `json:"source_schema_name"`
39-
}
40-
)
29+
type DiffDependencies struct {
30+
Type string `json:"type"`
31+
}
32+
33+
type DiffEntry struct {
34+
Type string `json:"type"`
35+
Status string `json:"status"`
36+
DiffDdl string `json:"diff_ddl"`
37+
GroupName string `json:"group_name"`
38+
Dependencies []DiffDependencies `json:"dependencies"`
39+
SourceSchemaName *string `json:"source_schema_name"`
40+
}
4141

4242
const (
4343
ShadowDbName = "supabase_shadow"
@@ -281,16 +281,6 @@ func GetGitRoot() (*string, error) {
281281
}
282282
}
283283

284-
func IsSchemaIgnoredFromDump(schema string) bool {
285-
ignoredSchemas := []string{"auth", "extensions", "pgbouncer", "storage"}
286-
for _, s := range ignoredSchemas {
287-
if s == schema {
288-
return true
289-
}
290-
}
291-
return false
292-
}
293-
294284
type (
295285
StatusMsg string
296286
ProgressMsg *float64
@@ -391,7 +381,66 @@ func ProcessDiffOutput(p *tea.Program, out io.Reader) ([]byte, error) {
391381
// TODO: Remove when https://github.com/supabase/pgadmin4/issues/24 is fixed.
392382
diffBytes := bytes.TrimPrefix(diffBytesBuf.Bytes(), []byte("NOTE: Configuring authentication for DESKTOP mode.\n"))
393383

394-
return diffBytes, nil
384+
return filterDiffOutput(diffBytes)
385+
}
386+
387+
func filterDiffOutput(diffBytes []byte) ([]byte, error) {
388+
var diffJson []DiffEntry
389+
if err := json.Unmarshal(diffBytes, &diffJson); err != nil {
390+
return nil, err
391+
}
392+
393+
filteredDiffDdls := []string{`-- This script was generated by the Schema Diff utility in pgAdmin 4
394+
-- For the circular dependencies, the order in which Schema Diff writes the objects is not very sophisticated
395+
-- and may require manual changes to the script to ensure changes are applied in the correct order.
396+
-- Please report an issue for any failure with the reproduction steps.`}
397+
398+
for _, diffEntry := range diffJson {
399+
if diffEntry.Status == "Identical" || diffEntry.DiffDdl == "" {
400+
continue
401+
}
402+
403+
switch diffEntry.Type {
404+
case "function", "mview", "table", "trigger_function", "type", "view":
405+
// skip
406+
default:
407+
continue
408+
}
409+
410+
{
411+
doContinue := false
412+
for _, dep := range diffEntry.Dependencies {
413+
if dep.Type == "extension" {
414+
doContinue = true
415+
break
416+
}
417+
}
418+
419+
if doContinue {
420+
continue
421+
}
422+
}
423+
424+
if isSchemaIgnored(diffEntry.GroupName) ||
425+
// Needed at least for trigger_function
426+
(diffEntry.SourceSchemaName != nil && isSchemaIgnored(*diffEntry.SourceSchemaName)) {
427+
continue
428+
}
429+
430+
filteredDiffDdls = append(filteredDiffDdls, strings.TrimSpace(diffEntry.DiffDdl))
431+
}
432+
433+
return []byte(strings.Join(filteredDiffDdls, "\n\n") + "\n"), nil
434+
}
435+
436+
func isSchemaIgnored(schema string) bool {
437+
ignoredSchemas := []string{"auth", "extensions", "pgbouncer", "realtime", "storage", "supabase_migrations"}
438+
for _, s := range ignoredSchemas {
439+
if s == schema {
440+
return true
441+
}
442+
}
443+
return false
395444
}
396445

397446
func ProcessPsqlOutput(out io.Reader, p *tea.Program) error {

0 commit comments

Comments
 (0)