Skip to main content

How to Enable Staged Schema Upgrades with Feature Flags

The standard staged rollout strategy requires two separate code deployments: one to add reading support, and another to enable writing. Runtime schema upgrades let you ship both in a single deployment and control when the upgrade takes effect using a feature flag or other runtime control.

This decouples code deployment from feature rollout, giving you:

  • Gradual rollout — enable the upgrade for a subset of users or documents first
  • Simpler deployment — one code version handles both the "before" and "after" schema states
note

The APIs described here are currently alpha. Import them from @fluidframework/tree/alpha.

How It Works​

Defining a staged schema upgrade prepares the view to handle a change before the document permits it. Use staged() to declare a future allowed type or stagedOptional() to prepare a required field to become optional. By default, new documents exclude those types and keep those fields required, so declaring the change does not enable it for writes.

An existing document may already permit the change because another client enabled it. Upgrades preserve that document's enabled types and optionality, even when the current client's policy does not enable them. The policy therefore controls which new staged upgrades a client may enable, not which existing upgrades it may remove.

With TreeViewConfigurationAlpha, you can selectively enable specific staged upgrades at view construction time:

import {
SchemaFactoryAlpha,
StagedSchemaUpgradePolicy,
TreeViewConfigurationAlpha,
} from "@fluidframework/tree/alpha";

const sf = new SchemaFactoryAlpha("MyApp");

// Create a staged type and extract its upgrade token
const stagedNewType = sf.staged(MyNewType);
const myUpgradeToken = stagedNewType.metadata.stagedSchemaUpgrade;

const view = tree.viewWith(
new TreeViewConfigurationAlpha({
schema: MySchema,
stagedUpgradePolicy:
StagedSchemaUpgradePolicy.enabledStagedUpgrades(myUpgradeToken),
}),
);

When a view is constructed with enabled staged upgrades:

  • initialize() includes the enabled staged changes in the new document's stored schema.
  • upgradeSchema() enables them when upgrading an existing document, while preserving any already-enabled staged upgrades.
  • compatibility reflects the enabled upgrades when determining what actions are possible.

The upgrade set is fixed at view construction time and cannot be changed afterwards.

Walkthrough: Adding RGB Colors to a Whiteboard App​

You have a collaborative whiteboard app where users place sticky notes. Currently, the color field on each note stores a plain string (e.g. "red", "#ff0000"). You want to add a new RgbColor type as an additional allowed type for the color field — a change that is backwards compatible but not forwards compatible.

Step 1: Define the Staged Schema​

Add the new type as a staged allowed type and capture its upgrade token:

import { SchemaFactoryAlpha, TreeViewConfigurationAlpha } from "@fluidframework/tree/alpha";

const sf = new SchemaFactoryAlpha("WhiteboardApp");

class RgbColor extends sf.object("RgbColor", {
r: sf.number,
g: sf.number,
b: sf.number,
}) {}

const stagedRgbColor = sf.staged(RgbColor);
const rgbUpgrade = stagedRgbColor.metadata.stagedSchemaUpgrade;

class Note extends sf.objectAlpha("Note", {
text: sf.string,
x: sf.number,
y: sf.number,
color: sf.required([sf.string, stagedRgbColor]),
}) {}

class Board extends sf.object("Board", {
notes: sf.array(Note),
}) {}

Your application code must handle RgbColor nodes correctly — rendering them, converting to CSS values, etc. — because once the upgrade is enabled, documents will contain them.

Step 2: Wire Up the Feature Flag​

Derive the stagedUpgradePolicy from your feature flag system when constructing the view:

import { StagedSchemaUpgradePolicy } from "@fluidframework/tree/alpha";

function openDocument(tree, featureFlags) {
const stagedUpgradePolicy = featureFlags.enableRgbColors
? StagedSchemaUpgradePolicy.enabledStagedUpgrades(rgbUpgrade)
: undefined; // Restrictive: no new staged upgrades; existing ones are preserved.

const view = tree.viewWith(
new TreeViewConfigurationAlpha({
schema: Board,
stagedUpgradePolicy,
}),
);

if (view.compatibility.canInitialize) {
view.initialize(new Board({ notes: [] }));
} else if (view.compatibility.canUpgrade) {
view.upgradeSchema();
}

return view;
}

Step 3: Deploy and Enable​

  1. Deploy the new code. With the feature flag off, clients understand RgbColor but do not introduce it into documents. New documents exclude it, and upgrading an existing string-only document leaves it string-only.

  2. Turn on the feature flag. New views are constructed with rgbUpgrade enabled:

    • New documents are initialized with stored schema that includes RgbColor.
    • Existing documents are upgraded to include RgbColor when upgradeSchema() is called.
    • Users can now assign RGB colors to notes.
  3. Clients without the flag (or clients who haven't picked up the flag yet) can still read and edit upgraded documents because their view schema already includes RgbColor as a staged type.

Step 4: Clean Up After Full Rollout​

Once the upgrade is complete and all documents have been migrated, remove the staged wrappers:

import { SchemaFactory, TreeViewConfiguration } from "@fluidframework/tree";

const sf = new SchemaFactory("WhiteboardApp");

class RgbColor extends sf.object("RgbColor", {
r: sf.number,
g: sf.number,
b: sf.number,
}) {}

class Note extends sf.object("Note", {
text: sf.string,
x: sf.number,
y: sf.number,
color: sf.required([sf.string, RgbColor]),
}) {}

class Board extends sf.object("Board", {
notes: sf.array(Note),
}) {}

// No more alpha config or feature flags needed
const config = new TreeViewConfiguration({ schema: Board });

Understanding Irreversibility​

Once a document's stored schema has been upgraded, it cannot be downgraded. Turning the feature flag off must therefore stop new enablement rather than undo enablement in existing documents. With the flag off, this client's policy excludes RgbColor from new documents and does not add it to documents that still permit only strings. For a document that already permits RgbColor, however, upgradeSchema() preserves that enabled upgrade while applying any other compatible additions.

This makes a feature-flag rollback safe only while the client retains the staged reading support:

  • Clients running the staged code (Step 1 and above) can still read, edit, and make compatible upgrades to already-upgraded documents.
  • Clients running older code (before the staged type was added) cannot open upgraded documents. This is why client saturation of the staged code is important before enabling the flag.

Plan your rollout with this in mind: ensure that all active clients have the staged code before turning on the flag.

Partially Enabled Tokens​

A token can be used at more than one field, but a stored document may permit its change at only some of them. For example, suppose one application version enables string priorities at primaryPriority. A later version also uses that staged token at fallbackPriority, which still permits only numbers in the document. With a restrictive policy, upgrading preserves strings at primaryPriority without adding them at fallbackPriority. To introduce strings at the second field, explicitly select the token in the policy and apply the upgrade.

To distinguish these states, the alpha isStagedUpgradeEnabled() query reports "disabled", "partial", or "enabled" across the locations declared by the view. The later view in this example reports "partial" until both fields permit strings. If your application requires the change at every such field, check for "enabled" rather than treating "partial" as sufficient. The query only reports the stored state; it does not perform the upgrade.

Best Practices​

  • Wait for client saturation before enabling. Monitor your client version distribution to ensure all (or tolerably most) users have deployed the code with the staged schema before enabling the flag.

  • One upgrade token per feature. Each staged() or stagedOptional() call creates a separate upgrade token. Group related schema changes under the same feature flag by enabling all their tokens together.

  • Test before enabling. Use testing schema upgrades to verify your code handles the upgraded schema correctly before turning on the flag in production.

  • Use snapshotSchemaCompatibility tests. Write snapshotSchemaCompatibility tests to verify compatibility across the view-schema versions in your rollout. Supplement them with the stored-state tests described below.

  • Test the stored states your rollout can produce. Different feature-flag combinations can enable different staged upgrades for the same view schema. Compatibility snapshots currently assume restrictive staging, so they do not exercise every enabled or partially enabled state. Cover those states with live-document tests, including upgrades by clients whose flags are off.

See Also​