Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] duplicate method to copy a PGLite DB into a new DB #416

Open
Scalahansolo opened this issue Nov 7, 2024 · 3 comments
Open

Comments

@Scalahansolo
Copy link

I wanted to throw out an idea for PGLite. I want to use it for a bunch of jest tests in our backend application. So far I have a setup that is working nicely. But... it takes roughly ~1-2 seconds to push my schema into the DB with Drizzle and than seed it with base data that is used in a ton of our tests.

It would be really awesome if I could set up a PGLite DB at the beginning of our test runs in something akin to globalSetup in jest, where I establish the DB and seed it with data, than pass that instance to all the suites. In the before all for each of the suites, they would call this duplicate method on the DB. This way each suite wouldn't have the pay the "start up" cost of getting the DB ready to run against.

@Scalahansolo Scalahansolo changed the title [Feature Request] duplicate method [Feature Request] duplicate method to copy a PGLite DB into a new DB Nov 7, 2024
@Soviut
Copy link

Soviut commented Nov 29, 2024

I have a similar problem except I don't seed any data; each test is responsible for creating all its own records.

That said, some kind of in-memory snapshot might also work. Create the snapshot right after provisioning the schemas (or seed data) and then pass the snapshot to each new PGLite instance.

What's good is any copy/duplicate solution should be safe in a parallel when you're running multiple tests concurrently.

@sepirak-nick
Copy link

sepirak-nick commented Feb 4, 2025

+1, this would be huge for running many tests at once. Running migrations (via Drizzle) for each test with our application takes 200-500ms per test.

Similarly, having some helper scripts where we can seed the database for different scenarios and run it against those states would also run quicker if the SQL only ever had to run once.

@sepirak-nick
Copy link

It turns out this is really easy to do right now with the available methods.

I've created a proof-of-concept here: https://github.com/scarabcoder/pglite-clone-poc

And it's definitely faster than running the SQL itself. Running a script that takes 600ms (inserting 18,000 generated rows) only takes 30ms to clone on my machine.

import {PGlite} from "@electric-sql/pglite";

export const clonePGlite = async (pgLite: PGlite): Promise<PGlite> => {
  const dump = await pgLite.dumpDataDir('none');

  return new PGlite({loadDataDir: dump});
}

Despite the name, dumpDataDir will keep it in file, and returns a Blob if the underlying PGlite instance was in-memory and not filed based.

If you wanted to clone a PGlite database that is running off of a file, I'm sure there's a way to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants