# Schema generation command

{% hint style="success" %}
This is the official documentation of the `agent_ruby` Ruby agent.
{% endhint %}

## Schema generation command

The `.forestadmin-schema.json` file is normally generated when your Rails server starts. However, in some workflows (CI/CD pipelines, pre-deployment validation), you may need to generate it without starting the server.

#### Usage

```bash
rails forest_admin:schema:generate
```

This command will:

1. Load your Rails environment and models
2. Execute your `create_agent.rb` customizations
3. Write the `.forestadmin-schema.json` file
4. **Skip** sending the schema to the Forest Admin API

{% hint style="info" %}
The schema is written to the path configured in your initializer (`config.schema_path`), which defaults to `.forestadmin-schema.json`.
{% endhint %}

#### Options

| Option       | Description               |
| ------------ | ------------------------- |
| `debug=true` | Show stack trace on error |

```bash
rails forest_admin:schema:generate debug=true
```

#### CI/CD example

You can use this command in your CI pipeline to ensure the schema is always up to date in your pull requests.

{% hint style="warning" %}
This command requires a **database connection** to introspect your tables and columns. Your CI environment must have a running database with the schema migrated before generating.
{% endhint %}

```bash
# Setup the database
rails db:create db:migrate

# Generate the schema
rails forest_admin:schema:generate

# Fail if the schema has changed (meaning someone forgot to commit it)
git diff --exit-code .forestadmin-schema.json
```

#### When does the schema change?

The schema needs to be regenerated when:

* A **migration** is applied (new columns, tables, type changes)
* **ActiveRecord associations** are modified (`has_many`, `belongs_to`, `has_one`...)
* **Customizations** in `create_agent.rb` are modified (smart fields, smart actions, charts, segments, renamed fields...)
