Skip to content

Commit

Permalink
fix(postgres): do not ignore custom PK constraint names
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed Mar 19, 2022
1 parent 74751fb commit 9e99430
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/utils/Configuration.ts
Expand Up @@ -208,6 +208,13 @@ export class Configuration<D extends IDatabaseDriver = IDatabaseDriver> {
return this.driver;
}

/**
* Returns true if the custom naming strategy is provided.
*/
hasCustomNamingStrategy(): boolean {
return !!this.options.namingStrategy;
}

/**
* Gets instance of NamingStrategy. (cached)
*/
Expand Down
7 changes: 5 additions & 2 deletions packages/knex/src/schema/SchemaGenerator.ts
Expand Up @@ -239,7 +239,7 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
schema = schemaName ?? schema ?? this.config.get('schema');

/* istanbul ignore next */
if (schema && schemaName ==='*') {
if (schema && schemaName === '*') {
return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
}

Expand Down Expand Up @@ -466,7 +466,10 @@ export class SchemaGenerator extends AbstractSchemaGenerator<AbstractSqlDriver>
});

for (const index of tableDef.getIndexes()) {
this.createIndex(table, index, !tableDef.getColumns().some(c => c.autoincrement));
this.createIndex(table, index,
!tableDef.getColumns().some(c => c.autoincrement)
|| (this.config.hasCustomNamingStrategy() && this.platform.supportsCustomPrimaryKeyNames()),
);
}

for (const check of tableDef.getChecks()) {
Expand Down
50 changes: 50 additions & 0 deletions tests/issues/GH2930.test.ts
@@ -0,0 +1,50 @@
import {
Entity,
PrimaryKey,
Property,
MikroORM,
UnderscoreNamingStrategy,
} from '@mikro-orm/core';
import type { SqliteDriver } from '@mikro-orm/sqlite';

@Entity()
class A {

@PrimaryKey()
id!: number;

@Property()
prop?: string;

}

describe('GH issue 2930', () => {

let orm: MikroORM<SqliteDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [A],
dbName: 'mikro_orm_test_gh2930',
type: 'postgresql',
namingStrategy: class extends UnderscoreNamingStrategy {

indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence' | 'check'): string {
if (type === 'primary') {
return `pk_${tableName}_${columns.join('_')}`;
}
return super.indexName(tableName, columns, type);
}

},
});
});

afterAll(() => orm.close(true));

test(`should not ignore custom pk name`, async () => {
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL();
expect(sql).toMatchSnapshot();
});

});
12 changes: 12 additions & 0 deletions tests/issues/__snapshots__/GH2920.test.ts.snap
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GH issue 2920 should not ignore custom pk name 1`] = `
"set names 'utf8';
set session_replication_role = 'replica';
create table \\"a\\" (\\"id\\" serial, \\"prop\\" varchar(255) not null);
alter table \\"a\\" add constraint \\"pk_a_id\\" primary key (\\"id\\");
set session_replication_role = 'origin';
"
`;

0 comments on commit 9e99430

Please sign in to comment.