# Database as Code

> Run your database as code: schema and migrations in version control, automated SQL review, pull-request approvals, CI/CD deployment, and drift detection. Self-hosted, open-source.

Source: https://www.bytebase.com/database-as-code/

---

## What happens when the database isn't code

### Schema lives outside version control

App code has a single source of truth in Git; the schema lives in someone's client, a wiki, and three slightly different environments. No one can say what production looks like.

### Changes bypass review and CI

A migration runs by hand, skipping the pull request, lint, and tests every other change clears. The first review happens in production.

### No history, no rollback

Without versioned migrations there's no diff, no author, no way back. Reproducing an environment means reverse-engineering a running database.

## Schema and migrations in version control

### Schema as code in Git

Track the full schema and every migration in version control, so the repository — not a running database — is the source of truth.

### GitOps change flow

Changes arrive as pull requests, get reviewed and approved, and deploy automatically on merge — no out-of-band SQL.

### Declarative or migration-based

Define the desired state and let Bytebase diff it, or manage versioned migration scripts — whichever model your team runs.

## Automated review and deploy in your pipeline

### Automated SQL review

100+ built-in rules catch unsafe migrations — missing indexes, locking DDL, backward-incompatible changes — before they merge.

### CI/CD deployment

Wire migrations into GitHub, GitLab, or Bitbucket; deployment runs in the same pipeline as the application.

### Rollback built in

Every change carries its diff and a generated rollback plan, so reverting is a click, not a rebuild.

## One source of truth across every environment

### Drift detection

Compare any environment against the versioned schema and catch out-of-band changes before they cause an incident.

### Reproducible environments

Stand up a new environment from the repository, not a hand-copied database dump.

### Full change history and audit

Every migration is logged with author, diff, and timestamp — an audit trail you don't assemble by hand.

## Frequently asked questions

### What does 'database as code' mean?

Managing your schema and changes the way you manage application code: schema and every migration in version control as the single source of truth, changes through pull-request review and CI/CD, every deployment versioned, auditable, and reversible.

### State-based vs migration-based database as code?

State-based (declarative) tools store the desired schema and generate the diff; migration-based tools store an ordered list of change scripts applied in sequence. Bytebase supports both.

### How is database as code different from database version control?

Version control — schema and migrations in Git — is the foundation. Database as code adds the rest of the workflow: automated SQL review, pull-request approvals, CI/CD deployment, drift detection, and an audit trail.

### Does it work with PostgreSQL, MySQL, SQL Server, and Oracle?

Yes. One database-as-code workflow runs across PostgreSQL, MySQL, SQL Server, Oracle, and 20+ engines, with the same version control, review, and deployment model everywhere.

### Can I use it with my existing CI/CD pipeline?

Yes. Migrations wire into GitHub, GitLab, or Bitbucket, and deployment runs in the same pipeline as your application — no separate migration runner to maintain.

### Is Bytebase self-hosted and open-source?

Yes. Bytebase runs in your own infrastructure as a single Docker image, so data and credentials never leave your network, and it's open-source.

## Get Started

- [Contact us](https://www.bytebase.com/contact-us/)
- [Start now (cloud)](https://console.bytebase.com)
