Skip to content

Commit

Permalink
fix(core): do not ignore schema name in collection updates
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 16, 2022
1 parent b47393e commit d688dc1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
const tableName = this.helper.getTableName(this.entityName) + (this.finalized && this.helper.isTableNameAliasRequired(this.type) ? ` as ${this.alias}` : '');
const qb = this.knex(tableName);

if (this._schema) {
qb.withSchema(this._schema);
}

if (this.context) {
qb.transacting(this.context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Entity, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core';
import { Collection, Entity, ManyToMany, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';

@Entity()
export class CoreEntity {
export class BookTag {

@PrimaryKey()
id!: number;
Expand All @@ -16,16 +16,34 @@ export class CoreEntity {

}

@Entity()
export class Book {

@PrimaryKey()
id!: number;

@Property()
name: string;

@ManyToMany(() => BookTag)
tags = new Collection<BookTag>(this);

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

}

describe('different schema from config', () => {

let orm: MikroORM<PostgreSqlDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
type: 'postgresql',
entities: [Book, BookTag],
dbName: 'mikro_orm_test_gh_2740',
schema: 'privateschema',
entities: [CoreEntity],
});
await orm.getSchemaGenerator().refreshDatabase();
});
Expand All @@ -35,31 +53,43 @@ describe('different schema from config', () => {
});

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

it('should respect the global schema config', async () => {
const entity = new CoreEntity('n');
const entity = new Book('n');
entity.tags.add(new BookTag('t'));
await orm.em.persistAndFlush(entity);
expect(entity.id).toBeDefined();
orm.em.clear();

const e = await orm.em.findOne(CoreEntity, entity);
const e = await orm.em.findOne(Book, entity);
expect(e).not.toBeNull();
expect(wrap(e).getSchema()).toBe('privateschema');
e!.tags.set([new BookTag('t2')]);
await orm.em.flush();
});

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 books = [new Book('n1'), new Book('n2'), new Book('n3')];
books[0].tags.add(new BookTag('t1'));
books[1].tags.add(new BookTag('t2'));
books[2].tags.add(new BookTag('t3'));
await orm.em.fork().persistAndFlush(books);

const res = await orm.em.find(CoreEntity, {});
const res = await orm.em.find(Book, {}, { populate: ['tags'] });
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}`);
res[0].tags.set([new BookTag('t21')]);
res[1].tags.set([new BookTag('t22')]);
res[2].tags.set([new BookTag('t23')]);
await orm.em.flush();
});

Expand Down

0 comments on commit d688dc1

Please sign in to comment.