Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/pg-schema-diff/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func parseConnConfig(c connFlags, logger log.Logger) (*pgx.ConnConfig, error) {
return pgx.ParseConfig(c.dsn)
}

// LogFmtToMap parses all LogFmt key/value pairs from the provided string into a
// logFmtToMap parses all LogFmt key/value pairs from the provided string into a
// map.
//
// All records are scanned. If a duplicate key is found, an error is returned.
func LogFmtToMap(logFmt string) (map[string]string, error) {
func logFmtToMap(logFmt string) (map[string]string, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexport this

logMap := make(map[string]string)
decoder := logfmt.NewDecoder(strings.NewReader(logFmt))
for decoder.ScanRecord() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pg-schema-diff/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestLogFmtToMap(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := LogFmtToMap(tt.args.logFmt)
got, err := logFmtToMap(tt.args.logFmt)
if tt.wantErr {
assert.Error(t, err)
return
Expand Down
35 changes: 22 additions & 13 deletions cmd/pg-schema-diff/plan_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ func parsePlanConfig(p planFlags) (planConfig, error) {
func parseSchemaSource(p schemaSourceFlags) (schemaSourceFactory, error) {
if len(p.schemaDirs) > 0 {
var ddl []string
// Ordering of execution of schema SQL can be guaranteed by:
// - Splitting across multiple directories and using multiple schema dir flags
// - Relying on lexical order of SQL files
for _, schemaDir := range p.schemaDirs {
stmts, err := getDDLFromPath(schemaDir)
if err != nil {
Expand Down Expand Up @@ -263,7 +266,7 @@ func parseSchemaConfig(p schemaFlags) []diff.PlanOpt {
// parseTimeoutModifier attempts to parse an option representing a statement timeout modifier in the
// form of regex=duration where duration could be a decimal number and ends with a unit
func parseTimeoutModifier(val string) (timeoutModifier, error) {
fm, err := LogFmtToMap(val)
fm, err := logFmtToMap(val)
if err != nil {
return timeoutModifier{}, fmt.Errorf("could not parse %q into logfmt: %w", val, err)
}
Expand Down Expand Up @@ -299,7 +302,7 @@ func parseTimeoutModifier(val string) (timeoutModifier, error) {
}

func parseInsertStatementStr(val string) (insertStatement, error) {
fm, err := LogFmtToMap(val)
fm, err := logFmtToMap(val)
if err != nil {
return insertStatement{}, fmt.Errorf("could not parse into logfmt: %w", err)
}
Expand Down Expand Up @@ -431,20 +434,26 @@ func applyPlanModifiers(
return plan, nil
}

// getDDLFromPath reads all .sql files under the given path (including sub-directories) and returns the DDL
// in lexical order.
func getDDLFromPath(path string) ([]string, error) {
fileEntries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
var ddl []string
for _, entry := range fileEntries {
if filepath.Ext(entry.Name()) == ".sql" {
if stmts, err := os.ReadFile(filepath.Join(path, entry.Name())); err != nil {
return nil, err
} else {
ddl = append(ddl, string(stmts))
}
if err := filepath.Walk(path, func(path string, entry os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walking path %q: %w", path, err)
}
if strings.ToLower(filepath.Ext(entry.Name())) != ".sql" {
return nil
}

if stmts, err := os.ReadFile(path); err != nil {
return fmt.Errorf("reading file %q: %w", entry.Name(), err)
} else {
ddl = append(ddl, string(stmts))
}
return nil
}); err != nil {
return nil, err
}
return ddl, nil
}
Expand Down