Skip to content

Commit

Permalink
fix(core): do not ignore schema name in batch queries
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 16, 2022
1 parent b569686 commit b47393e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
11 changes: 4 additions & 7 deletions packages/knex/src/AbstractSqlDriver.ts
Expand Up @@ -500,14 +500,11 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
}

protected getTableName<T>(meta: EntityMetadata<T>, options: NativeInsertUpdateManyOptions<T>): string {
let tableName = this.platform.quoteIdentifier(meta.collection);
const tableName = this.platform.quoteIdentifier(meta.collection);
const schema = this.getSchemaName(meta, options);

if (options.schema === '*') {
return this.platform.quoteIdentifier(this.config.get('schema')) + '.' + tableName;
}

if (meta.schema || options.schema) {
tableName = this.platform.quoteIdentifier(options.schema ?? meta.schema!) + '.' + tableName;
if (schema) {
return this.platform.quoteIdentifier(schema) + '.' + tableName;
}

return tableName;
Expand Down
@@ -1,4 +1,4 @@
import { Entity, MikroORM, PrimaryKey, wrap } from '@mikro-orm/core';
import { Entity, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';

@Entity()
Expand All @@ -7,6 +7,13 @@ export class CoreEntity {
@PrimaryKey()
id!: number;

@Property()
name: string;

constructor(name: string) {
this.name = name;
}

}

describe('different schema from config', () => {
Expand All @@ -27,8 +34,13 @@ describe('different schema from config', () => {
await orm.close();
});

beforeEach(async () => {
await orm.em.nativeDelete(CoreEntity, {});
orm.em.clear();
});

it('should respect the global schema config', async () => {
const entity = new CoreEntity();
const entity = new CoreEntity('n');
await orm.em.persistAndFlush(entity);
expect(entity.id).toBeDefined();
orm.em.clear();
Expand All @@ -38,4 +50,17 @@ describe('different schema from config', () => {
expect(wrap(e).getSchema()).toBe('privateschema');
});

it('should respect the global schema config (multi insert)', async () => {
await orm.em.fork().persistAndFlush([new CoreEntity('n1'), new CoreEntity('n2'), new CoreEntity('n3')]);

const res = await orm.em.find(CoreEntity, {});
expect(res).toHaveLength(3);
expect(wrap(res[0]).getSchema()).toBe('privateschema');
expect(wrap(res[1]).getSchema()).toBe('privateschema');
expect(wrap(res[2]).getSchema()).toBe('privateschema');

res.forEach(row => row.name = `name ${row.id}`);
await orm.em.flush();
});

});

0 comments on commit b47393e

Please sign in to comment.