-
Notifications
You must be signed in to change notification settings - Fork 381
feat(DEVWF-935): add deno script for running pg-delta #4605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9baac15
feat: implement pg-delta flag for schema diff
sweatybridge 8c0eb24
Merge branch 'develop' into pg-delta
sweatybridge 4af6d14
chore: update pgdelta library
sweatybridge 88cf15d
chore: reduce duplication
sweatybridge 3e6f681
fix: step down from login role after connect
sweatybridge ccf046e
chore: always set postgres role
sweatybridge c264100
chore: rename script
sweatybridge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| _ "embed" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/docker/docker/api/types/container" | ||
| "github.com/docker/docker/api/types/network" | ||
| "github.com/go-errors/errors" | ||
| "github.com/jackc/pgconn" | ||
| "github.com/jackc/pgx/v4" | ||
| "github.com/spf13/viper" | ||
| "github.com/supabase/cli/internal/gen/types" | ||
| "github.com/supabase/cli/internal/utils" | ||
| ) | ||
|
|
||
| //go:embed templates/delta.ts | ||
| var pgDeltaScript string | ||
|
|
||
| func DiffPgDelta(ctx context.Context, source, target pgconn.Config, schema []string, options ...func(*pgx.ConnConfig)) (string, error) { | ||
| env := []string{ | ||
| "SOURCE=" + utils.ToPostgresURL(source), | ||
| "TARGET=" + utils.ToPostgresURL(target), | ||
| } | ||
| if ca, err := types.GetRootCA(ctx, utils.ToPostgresURL(target), options...); err != nil { | ||
| return "", err | ||
| } else if len(ca) > 0 { | ||
| env = append(env, "PGDELTA_TARGET_SSLROOTCERT="+ca) | ||
| } | ||
| if len(schema) > 0 { | ||
| env = append(env, "INCLUDED_SCHEMAS="+strings.Join(schema, ",")) | ||
| } | ||
| loginRole := strings.Split(target.User, ".")[0] | ||
| if strings.EqualFold(loginRole, "cli_login_postgres") { | ||
| env = append(env, "ROLE=postgres") | ||
| } | ||
sweatybridge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var out bytes.Buffer | ||
| if err := diffWithStream(ctx, env, pgDeltaScript, &out); err != nil { | ||
| return "", err | ||
| } | ||
| return out.String(), nil | ||
| } | ||
|
|
||
| func diffWithStream(ctx context.Context, env []string, script string, stdout io.Writer) error { | ||
| cmd := []string{"edge-runtime", "start", "--main-service=."} | ||
| if viper.GetBool("DEBUG") { | ||
| cmd = append(cmd, "--verbose") | ||
| } | ||
| cmdString := strings.Join(cmd, " ") | ||
| entrypoint := []string{"sh", "-c", `cat <<'EOF' > index.ts && ` + cmdString + ` | ||
| ` + script + ` | ||
| EOF | ||
| `} | ||
| var stderr bytes.Buffer | ||
| if err := utils.DockerRunOnceWithConfig( | ||
| ctx, | ||
| container.Config{ | ||
| Image: utils.Config.EdgeRuntime.Image, | ||
| Env: env, | ||
| Entrypoint: entrypoint, | ||
| }, | ||
| container.HostConfig{ | ||
| Binds: []string{utils.EdgeRuntimeId + ":/root/.cache/deno:rw"}, | ||
| NetworkMode: network.NetworkHost, | ||
| }, | ||
| network.NetworkingConfig{}, | ||
| "", | ||
| stdout, | ||
| &stderr, | ||
| ); err != nil && !strings.HasPrefix(stderr.String(), "main worker has been destroyed") { | ||
| return errors.Errorf("error diffing schema: %w:\n%s", err, stderr.String()) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,23 @@ | ||
| import { createPlan } from "npm:@supabase/pg-delta@1.0.0-alpha.1"; | ||
| import { supabase } from "npm:@supabase/pg-delta@1.0.0-alpha.1/integrations/supabase"; | ||
|
|
||
| const source = Deno.env.get("SOURCE"); | ||
| const target = Deno.env.get("TARGET"); | ||
|
|
||
| const includedSchemas = Deno.env.get("INCLUDED_SCHEMAS"); | ||
| if (includedSchemas) { | ||
| supabase.filter = { schema: includedSchemas.split(",") }; | ||
| } | ||
| supabase.role = Deno.env.get("ROLE"); | ||
|
|
||
| try { | ||
| const result = await createPlan(source, target, supabase); | ||
| const statements = result?.plan.statements ?? []; | ||
| for (const sql of statements) { | ||
| console.log(`${sql};`); | ||
| } | ||
| } catch (e) { | ||
| console.error(e); | ||
| // Force close event loop | ||
| throw new Error(""); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.