-
Notifications
You must be signed in to change notification settings - Fork 29
fix: auto-detect schema from dump file header for plan/apply (#296) #312
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,14 +101,23 @@ func runPlan(cmd *cobra.Command, args []string) error { | |
| } | ||
| } | ||
|
|
||
| // Auto-detect schema from dump file if --schema was not explicitly set | ||
| effectiveSchema := planSchema | ||
| if !cmd.Flags().Changed("schema") && planFile != "" { | ||
| if detected, err := util.DetectSchemaFromFile(planFile); err == nil && detected != "" { | ||
| effectiveSchema = detected | ||
| fmt.Fprintf(os.Stderr, "Auto-detected schema '%s' from dump file\n", detected) | ||
| } | ||
| } | ||
|
Comment on lines
+104
to
+111
|
||
|
|
||
| // Create plan configuration | ||
| config := &PlanConfig{ | ||
| Host: planHost, | ||
| Port: planPort, | ||
| DB: planDB, | ||
| User: planUser, | ||
| Password: finalPassword, | ||
| Schema: planSchema, | ||
| Schema: effectiveSchema, | ||
| File: planFile, | ||
| ApplicationName: "pgschema", | ||
| // Plan database configuration | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| const schemaHeaderPrefix = "-- Dumped from schema: " | ||
|
|
||
| // DetectSchemaFromFile reads the header of a SQL dump file and extracts the | ||
| // schema name from the "-- Dumped from schema: <name>" metadata line. | ||
| // Returns empty string if the header is not found. | ||
| func DetectSchemaFromFile(filePath string) (string, error) { | ||
| f, err := os.Open(filePath) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| return detectSchemaFromReader(f) | ||
| } | ||
|
|
||
| // detectSchemaFromReader reads from an io.Reader and extracts the schema name | ||
| // from the pgschema dump header. Only scans the first 20 lines (header area). | ||
| func detectSchemaFromReader(r io.Reader) (string, error) { | ||
| scanner := bufio.NewScanner(r) | ||
| for i := 0; i < 20 && scanner.Scan(); i++ { | ||
| line := scanner.Text() | ||
| if strings.HasPrefix(line, schemaHeaderPrefix) { | ||
| return strings.TrimSpace(line[len(schemaHeaderPrefix):]), nil | ||
| } | ||
| } | ||
| return "", scanner.Err() | ||
|
Comment on lines
+27
to
+35
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestDetectSchemaFromReader(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| content string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "public schema", | ||
| content: `-- | ||
| -- pgschema database dump | ||
| -- | ||
|
|
||
| -- Dumped from database version PostgreSQL 17.7 | ||
| -- Dumped by pgschema version 1.7.1 | ||
| -- Dumped from schema: public | ||
|
|
||
| `, | ||
| expected: "public", | ||
| }, | ||
| { | ||
| name: "non-public schema", | ||
| content: `-- | ||
| -- pgschema database dump | ||
| -- | ||
|
|
||
| -- Dumped from database version PostgreSQL 17.7 | ||
| -- Dumped by pgschema version 1.7.1 | ||
| -- Dumped from schema: vehicle | ||
|
|
||
| `, | ||
| expected: "vehicle", | ||
| }, | ||
| { | ||
| name: "quoted schema name", | ||
| content: `-- | ||
| -- pgschema database dump | ||
| -- | ||
|
|
||
| -- Dumped from database version PostgreSQL 17.7 | ||
| -- Dumped by pgschema version 1.7.1 | ||
| -- Dumped from schema: my_schema | ||
|
|
||
| `, | ||
| expected: "my_schema", | ||
| }, | ||
|
Comment on lines
+41
to
+52
|
||
| { | ||
| name: "no schema header (old dump format)", | ||
| content: `-- | ||
| -- pgschema database dump | ||
| -- | ||
|
|
||
| -- Dumped from database version PostgreSQL 17.7 | ||
| -- Dumped by pgschema version 1.7.1 | ||
|
|
||
|
|
||
| CREATE TABLE IF NOT EXISTS users ( | ||
| id integer NOT NULL | ||
| ); | ||
| `, | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "empty file", | ||
| content: "", | ||
| expected: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result, err := detectSchemaFromReader(strings.NewReader(tt.content)) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if result != tt.expected { | ||
| t.Errorf("got %q, want %q", result, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/pgplex/pgschema/internal/version" | ||
| "github.com/pgplex/pgschema/ir" | ||
| ) | ||
|
|
||
| // GenerateDumpHeader generates the header for database dumps with metadata | ||
| func GenerateDumpHeader(schemaIR *ir.IR) string { | ||
| var header strings.Builder | ||
|
|
||
| header.WriteString("--\n") | ||
| header.WriteString("-- pgschema database dump\n") | ||
| header.WriteString("--\n") | ||
| header.WriteString("\n") | ||
|
|
||
| header.WriteString(fmt.Sprintf("-- Dumped from database version %s\n", schemaIR.Metadata.DatabaseVersion)) | ||
| header.WriteString(fmt.Sprintf("-- Dumped by pgschema version %s\n", version.App())) | ||
| header.WriteString("\n") | ||
| header.WriteString("\n") | ||
| return header.String() | ||
| } |
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.
Schema auto-detection ignores errors from DetectSchemaFromFile. If header parsing fails, apply will silently continue using the default schema, which can reintroduce the cross-schema risk this change is meant to prevent. Consider warning on detection errors or requiring --schema when detection fails.