GenerateIndexName implements the sql.IndexNameGenerator interface with PostgreSQL-compatible naming conventions: - UNIQUE indexes: _ [_col2...]_key - All other indexes: _ [_col2...]_idx Collisions are resolved by appending a numeric suffix (1, 2, …) to the base name. The c
(ctx *sql.Context, tableName string, idxDef sql.IndexDef, _ sql.Table)
| 269 | // view, sequence, or another index — blocks a candidate name, matching PostgreSQL's |
| 270 | // behavior where all relations in a schema share one namespace. |
| 271 | func (d *PgDatabase) GenerateIndexName(ctx *sql.Context, tableName string, idxDef sql.IndexDef, _ sql.Table) (string, error) { |
| 272 | colPart := strings.Join(idxDef.ColumnNames(), "_") |
| 273 | suffix := "_idx" |
| 274 | if idxDef.IsUnique() { |
| 275 | suffix = "_key" |
| 276 | } |
| 277 | base := tableName + "_" + colPart + suffix |
| 278 | |
| 279 | exists, _, err := d.doesRelationExist(ctx, base) |
| 280 | if err != nil { |
| 281 | return "", err |
| 282 | } |
| 283 | if !exists { |
| 284 | return base, nil |
| 285 | } |
| 286 | for i := 1; ; i++ { |
| 287 | candidate := fmt.Sprintf("%s%d", base, i) |
| 288 | exists, _, err = d.doesRelationExist(ctx, candidate) |
| 289 | if err != nil { |
| 290 | return "", err |
| 291 | } |
| 292 | if !exists { |
| 293 | return candidate, nil |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // doesRelationExist tests if a relation with the specified |name| exists in this database. If any relation with that |
| 299 | // name exists, this function returns true for |exists|, the relation type (e.g. index, view, table, sequence) for |
nothing calls this directly
no test coverage detected