-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): fix explicit schema name support
- Loading branch information
Showing
4 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/features/schema-generator/__snapshots__/schema-name-from-config.postgres.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
" | ||
`; |
61 changes: 61 additions & 0 deletions
61
tests/features/schema-generator/schema-name-from-config.postgres.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
|
||
}); |