From 68631ea786e40aecd8ffc31baead9a23699874b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Sat, 12 Feb 2022 22:11:29 +0100 Subject: [PATCH] fix(schema): fix explicit schema name support (#2752) --- .../core/src/metadata/MetadataDiscovery.ts | 2 +- packages/knex/src/schema/SchemaGenerator.ts | 6 +- ...ema-name-from-config.postgres.test.ts.snap | 57 +++++++++++++++++ .../schema-name-from-config.postgres.test.ts | 61 +++++++++++++++++++ 4 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 tests/features/schema-generator/__snapshots__/schema-name-from-config.postgres.test.ts.snap create mode 100644 tests/features/schema-generator/schema-name-from-config.postgres.test.ts diff --git a/packages/core/src/metadata/MetadataDiscovery.ts b/packages/core/src/metadata/MetadataDiscovery.ts index b76f5534f11c..b4ea7d70da35 100644 --- a/packages/core/src/metadata/MetadataDiscovery.ts +++ b/packages/core/src/metadata/MetadataDiscovery.ts @@ -529,7 +529,7 @@ export class MetadataDiscovery { ret.targetMeta = meta; ret.joinColumns = []; ret.inverseJoinColumns = []; - const schema = meta.schema ?? this.platform.getDefaultSchemaName(); + const schema = meta.schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName(); ret.referencedTableName = schema && schema !== '*' ? schema + '.' + meta.tableName : meta.tableName; if (owner) { diff --git a/packages/knex/src/schema/SchemaGenerator.ts b/packages/knex/src/schema/SchemaGenerator.ts index febb23a6e373..38996e44fd0f 100644 --- a/packages/knex/src/schema/SchemaGenerator.ts +++ b/packages/knex/src/schema/SchemaGenerator.ts @@ -66,7 +66,8 @@ export class SchemaGenerator extends AbstractSchemaGenerator getTargetSchema(schema?: string): DatabaseSchema { const metadata = this.getOrderedMetadata(schema); - return DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schema ?? this.platform.getDefaultSchemaName()); + const schemaName = schema ?? this.config.get('schema') ?? this.platform.getDefaultSchemaName(); + return DatabaseSchema.fromMetadata(metadata, this.platform, this.config, schemaName); } async getCreateSchemaSQL(options: { wrap?: boolean; schema?: string } = {}): Promise { @@ -122,8 +123,9 @@ export class SchemaGenerator extends AbstractSchemaGenerator } private getSchemaName(meta: EntityMetadata, options: { schema?: string }): string | undefined { + const schemaName = options.schema ?? this.config.get('schema'); /* istanbul ignore next */ - return meta.schema === '*' ? options.schema : meta.schema; + return meta.schema && meta.schema === '*' ? schemaName : (meta.schema ?? schemaName); } async updateSchema(options: { wrap?: boolean; safe?: boolean; dropTables?: boolean; fromSchema?: DatabaseSchema; schema?: string } = {}): Promise { diff --git a/tests/features/schema-generator/__snapshots__/schema-name-from-config.postgres.test.ts.snap b/tests/features/schema-generator/__snapshots__/schema-name-from-config.postgres.test.ts.snap new file mode 100644 index 000000000000..28f777099681 --- /dev/null +++ b/tests/features/schema-generator/__snapshots__/schema-name-from-config.postgres.test.ts.snap @@ -0,0 +1,57 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`adding FK column schema: adding 1:1 relation 1`] = ` +"set names 'utf8'; +set session_replication_role = 'replica'; + +create schema if not exists \\"test\\"; + +create table \\"test\\".\\"profile\\" (\\"id\\" varchar(255) not null); +alter table \\"test\\".\\"profile\\" add constraint \\"profile_pkey\\" primary key (\\"id\\"); + +create table \\"test\\".\\"user\\" (\\"id\\" varchar(255) not null); +alter table \\"test\\".\\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\"); + +create table \\"test\\".\\"user_profile\\" (\\"user_id\\" varchar(255) not null, \\"profile_id\\" varchar(255) not null); +alter table \\"test\\".\\"user_profile\\" add constraint \\"user_profile_pkey\\" primary key (\\"user_id\\", \\"profile_id\\"); + +alter table \\"test\\".\\"user_profile\\" add constraint \\"user_profile_user_id_foreign\\" foreign key (\\"user_id\\") references \\"test\\".\\"user\\" (\\"id\\") on update cascade on delete cascade; +alter table \\"test\\".\\"user_profile\\" add constraint \\"user_profile_profile_id_foreign\\" foreign key (\\"profile_id\\") references \\"test\\".\\"profile\\" (\\"id\\") on update cascade on delete cascade; + +set session_replication_role = 'origin'; +" +`; + +exports[`adding FK column schema: adding 1:1 relation 2`] = ` +"set names 'utf8'; +set session_replication_role = 'replica'; + +create schema if not exists \\"test\\"; + +create table \\"test\\".\\"profile\\" (\\"id\\" varchar(255) not null); +alter table \\"test\\".\\"profile\\" add constraint \\"profile_pkey\\" primary key (\\"id\\"); + +create table \\"test\\".\\"user\\" (\\"id\\" varchar(255) not null); +alter table \\"test\\".\\"user\\" add constraint \\"user_pkey\\" primary key (\\"id\\"); + +create table \\"test\\".\\"user_profile\\" (\\"user_id\\" varchar(255) not null, \\"profile_id\\" varchar(255) not null); +alter table \\"test\\".\\"user_profile\\" add constraint \\"user_profile_pkey\\" primary key (\\"user_id\\", \\"profile_id\\"); + +alter table \\"test\\".\\"user_profile\\" add constraint \\"user_profile_user_id_foreign\\" foreign key (\\"user_id\\") references \\"test\\".\\"user\\" (\\"id\\") on update cascade on delete cascade; +alter table \\"test\\".\\"user_profile\\" add constraint \\"user_profile_profile_id_foreign\\" foreign key (\\"profile_id\\") references \\"test\\".\\"profile\\" (\\"id\\") on update cascade on delete cascade; + +set session_replication_role = 'origin'; +" +`; + +exports[`adding FK column schema: adding 1:1 relation 3`] = ` +"set names 'utf8'; +set session_replication_role = 'replica'; + +drop table if exists \\"test\\".\\"user_profile\\" cascade; +drop table if exists \\"test\\".\\"user\\" cascade; +drop table if exists \\"test\\".\\"profile\\" cascade; + +set session_replication_role = 'origin'; +" +`; diff --git a/tests/features/schema-generator/schema-name-from-config.postgres.test.ts b/tests/features/schema-generator/schema-name-from-config.postgres.test.ts new file mode 100644 index 000000000000..1c4c3f1ea2bd --- /dev/null +++ b/tests/features/schema-generator/schema-name-from-config.postgres.test.ts @@ -0,0 +1,61 @@ +import 'reflect-metadata'; +import { Entity, MikroORM, PrimaryKey, ManyToMany, Collection } from '@mikro-orm/core'; +import type { PostgreSqlDriver } from '@mikro-orm/postgresql'; + + +@Entity() +class Profile { + + @PrimaryKey() + id!: string; + +} + + +@Entity() +class User { + + @PrimaryKey() + id!: string; + + @ManyToMany(() => Profile) + profile = new Collection(this); + +} + +describe('adding FK column', () => { + + let orm: MikroORM; + + beforeAll(async () => { + orm = await MikroORM.init({ + entities: [User, Profile], + type: 'postgresql', + dbName: 'fk-column-postgres-schema', + schema: 'test', + }); + await orm.getSchemaGenerator().ensureDatabase(); + await orm.getSchemaGenerator().dropSchema(); + }); + + afterAll(() => orm.close(true)); + + test('schema: adding 1:1 relation', async () => { + const diff1 = await orm.getSchemaGenerator().getCreateSchemaSQL(); + expect(diff1).toMatchSnapshot(); + const diff2 = await orm.getSchemaGenerator().getUpdateSchemaSQL(); + expect(diff2).toMatchSnapshot(); + const diff3 = await orm.getSchemaGenerator().getDropSchemaSQL(); + expect(diff3).toMatchSnapshot(); + + await orm.getSchemaGenerator().execute(diff1); // create + await orm.getSchemaGenerator().execute(diff3); // drop + await orm.getSchemaGenerator().execute(diff2); // update from scratch + + const diff4 = await orm.getSchemaGenerator().getUpdateSchemaSQL(); + expect(diff4).toBe(''); + + await orm.getSchemaGenerator().execute(diff3); // drop + }); + +});