feat(inspector): fail on duplicate index names per schema
PostgreSQL index names share the schema-wide relation namespace, so a reused name makes CREATE INDEX IF NOT EXISTS silently skip the duplicate. Add duplicate_index_name rule (enforce by default) covering indexes and PK/unique constraint backing indexes.
This commit is contained in:
@@ -643,3 +643,64 @@ func contains(slice []string, value string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// validateDuplicateIndexName checks that index names are unique per schema.
|
||||
// PostgreSQL keeps indexes in the schema-wide relation namespace, so a name
|
||||
// reused on another table makes CREATE INDEX IF NOT EXISTS silently skip it.
|
||||
// Primary key and unique constraints create backing indexes and share that
|
||||
// namespace too. An index and a constraint with the same name on the same
|
||||
// table describe one object and are not reported.
|
||||
func validateDuplicateIndexName(db *models.Database, rule Rule, ruleName string) []ValidationResult {
|
||||
results := []ValidationResult{}
|
||||
|
||||
for _, schema := range db.Schemas {
|
||||
// lowercased name -> "table" entries, one per distinct object
|
||||
owners := make(map[string][]string)
|
||||
display := make(map[string]string)
|
||||
order := []string{}
|
||||
|
||||
add := func(name, table string, sameTableMerges bool) {
|
||||
key := strings.ToLower(name)
|
||||
if _, seen := owners[key]; !seen {
|
||||
order = append(order, key)
|
||||
display[key] = name
|
||||
}
|
||||
if sameTableMerges && contains(owners[key], table) {
|
||||
return
|
||||
}
|
||||
owners[key] = append(owners[key], table)
|
||||
}
|
||||
|
||||
for _, table := range schema.Tables {
|
||||
for _, key := range sortedKeys(table.Indexes) {
|
||||
if name := table.Indexes[key].Name; name != "" {
|
||||
add(name, table.Name, false)
|
||||
}
|
||||
}
|
||||
for _, c := range sortConstraints(table.Constraints) {
|
||||
if c.Name == "" || (c.Type != models.PrimaryKeyConstraint && c.Type != models.UniqueConstraint) {
|
||||
continue
|
||||
}
|
||||
add(c.Name, table.Name, true)
|
||||
}
|
||||
}
|
||||
|
||||
for _, key := range order {
|
||||
tables := owners[key]
|
||||
results = append(results, createResult(
|
||||
ruleName,
|
||||
len(tables) == 1,
|
||||
rule.Message,
|
||||
formatLocation(schema.Name, display[key], "")+" on "+strings.Join(tables, ", "),
|
||||
map[string]interface{}{
|
||||
"schema": schema.Name,
|
||||
"index": display[key],
|
||||
"tables": tables,
|
||||
"occurrences": len(tables),
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user