Why previews miss real bugs
Most previews run on seed data: a few made-up products, users and orders. The real data is big. Copying it for every pull request takes time and storage, and someone has to clean up the copies later. So the code gets reviewed, but the data it will meet in production does not. A change that reads every price as “12.00” passes review, then fails on the first euro price written “24,00”. The code was reviewed. The data it met was not.
The usual fixes both have a cost. You can copy the real data for each pull request, and wait, and pay for every copy. Or you can keep one shared staging with real data, and let every pull request use it. Then one pull request’s migration rewrites the data that three others are testing on, and nobody knows whose change broke what.
The usual setup
What we’ll build
In this guide we’ll do it another way. Your app’s data lives in one place, on a branch called main. Each pull request gets its own copy of that branch, made in 10 ms, because a copy here shares the files instead of duplicating them. The preview can change its copy, even break it, and main never notices. And we’ll catch that euro bug on the way.
What we’re building
By the end, you’ll have:
- Your app’s data saved once, on a main branch that previews never write to.
- A full copy of that data for each pull request, made in 10 ms.
- A preview that runs on its copy, and can break it safely.
- A read-only key, so reviewers can look without touching.
- One call to ship the change, and one to throw the copy away.
You’ll need a Skywake API key and Node. Use an admin key: step 5 makes a new key, and that needs one. Set SKYWAKE_API_KEY, then install the SDK with npm i skywake.
Step 1
Keep your app’s data on main
Start with the data your app reads: the catalog, settings, uploaded files. Ours is a small shop:
shop-data/
- catalog/products.json# prices like “12.00” and “24,00”
- catalog/stock.json
- config/shipping.json
- uploads/banner.txt
We’ll upload it to a disk, a set of files with branches and history. The first upload creates the disk and its branch main. A branch is a named version of your files, like a Git branch. Each save is a commit, so you can always see what changed and go back.
import { Skywake } from "skywake";
const sky = new Skywake();
const shop = sky.disk("shop-data");
await shop.upload("./shop-data", { message: "Shop data today" });From here on, no preview writes to main. Each one gets its own branch, and main changes only when a pull request is merged.
Step 2
Give the pull request its own copy
Pull request 512 opens. We fork main into a new branch named after it. A fork is a new branch that starts exactly where another one is. It copies nothing: it shares main’s files until the pull request changes one.
const pr = await shop.fork("pr-512");
const start = await pr.head();pr-512 now has every file main has, with the same contents. We also keep start, the commit the copy began from. We’ll need it in step 4.
Step 3
Run the preview on the copy
The change under review moves prices into whole cents. Its migration reads the catalog, replaces each price with cents, and saves the result. We run it on the copy, exactly as it was written in the pull request:
type Product = { id: string; price: string; currency: string };
const file = "catalog/products.json";
async function migrate(toCents: (price: string) => number) {
const products: Product[] = JSON.parse(await pr.read(file));
const moved = products.map(({ price, ...product }) => ({
...product,
cents: toCents(price),
}));
await pr.write(file, JSON.stringify(moved, null, 2), {
message: "PR 512: store prices in cents",
});
}
await migrate((price) => Math.round(Number(price) * 100));
await pr.download("./previews/pr-512");download writes the branch’s files into a local folder. Start the preview with that folder as its data, and open the shop.
The mug costs 1200 cents, as it should. The tote bag has no price. Its price is “24,00”, and Number("24,00") is not a number, so it was saved as null. This is the bug seed data hides, found before anyone approved it, because the preview ran on the real catalog.
Worse, the migration dropped the old price field, so on this copy the tote’s price is gone. On a shared staging, everyone would now be testing on broken data. Here, only pr-512 changed. main has no new commit at all.
Step 4
Undo and try again
The author fixes the migration to read a comma as a decimal point. Before running it again, we put the copy back the way it started. restore makes the branch’s files what they were at a commit, as one new commit on top, so even the broken try stays in the history.
await pr.restore(start!);
const toCents = (price: string) =>
Math.round(Number(price.replace(",", ".")) * 100);
await migrate(toCents);
await pr.download("./previews/pr-512");Refresh the preview. Every product has a price now:
main, the whole time: unchanged
You can do this as many times as the change needs. Each try starts from the same real data, and nothing you do on the copy reaches anyone else.
Step 5
Let reviewers look, not touch
Reviewers should see the data the preview ran on, but not change it. We make them a key that can only read, only reach this disk, and stops working after a week:
const review = await sky.createKey({
name: "Review PR 512",
role: "read",
only: ["shop-data"],
expiresInDays: 7,
});Share review.key with the reviewer. With it, they can ask what the pull request changed in the data:
const reviewer = new Skywake({ key: review.key });
const copy = reviewer.disk("shop-data", { branch: "pr-512" });
const changed = await copy.diff("main");
console.log(changed);It prints one change: catalog/products.json, modified. diff compares the copy with main as it is now. Nothing has landed on main since the fork, so only the pull request’s own change shows. The reviewer can read any file on the copy. If they try to write one, the request is refused.
Tip: a read key sees the whole disk
A key is limited by disk, not by branch. This one can also read main, which is handy for comparing. It cannot change either. If some data must stay private, keep it on its own disk.
Step 6
Ship it, or throw it away
The pull request is approved. Merging the branch brings its changes into main. Nothing else writes to main, so this is the only way the new prices reach the real data.
await pr.mergeInto("main", { message: "Ship PR 512" });
await pr.destroy();
await sky.revokeKey(review.id);main now stores prices in cents, the copy is gone, and the reviewer’s key no longer works. If main had changed the same file in the meantime, the merge would save nothing and throw a MergeConflict that lists the file in conflicts. The branch and the key would stay.
Not every change on a copy should ship. Test orders made while clicking around a preview, for example, should not. In that case skip the merge and only delete the branch. The copy is gone, and main never saw it.
Doing it for every pull request
The steps above fit in one small function. Call it from your CI when a pull request is opened, merged or closed:
export async function onPullRequest(n: number, event: string) {
const name = `pr-${n}`;
const copy = sky.disk("shop-data", { branch: name });
if (event === "opened") {
await shop.fork(name);
await copy.download(`./previews/${name}`);
}
if (event === "merged") {
await copy.mergeInto("main");
}
if (event === "merged" || event === "closed") {
await copy.destroy();
}
}A few things to know as more pull requests run at once:
- Copies are quick. Creating a branch takes about 13 ms, and a fork copies nothing, whatever the size of your data.
- Copies stay apart. Two pull requests open at the same time each see only their own changes, never each other’s.
- Real data stays safe. Previews write only to their own branches, so
mainchanges only when a pull request is merged. - Clean up is one call. Deleting the branch removes the copy. There is no database or bucket left behind to find later.
- It all stays yours. Every copy lives in your own bucket.