๐Ÿš€ Add to your pipeline โ€” copy, paste, done

# bitbucket-pipelines.yml
image: node:20

pipelines:
  pull-requests:
    '**':
      - step:
          name: Schema Diff
          image: node:20
          variables:
            SCHEMA_PATH: "db/schema.sql"
            DIALECT: "postgres"
            POST_PR_COMMENT: "true"
            FAIL_ON_BREAKING: "true"
          script:
            - git fetch origin $BITBUCKET_PR_DESTINATION_BRANCH
            - git show origin/$BITBUCKET_PR_DESTINATION_BRANCH:$SCHEMA_PATH > /tmp/schema_base.sql 2>/dev/null || echo "-- No base schema" > /tmp/schema_base.sql
            - node ci/schemalens-diff.js /tmp/schema_base.sql $SCHEMA_PATH --dialect=$DIALECT --format=markdown --output=schema_diff_report.md
            - cat schema_diff_report.md
          artifacts:
            - schema_diff_report.md
Download bitbucket-pipelines.yml Bitbucket Docs

๐Ÿ’ฌ What the PR comment looks like

SL
SchemaLens Bot
commented 2 minutes ago

๐Ÿ” SchemaLens Schema Diff Report

๐ŸŸข Tables Added1
๐Ÿ”ด Tables Removed0
๐ŸŸก Tables Modified2
โš ๏ธ Breaking Changes1
๐Ÿ“Š Risk Score42/100 (Medium)

Generated Migration

ALTER TABLE users
  ADD COLUMN email_verified_at TIMESTAMP;

Generated by SchemaLens Bitbucket Pipelines

๐Ÿ“ฆ What the pipeline artifact looks like

schema_diff_report.md 2.4 KB

Team members can download the full markdown report from the Artifacts tab of any pipeline run. The report includes:

  • Complete diff summary with table counts
  • Breaking change list with severity levels
  • Generated migration SQL (full script with Pro)
  • Risk score and recommendation

Why add schema diff to Bitbucket Pipelines?

๐Ÿ›ก๏ธ Prevent production incidents

Breaking changes like dropped columns, removed indexes, or altered constraints get flagged before merge โ€” not after deploy.

๐Ÿ’ฌ PR comments, automatically

Every pull request gets a clear schema diff summary posted as a comment. Reviewers see exactly what changed without leaving Bitbucket.

๐Ÿ“ฆ Downloadable artifacts

The full markdown report is attached to every pipeline as an artifact. Download it for compliance docs, audits, or offline review.

โญ๏ธ Smart skip

Set SKIP_NO_SQL_CHANGE: "true" and the step skips entirely when no .sql files were modified โ€” saving CI minutes.

โšก Zero setup required

No database connections, no CLI installation, no license key. Just point the step at two SQL files.

๐Ÿšฆ Fail the pipeline on breaking changes

Set FAIL_ON_BREAKING: "true" and the pipeline fails if any dangerous schema changes are detected.

๐Ÿ“Š Risk score at a glance

Each diff gets a 0-100 risk score. High-risk migrations get extra scrutiny in code review.

๐Ÿ”“ 100% free for open source

The free tier includes breaking change detection, risk scoring, PR comments, and artifact reports. No credit card required.

How it works

1

Dump your schema

Export your database schema to a SQL file as part of your workflow (e.g., pg_dump --schema-only or commit your schema file to the repo).

2

Compare before and after

The step compares the schema from your target branch against the schema in the PR. Any drift is surfaced instantly.

3

Get a PR comment with the diff

Enable POST_PR_COMMENT: "true" and add a BITBUCKET_ACCESS_TOKEN repository variable. The step posts a formatted summary directly on the pull request.

Free vs Pro

FeatureFree TierPro (optional)
Schema diff summaryโœ…โœ…
Breaking change detectionโœ…โœ…
Risk scoreโœ…โœ…
PR commentsโœ…โœ…
Pipeline artifactsโœ…โœ…
Smart skip (no SQL changes)โœ…โœ…
Full migration SQLFirst 5 linesโœ… Complete script
Markdown exportโœ…โœ…
JSON exportโœ…โœ…
Rate limit15/min30/min

Full configuration reference

# Repository variables (Repository settings โ†’ Repository variables)
BITBUCKET_ACCESS_TOKEN  # optional โ€” OAuth token with pullrequest:write for PR comments
SL_LICENSE_KEY          # optional โ€” SchemaLens Pro license key for full migration output

# Step variables
SCHEMA_PATH              "db/schema.sql"      # Path to current schema SQL file
DIALECT                  "postgres"           # postgres | mysql | sqlite | mssql | oracle
FAIL_ON_BREAKING         "false"              # true = fail pipeline on breaking changes
POST_PR_COMMENT          "false"              # true = post report as PR comment (needs token)
SKIP_NO_SQL_CHANGE       "false"              # true = skip step when no .sql files changed

Example: PostgreSQL project with PR comments

# bitbucket-pipelines.yml
image: node:20

pipelines:
  pull-requests:
    '**':
      - step:
          name: Schema Diff
          image: node:20
          variables:
            SCHEMA_PATH: "db/schema.sql"
            DIALECT: "postgres"
            POST_PR_COMMENT: "true"
            FAIL_ON_BREAKING: "true"
            SKIP_NO_SQL_CHANGE: "true"
          script:
            - apt-get update && apt-get install -y jq curl
            - |
              if [ ! -f "$SCHEMA_PATH" ]; then
                echo "Schema file not found: $SCHEMA_PATH"
                exit 1
              fi
            - |
              if [ "$SKIP_NO_SQL_CHANGE" = "true" ]; then
                git fetch origin "$BITBUCKET_PR_DESTINATION_BRANCH"
                if git diff --name-only "origin/$BITBUCKET_PR_DESTINATION_BRANCH..$BITBUCKET_COMMIT" | grep -q '\.sql$'; then
                  echo "Schema files changed โ€” running diff."
                else
                  echo "No .sql files changed โ€” skipping schema diff to save CI minutes."
                  exit 0
                fi
              fi
            - |
              git fetch origin "$BITBUCKET_PR_DESTINATION_BRANCH"
              git show "origin/$BITBUCKET_PR_DESTINATION_BRANCH:$SCHEMA_PATH" > /tmp/schema_base.sql 2>/dev/null || echo "-- No base schema" > /tmp/schema_base.sql
            - |
              ENDPOINT="https://schemalens.tech/api/free-diff"
              if [ -n "$SL_LICENSE_KEY" ]; then
                ENDPOINT="https://schemalens.tech/api/diff"
                echo "Using SchemaLens Pro endpoint."
              fi

              BODY=$(jq -n \
                --arg schemaA "$(cat /tmp/schema_base.sql)" \
                --arg schemaB "$(cat "$SCHEMA_PATH")" \
                --arg dialect "$DIALECT" \
                --arg format "markdown" \
                '{schemaA: $schemaA, schemaB: $schemaB, dialect: $dialect, format: $format}')

              for attempt in 1 2 3; do
                HTTP_STATUS=$(curl -s -o /tmp/response.json -w "%{http_code}" -X POST "$ENDPOINT" \
                  -H "Content-Type: application/json" \
                  ${SL_LICENSE_KEY:+-H "X-License-Key: $SL_LICENSE_KEY"} \
                  -d "$BODY" || echo "000")
                if [ "$HTTP_STATUS" = "200" ]; then break; fi
                echo "Attempt $attempt failed ($HTTP_STATUS). Retrying..."
                sleep "$((attempt * 2))"
              done

              if [ "$HTTP_STATUS" != "200" ]; then
                echo "SchemaLens API failed after 3 attempts"
                exit 1
              fi
            - jq -r '.markdown // .migrationTeaser' /tmp/response.json > schema_diff_report.md
            - cat schema_diff_report.md
            - |
              if [ "$POST_PR_COMMENT" = "true" ] && [ -n "$BITBUCKET_ACCESS_TOKEN" ]; then
                REPORT=$(cat schema_diff_report.md | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
                curl -s -X POST \
                  -H "Authorization: Bearer $BITBUCKET_ACCESS_TOKEN" \
                  -H "Content-Type: application/json" \
                  "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/comments" \
                  -d "{\"content\": {\"raw\": \"$REPORT\"}}" || echo "Warning: failed to post PR comment."
              fi
            - |
              BCOUNT=$(jq -r '(.summary.breakingChangeCount // (.breakingChanges | length) // 0)' /tmp/response.json)
              if [ "$FAIL_ON_BREAKING" = "true" ] && [ "$BCOUNT" != "0" ]; then
                echo "Breaking changes detected: $BCOUNT"
                exit 1
              fi
          artifacts:
            - schema_diff_report.md

Start catching schema drift today

Free forever for open source. Upgrade to Pro for full migration generation.

Try SchemaLens Free View Pro Pricing

๐Ÿข Need this for your team?

Get a personalized walkthrough of SchemaLens for your engineering team โ€” including SSO, shared workspaces, and Slack alerts.

Book a Demo Free Team Audit