-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
refreshArrayTypes()
call to re-sync newly created complex…
… array columns, like enums
- Loading branch information
1 parent
bdd287c
commit 01efa6a
Showing
4 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@electric-sql/pglite': minor | ||
--- | ||
|
||
Add `refreshArrayTypes()` call to re-sync newly created complex array columns, like enums |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { PGlite } from '../dist/index.js' | ||
import { expectToThrowAsync } from './test-utils.js' | ||
|
||
describe('array types', () => { | ||
it('throws for array params enum when not calling refreshArrayTypes', async () => { | ||
const db = new PGlite() | ||
await db.query(` | ||
CREATE TYPE mood AS ENUM ('sad', 'happy'); | ||
`) | ||
|
||
await db.query(` | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT, | ||
moods mood[] | ||
); | ||
`) | ||
|
||
await expectToThrowAsync(async () => { | ||
await db.query( | ||
` | ||
INSERT INTO test (name, moods) VALUES ($1, $2); | ||
`, | ||
['test2', ['sad', 'happy']], | ||
) | ||
}, 'malformed array literal: "sad,happy"') | ||
}) | ||
|
||
it('works with new array types after calling refreshArrayTypes', async () => { | ||
const db = new PGlite() | ||
await db.query(` | ||
CREATE TYPE mood AS ENUM ('sad', 'happy'); | ||
`) | ||
|
||
await db.query(` | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT, | ||
moods mood[] | ||
); | ||
`) | ||
|
||
await db.refreshArrayTypes() | ||
|
||
await db.query( | ||
` | ||
INSERT INTO test (name, moods) VALUES ($1, $2); | ||
`, | ||
['test2', ['sad', 'happy']], | ||
) | ||
|
||
const res = await db.query(` | ||
SELECT * FROM test; | ||
`) | ||
|
||
expect(res).toEqual({ | ||
rows: [ | ||
{ | ||
id: 1, | ||
name: 'test2', | ||
moods: ['sad', 'happy'], | ||
}, | ||
], | ||
fields: [ | ||
{ | ||
name: 'id', | ||
dataTypeID: 23, | ||
}, | ||
{ | ||
name: 'name', | ||
dataTypeID: 25, | ||
}, | ||
{ | ||
name: 'moods', | ||
dataTypeID: 16384, | ||
}, | ||
], | ||
affectedRows: 0, | ||
}) | ||
}) | ||
|
||
it('refreshArrayTypes is indempotent', async () => { | ||
function getSize(obj) { | ||
return Object.keys(obj).length | ||
} | ||
|
||
const db = new PGlite() | ||
await db.waitReady | ||
|
||
const initialSerializersSize = getSize(db.serializers) | ||
const initialParsersSize = getSize(db.parsers) | ||
|
||
expect(initialSerializersSize).toBeGreaterThan(0) | ||
expect(initialParsersSize).toBeGreaterThan(0) | ||
|
||
await db.refreshArrayTypes() | ||
|
||
expect(getSize(db.serializers)).toBe(initialSerializersSize) | ||
expect(getSize(db.parsers)).toBe(initialParsersSize) | ||
|
||
await db.refreshArrayTypes() | ||
|
||
expect(getSize(db.serializers)).toBe(initialSerializersSize) | ||
expect(getSize(db.parsers)).toBe(initialParsersSize) | ||
}) | ||
}) |