{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gmr.github.io/pglifecycle/schemata/trigger.json",
  "title": "Trigger",
  "description": "Defines a trigger 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"
    },
    "when": {
      "title": "When to fire the trigger",
      "enum": [
        "BEFORE",
        "AFTER",
        "INSTEAD OF"
      ]
    },
    "events": {
      "title": "The events that cause the fire to trigger",
      "type": "array",
      "items": {
        "enum": [
          "INSERT",
          "UPDATE",
          "DELETE",
          "TRUNCATE"
        ]
      }
    },
    "for_each": {
      "title": "For Each Row or Statement",
      "enum": [
        "ROW",
        "STATEMENT"
      ],
      "default": "STATEMENT"
    },
    "constraint": {
      "title": "Constraint Trigger",
      "description": "Create the trigger as a CONSTRAINT TRIGGER (always AFTER and FOR EACH ROW); it may be deferred to the end of the transaction.\n",
      "type": "boolean"
    },
    "deferrable": {
      "title": "Deferrable",
      "description": "Whether a constraint trigger's firing can be deferred.",
      "type": "boolean"
    },
    "initially_deferred": {
      "title": "Initially Deferred",
      "description": "Whether a deferrable constraint trigger defers by default.",
      "type": "boolean"
    },
    "condition": {
      "title": "Trigger Condition",
      "description": "A Boolean expression that determines whether the trigger function will actually be executed. If WHEN is specified, the function will only be called if the condition returns true.\n",
      "type": "string"
    },
    "function": {
      "title": "Trigger Function",
      "description": "The trigger function to execute",
      "type": "string"
    },
    "arguments": {
      "title": "Funciton Arguments",
      "description": "An optional comma-separated list of arguments to be provided to the function when the trigger is executed.\n",
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "boolean"
          },
          {
            "type": "integer"
          },
          {
            "type": "number"
          },
          {
            "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": {
        "anyOf": [
          {
            "required": [
              "name"
            ]
          },
          {
            "required": [
              "when"
            ]
          },
          {
            "required": [
              "events"
            ]
          },
          {
            "required": [
              "for_each"
            ]
          },
          {
            "required": [
              "constraint"
            ]
          },
          {
            "required": [
              "deferrable"
            ]
          },
          {
            "required": [
              "initially_deferred"
            ]
          },
          {
            "required": [
              "condition"
            ]
          },
          {
            "required": [
              "function"
            ]
          },
          {
            "required": [
              "arguments"
            ]
          }
        ]
      }
    },
    {
      "required": [
        "name",
        "when",
        "events",
        "function"
      ],
      "not": {
        "required": [
          "sql"
        ]
      }
    }
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "deferrable": {
            "const": true
          }
        },
        "required": [
          "deferrable"
        ]
      },
      "then": {
        "properties": {
          "constraint": {
            "const": true
          }
        },
        "required": [
          "constraint"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "initially_deferred": {
            "const": true
          }
        },
        "required": [
          "initially_deferred"
        ]
      },
      "then": {
        "properties": {
          "constraint": {
            "const": true
          },
          "deferrable": {
            "const": true
          }
        },
        "required": [
          "constraint",
          "deferrable"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "constraint": {
            "const": true
          }
        },
        "required": [
          "constraint"
        ]
      },
      "then": {
        "properties": {
          "when": {
            "const": "AFTER"
          },
          "for_each": {
            "const": "ROW"
          }
        }
      }
    }
  ]
}