Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2e0dfa

Browse files
committed
fix: populate is_valid/is_ready for indexes
1 parent e6dcb25 commit d2e0dfa

7 files changed

Lines changed: 36 additions & 15 deletions

File tree

internal/audit/index_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestDuplicateIndexSkipsInvalid(t *testing.T) {
1111
snap.Tables = []schema.Table{{
1212
Schema: "public", Name: "orders",
1313
Indexes: []schema.Index{
14-
{Name: "idx_a", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true},
14+
{Name: "idx_a", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true, IsReady: true},
1515
{Name: "idx_b", Columns: []string{"user_id"}, IndexType: "btree", IsValid: false},
1616
},
1717
}}
@@ -26,8 +26,8 @@ func TestRedundantSkipsUniqueIndex(t *testing.T) {
2626
snap.Tables = []schema.Table{{
2727
Schema: "public", Name: "orders",
2828
Indexes: []schema.Index{
29-
{Name: "idx_unique_email", Columns: []string{"email"}, IndexType: "btree", IsUnique: true, IsValid: true},
30-
{Name: "idx_email_created", Columns: []string{"email", "created_at"}, IndexType: "btree", IsValid: true},
29+
{Name: "idx_unique_email", Columns: []string{"email"}, IndexType: "btree", IsUnique: true, IsValid: true, IsReady: true},
30+
{Name: "idx_email_created", Columns: []string{"email", "created_at"}, IndexType: "btree", IsValid: true, IsReady: true},
3131
},
3232
}}
3333
findings := checkRedundantIndexes(snap)
@@ -41,8 +41,8 @@ func TestNonUniqueRedundantWithUnique(t *testing.T) {
4141
snap.Tables = []schema.Table{{
4242
Schema: "public", Name: "orders",
4343
Indexes: []schema.Index{
44-
{Name: "idx_email", Columns: []string{"email"}, IndexType: "btree", IsValid: true},
45-
{Name: "idx_email_unique", Columns: []string{"email"}, IndexType: "btree", IsUnique: true, IsValid: true},
44+
{Name: "idx_email", Columns: []string{"email"}, IndexType: "btree", IsValid: true, IsReady: true},
45+
{Name: "idx_email_unique", Columns: []string{"email"}, IndexType: "btree", IsUnique: true, IsValid: true, IsReady: true},
4646
},
4747
}}
4848
findings := checkRedundantIndexes(snap)
@@ -59,8 +59,8 @@ func TestDuplicateIndexBothValid(t *testing.T) {
5959
snap.Tables = []schema.Table{{
6060
Schema: "public", Name: "orders",
6161
Indexes: []schema.Index{
62-
{Name: "idx_a", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true},
63-
{Name: "idx_b", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true},
62+
{Name: "idx_a", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true, IsReady: true},
63+
{Name: "idx_b", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true, IsReady: true},
6464
},
6565
}}
6666
findings := checkDuplicateIndexes(snap)

internal/audit/rules.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func checkDuplicateIndexes(snap *schema.SchemaSnapshot) []lint.Finding {
5858
}
5959
for i, a := range nonPrimary {
6060
for _, b := range nonPrimary[i+1:] {
61-
if !a.IsValid || !b.IsValid {
61+
// invalid/not-ready (failed/in-flight CIC) isn't safe to drop
62+
if !a.IsValid || !b.IsValid || !a.IsReady || !b.IsReady {
6263
continue
6364
}
6465
if !sliceEqual(a.Columns, b.Columns) || a.IndexType != b.IndexType {

internal/audit/rules_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func TestDuplicateIndexes(t *testing.T) {
2121
snap.Tables = []schema.Table{{
2222
Schema: "public", Name: "orders",
2323
Indexes: []schema.Index{
24-
{Name: "idx_a", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true},
25-
{Name: "idx_b", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true},
24+
{Name: "idx_a", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true, IsReady: true},
25+
{Name: "idx_b", Columns: []string{"user_id"}, IndexType: "btree", IsValid: true, IsReady: true},
2626
},
2727
}}
2828
findings := checkDuplicateIndexes(snap)
@@ -147,7 +147,7 @@ func TestDuplicateIndexes_Branching(t *testing.T) {
147147
}
148148

149149
idx := func(name string, cols []string, kind string, backs, valid bool) schema.Index {
150-
return schema.Index{Name: name, Columns: cols, IndexType: kind, IsValid: valid, BacksConstraint: backs}
150+
return schema.Index{Name: name, Columns: cols, IndexType: kind, IsValid: valid, IsReady: valid, BacksConstraint: backs}
151151
}
152152

153153
t.Run("both_back_constraints_warning_no_ddl", func(t *testing.T) {

internal/mcp/helpers.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ type (
139139
IsPrimary bool `json:"is_primary"`
140140
Predicate *string `json:"predicate,omitempty"`
141141
Definition string `json:"definition"`
142-
IsValid bool `json:"is_valid"`
142+
// only surfaced when problematic; a healthy index omits these
143+
IsValid *bool `json:"is_valid,omitempty"`
144+
IsReady *bool `json:"is_ready,omitempty"`
143145
}
144146

145147
compactTable struct {
@@ -180,7 +182,18 @@ func toCompactTable(t *schema.Table, sizing *schema.TableSizing) compactTable {
180182
}
181183
out.Indexes = make([]compactIndex, len(t.Indexes))
182184
for i, idx := range t.Indexes {
183-
out.Indexes[i] = compactIndex{idx.Name, idx.Columns, idx.IndexType, idx.IsUnique, idx.IsPrimary, idx.Predicate, idx.Definition, idx.IsValid}
185+
ci := compactIndex{
186+
Name: idx.Name, Columns: idx.Columns, IndexType: idx.IndexType,
187+
IsUnique: idx.IsUnique, IsPrimary: idx.IsPrimary,
188+
Predicate: idx.Predicate, Definition: idx.Definition,
189+
}
190+
if !idx.IsValid {
191+
ci.IsValid = &idx.IsValid
192+
}
193+
if !idx.IsReady {
194+
ci.IsReady = &idx.IsReady
195+
}
196+
out.Indexes[i] = ci
184197
}
185198
if pi := t.PartitionInfo; pi != nil {
186199
if len(pi.Children) > 20 {

internal/schema/introspect.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ type (
216216
isPrimary bool
217217
predicate *string
218218
definition string
219+
isValid bool
220+
isReady bool
219221
backsConstraint bool
220222
}
221223

@@ -415,7 +417,8 @@ func fetchIndexes(ctx context.Context, pool Querier) ([]rawIndex, error) {
415417
if err := r.Scan(
416418
&oid, &ri.name, &ri.indexType,
417419
&ri.isUnique, &ri.isPrimary, &ri.predicate,
418-
&ri.definition, &nKeyAtts, &ri.backsConstraint, &allCols, &totalCols,
420+
&ri.definition, &nKeyAtts, &ri.isValid, &ri.isReady,
421+
&ri.backsConstraint, &allCols, &totalCols,
419422
); err != nil {
420423
return ri, err
421424
}
@@ -648,6 +651,8 @@ func assembleTables(
648651
IsPrimary: ri.isPrimary,
649652
Predicate: ri.predicate,
650653
Definition: ri.definition,
654+
IsValid: ri.isValid,
655+
IsReady: ri.isReady,
651656
BacksConstraint: ri.backsConstraint,
652657
})
653658
}

internal/schema/sql/introspect.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ SELECT i.indrelid::int4 AS table_oid,
155155
pg_catalog.pg_get_expr(i.indpred, i.indrelid) AS predicate,
156156
pg_catalog.pg_get_indexdef(i.indexrelid) AS definition,
157157
i.indnkeyatts AS n_key_atts,
158+
i.indisvalid AS is_valid,
159+
i.indisready AS is_ready,
158160
-- check when index backs a UNIQUE/PK/EXCLUSION constraint
159161
EXISTS (
160162
SELECT 1 FROM pg_catalog.pg_constraint con
@@ -175,7 +177,6 @@ SELECT i.indrelid::int4 AS table_oid,
175177
JOIN pg_catalog.pg_am am ON am.oid = ci.relam
176178
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
177179
AND n.nspname NOT LIKE 'pg_temp_%'
178-
AND NOT i.indisvalid = false
179180
ORDER BY i.indrelid, ci.relname
180181

181182
-- name: fetch-table-stats

internal/schema/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type Index struct {
9898
Predicate *string `json:"predicate,omitempty"`
9999
Definition string `json:"definition"`
100100
IsValid bool `json:"is_valid"`
101+
IsReady bool `json:"is_ready"`
101102
BacksConstraint bool `json:"backs_constraint,omitempty"`
102103
}
103104

0 commit comments

Comments
 (0)