Store indexes in internal schema representation (#57)

Add information about indexes on a table to `pg-roll`'s internal state
storage.

For each table, store an additional JSON object mapping each index name
on the table to details of the index (initially just its name).

An example of the resulting JSON is:

```json
{
  "tables": {
    "fruits": {
      "oid": "16497",
      "name": "fruits",
      "columns": {
        "id": {
          "name": "id",
          "type": "integer",
          "comment": null,
          "default": "nextval('_pgroll_new_fruits_id_seq'::regclass)",
          "nullable": false
        },
        "name": {
          "name": "name",
          "type": "varchar(255)",
          "comment": null,
          "default": null,
          "nullable": false
        }
      },
      "comment": null,
      "indexes": {
        "_pgroll_idx_fruits_name": {
          "name": "_pgroll_idx_fruits_name"
        },
        "_pgroll_new_fruits_pkey": {
          "name": "_pgroll_new_fruits_pkey"
        },
        "_pgroll_new_fruits_name_key": {
          "name": "_pgroll_new_fruits_name_key"
        }
      }
    }
  }
}
```

Also add fields to the `Schema` model structs to allow the new `indexes`
field to be unmarshalled.
This commit is contained in:
Andrew Farries 2023-08-17 14:26:44 +01:00 committed by GitHub
parent 2fb44cf4ae
commit 0020c3e751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -31,6 +31,9 @@ type Table struct {
// Columns is a map of virtual column name -> column mapping
Columns map[string]Column `json:"columns"`
// Indexes is a map of the indexes defined on the table
Indexes map[string]Index `json:"indexes"`
}
type Column struct {
@ -47,6 +50,11 @@ type Column struct {
Comment string `json:"comment"`
}
type Index struct {
// Name is the name of the index in postgres
Name string `json:"name"`
}
func (s *Schema) GetTable(name string) *Table {
if s.Tables == nil {
return nil

View File

@ -117,6 +117,14 @@ BEGIN
ORDER BY
attr.attnum
) c
),
'indexes', (
SELECT json_object_agg(pi.indexrelid::regclass, json_build_object(
'name', pi.indexrelid::regclass
))
FROM pg_index pi
INNER JOIN pg_class pgc ON pi.indexrelid = pgc.oid
WHERE pi.indrelid = t.oid::regclass
)
)) FROM pg_class AS t
INNER JOIN pg_namespace AS ns ON t.relnamespace = ns.oid