-
Notifications
You must be signed in to change notification settings - Fork 220
feat: add pg-schema-diff namespace FK PoC #4903
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
Draft
turip
wants to merge
1
commit into
main
Choose a base branch
from
feat/pg-schema-diff-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,119 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| entmigrate "github.com/openmeterio/openmeter/openmeter/ent/db/migrate" | ||
| "github.com/openmeterio/openmeter/tools/migrate/namespacefks" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := run(); err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| var ( | ||
| childTables string | ||
| check bool | ||
| output string | ||
| ) | ||
|
|
||
| flag.StringVar(&childTables, "child-tables", "", "comma-separated child tables to include; empty includes all eligible tables") | ||
| flag.BoolVar(&check, "check", false, "fail when the generated SQL differs from the output file") | ||
| flag.StringVar(&output, "output", "-", "output file, or - for stdout") | ||
| flag.Parse() | ||
|
|
||
| generated, err := namespacefks.Generate(namespacefks.GenerateInput{ | ||
| Tables: entmigrate.Tables, | ||
| ChildTables: splitCommaSeparated(childTables), | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if output == "-" { | ||
| if check { | ||
| return errors.New("check requires an output file") | ||
| } | ||
|
|
||
| _, err := os.Stdout.Write(generated) | ||
| return err | ||
| } | ||
|
|
||
| if check { | ||
| existing, err := os.ReadFile(output) | ||
| if err != nil { | ||
| return fmt.Errorf("read generated namespace foreign keys: %w", err) | ||
| } | ||
|
|
||
| if !bytes.Equal(existing, generated) { | ||
| return fmt.Errorf("%s is stale; run make generate-namespace-fks", output) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { | ||
| return fmt.Errorf("create output directory: %w", err) | ||
| } | ||
|
|
||
| temporary, err := os.CreateTemp(filepath.Dir(output), ".namespace-fks-*.sql") | ||
| if err != nil { | ||
| return fmt.Errorf("create temporary output: %w", err) | ||
| } | ||
| temporaryName := temporary.Name() | ||
| defer os.Remove(temporaryName) | ||
|
|
||
| if err := temporary.Chmod(0o644); err != nil { | ||
| _ = temporary.Close() | ||
| return fmt.Errorf("set temporary output permissions: %w", err) | ||
| } | ||
|
|
||
| if _, err := temporary.Write(generated); err != nil { | ||
| _ = temporary.Close() | ||
| return fmt.Errorf("write temporary output: %w", err) | ||
| } | ||
|
|
||
| if err := temporary.Close(); err != nil { | ||
| return fmt.Errorf("close temporary output: %w", err) | ||
| } | ||
|
|
||
| if err := os.Rename(temporaryName, output); err != nil { | ||
| return fmt.Errorf("replace generated namespace foreign keys: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func splitCommaSeparated(value string) []string { | ||
| if strings.TrimSpace(value) == "" { | ||
| return nil | ||
| } | ||
|
|
||
| parts := strings.Split(value, ",") | ||
| tables := make([]string, 0, len(parts)) | ||
| seen := make(map[string]struct{}, len(parts)) | ||
| for _, part := range parts { | ||
| table := strings.TrimSpace(part) | ||
| if table == "" { | ||
| continue | ||
| } | ||
| if _, ok := seen[table]; ok { | ||
| continue | ||
| } | ||
|
|
||
| seen[table] = struct{}{} | ||
| tables = append(tables, table) | ||
| } | ||
|
|
||
| return tables | ||
| } |
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,88 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/openmeterio/openmeter/tools/migrate/pgschemadiff" | ||
| ) | ||
|
|
||
| const defaultNamespaceChildTables = "billing_profiles,billing_customer_overrides,billing_invoices" | ||
|
|
||
| func main() { | ||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) | ||
| defer stop() | ||
|
|
||
| if err := run(ctx, os.Args[1:], os.Stdout, os.Stderr); err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run(ctx context.Context, args []string, stdout, stderr io.Writer) error { | ||
| flags := flag.NewFlagSet("pgschemadiff", flag.ContinueOnError) | ||
| flags.SetOutput(stderr) | ||
|
|
||
| devDatabaseURL := flags.String("dev-dsn", "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable", "PostgreSQL instance on which disposable databases can be created") | ||
| entSchemaPath := flags.String("ent-schema", "./openmeter/ent/schema", "path to the Ent schema package") | ||
| namespaceChildTables := flags.String("namespace-fk-child-tables", defaultNamespaceChildTables, "comma-separated child tables for generated namespace foreign keys") | ||
| skipPlanValidation := flags.Bool("skip-plan-validation", false, "skip replaying the generated plan against a disposable database") | ||
| outputPath := flags.String("output", "-", "output SQL file, or - for stdout") | ||
| if err := flags.Parse(args); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger := slog.New(slog.NewTextHandler(stderr, &slog.HandlerOptions{Level: slog.LevelWarn})) | ||
| plan, err := pgschemadiff.GeneratePlan(ctx, pgschemadiff.GeneratePlanInput{ | ||
| DevDatabaseURL: *devDatabaseURL, | ||
| EntSchemaPath: *entSchemaPath, | ||
| NamespaceChildTables: splitCommaSeparated(*namespaceChildTables), | ||
| SkipPlanValidation: *skipPlanValidation, | ||
| Logger: logger, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| output := pgschemadiff.RenderSQL(plan, !*skipPlanValidation) | ||
| if *outputPath == "-" { | ||
| _, err := stdout.Write(output) | ||
| return err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(*outputPath), 0o755); err != nil { | ||
| return fmt.Errorf("create output directory: %w", err) | ||
| } | ||
| if err := os.WriteFile(*outputPath, output, 0o644); err != nil { | ||
| return fmt.Errorf("write schema diff: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func splitCommaSeparated(value string) []string { | ||
| parts := strings.Split(value, ",") | ||
| values := make([]string, 0, len(parts)) | ||
| seen := make(map[string]struct{}, len(parts)) | ||
| for _, part := range parts { | ||
| value := strings.TrimSpace(part) | ||
| if value == "" { | ||
| continue | ||
| } | ||
| if _, ok := seen[value]; ok { | ||
| continue | ||
| } | ||
|
|
||
| seen[value] = struct{}{} | ||
| values = append(values, value) | ||
| } | ||
|
|
||
| return values | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 Open source vulnerabilities detected - critical severity
Aikido detected 2 vulnerabilities across 2 packages, it includes 1 critical and 1 high vulnerabilities.
Details
Remediation:
github.com/jackc/pgx/v4— 1 CVE (critical) — no fix version availablegithub.com/jackc/pgproto3/v2— 1 CVE (high) — no fix version availableReply
@AikidoSec ignore: [REASON]to ignore this issue.More info