Skip to content

Database Migrations

The platform uses Alembic for PostgreSQL schema management. Each migration is a versioned script, with an upgrade() and a downgrade() function.


Overview

Migration files live in backend/app/db/migrations/versions/. Each file represents one atomic schema change, identified by a unique revision ID. Alembic tracks which migrations have been applied in the alembic_version table in your database.

Two branches, two heads. A full-profile checkout also has modules/backend/app/db/migrations/versions/, a separate branch labelled modules for the schema the optional modules own. A core checkout has no modules/ directory and therefore one head; a full checkout has two, and alembic_version holds one row per head. That is why every command below says heads (plural) and never head: alembic refuses the singular when more than one head exists.

Where the commands run. Every alembic and bootstrap command on this page runs inside the API container, with the image's own alembic and the container's POSTGRES_* settings: docker compose exec api ..., from the directory of your compose file (the repository root for the Quick Start stack, deploy/compose/ for the production compose file). The image keeps the repository's layout under its working directory /app, so -c backend/app/db/alembic.ini is the same path there as in a checkout. Under Helm, run the same command in an API pod with kubectl exec. On AWS, see Running Migrations in Production. Running alembic from a checkout instead is a contributor's task: Database Migrations with Alembic. CI runs this page's exec blocks as written against the Quick Start stack.


Running All Migrations

The API container runs the bootstrap (below) every time it starts, so a stack that is up is already at its heads. To apply all pending migrations by hand:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini upgrade heads

heads refers to the latest migration of every branch. This command applies all unapplied migrations in order. Alembic logs what it applies (to stderr); on a database already at its heads it applies nothing.

On a database with no tables at all, run the bootstrap instead — the historical migration chain cannot be replayed from zero, so a fresh schema is created from the models and stamped. On a database that has tables it runs upgrade heads:

docker compose exec api python -m backend.app.db.bootstrap

It ends with Database bootstrap complete (created) on a new database and Database bootstrap complete (upgraded) on one that had tables.

The bootstrap reads ENVIRONMENT from the container, which compose sets (development unless ENVIRONMENT was set when the stack was started); for another value, pass it to that one command: docker compose exec -e ENVIRONMENT=production api python -m backend.app.db.bootstrap. ENVIRONMENT is the environment the database belongs to (production, staging, or development for a local trial). On a database with no users the bootstrap creates the first administrator from FIRST_SUPERUSER and FIRST_SUPERUSER_PASSWORD, and it refuses a password that is empty, shorter than 8 characters or a well-known default unless ENVIRONMENT is development or test. In every environment it also refuses a password longer than 72 bytes when encoded as UTF-8 (a character outside ASCII takes two to four bytes). Once the database has users the setting is not read.

The bootstrap is also what the API container runs on start-up when RUN_MIGRATIONS=true, the image's default, and it is what makes a profile switch work; see "Switching profile" below. The AWS (CDK) deployment sets RUN_MIGRATIONS=false on its API tasks: there, the Deploy workflow's migration task is the only thing that runs the bootstrap (see On AWS, the API does not migrate).


Viewing Migration History

Show every migration the image carries, newest first:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini history

Output, for example:

t2012_flag_split -> t1934_metric_window (head), Metric window: a nullable conversion window, in hours, on each metric
t2011_flag_environments -> t2012_flag_split, Flag split: a nullable split on each feature flag and each flag environment setting
t449_identity_aliases -> t2011_flag_environments, Flag environments: each flag's staging and development settings, and each key's environment
t1812_user_totp -> t449_identity_aliases, Identity aliases: the table that links an anonymous id to a known id
4955571d232c -> t1812_user_totp, Two-step sign-in: each local account's authenticator secret and recovery codes
c448_balance_within -> 4955571d232c, Visual experiments: page changes, pages and goals, and the editor's tokens
7b6b4b4fe246 -> c448_balance_within, Experiments: balance the split within groups as users arrive
607c51a59bf5 -> 85dd4796bb8b, Events: an event_id is stored once per user, experiment, flag and event name
4c2c09a2b3e8 -> 607c51a59bf5, Feature flags: a value type and two values, held by one check
...
<base> -> 84a772608a6e, Initial schema

(head) marks the newest revision of each branch in the image; a full image also lists the modules branch, labelled (modules). history lists the image's revisions whether or not the database has them: current (next) says what the database has.


Checking Current State

Show which migration is currently applied to the database:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini current

Output, for a full install (a core install prints the first line only):

t1934_metric_window (head)
modules_0006_saml_requests (head)

One line per head: the revision ID of the last migration applied on each branch, and (head) when it is the newest the image has.


Creating a New Migration

A migration is written in a checkout and ships in a release; it is not created against a deployment. See Creating New Migrations.


Applying a Specific Migration

Apply migrations up to a specific revision (not necessarily the latest), named by its ID from history:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini upgrade <revision>

Name the revision rather than a relative step such as upgrade +1. A relative step counts along one line of revisions, and with two heads alembic refuses it: Relative revision +1 didn't produce 1 migrations (measured from a full checkout against a database at the core head, with the modules branch still to apply).


Rolling Back

Roll back the most recent migration of one branch. With two heads a bare -1 is ambiguous — alembic warns and picks one, which may not be the branch you meant — so name the branch:

The modules branch has six revisions: modules_0001_rbac, then modules_0002_warehouse_analysis, then modules_0003_lower_is_better, then modules_0004_rbac_activation, then modules_0005_redshift, then modules_0006_saml_requests. Name the modules revision to end at rather than counting back: a relative step such as modules@-2 counts from the newest revision the image has, so it unapplies a different revision once a release adds one.

The modules branch, back to a named revision:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade modules_0004_rbac_activation
docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade modules_0003_lower_is_better
docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade modules_0002_warehouse_analysis
docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade modules_0001_rbac

downgrade modules_0005_redshift unapplies modules_0006_saml_requests only, which drops the table sso_saml_requests, the SAML sign-ins in progress, and changes no other table. downgrade modules_0004_rbac_activation also unapplies modules_0005_redshift, which puts back the previous release's constraints on warehouse_connections and changes no row; it refuses while any Amazon Redshift connection is stored. downgrade modules_0003_lower_is_better also unapplies modules_0004_rbac_activation, which drops the column activated_at from user_custom_roles and direct_permission_grants, with any activation time stored in it. downgrade modules_0002_warehouse_analysis also unapplies modules_0003_lower_is_better, which drops the column warehouse_sources.lower_is_better, every metric source's lower-is-better setting. downgrade modules_0001_rbac also unapplies modules_0002_warehouse_analysis, which drops the three warehouse tables it created, with their rows. modules@-6 unapplies all six, newest first.

One step back on the core chain:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade <core revision id>

Not modules@base. modules_0001_rbac is a child of the core revision a7b8c9d0e1f2, not an alembic base, and with a single tree root alembic cannot filter a downgrade by branch label: downgrade modules@base resolves to the whole core chain to base, and drops every table in the schema.

modules_0001_rbac and modules_0002_warehouse_analysis downgrade only the objects their own upgrade() created (each marks them with a PostgreSQL COMMENT as it goes). On a database whose tables came from the bootstrap rather than from those migrations, their downgrade is a no-op — it will not drop populated tables it never made. modules_0003_lower_is_better drops its column on every database, however the column was made. modules_0004_rbac_activation drops its two columns on every database in the same way, modules_0005_redshift puts back its three constraints on every database, and modules_0006_saml_requests drops its table on every database.

modules_0002_warehouse_analysis removes saved warehouse connections

This revision replaces the warehouse_connections table with a new one and adds warehouse_sources and warehouse_analysis_runs. Every row of the earlier warehouse_connections is deleted — connections saved through the earlier warehouse endpoints — and nothing is copied into the new table.

Downgrading it cannot bring those rows back: downgrade modules_0001_rbac first unapplies modules_0006_saml_requests, which drops the table of SAML sign-ins in progress, modules_0005_redshift, which refuses while any Amazon Redshift connection is stored, modules_0004_rbac_activation, which drops the activation-time column of custom role assignments and direct grants, with any activation time stored in it, and modules_0003_lower_is_better, which drops each metric source's lower-is-better setting, then drops the three tables this revision created and puts back an empty table of the earlier shape. If you need the rows, take a snapshot of the database before upgrading to the release that carries this revision, and restore that snapshot to go back. The migration logs how many rows it removed:

modules_0002: removing 3 legacy warehouse connection rows

modules_0003_lower_is_better gives each warehouse metric source a direction

This revision adds the column warehouse_sources.lower_is_better (boolean, NOT NULL, default false), and every source already stored gets false.

The previous release runs against this schema unchanged: the sources it creates get false from the column's default.

Its downgrade drops the column on every database, however the column was made, and with it every metric source's setting; an upgrade afterwards sets each source back to false, not to what was stored.

Until someone marks them again, those metrics are analysed as higher is better. Before downgrading, note which metric sources are marked: the dashboard's Warehouse › Metric sources list tags each one "lower is better". The compliance audit trail keeps a record too, and a downgrade leaves it in place: each source's latest entry (resource_type=warehouse_source) carries its definition as last saved, lower_is_better included.

modules_0004_rbac_activation adds an activation time to custom role assignments and direct grants

This revision adds a nullable activated_at column to user_custom_roles and direct_permission_grants. It sets no default and fills in nothing, so every assignment and grant already stored has none. It logs them, so you have a record of what existed when it ran: one WARNING that counts them, then one line per row (at most 200, then … and N more), or one INFO line when there are none.

modules_0004_rbac_activation: 2 custom role assignments and 1 direct permission grant have no activation time (activated_at is empty; expired grants and grants with is_active false are not counted or listed). See docs/self-hosting/migrations.md, "modules_0004_rbac_activation":
assignment user_id=<uuid> role_name=data-scientist
grant user_id=<uuid> resource=experiment actions=["update"] expires_at=<iso or none>

Read the log where the 1ab99332f0ba section below says (Docker Compose, Helm, AWS). Downgrading this revision (to modules_0003_lower_is_better) drops both columns, and any activation time stored in them; upgrading afterwards adds them empty.

Since the release in which custom roles took effect, assigning a role or making a grant records an activation time, and only assignments and grants that have one take effect (see RBAC). Downgrading below this revision after that release drops every activation time: every assignment and grant is inactive again, and stays inactive after upgrading; assign the roles and make the grants again.

modules_0005_redshift lets a warehouse connection be an Amazon Redshift connection

This revision changes three constraints of warehouse_connections and no column or row, so that a connection may be an Amazon Redshift one (warehouse_type redshift). Like an Amazon Athena connection, it stores no secret and has the external ID generated when it is saved; unlike one, it has no per-query byte cap. Every connection already stored is kept as it is, and the previous release keeps saving BigQuery, Snowflake and Athena connections against the upgraded schema.

Downgrading this revision (to modules_0004_rbac_activation) puts back the previous release's constraints and changes no row. It refuses while any Amazon Redshift connection is stored, names them, and changes nothing:

modules_0005_redshift: refusing to downgrade: 1 warehouse connection(s) are Amazon Redshift connections (<id>). The previous release's warehouse_connections constraints refuse that type. Delete those connections, or restore the backup taken before the upgrade to this release. Nothing was changed.

modules_0006_saml_requests adds the table for SAML sign-ins in progress

This revision adds the table sso_saml_requests and its index, and changes no other table. Nothing reads or writes the table yet. It has no foreign key, so creating it locks no existing table, and it bounds its one wait at 0.25 s.

Downgrading this revision (to modules_0005_redshift) drops the table on every database, with any sign-in in progress, which must then be started again; no other table changes, and the previous release starts on the downgraded database. An upgrade afterwards creates the table empty.

1ab99332f0ba rewrites stored event times to UTC

events.created_at is stored as text, so time windows, ordering and "the earliest conversion" over events compare text. Events stored before this release kept the time exactly as the client sent it: with its own offset (2026-10-01T01:00:00+02:00), with Z, or with no offset at all. As text, those sort among each other by the digits, not by the moment, so an event could fall outside a time window it belongs in, and the wrong event could count as a user's first conversion. Events written by this release are already stored in one form, in UTC.

This revision rewrites every stored value that is not in that form to the same instant in it: 2026-10-01T01:00:00+02:00 becomes 2026-09-30T23:00:00+00:00 (.ffffff is kept when there are microseconds). A value with no offset is read as UTC, as the API reads it, and gets +00:00. Values already in the form are not touched, and no other column changes. A value that cannot be read as a time (an empty string, text that is not a date, or a date out of range) is left exactly as stored and counted.

What readers of results see: a converting user's first-conversion day in the daily results can move by one day. Totals, rates and significance do not change.

The downgrade cannot bring back the offsets clients sent. Each row keeps its instant; the offset it arrived with is not recorded anywhere. If you need the original text, take a snapshot of the database before upgrading, and restore that snapshot to go back. A previous release runs against the rewritten rows unchanged: the column type is the same and the values are in the form it writes for its own timestamps.

The migration prints one line when it finishes. It names event ids, never the stored values:

1ab99332f0ba: rewrote 1520 events.created_at values to UTC; 2 unreadable values left as stored (event ids: 0b5c6c2e-6f0e-4a1b-9c55-2f4f1f0f3a10, 7d1e2f30-1a2b-4c3d-8e9f-0a1b2c3d4e5f); 4.3 s

Before upgrading, under Docker Compose or Helm: how many rows will it rewrite? Run this read-only count with psql (see Resolve each group for how to open it). It is the migration's own selection, copied exactly. The statements here use the schema experimentation; if your POSTGRES_SCHEMA is different, replace it.

-- Count the event times the upgrade will rewrite
SELECT count(*) FROM experimentation.events
 WHERE created_at !~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]{6})?\+00:00$'
   OR created_at LIKE '%.000000+00:00';

If it returns 0, the migration changes nothing. Otherwise it scans the table once and rewrites the rows it counted. How long that takes depends on your database: the first measure of it is your own staging run's log line.

After upgrading, under Docker Compose or Helm: this lists what is left. Expect exactly the unreadable values the log line counted, which the migration leaves as they were:

-- List the event times left after the upgrade
SELECT id FROM experimentation.events
 WHERE created_at !~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]{6})?\+00:00$'
   OR created_at LIKE '%.000000+00:00';

A long run is not a failed run. Do not kill it. The rewrite commits batch by batch, so a run that is stopped keeps every batch it finished, and the next upgrade finishes the rest. Killing it only makes you wait twice.

  • Docker Compose. The API container runs the migration on start and serves once it has finished. If the migration runs longer than about 10 minutes, docker compose up reports the API unhealthy and fails while the migration is still running in the container. Do not restart it: wait for the line above in docker compose logs api.
  • Helm. The migrate init container runs it while the old pods keep serving. helm upgrade --wait --timeout 10m can report a failure while the init container is still running. Do not delete the pod: wait for the line in kubectl logs <pod> -c migrate.
  • AWS. The Deploy workflow's migration task runs it before traffic shifts, and the previous release keeps serving meanwhile. The AWS deployment gives you no psql, so use the migration's log line instead of the queries above, and read the staging deploy's line (rewritten, unreadable, seconds) before you deploy production. The Deploy workflow waits 30 minutes for the migration, and no setting changes that for one deploy. A longer run fails the deploy with Migration still running, and nothing shifts. Do not start another deploy yet: wait until the task named in that error has stopped (the error names its log group and stream; the ECS console shows the task's status as STOPPED), then run the Deploy workflow again for the same tag. Its migration finishes the rest.

Events written while the upgrade ran. Under Helm and on AWS the previous release keeps serving, and keeps storing times as clients send them, until the new release takes all the traffic. Rows it writes after the migration's scan stay as they were. Once the new release serves all traffic, run the migration again: downgrade to the revision before it and upgrade. The second log line says how many values it rewrote.

This re-run applies only while alembic current shows 1ab99332f0ba as the core head (with modules_0002_warehouse_analysis on a full install). Then the downgrade changes no data; it only moves the version row back. Complete it before you upgrade to the release that adds 806901fb7735, the experiments' stored correction settings. Past that release the recipe is not offered: downgrade a89544fb1075 would also run 806901fb7735's downgrade, which drops every experiment's stored correction method and confidence level, and the upgrade heads after it puts back the defaults (Benjamini-Hochberg, 0.95), not the choices that were stored.

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade a89544fb1075
docker compose exec api python -m alembic -c backend/app/db/alembic.ini upgrade heads

On AWS these are two runs of the Database Migration workflow: direction downgrade with target a89544fb1075, then direction upgrade with target heads.

Name a89544fb1075; do not use -1. On a full install downgrade -1 can step back the modules branch instead, and that downgrade drops the warehouse tables modules_0002_warehouse_analysis created. The Database Migration workflow refuses -1.

Past d29a479daafe that downgrade would also drop the table holdout_population, with every row in it, and three global_holdouts columns (see the next section).

Past 37dcb2969766 it also runs that revision's downgrade, which drops the table segment_members (the members of every id-list segment) and the column segments.kind. So it now stops while any id list has members: that downgrade refuses, prints how many rows segment_members holds, and changes nothing, because nothing could put the members back and every id-list segment would return as a rules segment with no rules (see 37dcb2969766).

Past 607c51a59bf5 it also runs that revision's downgrade, which drops feature_flags.value_type, on_value and off_value. So it also stops while any flag is a string, number or json flag: that downgrade refuses, names those flags, and changes nothing (see 607c51a59bf5).

Past 85dd4796bb8b it also drops the table event_dedup_keys, and with it every stored event_id (see 85dd4796bb8b).

Past 7b6b4b4fe246 it also runs that revision's downgrade, which drops rollout_stages.started_at, the recorded start of every rollout stage (see 7b6b4b4fe246).

Past c448_balance_within it also runs that revision's downgrade, which drops every experiment's balance setting, every user's group and every counter (see c448_balance_within).

Past 4955571d232c it also runs that revision's downgrade, which drops the table visual_tokens and the columns experiments.visual_config and variants.visual_changes: every visual experiment's pages, goals and page changes (see 4955571d232c).

Past t1812_user_totp it also runs that revision's downgrade, which drops the table user_totp: every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp).

Past t449_identity_aliases it also runs that revision's downgrade, which drops the table identity_aliases: every link between an anonymous id and a known id (see t449_identity_aliases).

Past t2011_flag_environments it also runs that revision's downgrade, which deactivates every staging and development API key and drops the column api_keys.environment and the table flag_environment_settings: every staging and development flag setting (see t2011_flag_environments).

Past t2012_flag_split it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split. So it also stops while any flag has a split or a rule value: that downgrade refuses, names those flags, and changes nothing (see t2012_flag_split).

Past t1934_metric_window it first runs that revision's downgrade, which drops metrics.conversion_window_hours: every metric's conversion window (see t1934_metric_window).

d29a479daafe records who each global holdout covers

This revision adds activated_at, deactivated_at and hash_salt to global_holdouts, a partial unique index that allows at most one active holdout, and the table holdout_population, which records each user first seen while a holdout is active and whether the holdout kept them out.

It deactivates all but one active holdout. Before the index is created, every active holdout except the most recently updated one is deactivated and stamped as ended; an ended holdout cannot be activated again. The kept row may not be the one the previous release was enforcing. The migration prints the ids it deactivated:

d29a479daafe: deactivated 1 global holdout(s) to keep one active: 7d1e2f30-1a2b-4c3d-8e9f-0a1b2c3d4e5f

Before upgrading, this read-only query lists the active holdouts; with more than one row, the first is the one kept:

-- List the active holdouts, the kept one first
SELECT id, name, is_active, updated_at FROM experimentation.global_holdouts
 WHERE is_active ORDER BY updated_at DESC, id;

The holdout active at the upgrade is never measurable. It keeps the salt every holdout used before (so users stay in or out of it exactly as before), and who it kept out before the upgrade was never recorded. To measure, deactivate it and create a new holdout.

The downgrade deletes the recorded memberships. d29a479daafe's downgrade drops holdout_population with all its rows, the index, and the three columns; nothing can rebuild the table. The deactivations are not reverted. Rolling back across this revision while a holdout is active also invalidates that holdout: the older release buckets users with the old salt and records nobody, so after upgrading again deactivate it and create a new one.

37dcb2969766 adds segments made from a list of user ids

This revision adds segments.kind (rules or id_list, default rules, with the check ck_segments_kind) and the table segment_members, one row per user id in an id-list segment. Every segment already stored becomes a rules segment, which is what it was; no row is changed.

The downgrade deletes every id list's members. 37dcb2969766's downgrade drops segment_members with all its rows, the check and segments.kind; nothing can rebuild the table, and an id-list segment comes back from a later upgrade as a rules segment with no rules. So while segment_members has any row the downgrade refuses, names the row count, and changes nothing:

37dcb2969766: refusing to downgrade: segment_members holds 3 row(s), ...

To go on, either remove the members first (POST /api/v1/segments/{id}/members/remove), or, once someone has agreed to lose them, run the same downgrade with the override:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini -x allow_member_loss=true downgrade d29a479daafe

The Database Migration workflow does not pass -x, so on AWS it stops at this revision while any id list has members.

From a release that carries 607c51a59bf5, the same downgrade first runs that revision's downgrade, which drops each flag's value type and values and refuses while any flag is a string, number or json flag (see 607c51a59bf5). From a release that carries 85dd4796bb8b, it also drops the table event_dedup_keys before that. From a release that carries 7b6b4b4fe246, it also runs that revision's downgrade, which drops rollout_stages.started_at, the recorded start of every rollout stage (see 7b6b4b4fe246). From a release that carries c448_balance_within, it also runs that revision's downgrade, which drops every experiment's balance setting, every user's group and every counter (see c448_balance_within). From a release that carries 4955571d232c, it also runs that revision's downgrade, which drops the table visual_tokens and the columns experiments.visual_config and variants.visual_changes (see 4955571d232c). From a release that carries t1812_user_totp, it also runs that revision's downgrade, which drops the table user_totp, every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp). From a release that carries t449_identity_aliases, it also runs that revision's downgrade, which drops the table identity_aliases, every link between an anonymous id and a known id (see t449_identity_aliases). From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

4c2c09a2b3e8 keeps rollback records without a safety configuration

This revision makes safety_rollback_records.safety_config_id nullable and changes its foreign key to feature_flag_safety_configs from ON DELETE CASCADE to ON DELETE SET NULL. It changes no row.

From this release a rollback of a flag that has no safety configuration of its own leaves it with none, so the flag stays on the global default thresholds, and the rollback record's safety_config_id is null. Earlier releases gave such a flag an empty configuration of its own at the rollback, with no thresholds, so the monitor stopped checking it against the defaults. Those configurations stay: this release does not remove them (#1206).

Deleting a safety configuration keeps the flag's rollback records: their safety_config_id becomes null. Before this revision the delete took them with it.

The downgrade changes nothing. 4c2c09a2b3e8's downgrade moves alembic_version back and leaves the column nullable and the key ON DELETE SET NULL. The rollback runbook undoes a migration while the release that contains it still serves, before the API is rolled back (Database Rollback Procedure), and this release records a null safety_config_id for each rollback of a flag on the defaults, which NOT NULL would refuse.

607c51a59bf5 gives each flag a value type and two values

This revision adds three columns to feature_flags: value_type (boolean, string, number or json, default boolean), and on_value and off_value (jsonb), the values a string, number or json flag serves to the users it is on for and to everyone else. It also adds the check check_flag_values: a boolean flag stores neither value, a string or number flag stores two different values of its type, and a json flag stores two different objects or arrays. Every flag already stored becomes a boolean flag, which is what it was.

The previous release runs against this schema unchanged: the flags it creates get boolean from the column's default, and it reads and updates flags without touching the two values.

The downgrade refuses while any flag is a string, number or json flag. 607c51a59bf5's downgrade drops the check and the three columns, which would delete each such flag's value type and both of its values, and nothing could rebuild them. So while any flag's value_type is not boolean the downgrade refuses, names those flags, and changes nothing:

607c51a59bf5: refusing to downgrade: 2 feature flag(s) serve a string, number or json value (checkout-copy, price-tier). ...

To go on, delete those flags or convert each to a boolean flag, or restore the backup taken before the upgrade. With only boolean flags stored, the downgrade drops the check and the three columns, and nothing else. Neither the upgrade nor the downgrade can be emitted as SQL (--sql).

From a release that carries 7b6b4b4fe246, a downgrade to 4c2c09a2b3e8 also runs that revision's downgrade, which drops rollout_stages.started_at, the recorded start of every rollout stage (see 7b6b4b4fe246). From a release that carries c448_balance_within, it also runs that revision's downgrade, which drops every experiment's balance setting, every user's group and every counter (see c448_balance_within). From a release that carries 4955571d232c, it also runs that revision's downgrade, which drops the table visual_tokens and the columns experiments.visual_config and variants.visual_changes (see 4955571d232c). From a release that carries t1812_user_totp, it also runs that revision's downgrade, which drops the table user_totp, every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp). From a release that carries t449_identity_aliases, it also runs that revision's downgrade, which drops the table identity_aliases, every link between an anonymous id and a known id (see t449_identity_aliases). From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

85dd4796bb8b stores an event sent again with its event_id once

This revision adds the table event_dedup_keys: one row per event stored with an event_id, holding the user id and a key made from the event's experiment, flag, event name and event_id (How events count). It changes nothing in events, and it has no foreign key to events, so it takes no lock on events and completes while the previous release keeps writing events.

The previous release runs against this schema unchanged: it never reads or writes the table, so an event it stores during the deploy is stored as every event was before, even when it carries an event_id.

The downgrade drops the table and every key in it. No event is changed or removed. Until the API is rolled back too, this release answers 500 to an event that carries an event_id, because the table is gone, and stores every other event as before; so roll the API back straight after the downgrade, as the rollback runbook does.

7b6b4b4fe246 records when each rollout stage started

This revision adds rollout_stages.started_at, the time the scheduler or POST /api/v1/rollout-schedules/stages/{stage_id}/advance started the stage. It is empty until a stage starts. The upgrade fills it for every stage in progress with the time the scheduler measured from before, so no stage's completion time moves; a stage that was pending, or that finished before the upgrade, stays empty.

The previous release runs against this schema unchanged: it never reads or writes the column. A stage it starts while a deploy is rolling has an empty started_at; this release then measures from the stage's updated_at, as before, so an edit to that stage restarts its clock until it completes. To list them:

-- Stages in progress with no recorded start
SELECT id FROM experimentation.rollout_stages
 WHERE status = 'IN_PROGRESS' AND started_at IS NULL;

The downgrade drops every recorded start. 7b6b4b4fe246's downgrade drops rollout_stages.started_at; it does not refuse, and nothing can restore the exact values. An upgrade afterwards sets each stage in progress from its updated_at, which an edit may have moved, so a stage edited in the meantime restarts its clock from the edit. Where the API migrates on start (Docker Compose, and the Helm chart's init container), the previous release refuses to start against this revision. To go back to it, first downgrade to the previous release's heads, with this release still running (An Older Image Against a Newer Database), and with the Engineering Lead's approval at the moment it runs when any stage is IN_PROGRESS. This revision's own down_revision is 85dd4796bb8b; from a release that carries c448_balance_within, this also runs that revision's downgrade, which drops every experiment's balance setting, every user's group and every counter (see c448_balance_within):

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade 85dd4796bb8b

From a release that carries 4955571d232c, it also runs that revision's downgrade, which drops the table visual_tokens and the columns experiments.visual_config and variants.visual_changes (see 4955571d232c). From a release that carries t1812_user_totp, it also runs that revision's downgrade, which drops the table user_totp, every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp). From a release that carries t449_identity_aliases, it also runs that revision's downgrade, which drops the table identity_aliases, every link between an anonymous id and a known id (see t449_identity_aliases). From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

On Helm, run the same command in an API pod with kubectl exec. Once the database is at the previous release's heads, start the previous release. The upgrade cannot be emitted as SQL (--sql).

c448_balance_within balances the split within groups

This revision adds what an experiment needs to balance its split within groups: experiments.balance_within, the setting, and experiments.balance_seed, the key of the experiment's block order, which the server makes and never answers; assignments.balance_group and assignments.balance_seq, the group each user was balanced in and their place in it; and the table balance_group_counters, each group's next place. Every column is nullable with no default, nothing is backfilled, and every experiment stored before the upgrade balances nothing.

The previous release runs against this schema unchanged: it never reads or writes the new columns or the table. While it serves, during a rolling deploy or for as long as a rollback to it lasts, it assigns every new user of a balanced experiment by the hash and stores no group, so those users stay outside the blocks for good. To count them:

-- Users of balanced experiments assigned without a group
SELECT a.experiment_id, count(*) FROM experimentation.assignments a
  JOIN experimentation.experiments e ON e.id = a.experiment_id
 WHERE e.balance_within IS NOT NULL AND a.balance_group IS NULL
 GROUP BY a.experiment_id;

The downgrade drops every balance setting. c448_balance_within adds the balance setting, each user's group and place in it, and the per-group counters. Its downgrade drops every experiment's balance setting, every user's group and every counter; they cannot be recovered. Run it only with a human go at that moment. It does not refuse, and it removes no assignment: every user keeps their variant. An upgrade afterwards starts every experiment with no balance setting. Where the API migrates on start (Docker Compose, and the Helm chart's init container), the previous release refuses to start against this revision. To go back to it, first downgrade to the previous release's heads, with this release still running (An Older Image Against a Newer Database). This revision's own down_revision is 7b6b4b4fe246; from a release that carries no later revision, this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade 7b6b4b4fe246

From a release that carries 4955571d232c, it also runs that revision's downgrade, which drops the table visual_tokens and the columns experiments.visual_config and variants.visual_changes (see 4955571d232c). From a release that carries t1812_user_totp, it also runs that revision's downgrade, which drops the table user_totp, every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp). From a release that carries t449_identity_aliases, it also runs that revision's downgrade, which drops the table identity_aliases, every link between an anonymous id and a known id (see t449_identity_aliases). From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

On Helm, run the same command in an API pod with kubectl exec. The upgrade cannot be emitted as SQL (--sql).

4955571d232c stores visual experiments

This revision adds what a visual experiment stores: experiments.visual_config, its pages, the page its editor opens and its goals; variants.visual_changes, a variant's page changes; and the table visual_tokens, for the visual editor's sign-in and its preview links. Both columns are empty on every experiment and variant already stored.

The previous release runs against this schema unchanged: it never reads or writes the two columns or the table, and stores experiments and variants as before.

The downgrade drops every visual experiment's design. 4955571d232c's downgrade drops the table visual_tokens and the columns experiments.visual_config and variants.visual_changes; it does not refuse, and nothing can restore them. The experiments stay, as ordinary experiments with no page changes. This revision's own down_revision is c448_balance_within; from a release that carries no later revision, this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade c448_balance_within

From a release that carries t1812_user_totp, it also runs that revision's downgrade, which drops the table user_totp, every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp). From a release that carries t449_identity_aliases, it also runs that revision's downgrade, which drops the table identity_aliases, every link between an anonymous id and a known id (see t449_identity_aliases). From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

On Helm, run the same command in an API pod with kubectl exec.

t1812_user_totp stores two-step sign-in secrets

This revision adds the table user_totp, one row per local account that has set two-step sign-in up or started to: the account's authenticator secret, sealed with a key derived from SECRET_KEY; its recovery codes, as bcrypt hashes; the last code step it used; and its count of wrong codes. The users table is not changed, and the new table is empty after the upgrade.

The previous release runs against this schema unchanged: it never reads or writes the table.

The downgrade drops every account's two-step sign-in. t1812_user_totp's downgrade drops the table user_totp: every account's two-step sign-in secret, recovery codes and wrong-code count. It does not refuse, and nothing can restore them: those accounts then sign in with the password alone and must set two-step sign-in up again. This revision's own down_revision is 4955571d232c; from a release that carries no later revision, this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade 4955571d232c

From a release that carries t449_identity_aliases, it also runs that revision's downgrade, which drops the table identity_aliases, every link between an anonymous id and a known id (see t449_identity_aliases). From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

On Helm, run the same command in an API pod with kubectl exec.

t449_identity_aliases adds the identity aliases table

This revision adds the table identity_aliases: one row per anonymous id that has been linked to a known id, holding the anonymous id (its primary key, so an anonymous id is linked at most once), the known id and when the link was stored, with an index on the known id. It adds no foreign key and changes no other table, and the new table is empty after the upgrade.

The previous release, 0.47.0, runs against this schema unchanged: it never reads or writes the table. So during this release's rolling upgrade, and after an API-only rollback to 0.47.0, a 0.47.0 task answers 404 on POST /api/v1/tracking/alias and ignores every link: it answers each id as an id of its own, and assigns it under that id (see Rolling back past identity aliasing).

From 0.47.0, this upgrade takes no lock on an existing table, so SDK requests do not wait for it. The upgrade itself waits at most 0.25 s, behind a concurrent creation of the same table; past that it stops with nothing applied and is simply run again. A lock timeout in any revision of an upgrade applies none of that run's revisions. An upgrade from 0.46.0 or earlier also runs 0.47.0's revisions 4955571d232c, t1812_user_totp and modules_0005_redshift, which set no lock timeout; 4955571d232c alters experiments and variants, so SDK requests that read them wait until the upgrade has its locks and commits.

The downgrade drops every link. t449_identity_aliases's downgrade drops the table identity_aliases: every link between an anonymous id and a known id. Assignments and events are untouched. It does not refuse, and nothing can restore the links: an upgrade afterwards creates the table empty, and the links must be sent again. Neither its upgrade nor its downgrade can be emitted as SQL (--sql). This revision's own down_revision is t1812_user_totp; from a release that carries no later revision, this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade t1812_user_totp

From a release that carries t2011_flag_environments, it also runs that revision's downgrade, which deactivates every staging and development API key and drops api_keys.environment and the table flag_environment_settings, every staging and development flag setting (see t2011_flag_environments). From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

On Helm, run the same command in an API pod with kubectl exec.

t2011_flag_environments adds each flag's staging and development settings

This revision adds the table flag_environment_settings, where a flag's configuration in staging or development is kept: one row per flag and environment, holding whether the flag is on there, its targeting rules and its rollout percentage. A flag's production configuration stays where it is, in feature_flags, so the table never holds a production row, and a flag with no row in an environment is off there. Deleting a flag deletes its rows. The revision also adds api_keys.environment (production, staging or development, default production) with the check check_api_key_environment.

Nothing is copied: after the upgrade every flag's configuration is its production configuration, every flag is off in staging and development, and every API key is a production key. SDKs get the same answers as before.

The previous release, 0.48.0, runs against this schema unchanged: it never reads or writes the table, and a key it creates gets production from the column's default. It has no concept of environments, though: while a task still runs 0.48.0 against the upgraded database, during a rolling upgrade and after an API-only rollback, it treats a staging or development key as a production key, and a key it creates is a production key even when the request asks for staging or development. Flag environments says what that task does with such a key.

From 0.48.0, t2011_flag_environments waits for two locks, each with its own bound: at most 0.5 s for the lock that creating the table takes on feature_flags, which a session writing to feature_flags, or a vacuum, analyze or schema change of it, holds up (SDK requests only read that table, and keep being answered), then at most 1.5 s for api_keys, which every SDK request reads. A lock that cannot be taken within its bound, or a deadlock, stops the upgrade with nothing applied, and it is simply run again.

If the upgrade to 0.49.0 stops with a lock-timeout message, re-run it: every revision runs in one transaction, so nothing was applied. While it runs, an SDK request can wait up to 3.75 seconds at most: the upgrade waits at most 3.75 seconds in all for its locks (3.5 seconds on the core profile, whose upgrade does not run modules_0006_saml_requests; on the full profile that revision runs first, before t2011_flag_environments takes the api_keys lock, so no SDK request queues through its wait of at most 0.25 seconds), and SDK requests queue behind the api_keys lock, which t2011_flag_environments takes (at most 1.5 seconds to take) and the upgrade holds until it commits, through the waits of t2012_flag_split (at most 1 second) and t1934_metric_window (at most 0.5 seconds); a request that reads feature_flags during those waits queues behind them too, for at most 1.5 seconds. An SDK whose timeout is shorter answers its fallback meanwhile (the edge SDK's default is 500 ms, and it answers enabled: false).

The downgrade can wait for more than one lock, and each wait has its own bound: at most 1.5 s for each lock it needs on api_keys (the row of each staging or development key, then the table), then at most 0.5 s for each lock it needs to drop flag_environment_settings (on that table, then on feature_flags). A lock that cannot be taken within its bound, or a deadlock, stops it with nothing changed; run it again.

The downgrade loses every staging and development setting. t2011_flag_environments's downgrade first deactivates every staging and development API key, then drops check_api_key_environment, api_keys.environment and the table flag_environment_settings. Rolling back below t2011_flag_environments loses every staging and development flag setting and deactivates every staging and development API key: the downgrade sets those keys inactive, then removes the column and the table. Those keys then answer 401; they are never served production's configuration. Production flags and keys are unaffected. Upgrading again does not restore the settings, and the deactivated keys stay inactive. It does not refuse over data. Neither its upgrade nor its downgrade can be emitted as SQL (--sql). This revision's own down_revision is t449_identity_aliases; from a release that carries no later revision, this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade t449_identity_aliases

From a release that carries t2012_flag_split, it also runs that revision's downgrade, which drops feature_flags.split and flag_environment_settings.split and refuses while any flag has a split or a rule value (see t2012_flag_split). From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

On Helm, run the same command in an API pod with kubectl exec.

Downgrading the database first, with this release still running, is the only order under Docker Compose and Helm (An Older Image Against a Newer Database). Until every API container or pod of this release has been replaced, each answers 500 to every request that presents an API key, production keys included: the SDKs it serves get errors from evaluation (/api/v1/feature-flags/evaluate/{key}) and from the ruleset (/api/v1/sdk/ruleset). t2012_flag_split is undone first, so every flag read and flag change answers 500 too, the dashboard's flag list included, and a bulk toggle 200 with every flag failed. t1934_metric_window is undone before it, so the experiment list, reading, creating, changing, pausing or cloning an experiment, and computing its results answer 500 too. Archiving or deleting a segment and listing a segment's experiments answer 500, as do the signed-in API key list, create and delete. Start the previous release straight after the downgrade.

t2012_flag_split adds a split to each flag and each flag environment setting

This revision adds the column split (jsonb) to feature_flags, for a flag's split in production, and to flag_environment_settings, for its split in staging or development. It is empty on every row after the upgrade, so SDKs get the same answers as before; a split or a rule value (a value of its own that a targeting rule carries) is set through the API afterwards.

The previous release, 0.48.0, runs against this schema unchanged: a flag it creates has no split, and a flag it updates keeps the one it has. A 0.48.0 task ignores splits and rule values and serves each such flag's on value. This holds during the rolling upgrade and after an API-only rollback to 0.48.0, until the splits and rule values are cleared with 0.49.0 serving.

From 0.48.0, this upgrade waits at most 1 s for the lock it needs on feature_flags, behind any session that has read that table and not ended its transaction; t2011_flag_environments, in the same run, has the lock on api_keys by then, so new SDK requests wait with it. From a database already at t2011_flag_environments it waits at most 1 s for each of feature_flags and flag_environment_settings. A lock that cannot be taken within its bound, or a deadlock, stops the upgrade with nothing applied, no other revision of the same run included, and it is simply run again.

The downgrade refuses while any flag has a split or a rule value. t2012_flag_split's downgrade drops feature_flags.split and flag_environment_settings.split. So while any flag has a split or a rule value in any environment it refuses, names those flags, and changes nothing:

Refusing to downgrade schema experimentation: t2012_flag_split found 2 feature flag(s) with a split or a rule value: checkout-copy (production), price-tier (development, staging). ...

To go on, clear them while the release that wrote them is still serving (the database is downgraded before the API is rolled back), or restore the backup taken before the upgrade. A boolean flag's rule value refuses nothing. With nothing to clear, the downgrade drops the two columns and nothing else. It waits at most 1 s for each of feature_flags and flag_environment_settings; a lock that cannot be taken within its bound, or a deadlock, stops it with nothing changed, and it is run again. Neither its upgrade nor its downgrade can be emitted as SQL (--sql), with or without a flag to name. This revision's own down_revision is t2011_flag_environments; from a release that carries no later revision, this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade t2011_flag_environments

From a release that carries t1934_metric_window, it first runs that revision's downgrade, which drops metrics.conversion_window_hours, every metric's conversion window (see t1934_metric_window).

A downgrade that also undoes t2011_flag_environments is one run. It holds feature_flags while it waits for api_keys, and an SDK request reads api_keys first, so the two can deadlock: the downgrade stops with nothing changed, and is run again, or the SDK requests waiting then fail, or both.

On Helm, run the same command in an API pod with kubectl exec.

Downgrading the database first, with this release still running, is the only order under Docker Compose and Helm (An Older Image Against a Newer Database). Once the downgrade has committed, this release answers 500 to every flag evaluation, flag read, flag change and ruleset request, until the API is rolled back. A bulk toggle answers 200 with every flag failed. If the run also undoes t2011_flag_environments, every request that presents an API key answers 500. From a release that carries t1934_metric_window, the experiment list, reading, creating, changing, pausing or cloning an experiment, and computing its results answer 500 too. Start the previous release straight after the downgrade.

t1934_metric_window adds a conversion window to each metric

This revision adds the column conversion_window_hours (integer) to metrics, with the check check_metric_conversion_window: empty, or a whole number of hours from 1 to 8760 (365 days). Every metric's window is empty after the upgrade. Results count a metric's events only within its window, which create and a draft's PUT set (A metric's conversion window).

The previous release, 0.48.0, runs against this schema unchanged: a metric it creates has no window. During the rolling upgrade, and after an API-only rollback, a draft saved in an older dashboard, or a PUT through an older API replica, clears the window: a PUT replaces the metrics, and the older release does not send or store the field.

From 0.48.0, this upgrade waits at most 0.5 s for the lock it needs on metrics, behind any session that has read that table and not ended its transaction, a results request among them; t2011_flag_environments and t2012_flag_split, in the same run, have their locks on api_keys and feature_flags by then, so new SDK requests wait with it. If the lock cannot be taken in time, or a deadlock is detected, the upgrade stops with nothing applied, no other revision of the same run included, and is simply run again.

The downgrade drops every window. t1934_metric_window's downgrade drops metrics.conversion_window_hours, and the check with it: every metric's conversion window. It does not refuse, and nothing restores the windows: an upgrade afterwards adds the column empty. It waits at most 0.5 s for metrics; a lock that cannot be taken within its bound, or a deadlock, stops it with nothing changed, and it is run again. Neither direction can be emitted as SQL (--sql). This revision's own down_revision is t2012_flag_split; this undoes this revision alone:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade t2012_flag_split

A downgrade that also undoes t2012_flag_split is one run: while that revision refuses, nothing of either is changed, and the windows stay.

On Helm, run the same command in an API pod with kubectl exec.

Downgrading the database first, with this release still running, is the only order under Docker Compose and Helm (An Older Image Against a Newer Database). Once the downgrade has committed, this release answers 500 to the experiment list, to reading, creating, changing, pausing or cloning an experiment, and to computing its results, until the API is rolled back. Assignment, event tracking, flag evaluation and the ruleset are still answered. GET /api/v1/results/{experiment_id} and GET /api/v1/experiments/{experiment_id}/results still serve an answer they cached before the downgrade until it expires (5 minutes for a running experiment, 24 hours otherwise), and with CACHE_ENABLED on (it is off by default) so does the experiment's daily-results, for an hour. Start the previous release straight after the downgrade.

Roll back to a specific revision:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade <revision>

Roll back all migrations (to the empty database state):

docker compose exec api python -m alembic -c backend/app/db/alembic.ini downgrade base

Note: Not all migrations are safely reversible. If a migration deletes a column, the upgrade drops its data, which no downgrade restores. Review the downgrade() function in each migration file before rolling back in production. A migration that rewrites data may not be able to undo it at all: see 1ab99332f0ba rewrites stored event times to UTC.


Stamping Without Running Migrations

The stamp command marks a migration as applied without actually running its SQL. Use this when:

  • You have manually applied schema changes and want to bring Alembic in sync
  • You are setting up Alembic on an existing database
  • You need to skip a problematic migration after fixing it manually

Mark current database state as "head":

docker compose exec api python -m alembic -c backend/app/db/alembic.ini stamp heads

Mark as a specific revision:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini stamp <revision>

Resolving "Multiple Heads" Errors

On a full image, a command that names the singular head fails:

FAILED: Multiple head revisions are present for given argument 'head'

Name heads (plural) instead. To see the heads the image has:

docker compose exec api python -m alembic -c backend/app/db/alembic.ini heads

A release image prints its core head, and a full image the modules head too, labelled (modules).


Environment Variables

Alembic and the bootstrap connect with these settings. The defaults below are the Quick Start compose file's:

Variable Quick Start default Description
POSTGRES_SERVER postgres Database host (the compose service; localhost from the host)
POSTGRES_PORT 5432 Database port
POSTGRES_USER postgres (experimently in deploy/compose/compose.yml) Database username
POSTGRES_PASSWORD postgres (none in deploy/compose/compose.yml, which requires it) Database password; set your own outside a local trial
POSTGRES_DB experimentation Database name
POSTGRES_SCHEMA experimentation PostgreSQL schema name

The database server's TimeZone must be UTC, as it is in the bundled PostgreSQL of both compose files and the Helm chart. On another time zone, some times the API stores move by the zone's offset: an experiment's updated_at, for example. Its start and end dates are stored in UTC whatever the zone.

To see the bundled database's time zone:

docker compose exec postgres sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc "SHOW TimeZone"'

It prints UTC.


Migration Guidelines

Do

  • Generate migrations with --autogenerate and always review the output
  • Test migrations on a copy of production data before applying to production
  • Include both upgrade() and downgrade() functions
  • Use descriptive migration names: add_bayesian_config_to_experiments
  • Commit migration files to version control alongside the model changes that require them
  • Expand, then contract. A migration in release N must not drop or rename anything release N-1's code uses. Add the new column or table in one release, move the code over, and drop the old one in a later release. During a rolling upgrade, release N-1 keeps serving against release N's schema after release N's migration has run.

Do Not

  • Edit migration files that have already been applied to production
  • Put a migration name in down_revision — it takes the revision ID of the migration it follows
  • Delete migration files from the versions directory
  • Apply migrations without setting the correct POSTGRES_SCHEMA environment variable (the schema namespace will be wrong)

Running Migrations in Production

In production (ECS Fargate), migrations run as a one-off ECS task of the family experimentation-migrate-<env>, before the new application version takes any traffic. You do not normally run it yourself: the Deploy workflow runs it, and the Database Migration workflow (.github/workflows/db-migrate.yml, Actions › Database Migration) runs an upgrade or a downgrade to a target you name, after taking a snapshot. Both run it the same way:

  1. register a new revision of experimentation-migrate-<env> whose backend container runs the image being deployed (the Database Migration workflow: the image the API is serving), named by its digest (scripts/register_task_definition.sh);
  2. run that revision's ARN, never the bare family, which resolves to whichever revision registered last, wait for it to stop, and print its log (scripts/run_migration_task.sh).

The task's command is MIGRATION_COMMAND in infrastructure/cdk/stacks/migration_task_stack.py: the bootstrap, python -m backend.app.db.bootstrap, which creates the schema of an empty database and runs upgrade heads on one with tables. The Database Migration workflow overrides it with python -m alembic -c backend/app/db/alembic.ini <direction> <target>. The path is relative to the image's WORKDIR /app, under which backend/Dockerfile copies the repository layout unchanged.

By hand, from a checkout with AWS credentials for the environment, the Deploy workflow's two steps are these. IMAGE is the image by digest (<registry>/<repository>@sha256:<digest>), and NETWORK the task's network configuration, from the Fargate stack's TaskSubnets and TaskSecurityGroup outputs:

ARN=$(bash scripts/register_task_definition.sh experimentation-migrate-staging "$IMAGE")
bash scripts/run_migration_task.sh experimentation-staging "$ARN" "$NETWORK" \
  '{"containerOverrides":[{"name":"backend","command":["python","-m","backend.app.db.bootstrap"]}]}' \
  /ecs/experimentation-migrate-staging

Switching Profile

The same database can be built by one profile and opened by the other. Both documented paths handle it the same way: python -m backend.app.db.bootstrap with ENVIRONMENT set (what the API container runs on start-up) and alembic upgrade heads (what the migration task runs) share the repairs below, because backend/app/db/migrations/env.py calls them after a command-line upgrade.

Core database, full image. The core chain marks the revisions that once created workspaces, sso_configs, phi_audit_logs and the rest as applied without creating them, and the modules branch does not re-create them (it owns the three RBAC tables only). So after the upgrade reaches this build's heads, the module tables the models declare and the database lacks are created from the models, the two workspace_id foreign keys are added, and a WARNING says so. Only module tables are ever created this way — an unmigrated core model is not, so a missing core migration still shows up as a missing table rather than being papered over. alembic upgrade heads --sql (offline mode) has no reconcile step: the SQL it emits for a profile switch is incomplete.

Full database, core image. alembic_version holds a revision a core build has no file for. alembic upgrade heads answers in one of two ways: when the core chain is already at its head there is nothing to apply, so it skips alembic, leaves the rows untouched and logs a WARNING naming the foreign revisions (exit 0); when the core chain is behind — a newer core image against a database the full image built — it refuses with a message naming them. downgrade and stamp do not skip: they fail with alembic's own "Can't locate revision identified by '…'", naming the modules revision the database records, and the schema is untouched.

The escape hatch, when you really do want the core image to own that database: take a backup, then from the core image

docker compose exec api python -m alembic -c backend/app/db/alembic.ini stamp --purge heads

--purge replaces the whole version table with this build's own heads, so the modules row goes and the core row stays. The module tables themselves are never dropped by switching down to core. They stay, unused, until the database goes back to the full profile.

Use it only when the core chain is already at this release's head, which is the case where upgrade heads skips with a WARNING. stamp records revisions without running them, so after the refusal above it would mark this release's pending core migrations as applied when they are not. Run the full image of this release first, then stamp.


An Older Image Against a Newer Database

Rolling back to an older image after a release that carried a migration leaves the database recorded at a revision the older image has no file for. Both documented paths refuse, exit non-zero and leave the database untouched, with a message naming the revision and the image's version: this database was migrated by a newer Controlvar release. The two ways out are the ones it names:

  • run the newer release that migrated the database, or
  • restore the backup taken before that upgrade, and run the older image against the restored database.

On Docker Compose, the Compose README's Upgrade procedure takes that backup before it upgrades, and its last step restores it only when the older image refuses the database.

A third way, when the newer release's migrations can be undone: with the newer release still running, downgrade to the heads of the older release, then start the older image. Those are its core head and, on a full install, its modules head too, one alembic downgrade <id> for each; alembic heads, run with the older image, prints them. The core head alone is not enough when the newer release also moved the modules branch: the older image still refuses, naming the modules revision. Each migration in between runs its downgrade on the way: from a release that carries 7b6b4b4fe246, whose own down_revision is 85dd4796bb8b, that includes its downgrade, which drops the recorded start of every rollout stage (see 7b6b4b4fe246); from a release that carries c448_balance_within, whose own down_revision is 7b6b4b4fe246, that includes its downgrade, which drops every experiment's balance setting, every user's group and every counter (see c448_balance_within). From a release that carries 4955571d232c, whose own down_revision is c448_balance_within, it includes that downgrade too, which drops every visual experiment's pages, goals and page changes (see 4955571d232c). From a release that carries t1812_user_totp, whose own down_revision is 4955571d232c, it includes that downgrade too, which drops every account's two-step sign-in secret, recovery codes and wrong-code count (see t1812_user_totp). From a release that carries t449_identity_aliases, whose own down_revision is t1812_user_totp, it includes that downgrade too, which drops every link between an anonymous id and a known id (see t449_identity_aliases).

Do not edit alembic_version to make the older image start.

The refusal is at start-up, so instances of the older image that were already running keep running. That, and the window in every rolling upgrade where the previous release still serves after the new release's migration has run, is why a migration must not drop or rename anything the previous release's code still uses (see Migration Guidelines).

On AWS, the API does not migrate

On the AWS (CDK) deployment the API task definition sets RUN_MIGRATIONS=false and SEED= (empty), for every environment and both profiles. An API task never runs the bootstrap; the only thing that writes the schema is the migration task the Deploy workflow runs before it shifts traffic (or the Database Migration workflow, run by hand). infrastructure/tests/test_api_task_does_not_migrate.py pins both values.

Why: an image refuses to start against a database a newer release has migrated (above). While the API ran the bootstrap on every start, any API task started from an older revision after a Deploy's migration -- a replaced task, a scale-out, a rollback -- exited instead of serving. With the bootstrap out of the API task, an older revision starts against the newer schema and serves it.

What this means for you:

  • cdk deploy alone no longer creates the schema. After the first cdk deploy of an environment, the API runs against an empty database until the first Deploy creates it. In that window /health answers 200 -- it checks that the database answers, not that the schema is there -- so the load balancer's health checks pass, while real requests answer 500 and the API logs errors. That is expected until the first Deploy.
  • The first Deploy may be refused by the API's 5xx alarms. Anything that reaches the schema-less API in that window, a scanner included, can put experimentation-api-5xx-blue-<env> or -green-<env> into ALARM, and the Deploy refuses to start while one is. The way through is Deploy's existing break-glass (rollback runbook, "Fix forward while an alarm is firing"), and whether to use it is a person's decision at that moment. Keep the time between the first cdk deploy and the first Deploy short.
  • Rolling back relies on backward-compatible migrations. A rollback runs the older release against the newer schema. That works only when the migrations in between are backward-compatible (see Migration Guidelines). For one that is not -- a dropped or renamed table or column the older release reads -- undo the migration while the release that contains it is still serving, and only then roll the API back (rollback runbook, Database Rollback Procedure, Step 2). Once the API has been rolled back, the workflow can no longer undo it; the emergency route is then a point-in-time restore to a new cluster, which a redeploy does not pick up; scripts/restore_repoint.sh points the application at it (the same procedure, Step 3). Some data cannot come back without such a restore: modules_0002_warehouse_analysis, which shipped in 0.11.0, drops the earlier warehouse_connections table with its rows, and neither a rollback nor a downgrade restores them. And 1ab99332f0ba rewrites stored event times to UTC: a rollback runs against those rows unchanged, but the offsets clients sent come back only from a restore to a time before it ran. d29a479daafe is backward-compatible for a rollback, but undoing it drops holdout_population and its rows. 37dcb2969766 is backward-compatible for a rollback too, and undoing it drops segment_members and its rows; it refuses while that table has any. 4c2c09a2b3e8 is backward-compatible for a rollback, and undoing it changes nothing. 607c51a59bf5 is backward-compatible for a rollback too, and undoing it drops each flag's value type and values; it refuses while any flag is a string, number or json flag. 85dd4796bb8b is backward-compatible for a rollback too, and undoing it drops event_dedup_keys, every stored event_id, and no event. 7b6b4b4fe246 is backward-compatible for a rollback too, and undoing it drops the recorded start of every rollout stage; an upgrade afterwards sets each stage in progress from its updated_at. c448_balance_within is backward-compatible for a rollback too: the older release assigns every balanced experiment's new users by the hash, with no group, for as long as it serves. Undoing it drops every experiment's balance setting, every user's group and every counter. 4955571d232c is backward-compatible for a rollback too, and undoing it drops every visual experiment's pages, goals and page changes, and every visual editor sign-in and preview link. t1812_user_totp is backward-compatible for a rollback too, and undoing it drops every account's two-step sign-in secret, recovery codes and wrong-code count: those accounts then sign in with the password alone. t449_identity_aliases is backward-compatible for a rollback too, and undoing it drops every link between an anonymous id and a known id and keeps every assignment and event; an upgrade afterwards creates the table empty. modules_0003_lower_is_better is backward-compatible for a rollback, and undoing it drops every warehouse metric source's lower-is-better setting; an upgrade afterwards sets each back to false, not to the choice. modules_0004_rbac_activation is backward-compatible for a rollback too, and undoing it drops the activation-time column of custom role assignments and direct grants, with any activation time stored in it; an upgrade afterwards adds the column empty. modules_0005_redshift is backward-compatible for a rollback too, and undoing it changes no row; it refuses while any Amazon Redshift connection is stored. modules_0006_saml_requests is backward-compatible for a rollback too, and undoing it drops the table of SAML sign-ins in progress and changes no other table.
  • Compose runs one API container, which is the only writer and keeps RUN_MIGRATIONS=true. The chart already keeps the bootstrap out of the serving container: it runs it in an init container.

An environment whose Fargate stack was deployed before this change still has API revisions that run the bootstrap on start. Those revisions keep doing so, and a rollback to one of them after a newer migration refuses to start. Deploy the Fargate stack from a checkout with this change before relying on rollback.

The Deploy workflow checks the revision it is about to deploy. After it registers the API revision it reads back what ECS stored, and refuses to create the CodeDeploy deployment unless the backend container sets RUN_MIGRATIONS=false exactly. By then the migration has run; the revision serving before keeps serving. The refusal names the fix, a cdk deploy of the Fargate stack from a checkout with this change (rollback runbook). Rollback does not check its target in the same way; the runbook gives the command to check one by hand.


Email addresses that differ only in case

From this release an email address belongs to one account whatever its letter case: Bob@acme.com and bob@acme.com are the same address, and the database refuses a second account holding it (the unique index ix_users_email_lower). A database can already hold two such accounts if an administrator, SQL or a restore created them. The upgrade then stops before it changes anything:

Refusing to upgrade schema experimentation: 2 groups of accounts have email addresses that differ only in letter case, and from this release an email address belongs to one account whatever its case.
Accounts that share an address (user ids, one group per line, oldest first):
  11111111-1111-1111-1111-111111111111 22222222-2222-2222-2222-222222222222
  33333333-3333-3333-3333-333333333333 44444444-4444-4444-4444-444444444444
Nothing has been changed: the database is still at the revision it had before this upgrade, and the release you upgraded from runs against it as before.
See docs/self-hosting/migrations.md#email-addresses-that-differ-only-in-case

The message names account ids only, never an address. It lists at most 20 groups, then ... and K more groups; the listing query below shows them all. Every pending migration of that upgrade is rolled back with it, so the database is exactly as the release you upgraded from left it.

Where you see it, and how to keep serving:

  • Docker Compose. The API container runs the upgrade on start, exits, and restarts until the database is fixed: docker compose logs api --tail 100. To serve meanwhile, start the previous release's image; nothing was migrated, so it starts normally.
  • Helm. The migrate init container fails and the rolling update stalls; the old pods keep serving. kubectl logs <pod> -c migrate.
  • AWS. The Deploy workflow's migration step fails and nothing shifts; the previous release keeps serving. The step prints the migration task's last 100 log lines as one tab-separated line, so the message above appears inside that line, ending with the docs/self-hosting/migrations.md#email-addresses-that-differ-only-in-case link. The task's log stream in CloudWatch shows it line by line.

Resolve each group

For each group, choose the account that keeps the address and retire the others. Do this with psql, using the API's POSTGRES_* settings. Under Docker Compose, from deploy/compose/ (or the repository root for the Quick Start stack), this opens it; it reads statements until you type \q:

docker compose exec postgres sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB"'

The statements below use the schema experimentation. If your POSTGRES_SCHEMA is different, replace it.

  1. List every group. Save this output somewhere private before you change anything: its email column is the record of each address as it was, which is what you need to undo a step by hand.
-- List the accounts in each group
SELECT lower(email) AS address, email, id, username, created_at, is_active,
       is_superuser, role, hashed_password IS NOT NULL AS has_password
  FROM experimentation.users
 WHERE lower(email) IN (SELECT lower(email) FROM experimentation.users
                         WHERE email IS NOT NULL
                         GROUP BY lower(email) HAVING count(*) > 1)
 ORDER BY lower(email), created_at;
  1. Choose the account to keep. Keep the account the person actually signs in with; if you are not sure, ask them. There is no last-sign-in column to go by.

  2. Give the kept account the role it needs. If the account you are about to retire has the role or the superuser flag the person needs, copy them to the account you keep first. Replace both ids with ids from the listing:

-- Give the kept account the role of the one you retire
UPDATE experimentation.users AS kept
   SET role = retired.role, is_superuser = retired.is_superuser
  FROM experimentation.users AS retired
 WHERE kept.id = '<id to keep>' AND retired.id = '<id to retire>';
  1. Retire the other account. It keeps its history and everything it owns, but it can no longer sign in and no longer holds the address. Its address becomes retired-<its id>@<its own domain>:
-- Retire the other account
UPDATE experimentation.users
   SET email = 'retired-' || id || '@' || split_part(email, '@', 2),
       is_active = false
 WHERE id = '<id to retire>';

The retired address keeps the account's own domain because the API refuses addresses under reserved domains such as .invalid, .test or .local; an account given one could no longer be edited through the API.

email holds at most 100 characters, and retired-<id>@ takes 45 of them, so this works when the domain is at most 55 characters. For a longer domain the statement fails with value too long for type character varying(100) and changes nothing. Then either use the shorter form below, which fits a domain of up to 61 characters, or give the account an address its owner actually holds (with the SQL above, or with PUT /api/v1/admin/users/{id} from the release that is still serving, which needs username and email in the body):

-- If the domain is longer than 55 characters
UPDATE experimentation.users
   SET email = 'r-' || id || '@' || split_part(email, '@', 2),
       is_active = false
 WHERE id = '<id to retire>';
  1. Check that no group is left. This returns no rows when you are done:
-- Check that no group is left
SELECT array_agg(id ORDER BY created_at) AS ids
  FROM experimentation.users
 WHERE email IS NOT NULL
 GROUP BY lower(email) HAVING count(*) > 1;
  1. Run the upgrade again the way you ran it the first time. It builds the index itself.

Do not delete the account instead of retiring it. Deleting a user deletes or detaches what refers to it: its API keys, reports, notification preferences, role assignments, workspace memberships and visual editor sign-ins and preview links are deleted with it, and the experiments, flags, audit records and everything else that names it lose that reference. Retiring keeps all of that.

Do not build the index by hand. Run the upgrade, which locks the table, checks for groups and builds the index in one transaction. An index built another way can be left half-built: a CREATE UNIQUE INDEX CONCURRENTLY that fails on a group leaves an index that enforces nothing. The upgrade refuses to continue past an index called ix_users_email_lower that is not exactly the one it builds, and names the DROP INDEX to run before you try again.

If the upgrade says another session is writing to users. The upgrade waits at most 30 seconds for the lock on users, so that it never holds up the API's writes for long. Nothing has been changed; run it again when that session has finished.