github.com/supabase/cli@v1.168.1/internal/inspect/unused_indexes/unused_indexes.go (about)

     1  package unused_indexes
     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  	Table       string
    20  	Index       string
    21  	Index_size  string
    22  	Index_scans int64
    23  }
    24  
    25  func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
    26  	conn, err := utils.ConnectByConfig(ctx, config, options...)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	rows, err := conn.Query(ctx, inspect.UNUSED_INDEXES_QUERY, reset.LikeEscapeSchema(utils.InternalSchemas))
    31  	if err != nil {
    32  		return errors.Errorf("failed to query rows: %w", err)
    33  	}
    34  	result, err := pgxv5.CollectRows[Result](rows)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	table := "|Table|Index|Index Size|Index Scans\n|-|-|-|-|\n"
    40  	for _, r := range result {
    41  		table += fmt.Sprintf("|`%s`|`%s`|`%s`|`%d`|\n", r.Table, r.Index, r.Index_size, r.Index_scans)
    42  	}
    43  	return list.RenderTable(table)
    44  }