r/SQLServer • • 4d ago

Community Share I built an open-source tool that gives every dev, test and PR its own writable copy of a real SQL Server database, in seconds

I've worked with SQL Server for 20+ years, and one thing still bugs me. Integration tests either run against a toy database with 20 rows of seed data, or against one shared copy the whole team steps on. Restoring the real backup for every branch is slow and eats disk, so most teams just don't bother. They test against the toy.

I got annoyed enough to build something. SqlBranch: you restore a full backup once into an image, run your masking script before it's sealed, and then hand out writable branches of it. One per dev, per test, per PR, per AI agent. A branch is a copy-on-write clone of the image's data files (XFS reflink), attached as its own database on a SQL Server Developer container. For a 50GB database, a branch takes about 2.5 seconds end to end and around 300KB of disk until it starts writing. The image stays untouched. SqlBranch never connects to production, it only reads a backup file you already have.

I posted it in r/dotnet last week and got good pushback, so let me get ahead of the obvious ones:

  • Why not database snapshots? Read-only, same instance as the source. You can't hand 20 people their own writable copy.
  • Why not just restore? On good storage, fair point, a restore is quick. It hurts when you need ten copies at once. Devs, PR builds, parallel test shards. That's ten full copies on disk, and every reset is another restore. Here the restore happens once.
  • Why not Testcontainers + Respawn, or LocalDB with a transaction per test? Great tools, wrong problem. They're for schema plus seed data. This is for when you need the real data. Real volume, real stats, real plans.
  • Why would a test need a 50GB database? Migrations, mostly. A NOT NULL column or a new unique index that passes on 20 rows and fails on prod data. Performance work too, and reproducing a prod bug.
  • Azure SQL Database, where all you have is a bacpac? There's a --from-bacpac build. Still preview, and it needs your own SqlPackage.
  • What's the catch? Heavy scattered updates on a branch run about 4 to 5 times slower than on a full copy. Copy-on-write tax. Inserts cost the same. If you're benchmarking or doing write-heavy work, make a full-copy branch instead. It's dev/test only (Developer edition), full single-file backups only, no FILESTREAM or memory-optimized filegroups. Linux with Docker, macOS works, Windows isn't verified yet.

This weekend I merged image refresh, shipping in 0.3. Rebuild the image nightly: restore last night's backup, mask it, publish it as a new version of the same image, while people keep working on their branches. A branch stays on its version until the owner resets to the latest, and you pick how many old versions to keep. Export/import so teammates and CI can pull the image instead of restoring is still preview.

It's open source (Apache 2.0), on NuGet as SqlBranch: https://github.com/vuglll/SqlBranch — https://www.nuget.org/packages/SqlBranch

Honestly curious what people who run SQL Server at scale think.

What would stop you from using this?

10 Upvotes

8 comments sorted by

4

u/Black_Magic100 3d ago

This doesn't replace enterprise SAN snapping and mounting, but I could see it being useful for containerized CI testing. The trick would be not altering the database names (no shared instances) and providing images that would allow developers to choose which databases they need.

1

u/vuglll 3d ago

If you've got SAN snapshots, you probably don't need this. It's more for teams that don't, or for when you want the same thing on a laptop or a CI runner. CI is honestly where I think it fits best. Good call on keeping the db names as they are, since too many apps have them hardcoded somewhere. Picking which databases go into the image is a nice idea too. I will add that to my roadmap

2

u/RVECloXG3qJC 3d ago

What do you mean by restoring a backup once into an image?

2

u/vuglll 3d ago

Basically you restore the .bak once, and that restored copy becomes a read-only base. Every branch after that is a copy-on-write clone of it, so it spins up in a few seconds instead of doing a full restore each time. It only stores the pages you actually change. I refresh the base every week or so, and that's usually plenty.

2

u/Darex97 14h ago

We use Testcontainers with a real SQL Server and a transaction per test, which works fine until you need real data volume, so this is interesting.

Could a branch be created from an xUnit fixture, one per test class or CI shard, and dropped after? What does teardown cost?

Most of our team is on Windows with Docker Desktop. Since branches rely on XFS reflink, would it work inside WSL2 if the data sits on an XFS volume, or is that a dead end for now?

1

u/vuglll 10h ago

Yes, that's exactly what the SqlBranch.Testing package is for:

public sealed class ShopDb : IAsyncLifetime
{
    public SqlBranchDatabase Db { get; private set; } = null!;
    public async Task InitializeAsync() => Db = await SqlBranchDatabase.CreateAsync("shop");
    public async Task DisposeAsync() => await Db.DisposeAsync();
}

You can keep your transaction-per-test inside the branch.

Teardown is cheap. It drops the database and deletes the files, about a quarter second each. Creates are about half a second, but they run one at a time, so ten shards starting together wait a few seconds. Branch pooling is on the roadmap: it keeps a few branches attached and ready, so a test grabs one almost instantly. If a run crashes, its branches expire after an hour and get cleaned up.

WSL2: honestly, I don't know yet, because I don't have a Windows machine to test on. You wouldn't need your own XFS volume, though. init creates one inside Docker's VM, same as on Mac. If that fails, it falls back to full copies, which work but are slow for big databases. There's a 5-minute check script in the repo (tools/windows-check.ps1). If anyone on your team is up for running it and sending me the log, I'd owe you one.