@@ -69,6 +69,8 @@ func NewGenerateCommand() *cobra.Command {
6969 return nil
7070 },
7171 Run : func (cmd * cobra.Command , args []string ) {
72+ ctx := context .Background ()
73+
7274 config , err := internal .ReadConfig ()
7375 if err != nil {
7476 log .Fatalf ("Failed to read config: %v\n " , err )
@@ -91,7 +93,7 @@ func NewGenerateCommand() *cobra.Command {
9193 initial := migrationsCount == 0
9294
9395 runnerFunc = func () error {
94- return runWithStdout (config , initial )
96+ return runWithStdout (ctx , config , initial )
9597 }
9698 } else {
9799 migrationName := args [0 ]
@@ -114,7 +116,7 @@ func NewGenerateCommand() *cobra.Command {
114116 }()
115117
116118 runnerFunc = func () error {
117- return runWithFile (config , newMigrationFilePath , migrationNumber )
119+ return runWithFile (ctx , config , newMigrationFilePath , migrationNumber )
118120 }
119121 }
120122
@@ -143,13 +145,13 @@ func NewGenerateCommand() *cobra.Command {
143145 return generateCmd
144146}
145147
146- func setupDatabase (name string , port uint32 ) (* embeddedpostgres.EmbeddedPostgres , * pgx.Conn ) {
148+ func setupDatabase (ctx context. Context , name string , port uint32 ) (* embeddedpostgres.EmbeddedPostgres , * pgx.Conn ) {
147149 postgres , dsn := internal .NewPostgresDatabase (fmt .Sprintf ("/tmp/trek/%s" , name ), port )
148150 err := postgres .Start ()
149151 if err != nil {
150152 log .Fatalf ("Failed to start %s database: %v" , name , err )
151153 }
152- conn , err := pgx .Connect (context . Background () , dsn )
154+ conn , err := pgx .Connect (ctx , dsn )
153155 if err != nil {
154156 log .Fatalf ("Unable to connect to %s database: %v" , name , err )
155157 }
@@ -160,6 +162,7 @@ func setupDatabase(name string, port uint32) (*embeddedpostgres.EmbeddedPostgres
160162type RunnerFunc = func () error
161163
162164func runWithStdout (
165+ ctx context.Context ,
163166 config * internal.Config ,
164167 initial bool ,
165168) error {
@@ -168,20 +171,21 @@ func runWithStdout(
168171 return fmt .Errorf ("failed to check if model has been updated: %w" , err )
169172 }
170173 if updated {
171- targetPostgres , targetConn := setupDatabase ("target" , 5432 )
174+ targetPostgres , targetConn := setupDatabase (ctx , "target" , 5432 )
172175 defer func () {
173- _ = targetConn .Close (context . Background () )
176+ _ = targetConn .Close (ctx )
174177 _ = targetPostgres .Stop ()
175178 }()
176179
177- migratePostgres , migrateConn := setupDatabase ("migrate" , 5433 )
180+ migratePostgres , migrateConn := setupDatabase (ctx , "migrate" , 5433 )
178181 defer func () {
179- _ = migrateConn .Close (context . Background () )
182+ _ = migrateConn .Close (ctx )
180183 _ = migratePostgres .Stop ()
181184 }()
182185
183186 var statements string
184187 statements , err = generateMigrationStatements (
188+ ctx ,
185189 config ,
186190 initial ,
187191 targetConn ,
@@ -236,6 +240,7 @@ func runWithStdout(
236240}
237241
238242func runWithFile (
243+ ctx context.Context ,
239244 config * internal.Config ,
240245 newMigrationFilePath string ,
241246 migrationNumber uint ,
@@ -252,20 +257,21 @@ func runWithFile(
252257 }
253258 }
254259
255- targetPostgres , targetConn := setupDatabase ("target" , 5432 )
260+ targetPostgres , targetConn := setupDatabase (ctx , "target" , 5432 )
256261 defer func () {
257- _ = targetConn .Close (context . Background () )
262+ _ = targetConn .Close (ctx )
258263 _ = targetPostgres .Stop ()
259264 }()
260265
261- migratePostgres , migrateConn := setupDatabase ("migrate" , 5433 )
266+ migratePostgres , migrateConn := setupDatabase (ctx , "migrate" , 5433 )
262267 defer func () {
263- _ = migrateConn .Close (context . Background () )
268+ _ = migrateConn .Close (ctx )
264269 _ = migratePostgres .Stop ()
265270 }()
266271
267272 var statements string
268273 statements , err = generateMigrationStatements (
274+ ctx ,
269275 config ,
270276 migrationNumber == 1 ,
271277 targetConn ,
@@ -301,7 +307,7 @@ func runWithFile(
301307 return fmt .Errorf ("failed to write template files: %w" , err )
302308 }
303309
304- updated , err = generateDiffLockFile (newMigrationFilePath , targetConn , migrateConn )
310+ updated , err = generateDiffLockFile (ctx , newMigrationFilePath , targetConn , migrateConn )
305311 if err != nil {
306312 return fmt .Errorf ("failed to generate diff lock file: %w" , err )
307313 }
@@ -335,12 +341,17 @@ func checkIfUpdated(config *internal.Config) (bool, error) {
335341 return true , nil
336342}
337343
338- func generateDiffLockFile (newMigrationFilePath string , targetConn , migrateConn * pgx.Conn ) (bool , error ) {
344+ func generateDiffLockFile (
345+ ctx context.Context ,
346+ newMigrationFilePath string ,
347+ targetConn ,
348+ migrateConn * pgx.Conn ,
349+ ) (bool , error ) {
339350 newMigrationFileContent , err := os .ReadFile (newMigrationFilePath )
340351 if err != nil {
341352 return false , fmt .Errorf ("failed to read new migratio file: %w" , err )
342353 }
343- _ , err = migrateConn .Exec (context . Background () , string (newMigrationFileContent ))
354+ _ , err = migrateConn .Exec (ctx , string (newMigrationFileContent ))
344355 if err != nil {
345356 return false , fmt .Errorf ("failed to apply generated migration: %w" , err )
346357 }
@@ -549,6 +560,7 @@ var ErrInvalidModel = errors.New("invalid model")
549560
550561//nolint:cyclop
551562func generateMigrationStatements (
563+ ctx context.Context ,
552564 config * internal.Config ,
553565 initial bool ,
554566 targetConn ,
@@ -573,17 +585,17 @@ func generateMigrationStatements(
573585 return "" , ErrInvalidModel
574586 }
575587
576- err = internal .CreateUsers (migrateConn , config .DatabaseUsers )
588+ err = internal .CreateUsers (ctx , migrateConn , config .DatabaseUsers )
577589 if err != nil {
578590 return "" , fmt .Errorf ("failed to setup migrate database: %w" , err )
579591 }
580592
581- err = internal .CreateUsers (targetConn , config .DatabaseUsers )
593+ err = internal .CreateUsers (ctx , targetConn , config .DatabaseUsers )
582594 if err != nil {
583595 return "" , fmt .Errorf ("failed to setup target database: %w" , err )
584596 }
585597
586- err = executeTargetSQL (targetConn , config )
598+ err = executeTargetSQL (ctx , targetConn , config )
587599 if err != nil {
588600 log .Println (err )
589601
@@ -661,7 +673,7 @@ func executeMigrateSQL(migrateConn *pgx.Conn) error {
661673 return nil
662674}
663675
664- func executeTargetSQL (targetConn * pgx.Conn , config * internal.Config ) error {
676+ func executeTargetSQL (ctx context. Context , targetConn * pgx.Conn , config * internal.Config ) error {
665677 wd , err := os .Getwd ()
666678 if err != nil {
667679 return fmt .Errorf ("failed to get working directory: %w" , err )
@@ -672,7 +684,7 @@ func executeTargetSQL(targetConn *pgx.Conn, config *internal.Config) error {
672684 return fmt .Errorf ("failed to read target sql: %w" , err )
673685 }
674686
675- _ , err = targetConn .Exec (context . Background () , string (targetSQL ))
687+ _ , err = targetConn .Exec (ctx , string (targetSQL ))
676688 if err != nil {
677689 return fmt .Errorf ("failed to execute target sql: %w" , err )
678690 }
0 commit comments