# Environments, staging and deploying to production
Source: https://sovaria.in/docs/environments-and-staging

Staging environments with their own database (empty, structure only, or a masked sample or all of production's data), functions, policies and URL; a preview per pull request; and a checked, all-or-nothing deploy to production.

An **environment** is an extra database for the same project, created in the project's own database container. By default it starts as a copy of production's **structure** — tables, views, functions, row-level security policies and migration history — with **no rows**; it can also start empty, or with a masked sample or all of production's rows (see [below](#data)). It gets its own API URL, and its own functions and access policies, using production's for anything it doesn't change. Work there never touches production.

## Create an environment

1. **Open Branches** — Choose **New environment** on the **Branches** page (or, in the console top bar, the database menu's **+ New environment or API version**), then **Staging**.
2. **Name it** — Lowercase letters, digits and dashes, starting with a letter — for example `staging` or `preview`.
3. **Check it out** — When it is built, check it out (Environments page or the top bar's environment picker): the whole console then works on it. Its API URL is shown in **Connect**; point your test app at it. See [Branches and previews](https://sovaria.in/docs/branches-and-previews) for diff, reset, restore and revert.

## What the database starts with

When you create an environment, **Data** chooses what its database starts with:

| Option | You get | Plan |
|---|---|---|
| **Structure only** (default) | Production's tables, views, functions, policies and migration history. No rows. | Every plan with environments |
| **Empty database** | Nothing: for trying a new design from scratch. | Every plan with environments |
| **Structure + sample** | Plus up to 1,000 (Pro) / 10,000 (Ultra, Team) / 100,000 (Enterprise) of the newest rows per table, and every row those rows refer to, so every foreign key resolves. | Pro and higher |
| **Structure + all data** | Plus every row, as of one moment. Refused if the database is larger than 5 GB (Ultra) / 20 GB (Team) / 100 GB (Enterprise); choose a sample or fewer tables then. | Ultra and higher |

(Diagram: Copying a sample into a new environment — Rows never leave your project's database server.)

- **Where it is copied.** Inside your project's own database server: production's rows are never sent anywhere else.
- **Personal data is masked by default.** Columns that look personal — emails, phone numbers, names in people tables (users, customers, members…), addresses, IP addresses, passwords, tokens and secrets, card numbers, birth dates — get realistic fakes. The fakes are deterministic for one copy, so joins and unique keys still work, but they can't be traced back to the real values. The copy lists every column it masked.
- **Turning masking off** (or copying a column as is) is for organization owners and admins only, like any export of your data, and is recorded in the audit log.
- **App users are never copied**, and neither are passwords, API keys or function secrets: the environment has its own.
- **Choose tables.** Leave out big or sensitive tables (an audit log, events) or copy only some.
- **Names with control characters** (a table or column whose name holds an invisible control character) can't be copied: the copy is refused with `UNSUPPORTED_TABLE_NAME` and the names to rename.
- **It runs after the environment exists.** The environment shows *copying data…* until it is done. If a copy fails, its tables are left empty — never half filled — and you see why.
- Triggers don't run during the copy, and sequences continue after production's, so new rows never collide with copied ones.

CLI:
```bash
sov env create staging --data sample --rows 500 --exclude audit_log
sov env create full-copy --data all            # Ultra and higher
sov env copy staging                           # tables, rows, masked columns
sov env create blank --data empty
```

JavaScript:
```ts
await sovaria.api.governance.createEnvironment({
  envId: crypto.randomUUID(), name: 'staging', database: 'sample', sampleRows: 500, excludeTables: ['audit_log'],
})
await sovaria.api.governance.getEnvironmentCopy({ envId })  // { state: 'done', detail: { rows, tables, masked } }
```

## A preview environment for every pull request

With a GitHub repository linked (Functions › GitHub), turn on **A preview environment for every pull request** (Pro and higher). Then each pull request into the linked branch gets an environment named `pr-<number>`:

1. **Opened** — The environment is created — production's structure, or structure + a masked sample if you choose so and your plan has it — the pull request's functions are deployed into it, and its migration files that the environment doesn't have yet are applied to its database. A comment on the pull request gives the preview's API URL.
2. **New commits** — Only the functions that changed are deployed again, and new migration files are applied.
3. **Closed or merged** — The environment and its database are deleted.

- Pull requests from forks and drafts don't get a preview: a fork's code would run in your project.
- Previews count against your environments; when the limit is reached the preview shows why it wasn't created, and the next push retries.
- The webhook needs pull request events as well as pushes: Sovaria adds it when it can, otherwise add it by hand with the secret shown once.
- `sov env previews` (or the Environments page) lists open previews with their status and URL.
- A migration file edited after it was applied to a preview is never applied again on top of the old one: the preview stops and says so, or rebuilds from production with **Rebuild on edit**. A preview is deleted `env.preview_ttl_days` after its last push.

## Make changes in staging first

Turn on **Project settings → Database → Staging first**. If the project has no environment yet, Sovaria builds `staging` first and shows the progress. From then on, tables, columns and migrations are made in staging, and production changes only through **Deploy to production**.

## Deploy to production

**Deploy to production** (top bar) lists everything the environment has that production doesn't: its migrations, its new and changed functions and its new and changed access policies. Before anything is applied it is all checked with nothing written — the migrations run on production inside a transaction that is rolled back, every function and policy is validated, and production's access is simulated with the new policies. Only if everything passes is it applied, as one commit you can undo from [Version history: see, undo and restore changes](https://sovaria.in/docs/version-history). If something fails, nothing is applied and you see what and why.

> **Tip:** Run a simulation against the environment before deploying — `sov sim run --env <environment-id> --fail-on critical` — so access problems show up before production.

## What is separate and what is shared

|  | Per environment | Shared with production |
|---|---|---|
| Database: tables, rows, row-level security | Yes |  |
| API URL | Yes |  |
| Migrations history | Yes (starts as production's) |  |
| Functions | Its own versions; production's for any it hasn't changed |  |
| Access policies | Its own; production's for any it hasn't replaced |  |
| Function secrets and environment variables | Yes (never copied or deployed) |  |
| Storage buckets, API keys, team, billing |  | Yes |
| Daily backups | No for staging (take one on demand); yes for an API version | Production keeps its own |

Because storage is shared, **production's bucket rules always decide file access**, also through an environment's URL: a bucket rule saved in an environment never opens production's files. An environment id must be new: the id of a deleted project or environment is refused.

Policies you save while **production** is selected are production's, even when the request names production's own environment id (the console, `sov link --env` and SDK scopes do): they apply to every request, with or without an environment id. Simulations and `sov rules test` see an environment's policies exactly as its live traffic does.

An environment can also be an **API version** of your app — production stays `v1`, the environment is `v2` with its own URL, and both keep working. See [API versions: run v1 and v2 side by side](https://sovaria.in/docs/api-versions).

## Plans and extra environments

|  | Free | Pro | Ultra | Team | Enterprise |
|---|---|---|---|---|---|
| Environments per project, staging and API versions together (besides production) | — | 2 | 5 | 10 | 50 |
| Start an environment from another environment (not only production) | — | — | Yes | Yes | Yes |
| Start with a copy of the data | — | Sample | Sample or all | Sample or all | Sample or all |
| Rows per table in a sample | — | 1,000 | 10,000 | 10,000 | 100,000 |
| Largest database copied in full | — | — | 5 GB | 20 GB | 100 GB |
| Preview environment per pull request | — | Yes | Yes | Yes | Yes |

On Pro, Ultra and Team you can add up to 10 more with the **Extra environment** add-on ($5 per month each) under **Billing**. An environment's disk counts towards database storage like production's.

## Common mistakes

- Expecting staging to have production's rows. By default it starts with none; choose a sample when you create it, or seed it with test data.
- Pointing a test app at production's URL. Use the environment's URL from **Connect**.
- Writing migrations without down SQL — then they can't be undone from [Version history: see, undo and restore changes](https://sovaria.in/docs/version-history).
