> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/database-devops/3.0/use-db-devops/governance-and-compliance/using-approval-gates-with-harness-ui.md).

# Preview SQL with Manual Approval

Learn how to preview SQL changes in Harness DB DevOps pipelines and enforce manual approval before applying schema changes.

Harness DB DevOps allows you to integrate a **Preview SQL** step in your pipeline to review generated SQL before applying schema changes to the database.\
This workflow ensures **governance, visibility, and control**, as you can add a **Manual Approval** step between previewing and executing the changes.

The typical workflow looks like this:

1. Run the **Preview SQL** step to generate SQL for the changes.
2. Pause the pipeline at a **Manual Approval** gate for human verification.
3. After approval, proceed with the **DB Schema Apply** step to deploy the changes.

{% tabs %}
{% tab title="Pipeline Setup" %}
![preview-and-approval-using-harness-ui](https://363656700-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2pV6NGg9g53LONdeVP3y%2Fuploads%2Fgit-blob-d926a2dd770e76f09a8d1a3485c0aeb9c5f51386%2Fpreview-and-approval-using-harness-ui.png?alt=media)
{% endtab %}

{% tab title="YAML Setup" %}

```yaml
pipeline:
  name: approval
  identifier: muxsql
  projectIdentifier: default_project
  orgIdentifier: default
  tags: {}
  stages:
    - stage:
        name: deploy
        identifier: deploy
        description: "Pipeline to deploy DB schema changes with approval gates"
        type: Custom
        spec:
          execution:
            steps:
              - stepGroup:
                  name: preview
                  identifier: preview
                  steps:
                    - step:
                        type: DBSchemaUpdateSQL
                        name: DBSchemaUpdateSQL_1
                        identifier: DBSchemaUpdateSQL_1
                        spec:
                          connectorRef: account.harnessImage
                          dbSchema: cockroachDB
                          dbInstance: cab
                        timeout: 10m
                  stepGroupInfra:
                    type: KubernetesDirect
                    spec:
                      connectorRef: db
              - step:
                  type: HarnessApproval
                  name: Approval
                  identifier: Approval
                  spec:
                    approvalMessage: Please review the following information and approve the pipeline progression <+execution.steps.Preview.steps.DBSchemaUpdateSQL_1.output.sqlCommands>
                    includePipelineExecutionHistory: true
                    isAutoRejectEnabled: false
                    approvers:
                      userGroups:
                        - account._account_all_users
                      minimumCount: 1
                      disallowPipelineExecutor: false
                    approverInputs: []
                  timeout: 1d
              - stepGroup:
                  name: deploy
                  identifier: deploy
                  steps:
                    - step:
                        type: DBSchemaApply
                        name: DB Schema Apply
                        identifier: DB_Schema_Apply
                        spec:
                          connectorRef: account.harnessImage
                          dbSchema: cockroachDB
                          dbInstance: cab
                          markNextChangeSetRun: true
                          delegateSelectors:
                            - harness-gke-delegate
                        timeout: 10m
                  stepGroupInfra:
                    type: KubernetesDirect
                    spec:
                      connectorRef: db
            rollbackSteps: []
          serviceDependencies: []
        tags: {}
```

{% endtab %}
{% endtabs %}

### Set up the workflow <a href="#set-up-the-workflow" id="set-up-the-workflow"></a>

In your pipeline’s **Execution** tab, add the following steps:

| Step                | Description                                                                  |
| ------------------- | ---------------------------------------------------------------------------- |
| **Preview SQL**     | Generates SQL statements from your changelog or schema definition.           |
| **Approval**        | Adds a manual approval gate where users must review and confirm SQL changes. |
| **DB Schema Apply** | Applies the approved SQL changes to the target database instance.            |

### SQL preview <a href="#sql-preview" id="sql-preview"></a>

The **Preview SQL** step shows a generated SQL script based on the detected changes.\
This allows DBAs and developers to:

* Validate the correctness of the SQL.
* Identify potential issues such as destructive changes.
* Ensure compliance with internal policies.

Example output:

```sql
CREATE TABLE products
(
    id SERIAL,
    name TEXT NOT NULL,
    price NUMERIC(10,2) NOT NULL DEFAULT 0.00,
    CONSTRAINT products_pkey PRIMARY KEY (id)
);
```

### Reference SQL output in approval messages <a href="#reference-sql-output-in-approval-messages" id="reference-sql-output-in-approval-messages"></a>

To display the generated SQL in your approval message, use the Preview SQL step's output variable:

```sh
<+execution.steps.[StepGroup_ID].steps.[PreviewSQL_Step_ID].output.sqlCommands>
```

For example, if your step group identifier is `Preview_SQL` and the Preview SQL step identifier is `Test_Policies`, use:

```sh
<+execution.steps.Preview_SQL.steps.Test_Policies.output.sqlCommands>
```

![sql-approval-message](https://363656700-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2pV6NGg9g53LONdeVP3y%2Fuploads%2Fgit-blob-062df7288c5b456a70e5acc4d459136711c70aa8%2Fsql-approval-message.png?alt=media)

### Manual approval step <a href="#manual-approval-step" id="manual-approval-step"></a>

The Approval step acts as a gate in the pipeline. When the pipeline reaches this stage:

* A notification is sent to the designated approvers.
* Approvers can review the Preview SQL output before continuing.
* The pipeline proceeds to the DB Schema Apply step only after approval. This ensures that no schema changes are deployed without human validation.

### Apply schema changes <a href="#apply-schema-changes" id="apply-schema-changes"></a>

After approval, the pipeline executes the `Apply Schema` step. This deploys the reviewed SQL script to the configured database instance. For example:

* **Preview SQL:** shows the generated ALTER TABLE statement.
* **Approval:** DBA confirms the change is safe.
* **DB Schema Apply:** Executes the approved change on the target database.

### Next steps <a href="#next-steps" id="next-steps"></a>

* Go to [Apply a DB schema step](/database-devops/3.0/use-db-devops/deployment-pipeline-configuration/step-types/apply-dbschema-step.md) to deploy schema changes in your pipeline.
* Go to [Rollback for database schemas](/database-devops/3.0/use-db-devops/deployment-pipeline-configuration/rollback-and-failure-strategies/rollback-for-database-schemas.md) to configure automated rollback for failed deployments.

{% @harness-feedback/feedback %}
