@@ -20,14 +20,28 @@ import (
2020 "github.com/printeers/trek/internal/postgres"
2121)
2222
23+ // ExitError is an error that carries a specific exit code.
24+ type ExitError struct {
25+ Code int
26+ Message string
27+ }
28+
29+ func (e * ExitError ) Error () string {
30+ return e .Message
31+ }
32+
33+ // ErrDiffDetected is returned by generate when diff statements are generated.
34+ var ErrDiffDetected = & ExitError {Code : 2 , Message : "diff statements detected" }
35+
2336//nolint:gocognit,cyclop
2437func NewGenerateCommand () * cobra.Command {
2538 var (
26- dev bool
27- cleanup bool
28- overwrite bool
29- stdout bool
30- check bool
39+ dev bool
40+ cleanup bool
41+ overwrite bool
42+ stdout bool
43+ check bool
44+ errorOnDiff bool
3145 )
3246
3347 generateCmd := & cobra.Command {
@@ -99,7 +113,7 @@ func NewGenerateCommand() *cobra.Command {
99113 }
100114 }
101115
102- err = runWithStdout (ctx , config , wd , tmpDir , migrationsDir , len (migrationFiles ) == 0 )
116+ err = runWithStdout (ctx , config , wd , tmpDir , migrationsDir , len (migrationFiles ) == 0 , errorOnDiff )
103117 if err != nil {
104118 return err
105119 }
@@ -115,7 +129,7 @@ func NewGenerateCommand() *cobra.Command {
115129 return fmt .Errorf ("failed to create temporary directory: %w" , err )
116130 }
117131
118- err = runWithStdout (ctx , config , wd , tmpDir , migrationsDir , len (migrationFiles ) == 0 )
132+ err = runWithStdout (ctx , config , wd , tmpDir , migrationsDir , len (migrationFiles ) == 0 , errorOnDiff )
119133 if err != nil {
120134 return err
121135 }
@@ -156,7 +170,8 @@ func NewGenerateCommand() *cobra.Command {
156170 }
157171
158172 var updated bool
159- updated , err = runWithFile (ctx , config , wd , tmpDir , migrationsDir , newMigrationFilePath , migrationNumber )
173+ updated , err = runWithFile (
174+ ctx , config , wd , tmpDir , migrationsDir , newMigrationFilePath , migrationNumber , errorOnDiff )
160175 if err != nil {
161176 return err
162177 }
@@ -200,6 +215,7 @@ func NewGenerateCommand() *cobra.Command {
200215 generateCmd .Flags ().BoolVar (& overwrite , "overwrite" , false , "Overwrite existing files" )
201216 generateCmd .Flags ().BoolVar (& stdout , "stdout" , false , "Output migration statements to stdout" )
202217 generateCmd .Flags ().BoolVar (& check , "check" , true , "Run checks after generating the migration" )
218+ generateCmd .Flags ().BoolVar (& errorOnDiff , "error-on-diff" , false , "Exit with code 2 if diff statements are generated" )
203219
204220 return generateCmd
205221}
@@ -222,6 +238,7 @@ func runWithStdout(
222238 tmpDir ,
223239 migrationsDir string ,
224240 initial bool ,
241+ errorOnDiff bool ,
225242) error {
226243 updated , err := checkIfUpdated (config , wd )
227244 if err != nil {
@@ -313,6 +330,10 @@ func runWithStdout(
313330 fmt .Println ("--" )
314331 fmt .Println (statements )
315332 fmt .Println ("--" )
333+
334+ if errorOnDiff && statements != "" {
335+ return ErrDiffDetected
336+ }
316337 }
317338
318339 return nil
@@ -327,6 +348,7 @@ func runWithFile(
327348 migrationsDir ,
328349 newMigrationFilePath string ,
329350 migrationNumber uint ,
351+ errorOnDiff bool ,
330352) (bool , error ) {
331353 updated , err := checkIfUpdated (config , wd )
332354 if err != nil {
@@ -412,6 +434,10 @@ func runWithFile(
412434 return false , fmt .Errorf ("failed to write template files: %w" , err )
413435 }
414436
437+ if errorOnDiff && statements != "" {
438+ return true , ErrDiffDetected
439+ }
440+
415441 return true , nil
416442 }
417443
0 commit comments