Skip to content

Commit

Permalink
fix(schema): fix explicit schema name support (#2752)
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 12, 2022
1 parent 8d9c4c0 commit 68631ea
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>

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<string> {
Expand Down Expand Up @@ -122,8 +123,9 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
}

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<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
"
`;
Original file line number Diff line number Diff line change
@@ -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<Profile>(this);

}

describe('adding FK column', () => {

let orm: MikroORM<PostgreSqlDriver>;

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
});

});

0 comments on commit 68631ea

Please sign in to comment.