> ## Documentation Index
> Fetch the complete documentation index at: https://gproxy.leenhawk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Upgrading from v3 to v4

> Back up a v3 deployment, start v4 on the same database, verify the conversion, and roll back if needed.

These notes are for administrators moving a v3 deployment to v4. v4 recognizes a v3 database when it starts and converts it in place, on SQLite, PostgreSQL and MySQL. You keep the same data directory or connection string and the same `GPROXY_MASTER_KEY`; there is nothing to export and no password to reset.

## What changes

The database schema is replaced. v4 reads the v3 tables and writes v4 tables in their place before it serves any traffic. What happens to each kind of data:

* Providers and credentials: known channel names and their configuration are translated, and secrets are re-sealed under the configured master key.
* Routes and members: a route's public model name becomes the route name. Each additional name becomes a separate route with a copy of the members.
* Users, passwords, API keys, organizations and teams: carried over. Password hashes (Argon2) and API key digests are preserved, so existing usernames, passwords and keys keep working.
* Prices and budgets: converted to v4's units. v4 stores amounts to 9 decimal places; a value with more digits is rounded half to even and the change is listed in the report.
* Permissions, rate limits and rewrite rules: supported forms are migrated. Changed meanings and forms that cannot be mapped are listed in the report.
* Team and organization permissions: expanded into one user rule per imported member, disabled users included. The rules reflect membership at the time of the upgrade and do not follow later membership changes. A group with no imported members is reported as skipped. Instances upgraded by a release that did not yet expand these permissions lost them, and a later release does not restore them.
* Usage history: imported in bounded batches with the original token counts, attribution and settled costs. Nothing is repriced and no quota is settled again. v3 fields without a v4 column are kept in `metrics.v3`.
* Captures, request logs, sessions and audit history: not converted. They remain in the v3 backup. Everyone has to sign in to the console again.

A provider whose channel or configuration cannot be translated is left out together with the rows that depend on it, and the report says so. This applies only to translation: a database read error or a wrong master key still stops the upgrade.

Your v3 launch settings keep working. v4 still accepts `--persistence db` and a `sqlite://` DSN. The Docker image has the same layout as v3 (data in `/app/data`, user `65532:65532`), but v3 image tags run v3; switch to a v4 tag.

## Before you start

1. Find where your `GPROXY_MASTER_KEY` is set (a `.env` file, the service unit, the container environment) and keep a copy. v4 needs the same key to open the credentials; with a different key the upgrade stops before changing anything.
2. Keep a copy of the v3 executable or note the v3 image tag. You need it to roll back.
3. Plan to stop every v3 instance. Only one process may write to the database during the upgrade. If you run v3 and v4 side by side on separate databases, do not give them the same OAuth credentials: each refreshes the tokens and invalidates the other's.
4. For PostgreSQL or MySQL on the CLI, make sure your v4 build includes the driver for that database.

Then back up the database with v3 stopped:

> [tab] SQLite
>
> Copy the whole data directory. It holds `gproxy.db` and, if present, `gproxy.db-wal` and `gproxy.db-shm`; the WAL file can hold committed data that is not yet in `gproxy.db`, so copy all three together.
>
> ```sh theme={null}
> cp -a data data.v3-backup
> ```
>
> v4 also writes its own backup next to the database during the upgrade (see below), so leave free space for two more copies of the database: the backup and the converted copy.

> [tab] PostgreSQL
>
> ```sh theme={null}
> pg_dump --format=custom --file=gproxy-v3.dump "$GPROXY_DSN"
> ```

> [tab] MySQL
>
> ```sh theme={null}
> mysqldump --single-transaction --databases gproxy > gproxy-v3.sql
> ```

## Upgrading

1. Stop all v3 instances.
2. Install the v4 executable in place of v3, or change the container image to a v4 tag.
3. Start v4 with the same data directory or connection string and the same `GPROXY_MASTER_KEY`. You can also run the conversion as its own step with `gproxy migrate`, which takes the same path and exits.
4. Watch the log. On SQLite you should see `migrating v3 database to v4`, a `migrated` line per table, `v3 import complete` with row, dropped and warning counts, and finally `v3 migration complete` with the path of the backup. Anything that changed meaning is printed under `Changed on the way in`, and rows with no v4 form are printed under `NOT migrated`.
5. When the log shows the server listening, sign in to the console.

## How the conversion works

### SQLite

Before it opens the HTTP port, v4 takes a consistent snapshot of the v3 database with `VACUUM INTO`, which includes committed WAL contents, and keeps it beside the database as `gproxy.db.v3-*.bak`. It converts that snapshot into a new database in a temporary directory beside the original, decrypting credentials with the existing master key. Only when the import has succeeded does it rename the new database over the original path.

If the conversion fails, the original file is left as it was: fix the cause the log names (for example, restore the original master key) and start again. If the process is interrupted before the rename, the path still holds the v3 database and the next start tries again. After the rename, the path holds a v4 database whose migration ledger marks it as current, and later starts are ordinary v4 starts. Nothing is imported twice.

### PostgreSQL and MySQL

v4 first reads the v3 data and checks it, including that the master key opens the credentials. Only then does it rename every v3 table to `gproxy_v3_<original name>` and create the v4 tables under the original names. Configuration, identity and usage history are converted from the renamed tables, and the instance opens to traffic only after the last step.

Progress is kept in the table `gproxy_v3_upgrade`. Its `report_json` column holds the skipped rows and the warnings about changed meaning. Identity and usage are written in batches with a checkpoint after each, so an interrupted upgrade resumes where it stopped on the next start.

Each backend keeps a second process out in its own way:

* PostgreSQL holds a session advisory lock for the whole upgrade and renames the v3 tables in one transaction.
* MySQL holds a session lock (`GET_LOCK`) across DDL statements, which commit implicitly, and renames all v3 tables in one `RENAME TABLE` statement.

A second PostgreSQL or MySQL instance that finds the lock taken stops with `another instance is migrating this database; retry after it finishes`.

After a failure, the v3 data is still in the archive tables and the v4 tables may already hold part of the import. Fix the reported error and restart; the upgrade continues. Do not delete `gproxy_v3_upgrade` or rename the archive tables while the upgrade is incomplete.

## Verifying the upgrade

1. Read the conversion report: on SQLite in the startup log, on the other backends in `gproxy_v3_upgrade.report_json`. Check the skipped providers and the permission changes in particular.
2. Run `gproxy migrate --status`. It lists each schema migration of this build and whether the database has it. It only reads; it never performs the v3 conversion.
3. In the console, check that providers, credentials, routes, users and API keys are present, and that usage history shows the old totals.
4. Send a request through an existing API key, as in [Your first request](https://gproxy.leenhawk.com/getting-started/first-request.md).

Once you are satisfied, you can delete `gproxy.db.v3-*.bak` (SQLite) or drop the `gproxy_v3_<name>` archive tables (other backends). Keep `gproxy_v3_upgrade` if you want to keep the report. From here on, v4 schema changes are recorded in SeaORM's `seaql_migrations` table.

## Rolling back

v3 cannot read a v4 database, and a partially converted database is neither. Rolling back always means restoring the v3 data. Anything written after the upgrade (new usage, configuration changes) is lost.

> [tab] SQLite
>
> 1. Stop v4.
> 2. Move `gproxy.db` and any `gproxy.db-wal` and `gproxy.db-shm` aside.
> 3. Copy `gproxy.db.v3-*.bak` (or `gproxy.db` from your own backup) to `gproxy.db`.
> 4. Reinstall the v3 executable or image tag and start it with the original settings and master key.

> [tab] PostgreSQL and MySQL
>
> 1. Stop all instances.
> 2. Restore the backup you took before the upgrade into an empty database.
> 3. Start v3 against the restored database with the original master key.
>
> Do not point v3 at a database that is partly or fully converted.

## Importing into a separate instance

The upgrade above converts a database in place. To bring a v3 database or export into a different v4 instance instead, use `import --from-v3`:

```sh theme={null}
gproxy --data-dir ./other-instance migrate
gproxy --data-dir ./other-instance import --from-v3 /path/to/v3-backup.db
```

The destination must be empty, or hold only an unfinished earlier run of the same import. If the source's secrets are sealed, pass the v3 master key with `--source-master-key` or `GPROXY_IMPORT_SOURCE_MASTER_KEY`. `--skip-unmappable-providers` leaves out providers that cannot be translated, as the automatic upgrade does; without it, such a provider stops the import.

The input can also be the JSON document from v3's `POST /admin/api/export`, taken with `{"include_secrets": true}`. Such an export lacks password hashes, permissions, rate limits and provider models, which a SQLite database carries. When importing an export without hashes, set a console password with `--admin-password`; a SQLite import does not need it.


## Related topics

- [Upgrading from v2 to v4](https://gproxy.leenhawk.com/deployment/v2-to-v4.md)
- [Upgrading from v2 to v3](https://gproxy.leenhawk.com/deployment/v2-to-v3.md)
- [Architecture](https://gproxy.leenhawk.com/development/architecture.md)
- [Embedding the SDK](https://gproxy.leenhawk.com/development/embedding.md)
- [Concepts](https://gproxy.leenhawk.com/introduction/concepts.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.