github.com/supabase/cli@v1.168.1/internal/inspect/index_usage/index_usage.go (about) 1 package index_usage 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/go-errors/errors" 8 "github.com/jackc/pgconn" 9 "github.com/jackc/pgx/v4" 10 "github.com/spf13/afero" 11 "github.com/supabase/cli/internal/db/reset" 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 Name string 20 Percent_of_times_index_used string 21 Rows_in_table int64 22 } 23 24 func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error { 25 conn, err := utils.ConnectByConfig(ctx, config, options...) 26 if err != nil { 27 return err 28 } 29 rows, err := conn.Query(ctx, inspect.INDEX_USAGE_QUERY, reset.LikeEscapeSchema(utils.InternalSchemas)) 30 if err != nil { 31 return errors.Errorf("failed to query rows: %w", err) 32 } 33 result, err := pgxv5.CollectRows[Result](rows) 34 if err != nil { 35 return err 36 } 37 // TODO: implement a markdown table marshaller 38 table := "|Table name|Percentage of times index used|Rows in table|\n|-|-|-|\n" 39 for _, r := range result { 40 table += fmt.Sprintf("|`%s`|`%s`|`%d`|\n", r.Name, r.Percent_of_times_index_used, r.Rows_in_table) 41 } 42 return list.RenderTable(table) 43 }