writeIndexInternal is the core index writing function with options for different modes
(out io.Writer, schema string, table string, index *storepb.IndexMetadata, useOnlyClause bool, includeTerminatorAndComment bool)
| 3344 | |
| 3345 | // writeIndexInternal is the core index writing function with options for different modes |
| 3346 | func writeIndexInternal(out io.Writer, schema string, table string, index *storepb.IndexMetadata, useOnlyClause bool, includeTerminatorAndComment bool) error { |
| 3347 | if index.Unique { |
| 3348 | if _, err := io.WriteString(out, `CREATE UNIQUE INDEX "`); err != nil { |
| 3349 | return err |
| 3350 | } |
| 3351 | } else { |
| 3352 | if _, err := io.WriteString(out, `CREATE INDEX "`); err != nil { |
| 3353 | return err |
| 3354 | } |
| 3355 | } |
| 3356 | if _, err := io.WriteString(out, index.Name); err != nil { |
| 3357 | return err |
| 3358 | } |
| 3359 | |
| 3360 | // TODO: useOnlyClause assumes partition-structured metadata. If legacy |
| 3361 | // PostgreSQL table inheritance support is ever needed, revisit with |
| 3362 | // definition-driven ONLY extraction. |
| 3363 | if useOnlyClause { |
| 3364 | if _, err := io.WriteString(out, `" ON ONLY "`); err != nil { |
| 3365 | return err |
| 3366 | } |
| 3367 | } else { |
| 3368 | if _, err := io.WriteString(out, `" ON "`); err != nil { |
| 3369 | return err |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | if _, err := io.WriteString(out, schema); err != nil { |
| 3374 | return err |
| 3375 | } |
| 3376 | if _, err := io.WriteString(out, `"."`); err != nil { |
| 3377 | return err |
| 3378 | } |
| 3379 | if _, err := io.WriteString(out, table); err != nil { |
| 3380 | return err |
| 3381 | } |
| 3382 | if _, err := io.WriteString(out, `"`); err != nil { |
| 3383 | return err |
| 3384 | } |
| 3385 | |
| 3386 | // Add USING clause for non-btree indexes |
| 3387 | if index.Type != "" && index.Type != "btree" { |
| 3388 | if _, err := fmt.Fprintf(out, " USING %s", strings.ToUpper(index.Type)); err != nil { |
| 3389 | return err |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | if _, err := io.WriteString(out, ` `); err != nil { |
| 3394 | return err |
| 3395 | } |
| 3396 | if err := writeIndexKeyList(out, index); err != nil { |
| 3397 | return err |
| 3398 | } |
| 3399 | |
| 3400 | // Write INCLUDE and WHERE clauses from the full index definition (from pg_get_indexdef). |
| 3401 | if index.Definition != "" { |
| 3402 | includeClause, whereClause := extractIndexClauses(index.Definition) |
| 3403 | if includeClause != "" { |
no test coverage detected