48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package index_usage
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"fmt"
|
|
|
|
"github.com/go-errors/errors"
|
|
"github.com/jackc/pgconn"
|
|
"github.com/jackc/pgx/v4"
|
|
"github.com/spf13/afero"
|
|
"github.com/supabase/cli/internal/db/reset"
|
|
"github.com/supabase/cli/internal/migration/list"
|
|
"github.com/supabase/cli/internal/utils"
|
|
"github.com/supabase/cli/pkg/pgxv5"
|
|
)
|
|
|
|
//go:embed index_usage.sql
|
|
var IndexUsageQuery string
|
|
|
|
type Result struct {
|
|
Name string
|
|
Percent_of_times_index_used string
|
|
Rows_in_table int64
|
|
}
|
|
|
|
func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
|
|
conn, err := utils.ConnectByConfig(ctx, config, options...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Close(context.Background())
|
|
rows, err := conn.Query(ctx, IndexUsageQuery, reset.LikeEscapeSchema(utils.InternalSchemas))
|
|
if err != nil {
|
|
return errors.Errorf("failed to query rows: %w", err)
|
|
}
|
|
result, err := pgxv5.CollectRows[Result](rows)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// TODO: implement a markdown table marshaller
|
|
table := "|Table name|Percentage of times index used|Rows in table|\n|-|-|-|\n"
|
|
for _, r := range result {
|
|
table += fmt.Sprintf("|`%s`|`%s`|`%d`|\n", r.Name, r.Percent_of_times_index_used, r.Rows_in_table)
|
|
}
|
|
return list.RenderTable(table)
|
|
}
|