github.com/supabase/cli@v1.168.1/internal/inspect/locks/locks.go (about) 1 package locks 2 3 import ( 4 "context" 5 "fmt" 6 "regexp" 7 8 "github.com/go-errors/errors" 9 "github.com/jackc/pgconn" 10 "github.com/jackc/pgx/v4" 11 "github.com/spf13/afero" 12 "github.com/supabase/cli/internal/inspect" 13 "github.com/supabase/cli/internal/migration/list" 14 "github.com/supabase/cli/internal/utils" 15 "github.com/supabase/cli/internal/utils/pgxv5" 16 ) 17 18 type Result struct { 19 Pid int 20 Relname string 21 Transactionid string 22 Granted bool 23 Query string 24 Age string 25 } 26 27 func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error { 28 conn, err := utils.ConnectByConfig(ctx, config, options...) 29 if err != nil { 30 return err 31 } 32 rows, err := conn.Query(ctx, inspect.LOCKS_QUERY) 33 if err != nil { 34 return errors.Errorf("failed to query rows: %w", err) 35 } 36 result, err := pgxv5.CollectRows[Result](rows) 37 if err != nil { 38 return err 39 } 40 41 table := "|pid|relname|transaction id|granted|query|age|\n|-|-|-|-|-|-|\n" 42 for _, r := range result { 43 // remove whitespace from query 44 re := regexp.MustCompile(`\s+|\r+|\n+|\t+|\v`) 45 query := re.ReplaceAllString(r.Query, " ") 46 47 // escape pipes in query 48 re = regexp.MustCompile(`\|`) 49 query = re.ReplaceAllString(query, `\|`) 50 table += fmt.Sprintf("|`%d`|`%s`|`%s`|`%t`|%s|`%s`|\n", r.Pid, r.Relname, r.Transactionid, r.Granted, query, r.Age) 51 } 52 return list.RenderTable(table) 53 }