@@ -224,6 +224,9 @@ func parsePlanConfig(p planFlags) (planConfig, error) {
224224func 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
265268func 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
301304func 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.
434439func 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