Skip to content

Commit 6937348

Browse files
Support nested directories (#171)
1 parent ba73d0a commit 6937348

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

cmd/pg-schema-diff/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ func parseConnConfig(c connFlags, logger log.Logger) (*pgx.ConnConfig, error) {
3232
return pgx.ParseConfig(c.dsn)
3333
}
3434

35-
// LogFmtToMap parses all LogFmt key/value pairs from the provided string into a
35+
// logFmtToMap parses all LogFmt key/value pairs from the provided string into a
3636
// map.
3737
//
3838
// All records are scanned. If a duplicate key is found, an error is returned.
39-
func LogFmtToMap(logFmt string) (map[string]string, error) {
39+
func logFmtToMap(logFmt string) (map[string]string, error) {
4040
logMap := make(map[string]string)
4141
decoder := logfmt.NewDecoder(strings.NewReader(logFmt))
4242
for decoder.ScanRecord() {

cmd/pg-schema-diff/flags_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestLogFmtToMap(t *testing.T) {
6060
}
6161
for _, tt := range tests {
6262
t.Run(tt.name, func(t *testing.T) {
63-
got, err := LogFmtToMap(tt.args.logFmt)
63+
got, err := logFmtToMap(tt.args.logFmt)
6464
if tt.wantErr {
6565
assert.Error(t, err)
6666
return

cmd/pg-schema-diff/plan_cmd.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ func parsePlanConfig(p planFlags) (planConfig, error) {
224224
func parseSchemaSource(p schemaSourceFlags) (schemaSourceFactory, error) {
225225
if len(p.schemaDirs) > 0 {
226226
var ddl []string
227+
// Ordering of execution of schema SQL can be guaranteed by:
228+
// - Splitting across multiple directories and using multiple schema dir flags
229+
// - Relying on lexical order of SQL files
227230
for _, schemaDir := range p.schemaDirs {
228231
stmts, err := getDDLFromPath(schemaDir)
229232
if err != nil {
@@ -263,7 +266,7 @@ func parseSchemaConfig(p schemaFlags) []diff.PlanOpt {
263266
// parseTimeoutModifier attempts to parse an option representing a statement timeout modifier in the
264267
// form of regex=duration where duration could be a decimal number and ends with a unit
265268
func parseTimeoutModifier(val string) (timeoutModifier, error) {
266-
fm, err := LogFmtToMap(val)
269+
fm, err := logFmtToMap(val)
267270
if err != nil {
268271
return timeoutModifier{}, fmt.Errorf("could not parse %q into logfmt: %w", val, err)
269272
}
@@ -299,7 +302,7 @@ func parseTimeoutModifier(val string) (timeoutModifier, error) {
299302
}
300303

301304
func parseInsertStatementStr(val string) (insertStatement, error) {
302-
fm, err := LogFmtToMap(val)
305+
fm, err := logFmtToMap(val)
303306
if err != nil {
304307
return insertStatement{}, fmt.Errorf("could not parse into logfmt: %w", err)
305308
}
@@ -431,20 +434,26 @@ func applyPlanModifiers(
431434
return plan, nil
432435
}
433436

437+
// getDDLFromPath reads all .sql files under the given path (including sub-directories) and returns the DDL
438+
// in lexical order.
434439
func getDDLFromPath(path string) ([]string, error) {
435-
fileEntries, err := os.ReadDir(path)
436-
if err != nil {
437-
return nil, err
438-
}
439440
var ddl []string
440-
for _, entry := range fileEntries {
441-
if filepath.Ext(entry.Name()) == ".sql" {
442-
if stmts, err := os.ReadFile(filepath.Join(path, entry.Name())); err != nil {
443-
return nil, err
444-
} else {
445-
ddl = append(ddl, string(stmts))
446-
}
441+
if err := filepath.Walk(path, func(path string, entry os.FileInfo, err error) error {
442+
if err != nil {
443+
return fmt.Errorf("walking path %q: %w", path, err)
447444
}
445+
if strings.ToLower(filepath.Ext(entry.Name())) != ".sql" {
446+
return nil
447+
}
448+
449+
if stmts, err := os.ReadFile(path); err != nil {
450+
return fmt.Errorf("reading file %q: %w", entry.Name(), err)
451+
} else {
452+
ddl = append(ddl, string(stmts))
453+
}
454+
return nil
455+
}); err != nil {
456+
return nil, err
448457
}
449458
return ddl, nil
450459
}

0 commit comments

Comments
 (0)