# Manage SQL Models using Rudder CLI


> Define and manage SQL models as YAML configurations using the Rudder CLI tool.



The **SQL Models** feature in Rudder CLI lets you manage your [Reverse ETL SQL model sources](https://www.rudderstack.com/docs/activate/models/) through a Git-based workflow. It lets you store your SQL model configurations as YAML files in Git repositories, and use standard Git workflows to collaborate on any changes.

This approach brings version control, collaboration, and review processes to your SQL model configurations, addressing key limitations of managing these resources solely through the UI.

## Key features

**Bi-directional management**

- Create new SQL model resources directly from CLI
- Import existing SQL model sources from your workspace into Git

**Validation and preview**

- Validate SQL syntax and connectivity before deployment
- Preview query results to ensure correctness
- Check primary key constraints and column mappings

**Flexible configuration**

- Define SQL queries inline in YAML files or reference external `.sql` files
- Support for multiple warehouse types (listed below)

**Automation ready**

- GitHub Actions integration for CI/CD workflows
- Dry-run capabilities to preview changes before applying them
- Cross-domain resource management alongside [Data Catalog and Tracking Plan resources](https://www.rudderstack.com/docs/rudder-cli/data-catalog-and-tracking-plans/)

## Supported warehouses

The SQL Models feature supports the following data warehouses:

- PostgreSQL
- MySQL
- Snowflake
- Amazon Redshift
- Google BigQuery
- Databricks
- Trino

## Workflow overview

1. Configure your Git repository and Rudder CLI project.
2. Create new SQL models or import existing ones from your workspace.
3. Make changes to your SQL models using your preferred editor.
4. Validate your changes using the `validate` command and `apply` command in dry-run mode (via the `--dry-run` flag).
5. Use the `preview` command for SQL validation and connection validation.
6. Apply your changes using the `apply` command.
7. Create pull requests for team review and approval.
8. Automatically sync approved changes to your workspace via GitHub Actions.

## Before you begin

Before you begin, make sure you have:

- [Rudder CLI](https://www.rudderstack.com/docs/rudder-cli/) installed and configured locally
- A Rudder CLI project directory for storing the SQL model YAML resources
- [GitHub Actions](https://www.rudderstack.com/docs/rudder-cli/github-actions/) configured if you want automated syncing in CI/CD
- A [workspace-level Service Access Token](https://www.rudderstack.com/docs/access-management/service-access-tokens/#workspace-sat) in the RudderStack dashboard with the following [permissions](https://www.rudderstack.com/docs/access-management/policies-overview/#resource-permissions) to manage SQL models:

| Resource | Permissions |
| :----| :-----|
| SQL Models | **Create & Delete**, **Edit** |

- **If you're on Free or self-hosted plan, or for testing and development only**: Generate a [Personal Access Token](https://www.rudderstack.com/docs/access-management/personal-access-tokens/) with **Read-Write** role
**Warning:** Any action authenticated by a Personal Access Token will break if the user generating the token is removed from the organization or there is a breaking change to their permissions.

**Token permissions for legacy RBAC system**

If you are on the [legacy Permissions Management (RBAC) system](https://www.rudderstack.com/docs/archive/dashboard-guides/user-management/), your workspace-level Service Access Token should have minimum **Admin** permissions.

See [Generate a workspace-level Service Access Token](https://www.rudderstack.com/docs/archive/dashboard-guides/service-access-tokens/#generate-service-access-token) for steps to create the token.

![workspace-level Service Access Token with Admin permission](/docs/images/access-management/permissions/legacy/admin.webp)

## Recommended project structure

The SQL model resources need to be in the location you pass to the [`apply` command](https://www.rudderstack.com/docs/rudder-cli/sql-models/create/#apply-the-sql-model). If you don't specify a location, the current directory is used as the project location.

**Info:** The `apply` command syncs all Rudder CLI resources it finds under that path (SQL models, Data Catalog, and so on).

A recommended directory structure for your CLI project is shown:

```text
my-rudder-project/
├── sql-models/
│   ├── user-analytics.yaml
│   ├── product-models.yaml
│   └── sql/
│       ├── user-analytics.sql
│       └── product-views.sql
├── data-catalog/
│   ├── events/
│   │   └── product-events.yaml
│   └── properties/
│       └── user-properties.yaml
└── README.md
```

#### Key considerations

- **File discovery**: The CLI recursively scans the specified directory for all `.yaml` and `.yml` files.
- **Flexible organization**: You can organize files in any directory structure that makes sense for your project.
- **External SQL files**: When using the `file` option in your YAML configurations, ensure the SQL files are accessible relative to the YAML file location.
- **Mixed resources**: The CLI can manage SQL models alongside Data Catalog resources (events, properties, custom types, Tracking Plans) from the same project directory.

## Get started

- Follow the [End-to-End Walkthrough: SQL Models with Rudder CLI](https://www.rudderstack.com/docs/rudder-cli/sql-models-walkthrough/) for a single guided path through **create**, **import**, **validate**, **preview**, and **apply**
- See [Create New SQL Model Resources](https://www.rudderstack.com/docs/rudder-cli/sql-models/create/), [Import Existing SQL Model Resources](https://www.rudderstack.com/docs/rudder-cli/sql-models/import/), and [Validate and Preview Your Models](https://www.rudderstack.com/docs/rudder-cli/sql-models/validate/) for focused how-tos
- See the [SQL Model Resources YAML Reference](https://www.rudderstack.com/docs/rudder-cli/yaml-sql-models/) for field-level specs
- Sync a SQL model to a destination with [Manage Reverse ETL Connections using Rudder CLI](https://www.rudderstack.com/docs/rudder-cli/retl-connections/)
- Automate with [GitHub Actions for Rudder CLI](https://www.rudderstack.com/docs/rudder-cli/github-actions/)

