@@ -461,7 +461,7 @@ var (
461461 modelContent = ""
462462)
463463
464- //nolint:cyclop
464+ //nolint:cyclop,gocognit
465465func generateMigrationStatements (
466466 ctx context.Context ,
467467 config * configuration.Config ,
@@ -475,25 +475,40 @@ func generateMigrationStatements(
475475) (string , error ) {
476476 log .Println ("Generating migration statements" )
477477
478- err := internal . PgModelerExportToFile (
479- ctx ,
480- filepath .Join (wd , fmt .Sprintf ("%s.dbm " , config .ModelName )),
481- filepath . Join ( wd , fmt . Sprintf ( "%s.sql" , config . ModelName )),
482- )
478+ dbmPath := filepath . Join ( wd , fmt . Sprintf ( "%s.dbm" , config . ModelName ))
479+ // Generate SQL file in tmpDir for internal use during migration generation
480+ tmpSQLPath := filepath .Join (tmpDir , fmt .Sprintf ("%s.sql " , config .ModelName ))
481+
482+ err := internal . PgmodelerExportSQL ( ctx , dbmPath , tmpSQLPath )
483483 if err != nil {
484484 return "" , fmt .Errorf ("failed to export model: %w" , err )
485485 }
486486
487- go func () {
488- err = internal .PgModelerExportToPng (
489- ctx ,
490- filepath .Join (wd , fmt .Sprintf ("%s.dbm" , config .ModelName )),
491- filepath .Join (wd , fmt .Sprintf ("%s.png" , config .ModelName )),
492- )
493- if err != nil {
494- log .Printf ("Failed to export png: %v\n " , err )
495- }
496- }()
487+ if pngPath := config .GetOutputPath ("png" ); pngPath != "" {
488+ go func () {
489+ pngErr := internal .PgmodelerExportPNG (
490+ ctx ,
491+ dbmPath ,
492+ filepath .Join (wd , pngPath ),
493+ )
494+ if pngErr != nil {
495+ log .Printf ("Failed to export png: %v\n " , pngErr )
496+ }
497+ }()
498+ }
499+
500+ if svgPath := config .GetOutputPath ("svg" ); svgPath != "" {
501+ go func () {
502+ svgErr := internal .PgmodelerExportSVG (
503+ ctx ,
504+ dbmPath ,
505+ filepath .Join (wd , svgPath ),
506+ )
507+ if svgErr != nil {
508+ log .Printf ("Failed to export svg: %v\n " , svgErr )
509+ }
510+ }()
511+ }
497512
498513 for _ , role := range config .Roles {
499514 _ , err = targetConn .Exec (ctx , fmt .Sprintf ("CREATE ROLE %q WITH LOGIN;" , role .Name ))
@@ -502,55 +517,69 @@ func generateMigrationStatements(
502517 }
503518 }
504519
505- err = executeTargetSQL (ctx , config , wd , targetConn )
520+ err = executeTargetSQL (ctx , tmpSQLPath , targetConn )
506521 if err != nil {
507522 return "" , fmt .Errorf ("failed to execute target sql: %w" , err )
508523 }
509524
525+ var output string
526+
510527 if initial {
511528 // If we are developing the schema initially, there will be no diffs,
512529 // and we want to copy over the schema file to the initial migration file
513530 var input []byte
514- input , err = os .ReadFile (filepath . Join ( wd , fmt . Sprintf ( "%s.sql" , config . ModelName )) )
531+ input , err = os .ReadFile (tmpSQLPath )
515532 if err != nil {
516533 return "" , fmt .Errorf ("failed to read sql file: %w" , err )
517534 }
535+ output = string (input )
536+ } else {
537+ err = executeMigrateSQL (migrationsDir , migrateConn )
538+ if err != nil {
539+ return "" , fmt .Errorf ("failed to execute migrate sql: %w" , err )
540+ }
518541
519- return string (input ), nil
520- }
521-
522- err = executeMigrateSQL (migrationsDir , migrateConn )
523- if err != nil {
524- return "" , fmt .Errorf ("failed to execute migrate sql: %w" , err )
525- }
542+ var statements string
543+ statements , err = internal .Diff (
544+ ctx ,
545+ postgresConn ,
546+ migrateConn ,
547+ targetConn ,
548+ )
549+ if err != nil {
550+ return "" , fmt .Errorf ("failed to diff: %w" , err )
551+ }
526552
527- statements , err := internal .Diff (
528- ctx ,
529- postgresConn ,
530- migrateConn ,
531- targetConn ,
532- )
533- if err != nil {
534- return "" , fmt .Errorf ("failed to diff: %w" , err )
535- }
553+ var extraStatements string
554+ extraStatements , err = generateMissingPermissionStatements (ctx , tmpDir , statements , targetConn , migrateConn )
555+ if err != nil {
556+ return "" , fmt .Errorf ("failed to generate missing permission statements: %w" , err )
557+ }
536558
537- extraStatements , err := generateMissingPermissionStatements (ctx , tmpDir , statements , targetConn , migrateConn )
538- if err != nil {
539- return "" , fmt .Errorf ("failed to generate missing permission statements: %w" , err )
559+ if statements != "" {
560+ output += statements
561+ }
562+ if statements != "" && extraStatements != "" {
563+ output += "\n \n "
564+ }
565+ if extraStatements != "" {
566+ output += "-- Statements generated automatically, please review:\n " + extraStatements
567+ }
568+ if output != "" {
569+ output += "\n "
570+ }
540571 }
541572
542- var output string
543- if statements != "" {
544- output += statements
545- }
546- if statements != "" && extraStatements != "" {
547- output += "\n \n "
548- }
549- if extraStatements != "" {
550- output += "-- Statements generated automatically, please review:\n " + extraStatements
551- }
552- if output != "" {
553- output += "\n "
573+ // Copy SQL to output path if enabled
574+ if sqlPath := config .GetOutputPath ("sql" ); sqlPath != "" {
575+ sqlContent , readErr := os .ReadFile (tmpSQLPath )
576+ if readErr != nil {
577+ return "" , fmt .Errorf ("failed to read sql file: %w" , readErr )
578+ }
579+ err = os .WriteFile (filepath .Join (wd , sqlPath ), sqlContent , 0o644 ) //nolint:gosec
580+ if err != nil {
581+ return "" , fmt .Errorf ("failed to write sql output file: %w" , err )
582+ }
554583 }
555584
556585 return output , nil
@@ -569,8 +598,8 @@ func executeMigrateSQL(migrationsDir string, migrateConn *pgx.Conn) error {
569598 return nil
570599}
571600
572- func executeTargetSQL (ctx context.Context , config * configuration. Config , wd string , targetConn * pgx.Conn ) error {
573- targetSQL , err := os .ReadFile (filepath . Join ( wd , fmt . Sprintf ( "%s.sql" , config . ModelName )) )
601+ func executeTargetSQL (ctx context.Context , sqlPath string , targetConn * pgx.Conn ) error {
602+ targetSQL , err := os .ReadFile (sqlPath )
574603 if err != nil {
575604 return fmt .Errorf ("failed to read target sql: %w" , err )
576605 }
0 commit comments