Skip to content

Commit

Permalink
feat: add support for loading compressed dumps via loadDataDir that a…
Browse files Browse the repository at this point in the history
…re not (#498)
  • Loading branch information
samwillis authored Jan 20, 2025
1 parent ac7622c commit 67fb2aa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/witty-spiders-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite': patch
---

feat: add support for loading compressed dumps via loadDataDir that are not labeled with mimetype
14 changes: 13 additions & 1 deletion packages/pglite/src/fs/tarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ export async function loadTar(
tarball = await unzip(tarball)
}

const files = untar(tarball)
let files
try {
files = untar(tarball)
} catch (e) {
if (e instanceof Error && e.message.includes('File is corrupted')) {
// The file may be compressed, but had the wrong mime type, try unzipping it
tarball = await unzip(tarball)
files = untar(tarball)
} else {
throw e
}
}

for (const file of files) {
const filePath = pgDataDir + file.name

Expand Down
28 changes: 28 additions & 0 deletions packages/pglite/tests/dump.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,32 @@ describe('dump', () => {

expect(ret1).toEqual(ret2)
})

it('dump data dir and load it - compressed but not specified', async () => {
const pg1 = new PGlite()
await pg1.exec(`
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
name TEXT
);
`)
pg1.exec("INSERT INTO test (name) VALUES ('test');")

const ret1 = await pg1.query('SELECT * FROM test;')

const file = await pg1.dumpDataDir()

// remove the mime type from the file
const file2 = new Blob([file])

expect(typeof file2).toBe('object')

const pg2 = new PGlite({
loadDataDir: file2,
})

const ret2 = await pg2.query('SELECT * FROM test;')

expect(ret1).toEqual(ret2)
})
})

0 comments on commit 67fb2aa

Please sign in to comment.