# Modify Table Schema Tool

Changes a table's schema by stating the changes to make.

## When to Use

Use this whenever an existing table needs a different shape: a new column, a column gone, a column
renamed, or a column's type or nullability changed.

You state **the changes**, not the schema you want to end up with. Columns you do not mention are
left alone, so there is no need to restate a schema to alter one part of it, and no risk of dropping
something by forgetting to include it.

## Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `table_id` | string | Yes | The table's id or slug, as `list_tables` gives them; the slug aliases the id and either is accepted |
| `patches` | array | Yes | The changes to apply, in order |

Each patch is one of four operations, chosen by `op`:

### `add`

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `name` | string | Yes | New column name |
| `column_type` | string | Yes | `text`, `number`, `boolean`, `date`, `date_time`, `id`, `jsonb`, `object`, `array_untyped` |
| `nullable` | boolean | No | Defaults to `true` |
| `indexed` | boolean | No | Defaults to `true`. `false` is accepted only on a top-level column stored outside JSON |
| `description` | string | No | |

### `remove`

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `name` | string | Yes | Column to drop, along with every value in it |

### `rename`

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `from` | string | Yes | Current name |
| `to` | string | Yes | New name |

### `alter`

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `name` | string | Yes | Column to change |
| `column_type` | string | No | Omit to leave the type as it is |
| `nullable` | boolean | No | Omit to leave nullability as it is |
| `indexed` | boolean | No | Omit to leave indexing as it is. `false` is accepted only on a top-level column stored outside JSON |

## Returns

| Field | Type | Description |
|-------|------|-------------|
| `success` | boolean | Whether the change succeeded |
| `table_id` | string | The table identifier |
| `columns` | array | The full schema after the change |
| `changes` | array | What each patch did, one entry per patch |

## Example

Rename a column, widen another, and drop one that is no longer used:

```json
{
  "table_id": "calendar-blocks",
  "patches": [
    { "op": "rename", "from": "calendar_type", "to": "type" },
    { "op": "alter", "name": "recurrence", "column_type": "jsonb" },
    { "op": "remove", "name": "local_date" }
  ]
}
```

## Notes

- **All patches apply in one transaction.** If any is rejected, none are applied and the table is
  left exactly as it was. A batch is safe to attempt.
- Patches apply **in order**, so renaming a column and then altering it under its new name works.
- **Renaming keeps the data.** Type, nullability, and every stored value survive. Prefer a `rename`
  patch over `remove` plus `add`, which destroys the column's contents.
- **A rename breaks references, and nothing is rewritten.** Workflows binding the column, Data Views
  selecting it, and saved queries filtering on it all keep pointing at a name that no longer exists.
  Check what reads the table before renaming, and update those references afterwards.
- **`remove` is irreversible.** The column and its values are gone. Confirm with the user before
  removing a column that holds data.
- Adding a **required** column to a table that already has rows is rejected: the existing rows would
  have no value for it. Add it nullable, populate it, then `alter` it to required.
- Making a column required is rejected while any row holds a null in it.
- An index speeds up `eq` and `in` filters on the column, and range filters and sorts on number and
  date columns; text sorts and text range filters are not served by it. It costs storage and write
  time. Every column is indexed unless its declaration turns that off, and a column stored as JSON
  (`json`, `object`, `list`, `block`, `tool_group`) or a field inside an object is always indexed.
  Indexes are built in the background shortly after the change is saved; `indexed` in the result is
  the declared setting.
- Widening a scalar column to a JSONB-backed type is supported. Narrowing a JSONB-backed column back
  to a scalar is not — the stored values have no single safe interpretation.
- Narrowing within JSONB, such as `jsonb` to a typed `object`, is allowed only if every existing
  value already matches the target shape.
- The row `id` is not a declared column and cannot be added, renamed, altered, or removed.
- Column names allow letters, numbers, underscore, and hyphen only.
