Skip to content

Commit

Permalink
fix(postgres): quote array literal items containing a comma
Browse files Browse the repository at this point in the history
Closes #3810
  • Loading branch information
B4nan committed Dec 2, 2022
1 parent 8f4aaf5 commit 5ffa81c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/postgresql/src/PostgreSqlPlatform.ts
Expand Up @@ -142,7 +142,7 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
}

marshallArray(values: string[]): string {
const quote = (v: string) => v === '' || v.match(/["{}]/) ? JSON.stringify(v) : v;
const quote = (v: string) => v === '' || v.match(/["{},]/) ? JSON.stringify(v) : v;
return `{${values.map(v => quote('' + v)).join(',')}}`;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/issues/GH3810.test.ts
@@ -0,0 +1,59 @@
import { Entity, OptionalProps, PrimaryKey, Property, SimpleLogger } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';
import { mockLogger } from '../helpers';

@Entity()
class User {

[OptionalProps]?: 'options';

@PrimaryKey()
id!: number;

@Property({ type: 'string[]', default: ['foo'] })
options = ['foo'];

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
dbName: 'mikro_orm_test_gh_3810',
loggerFactory: options => new SimpleLogger(options),
});
await orm.schema.refreshDatabase();
});

afterAll(async () => {
await orm.close();
});

test('3810', async () => {
const mock = mockLogger(orm, ['query', 'query-params']);
const u1 = orm.em.create(User, { options: ['foo,'] });
await orm.em.flush();

u1.options.push('asd,');
u1.options.push('bar');
u1.options.push(',baz');
await orm.em.flush();

await orm.em.refresh(u1);
u1.options.push('qux,');
await orm.em.flush();

expect(mock.mock.calls).toEqual([
['[query] begin'],
["[query] insert into \"user\" (\"options\") values ('{\"foo,\"}') returning \"id\", \"options\""],
['[query] commit'],
['[query] begin'],
["[query] update \"user\" set \"options\" = '{\"foo,\",\"asd,\",bar,\",baz\"}' where \"id\" = 1"],
['[query] commit'],
['[query] select "u0".* from "user" as "u0" where "u0"."id" = 1 limit 1'],
['[query] begin'],
["[query] update \"user\" set \"options\" = '{\"foo,\",\"asd,\",bar,\",baz\",\"qux,\"}' where \"id\" = 1"],
['[query] commit'],
]);
});

0 comments on commit 5ffa81c

Please sign in to comment.