github.com/blend/go-sdk@v1.20220411.3/examples/db/migration/timeouts/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"database/sql"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/db"
    16  	"github.com/blend/go-sdk/db/migration"
    17  	"github.com/blend/go-sdk/logger"
    18  )
    19  
    20  func main() {
    21  	suite := migration.New(migration.OptGroups(
    22  		migration.NewGroup(migration.OptGroupActions(
    23  			migration.NewStep(
    24  				migration.Always(),
    25  				migration.ActionFunc(func(ctx context.Context, connection *db.Connection, tx *sql.Tx) error {
    26  					return db.IgnoreExecResult(connection.Invoke(db.OptTimeout(500 * time.Millisecond)).Exec("select pg_sleep(10);"))
    27  				}),
    28  			),
    29  		),
    30  		)))
    31  
    32  	suite.Log = logger.Prod()
    33  
    34  	conn, err := db.Open(db.New(db.OptConfigFromEnv()))
    35  	if err != nil {
    36  		logger.FatalExit(err)
    37  	}
    38  	suite.Log.Info("starting migrations")
    39  	outerTimeout, cancel := context.WithTimeout(context.Background(), time.Second)
    40  	defer cancel()
    41  	if err := suite.Apply(outerTimeout, conn); err != nil {
    42  		logger.FatalExit(err)
    43  	}
    44  }