{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://catalog.lintel.tools/schemas/schemastore/vespertide-migration/latest.json",
  "title": "MigrationPlan",
  "x-lintel": {
    "source": "https://www.schemastore.org/vespertide-migration.json",
    "sourceSha256": "05fc7d8c44199403bee95997c01ff6fe4372fc12eaa5ba763220d44dd6172c12",
    "fileMatch": [
      "**/migrations/**/*.vespertide.json",
      "**/migrations/**/*.vespertide.yaml",
      "**/migrations/**/*.vespertide.yml"
    ],
    "parsers": [
      "json",
      "yaml"
    ]
  },
  "type": "object",
  "properties": {
    "actions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/MigrationAction"
      }
    },
    "comment": {
      "type": [
        "string",
        "null"
      ]
    },
    "created_at": {
      "type": [
        "string",
        "null"
      ],
      "default": null
    },
    "version": {
      "type": "integer",
      "format": "uint32",
      "minimum": 0
    }
  },
  "$defs": {
    "ColumnDef": {
      "type": "object",
      "properties": {
        "comment": {
          "type": [
            "string",
            "null"
          ]
        },
        "default": {
          "anyOf": [
            {
              "$ref": "#/$defs/DefaultValue"
            },
            {
              "type": "null"
            }
          ]
        },
        "foreign_key": {
          "anyOf": [
            {
              "$ref": "#/$defs/ForeignKeySyntax"
            },
            {
              "type": "null"
            }
          ]
        },
        "index": {
          "anyOf": [
            {
              "$ref": "#/$defs/StrOrBoolOrArray"
            },
            {
              "type": "null"
            }
          ]
        },
        "name": {
          "type": "string"
        },
        "nullable": {
          "type": "boolean"
        },
        "primary_key": {
          "anyOf": [
            {
              "$ref": "#/$defs/PrimaryKeySyntax"
            },
            {
              "type": "null"
            }
          ]
        },
        "type": {
          "$ref": "#/$defs/ColumnType"
        },
        "unique": {
          "anyOf": [
            {
              "$ref": "#/$defs/StrOrBoolOrArray"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "name",
        "type",
        "nullable"
      ]
    },
    "ColumnType": {
      "anyOf": [
        {
          "$ref": "#/$defs/SimpleColumnType"
        },
        {
          "$ref": "#/$defs/ComplexColumnType"
        }
      ]
    },
    "ComplexColumnType": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "varchar"
            },
            "length": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            }
          },
          "required": [
            "kind",
            "length"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "numeric"
            },
            "precision": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            },
            "scale": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            }
          },
          "required": [
            "kind",
            "precision",
            "scale"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "char"
            },
            "length": {
              "type": "integer",
              "format": "uint32",
              "minimum": 0
            }
          },
          "required": [
            "kind",
            "length"
          ]
        },
        {
          "type": "object",
          "properties": {
            "custom_type": {
              "type": "string"
            },
            "kind": {
              "type": "string",
              "const": "custom"
            }
          },
          "required": [
            "kind",
            "custom_type"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "enum"
            },
            "name": {
              "type": "string"
            },
            "values": {
              "$ref": "#/$defs/EnumValues"
            }
          },
          "required": [
            "kind",
            "name",
            "values"
          ]
        }
      ]
    },
    "DefaultValue": {
      "description": "A value that can be a string, boolean, or number.\nThis is used for default values where columns can use literal values directly.",
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "type": "integer",
          "format": "int64"
        },
        {
          "type": "number",
          "format": "double"
        },
        {
          "type": "string"
        }
      ]
    },
    "EnumValues": {
      "description": "Enum values definition - either all string or all integer",
      "anyOf": [
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/NumValue"
          }
        }
      ]
    },
    "ForeignKeyDef": {
      "type": "object",
      "properties": {
        "on_delete": {
          "anyOf": [
            {
              "$ref": "#/$defs/ReferenceAction"
            },
            {
              "type": "null"
            }
          ]
        },
        "on_update": {
          "anyOf": [
            {
              "$ref": "#/$defs/ReferenceAction"
            },
            {
              "type": "null"
            }
          ]
        },
        "ref_columns": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "ref_table": {
          "type": "string"
        }
      },
      "required": [
        "ref_table",
        "ref_columns"
      ]
    },
    "ForeignKeySyntax": {
      "anyOf": [
        {
          "description": "table.column",
          "type": "string"
        },
        {
          "$ref": "#/$defs/ForeignKeyDef"
        }
      ]
    },
    "MigrationAction": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/ColumnDef"
              }
            },
            "constraints": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/TableConstraint"
              }
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "create_table"
            }
          },
          "required": [
            "type",
            "table",
            "columns",
            "constraints"
          ]
        },
        {
          "type": "object",
          "properties": {
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "delete_table"
            }
          },
          "required": [
            "type",
            "table"
          ]
        },
        {
          "type": "object",
          "properties": {
            "column": {
              "$ref": "#/$defs/ColumnDef"
            },
            "fill_with": {
              "description": "Optional fill value to backfill existing rows when adding NOT NULL without default.",
              "type": [
                "string",
                "null"
              ]
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "add_column"
            }
          },
          "required": [
            "type",
            "table",
            "column"
          ]
        },
        {
          "type": "object",
          "properties": {
            "from": {
              "type": "string"
            },
            "table": {
              "type": "string"
            },
            "to": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "rename_column"
            }
          },
          "required": [
            "type",
            "table",
            "from",
            "to"
          ]
        },
        {
          "type": "object",
          "properties": {
            "column": {
              "type": "string"
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "delete_column"
            }
          },
          "required": [
            "type",
            "table",
            "column"
          ]
        },
        {
          "type": "object",
          "properties": {
            "column": {
              "type": "string"
            },
            "new_type": {
              "$ref": "#/$defs/ColumnType"
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "modify_column_type"
            }
          },
          "required": [
            "type",
            "table",
            "column",
            "new_type"
          ]
        },
        {
          "type": "object",
          "properties": {
            "column": {
              "type": "string"
            },
            "fill_with": {
              "description": "Required when changing from nullable to non-nullable to backfill existing NULL values.",
              "type": [
                "string",
                "null"
              ]
            },
            "nullable": {
              "type": "boolean"
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "modify_column_nullable"
            }
          },
          "required": [
            "type",
            "table",
            "column",
            "nullable"
          ]
        },
        {
          "type": "object",
          "properties": {
            "column": {
              "type": "string"
            },
            "new_default": {
              "description": "The new default value, or None to remove the default.",
              "type": [
                "string",
                "null"
              ]
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "modify_column_default"
            }
          },
          "required": [
            "type",
            "table",
            "column"
          ]
        },
        {
          "type": "object",
          "properties": {
            "column": {
              "type": "string"
            },
            "new_comment": {
              "description": "The new comment, or None to remove the comment.",
              "type": [
                "string",
                "null"
              ]
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "modify_column_comment"
            }
          },
          "required": [
            "type",
            "table",
            "column"
          ]
        },
        {
          "type": "object",
          "properties": {
            "constraint": {
              "$ref": "#/$defs/TableConstraint"
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "add_constraint"
            }
          },
          "required": [
            "type",
            "table",
            "constraint"
          ]
        },
        {
          "type": "object",
          "properties": {
            "constraint": {
              "$ref": "#/$defs/TableConstraint"
            },
            "table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "remove_constraint"
            }
          },
          "required": [
            "type",
            "table",
            "constraint"
          ]
        },
        {
          "type": "object",
          "properties": {
            "from": {
              "type": "string"
            },
            "to": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "rename_table"
            }
          },
          "required": [
            "type",
            "from",
            "to"
          ]
        },
        {
          "type": "object",
          "properties": {
            "sql": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "raw_sql"
            }
          },
          "required": [
            "type",
            "sql"
          ]
        }
      ]
    },
    "NumValue": {
      "description": "Integer enum variant with name and numeric value",
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "value": {
          "type": "integer",
          "format": "int32"
        }
      },
      "required": [
        "name",
        "value"
      ]
    },
    "PrimaryKeyDef": {
      "type": "object",
      "properties": {
        "auto_increment": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "PrimaryKeySyntax": {
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "$ref": "#/$defs/PrimaryKeyDef"
        }
      ]
    },
    "ReferenceAction": {
      "type": "string",
      "enum": [
        "cascade",
        "restrict",
        "set_null",
        "set_default",
        "no_action"
      ]
    },
    "SimpleColumnType": {
      "type": "string",
      "enum": [
        "small_int",
        "integer",
        "big_int",
        "real",
        "double_precision",
        "text",
        "boolean",
        "date",
        "time",
        "timestamp",
        "timestamptz",
        "interval",
        "bytea",
        "uuid",
        "json",
        "jsonb",
        "inet",
        "cidr",
        "macaddr",
        "xml"
      ]
    },
    "StrOrBoolOrArray": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "boolean"
        }
      ]
    },
    "TableConstraint": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "auto_increment": {
              "type": "boolean",
              "default": false
            },
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "type": {
              "type": "string",
              "const": "primary_key"
            }
          },
          "required": [
            "type",
            "columns"
          ]
        },
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "name": {
              "type": [
                "string",
                "null"
              ]
            },
            "type": {
              "type": "string",
              "const": "unique"
            }
          },
          "required": [
            "type",
            "columns"
          ]
        },
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "name": {
              "type": [
                "string",
                "null"
              ]
            },
            "on_delete": {
              "anyOf": [
                {
                  "$ref": "#/$defs/ReferenceAction"
                },
                {
                  "type": "null"
                }
              ]
            },
            "on_update": {
              "anyOf": [
                {
                  "$ref": "#/$defs/ReferenceAction"
                },
                {
                  "type": "null"
                }
              ]
            },
            "ref_columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "ref_table": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "foreign_key"
            }
          },
          "required": [
            "type",
            "columns",
            "ref_table",
            "ref_columns"
          ]
        },
        {
          "type": "object",
          "properties": {
            "expr": {
              "type": "string"
            },
            "name": {
              "type": "string"
            },
            "type": {
              "type": "string",
              "const": "check"
            }
          },
          "required": [
            "type",
            "name",
            "expr"
          ]
        },
        {
          "type": "object",
          "properties": {
            "columns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "name": {
              "type": [
                "string",
                "null"
              ]
            },
            "type": {
              "type": "string",
              "const": "index"
            }
          },
          "required": [
            "type",
            "columns"
          ]
        }
      ]
    }
  },
  "required": [
    "version",
    "actions"
  ]
}
