{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gmr.github.io/pglifecycle/schemata/materialized_view.json",
  "title": "Materialized View",
  "description": "Defines a materialized view",
  "type": "object",
  "properties": {
    "schema": {
      "title": "Schema",
      "description": "The schema the materialized view is created in",
      "type": "string"
    },
    "name": {
      "title": "Name",
      "description": "The materialized view name",
      "type": "string"
    },
    "owner": {
      "title": "Owner",
      "description": "The role that owns the materialized view",
      "type": "string"
    },
    "sql": {
      "title": "SQL Statement",
      "description": "User-provided raw SQL snippet",
      "type": "string"
    },
    "columns": {
      "title": "Column names",
      "description": "An optional list of names to be used for columns of the materialized view.\nIf not given, the column names are deduced from the query.\n",
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "string"
          },
          {
            "type": "object",
            "properties": {
              "name": {
                "title": "Column Name",
                "type": "string"
              },
              "comment": {
                "title": "Comment",
                "type": "string"
              }
            },
            "required": [
              "name"
            ]
          }
        ]
      }
    },
    "table_access_method": {
      "title": "Table Access Method",
      "description": "Define the table access method",
      "type": "string"
    },
    "storage_parameters": {
      "title": "Storage Parameters",
      "description": "Storage parameter settings for the materialized view. A `toast.` prefix targets the view's TOAST table instead of the view itself.\n",
      "type": "object",
      "propertyNames": {
        "pattern": "^(toast\\.)?[A-Za-z0-9_]+$"
      }
    },
    "tablespace": {
      "title": "Tablespace",
      "description": "Specifies the name of the tablespace in which the materialized view is to be created.\n",
      "type": "string"
    },
    "query": {
      "title": "Query",
      "description": "A SELECT or VALUES command which will provide the columns and rows of the\nmaterialized view.\n",
      "type": "string"
    },
    "indexes": {
      "title": "Indexes",
      "description": "An array of indexes on the materialized view",
      "type": "array",
      "items": {
        "title": "Index",
        "description": "Defines an index on a table",
        "type": "object",
        "properties": {
          "name": {
            "title": "Name",
            "description": "The index name",
            "type": "string"
          },
          "sql": {
            "title": "SQL Statement",
            "description": "User-provided raw SQL snippet",
            "type": "string"
          },
          "unique": {
            "title": "Unique",
            "description": "Specifies that the index is a unique index",
            "type": "boolean",
            "default": false
          },
          "recurse": {
            "title": "Recurse to Partitions",
            "description": "Used to specify that the index should be created on partitions of the table.\n",
            "type": "boolean"
          },
          "method": {
            "title": "Index Method",
            "description": "The name of the index method to be used",
            "enum": [
              "brin",
              "btree",
              "gin",
              "gist",
              "hash",
              "spgist"
            ],
            "default": "btree"
          },
          "columns": {
            "title": "Columns",
            "description": "Defines the columns that are indexed",
            "type": "array",
            "items": {
              "title": "Column",
              "type": "object",
              "properties": {
                "name": {
                  "title": "Column Name",
                  "description": "Specify either the column name, mutally exclusive from expression.",
                  "type": "string"
                },
                "expression": {
                  "title": "Expression",
                  "type": "string",
                  "description": "Specify an expression for the column, mutally exclusive from name."
                },
                "collation": {
                  "title": "Column Collation",
                  "description": "Assigns a collation to the column (which must be of a collatable data type). If not specified, the column data type's default collation is used.\n",
                  "type": "string"
                },
                "opclass": {
                  "title": "Operator Class",
                  "description": "The name of an operator class",
                  "type": "string"
                },
                "direction": {
                  "title": "Sort Direction",
                  "description": "Specifies the sort direction in the index for the column",
                  "enum": [
                    "ASC",
                    "DESC"
                  ],
                  "default": "ASC"
                },
                "null_placement": {
                  "title": "NULL value placement",
                  "description": "Specifies the placement of null values in the index.",
                  "enum": [
                    "FIRST",
                    "LAST"
                  ]
                }
              },
              "additionalProperties": false,
              "oneOf": [
                {
                  "required": [
                    "name"
                  ],
                  "not": {
                    "required": [
                      "expression"
                    ]
                  }
                },
                {
                  "required": [
                    "expression"
                  ],
                  "not": {
                    "required": [
                      "name"
                    ]
                  }
                }
              ]
            }
          },
          "include": {
            "title": "Include Columns",
            "description": "Use to provide a list of non-key columns to provide in the index.",
            "type": "array",
            "items": {
              "title": "Column Name",
              "type": "string"
            }
          },
          "storage_parameters": {
            "title": "Storage Parameters",
            "description": "Storage parameter settings for the index",
            "type": "object",
            "propertyNames": {
              "pattern": "^[A-Za-z0-9_]*$"
            }
          },
          "tablespace": {
            "title": "Tablespace",
            "description": "Specifies the tablespace to use when creating the index",
            "type": "string"
          },
          "nulls_not_distinct": {
            "title": "Nulls Not Distinct",
            "description": "Renders NULLS NOT DISTINCT on a unique index (PostgreSQL 15+), where one null equals another. Absent means nulls are distinct, the default.\n",
            "type": "boolean"
          },
          "where": {
            "title": "Where",
            "description": "Use to provide an expression for creating a partial index",
            "type": "string"
          },
          "comment": {
            "title": "Comment",
            "description": "An optional comment about the table",
            "type": "string"
          },
          "dependencies": {
            "title": "Dependencies",
            "description": "Objects this object depends on, for the cases the dependency graph cannot infer: an ordering the topological sort would otherwise get wrong, such as a foreign key between tables or an inheritance parent.\nEach entry names an object as `schema.name`, or unqualified for a schemaless type. A function or aggregate carries its argument signature (`test.f(integer)`) and a cast its types (`(int4 AS timestamp)`), since the name alone is ambiguous.\n",
            "type": "object",
            "properties": {
              "aggregates": {
                "description": "The aggregates this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "collations": {
                "description": "The collations this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "domains": {
                "description": "The domains this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "event_triggers": {
                "description": "The event triggers this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "extensions": {
                "description": "The extensions this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "foreign_data_wrappers": {
                "description": "The foreign data wrappers this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "functions": {
                "description": "The functions this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "languages": {
                "description": "The languages this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "materialized_views": {
                "description": "The materialized views this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "publications": {
                "description": "The publications this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "sequences": {
                "description": "The sequences this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "servers": {
                "description": "The servers this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "subscriptions": {
                "description": "The subscriptions this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "tables": {
                "description": "The tables this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "types": {
                "description": "The types this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "user_mappings": {
                "description": "The user mappings this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "users": {
                "description": "The users this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "views": {
                "description": "The views this object depends on.",
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "additionalProperties": false
          }
        },
        "additionalProperties": false,
        "oneOf": [
          {
            "required": [
              "sql"
            ],
            "not": {
              "required": [
                "name",
                "method",
                "columns",
                "storage_parameters",
                "unique",
                "recurse",
                "include",
                "where"
              ]
            }
          },
          {
            "required": [
              "name",
              "columns"
            ],
            "not": {
              "required": [
                "sql"
              ]
            }
          }
        ]
      }
    },
    "comment": {
      "title": "Comment",
      "description": "An optional comment about the materialized view.",
      "type": "string"
    },
    "dependencies": {
      "title": "Dependencies",
      "description": "Objects this object depends on, for the cases the dependency graph cannot infer: an ordering the topological sort would otherwise get wrong, such as a foreign key between tables or an inheritance parent.\nEach entry names an object as `schema.name`, or unqualified for a schemaless type. A function or aggregate carries its argument signature (`test.f(integer)`) and a cast its types (`(int4 AS timestamp)`), since the name alone is ambiguous.\n",
      "type": "object",
      "properties": {
        "aggregates": {
          "description": "The aggregates this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "collations": {
          "description": "The collations this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "domains": {
          "description": "The domains this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "event_triggers": {
          "description": "The event triggers this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "extensions": {
          "description": "The extensions this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "foreign_data_wrappers": {
          "description": "The foreign data wrappers this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "functions": {
          "description": "The functions this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "languages": {
          "description": "The languages this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "materialized_views": {
          "description": "The materialized views this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "publications": {
          "description": "The publications this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "sequences": {
          "description": "The sequences this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "servers": {
          "description": "The servers this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "subscriptions": {
          "description": "The subscriptions this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "tables": {
          "description": "The tables this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "types": {
          "description": "The types this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "user_mappings": {
          "description": "The user mappings this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "users": {
          "description": "The users this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "views": {
          "description": "The views this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,
  "requires": [
    "schema",
    "name"
  ],
  "oneOf": [
    {
      "required": [
        "query"
      ],
      "not": {
        "required": [
          "sql"
        ]
      }
    },
    {
      "required": [
        "sql"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "columns"
            ]
          },
          {
            "required": [
              "storage_parameters"
            ]
          },
          {
            "required": [
              "tablespace"
            ]
          },
          {
            "required": [
              "query"
            ]
          },
          {
            "required": [
              "indexes"
            ]
          }
        ]
      }
    }
  ]
}